[hackers] [sbase] Adding ARGBEGIN for basename. || Christoph Lohmann

From: <hg_AT_suckless.org>
Date: Mon, 23 Apr 2012 15:50:55 +0200 (CEST)

changeset: 108:a2bcdadccf77
tag: tip
user: Christoph Lohmann <20h_AT_r-36.net>
date: Mon Apr 23 15:50:47 2012 +0200
files: Makefile arg.h basename.1 basename.c
description:
Adding ARGBEGIN for basename.


diff -r a9f4ddff0da7 -r a2bcdadccf77 Makefile
--- a/Makefile Wed Apr 18 13:41:05 2012 +0200
+++ b/Makefile Mon Apr 23 15:50:47 2012 +0200
_AT_@ -1,6 +1,6 @@
 include config.mk
 
-HDR = fs.h text.h util.h
+HDR = fs.h text.h util.h arg.h
 LIB = \
         util/afgets.o \
         util/agetcwd.o \
diff -r a9f4ddff0da7 -r a2bcdadccf77 arg.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/arg.h Mon Apr 23 15:50:47 2012 +0200
_AT_@ -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]))
+
diff -r a9f4ddff0da7 -r a2bcdadccf77 basename.1
--- a/basename.1 Wed Apr 18 13:41:05 2012 +0200
+++ b/basename.1 Mon Apr 23 15:50:47 2012 +0200
_AT_@ -3,6 +3,10 @@
 basename \- strip leading path component
 .SH SYNOPSIS
 .B basename
+.RB [ \-a ]
+.RB [ \-z ]
+.RB [ \-s
+.IR suffix ]
 .I string
 .RI [ suffix ]
 .SH DESCRIPTION
_AT_@ -12,6 +16,17 @@
 with any leading path components, and the
 .IR suffix ,
 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
 .IR dirname (1),
 .IR basename (3)
diff -r a9f4ddff0da7 -r a2bcdadccf77 basename.c
--- a/basename.c Wed Apr 18 13:41:05 2012 +0200
+++ b/basename.c Mon Apr 23 15:50:47 2012 +0200
_AT_@ -4,25 +4,63 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <stdbool.h>
+
+#include "arg.h"
 #include "util.h"
 
+char *argv0;
+
+void
+usage(void)
+{
+ eprintf("usage: %s [-ahz] [-s suffix] name [suffix]\n",
+ basename(argv0));
+}
+
 int
 main(int argc, char *argv[])
 {
- char *s;
- size_t n;
+ char *s, *suffix = NULL;
+ size_t n, sn;
+ bool aflag = false, zflag = false;
 
- if(getopt(argc, argv, "") != -1)
- exit(EXIT_FAILURE);
- if(optind == argc)
- eprintf("usage: %s string [suffix]\n", argv[0]);
+ ARGBEGIN {
+ case 'a':
+ aflag = true;
+ break;
+ case 's':
+ suffix = EARGF(usage());
+ break;
+ case 'z':
+ zflag = true;
+ break;
+ case 'h':
+ default:
+ usage();
+ } ARGEND;
 
- s = basename(argv[optind++]);
- if(optind < argc && strlen(s) > strlen(argv[optind])) {
- n = strlen(s) - strlen(argv[optind]);
- if(!strcmp(&s[n], argv[optind]))
- s[n] = '\0';
+ if (argc < 1)
+ usage();
+
+ if (!aflag && argc == 2)
+ 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;
 }
+
Received on Mon Apr 23 2012 - 15:50:55 CEST

This archive was generated by hypermail 2.3.0 : Mon Apr 23 2012 - 16:00:15 CEST