Adding ARGBEGIN for basename.

This commit is contained in:
Christoph Lohmann 2012-04-23 15:50:47 +02:00
parent 96b15a5afa
commit f75d7a47ff
4 changed files with 97 additions and 13 deletions

View File

@ -1,6 +1,6 @@
include config.mk include config.mk
HDR = fs.h text.h util.h HDR = fs.h text.h util.h arg.h
LIB = \ LIB = \
util/afgets.o \ util/afgets.o \
util/agetcwd.o \ util/agetcwd.o \

31
arg.h Normal file
View File

@ -0,0 +1,31 @@
extern char *argv0;
#define USED(x) ((void)(x))
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
argv[0] && argv[0][1]\
&& argv[0][0] == '-';\
argc--, argv++) {\
char _argc;\
char **_argv;\
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
argv++;\
argc--;\
break;\
}\
for (argv[0]++, _argv = argv; argv[0][0];\
argv[0]++) {\
if (_argv != argv)\
break;\
_argc = argv[0][0];\
switch (_argc)
#define ARGEND }\
USED(_argc);\
}\
USED(argv);\
USED(argc);
#define EARGF(x) ((argv[1] == NULL)? ((x), abort(), (char *)0) :\
(argc--, argv++, argv[0]))

View File

@ -3,6 +3,10 @@
basename \- strip leading path component basename \- strip leading path component
.SH SYNOPSIS .SH SYNOPSIS
.B basename .B basename
.RB [ \-a ]
.RB [ \-z ]
.RB [ \-s
.IR suffix ]
.I string .I string
.RI [ suffix ] .RI [ suffix ]
.SH DESCRIPTION .SH DESCRIPTION
@ -12,6 +16,17 @@ prints the
with any leading path components, and the with any leading path components, and the
.IR suffix , .IR suffix ,
removed. removed.
.SH OPTIONS
.TP
.BI \-a
multiple arguments will each be treated as strings
.TP
.BI \-s " suffix"
specifies the suffix that should be removed
.TP
.BI \-z
output will be separated with NUL
.TP
.SH SEE ALSO .SH SEE ALSO
.IR dirname (1), .IR dirname (1),
.IR basename (3) .IR basename (3)

View File

@ -4,25 +4,63 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdbool.h>
#include "arg.h"
#include "util.h" #include "util.h"
char *argv0;
void
usage(void)
{
eprintf("usage: %s [-ahz] [-s suffix] name [suffix]\n",
basename(argv0));
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *s; char *s, *suffix = NULL;
size_t n; size_t n, sn;
bool aflag = false, zflag = false;
if(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(EXIT_FAILURE); case 'a':
if(optind == argc) aflag = true;
eprintf("usage: %s string [suffix]\n", argv[0]); break;
case 's':
suffix = EARGF(usage());
break;
case 'z':
zflag = true;
break;
case 'h':
default:
usage();
} ARGEND;
s = basename(argv[optind++]); if (argc < 1)
if(optind < argc && strlen(s) > strlen(argv[optind])) { usage();
n = strlen(s) - strlen(argv[optind]);
if(!strcmp(&s[n], argv[optind])) if (!aflag && argc == 2)
s[n] = '\0'; suffix = argv[1];
if (suffix)
sn = strlen(suffix);
for (; argc > 0; argc--, argv++) {
s = basename(argv[0]);
if (suffix) {
n = strlen(s) - sn;
if (!strcmp(&s[n], suffix))
s[n] = '\0';
}
printf("%s%c", s, (zflag)? '\0' : '\n');
if (!aflag)
break;
} }
puts(s);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }