1
0
mirror of git://git.suckless.org/sbase synced 2024-12-23 07:25:07 +00:00

Audit basename(1)

1) be stricter which number of arguments is accepted (1 or 2)
2) basename already returns a pointer to "." is argv[0] is ""
3) No need to check for *p != '/', because basename() only returns
   a string beginning with '/' which has length 1, so if strlen(p)
   == 1, the only way for suffix to be "evaluated" is for off to
   be > 0, being equal to suffix being "", but "" != "/".
4) don't calculate strlen twice for each string. Store it in a
   ssize_t and check if it's > 0.
This commit is contained in:
FRIGN 2015-02-28 14:48:44 +01:00
parent 44bd931834
commit e7c33c4af3
2 changed files with 11 additions and 14 deletions

6
README
View File

@ -7,9 +7,9 @@ across UNIX and UNIX-like systems.
The following tools are implemented ('*' == finished, '#' == UTF-8 support, The following tools are implemented ('*' == finished, '#' == UTF-8 support,
'=' == implicit UTF-8 support, '|' == audited): '=' == implicit UTF-8 support, '|' == audited):
UTILITY POSIX 2008 COMPLIANT MISSING OPTIONS UTILITY POSIX 2008 COMPLIANT MISSING OPTIONS
------- -------------------- --------------- ------- -------------------- ---------------
=* basename yes none =*| basename yes none
=* cal yes none =* cal yes none
=* cat yes none =* cat yes none
=* chgrp yes none =* chgrp yes none

View File

@ -5,8 +5,6 @@
#include "util.h" #include "util.h"
static void usage(void);
void void
usage(void) usage(void)
{ {
@ -16,25 +14,24 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
ssize_t off;
char *p; char *p;
size_t off;
ARGBEGIN { ARGBEGIN {
default: default:
usage(); usage();
} ARGEND; } ARGEND;
if (argc < 1) if (argc != 1 && argc != 2)
usage(); usage();
p = strlen(argv[0]) ? basename(argv[0]) : "."; p = basename(argv[0]);
if (argc == 2 && *p != '/') { if (argc == 2) {
if (strlen(argv[1]) < strlen(p)) { off = strlen(p) - strlen(argv[1]);
off = strlen(p) - strlen(argv[1]); if (off > 0 && !strcmp(p + off, argv[1]))
if (strcmp(&p[off], argv[1]) == 0) p[off] = '\0';
p[off] = '\0';
}
} }
puts(p); puts(p);
return 0; return 0;
} }