[GNU Manual] [POSIX requirement] [Linux man] [FreeBSD man]
Summary
basename - strip directory and suffix from a file name
Lines of code: 191
Principal syscall: None
Support syscalls: None
Options: 8 (3 short, 5 long)
Spirtually descended from basename in Version 7 UNIX (1979)
Added to Shellutils in November 1992 [First version]
Number of revisions: 102 [Code Evolution]
perform_basename()
- The heart of the basename operationremove_suffix()
- Removes a matching suffix from the input name
error()
- Outputs error message to standard error with possible process termination
Setup
The setup for basename is limited to a few local variables in main()
:
multiple_names
- Flag for supporting more than one argument (-a)use_nuls
- Flag for the zero option (-z)*suffix
- The suffix string to match and remove (-s)
Parsing
Parsing for basename checks if the user requested a NUL termed output. This is useful when the output is piped to another program rather than the terminal. It also checks for the suffix to remove and if more than one input name should be processed.
Parsing failures
Parsing could fail if the user doesn't specify at least one target or specifies more than expected.
Execution
The user should have provided at least one full path to a file and possibly more. Thus execution does this for each input target:
- Removes all trailing slashes from the file name
- Checks if there is a matching suffix and replaces characters with
\0
- Outputs the name, now without path or suffix
basename()
should not fail during the execution stage