Eliminating the getopt disgrace.

This commit is contained in:
Christoph Lohmann 2013-06-14 20:20:47 +02:00
parent 75c97de593
commit 4d38f60685
28 changed files with 724 additions and 476 deletions

28
chmod.c
View File

@ -13,26 +13,32 @@ static bool rflag = false;
static char oper = '='; static char oper = '=';
static mode_t mode = 0; static mode_t mode = 0;
static void
usage(void)
{
eprintf("usage: %s [-r] mode [file...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
while((c = getopt(argc, argv, "r")) != -1) ARGBEGIN {
switch(c) {
case 'r': case 'r':
rflag = true; rflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind == argc)
eprintf("usage: %s [-r] mode [file...]\n", argv[0]);
parsemode(argv[optind++]); if(argc < 1)
for(; optind < argc; optind++) usage();
chmodr(argv[optind]);
return EXIT_SUCCESS; parsemode(argv[0]);
for(++argv; argc > 0; argc--)
chmodr(argv[0]);
return 0;
} }
void void

32
chown.c
View File

@ -14,22 +14,30 @@ static bool rflag = false;
static struct passwd *pw = NULL; static struct passwd *pw = NULL;
static struct group *gr = NULL; static struct group *gr = NULL;
static void
usage(void)
{
eprintf("usage: %s [-r] [owner][:[group]] file...\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c, *owner, *group; char *owner, *group;
while((c = getopt(argc, argv, "r")) != -1) ARGBEGIN {
switch(c) {
case 'r': case 'r':
rflag = true; rflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind == argc) if(argc == 0)
eprintf("usage: %s [-r] [owner][:group] [file...]\n", argv[0]); usage();
owner = argv[optind++];
owner = argv[0];
argv++;
if((group = strchr(owner, ':'))) if((group = strchr(owner, ':')))
*group++ = '\0'; *group++ = '\0';
@ -49,9 +57,10 @@ main(int argc, char *argv[])
else if(!gr) else if(!gr)
eprintf("getgrnam %s: no such group\n", group); eprintf("getgrnam %s: no such group\n", group);
} }
for(; optind < argc; optind++) for(; argc > 0; argc--, argv++)
chownpwgr(argv[optind]); chownpwgr(argv[0]);
return EXIT_SUCCESS;
return 0;
} }
void void
@ -63,3 +72,4 @@ chownpwgr(const char *path)
if(rflag) if(rflag)
recurse(path, chownpwgr); recurse(path, chownpwgr);
} }

37
cmp.c
View File

@ -7,19 +7,24 @@
enum { Same = 0, Diff = 1, Error = 2 }; enum { Same = 0, Diff = 1, Error = 2 };
static void
usage(void)
{
enprintf(Error, "usage: %s [-ls] file1 file2\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool lflag = false; bool lflag = false;
bool sflag = false; bool sflag = false;
bool same = true; bool same = true;
char c;
int b[2], i; int b[2], i;
long line = 1, n = 1; long line = 1, n = 1;
FILE *fp[2]; FILE *fp[2];
if((c = getopt(argc, argv, "ls")) != -1) ARGBEGIN {
switch(c) {
case 'l': case 'l':
lflag = true; lflag = true;
break; break;
@ -27,31 +32,35 @@ main(int argc, char *argv[])
sflag = true; sflag = true;
break; break;
default: default:
exit(Error); usage();
} ARGEND;
if(argc < 2)
usage();
for(i = 0; i < 2; i++) {
if(!(fp[i] = fopen(argv[i], "r")))
enprintf(Error, "fopen %s:", argv[i]);
} }
if(optind != argc-2) for(n = 1; ((b[0] = getc(fp[0])) != EOF) \
enprintf(Error, "usage: %s [-ls] file1 file2\n", argv[0]); | ((b[1] = getc(fp[1])) != EOF); n++) {
for(i = 0; i < 2; i++)
if(!(fp[i] = fopen(argv[optind+i], "r")))
enprintf(Error, "fopen %s:", argv[optind+i]);
for(n = 1; ((b[0] = getc(fp[0])) != EOF) | ((b[1] = getc(fp[1])) != EOF); n++) {
if(b[0] == '\n') if(b[0] == '\n')
line++; line++;
if(b[0] == b[1]) if(b[0] == b[1])
continue; continue;
for(i = 0; i < 2; i++) for(i = 0; i < 2; i++)
if(b[i] == EOF) if(b[i] == EOF)
enprintf(Diff, "cmp: EOF on %s\n", argv[optind+i]); enprintf(Diff, "cmp: EOF on %s\n", argv[i]);
if(!lflag) { if(!lflag) {
if(!sflag) if(!sflag)
printf("%s %s differ: char %ld, line %ld\n", printf("%s %s differ: char %ld, line %ld\n",
argv[optind], argv[optind+1], n, line); argv[0], argv[1], n, line);
exit(Diff); exit(Diff);
} } else {
else {
printf("%4ld %3o %3o\n", n, b[0], b[1]); printf("%4ld %3o %3o\n", n, b[0], b[1]);
same = false; same = false;
} }
} }
return same ? Same : Diff; return same ? Same : Diff;
} }

26
date.c
View File

@ -5,10 +5,17 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-u] [-d format] [+FORMAT]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char buf[BUFSIZ], c; char buf[BUFSIZ];
char *fmt = "%c"; char *fmt = "%c";
struct tm *now = NULL; struct tm *now = NULL;
struct tm *(*tztime)(const time_t *) = localtime; struct tm *(*tztime)(const time_t *) = localtime;
@ -16,24 +23,25 @@ main(int argc, char *argv[])
time_t t; time_t t;
t = time(NULL); t = time(NULL);
while((c = getopt(argc, argv, "d:u")) != -1) ARGBEGIN {
switch(c) {
case 'd': case 'd':
t = estrtol(optarg, 0); t = estrtol(EARGF(usage()), 0);
break; break;
case 'u': case 'u':
tztime = gmtime; tztime = gmtime;
tz = "gm"; tz = "gm";
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind < argc && argv[optind][0] == '+') if(argc > 0 && argv[0][0] == '+')
fmt = &argv[optind][1]; fmt = &argv[0][1];
if(!(now = tztime(&t))) if(!(now = tztime(&t)))
eprintf("%stime failed\n", tz); eprintf("%stime failed\n", tz);
strftime(buf, sizeof buf, fmt, now); strftime(buf, sizeof buf, fmt, now);
puts(buf); puts(buf);
return EXIT_SUCCESS;
return 0;
} }

View File

@ -5,14 +5,26 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s string\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
if(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(EXIT_FAILURE); default:
if(optind != argc-1) usage();
eprintf("usage: %s string\n", argv[0]); } ARGEND;
if(argc < 1)
usage();
puts(dirname(argv[optind])); puts(dirname(argv[optind]));
return EXIT_SUCCESS;
return 0;
} }

24
echo.c
View File

@ -5,23 +5,31 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-n] text\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool nflag = false; bool nflag = false;
char c;
while((c = getopt(argc, argv, "n")) != -1) ARGBEGIN {
switch(c) {
case 'n': case 'n':
nflag = true; nflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
for(; optind < argc; optind++)
putword(argv[optind]); for(; argc > 0; argc--, argv++)
putword(argv[0]);
if(!nflag) if(!nflag)
putchar('\n'); putchar('\n');
return EXIT_SUCCESS;
return 0;
} }

30
fold.c
View File

@ -13,15 +13,20 @@ static void foldline(const char *, long);
static bool bflag = false; static bool bflag = false;
static bool sflag = false; static bool sflag = false;
static void
usage(void)
{
eprintf("usage: %s [-bs] [-w width] [FILE...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
long width = 80; long width = 80;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "bsw:")) != -1) ARGBEGIN {
switch(c) {
case 'b': case 'b':
bflag = true; bflag = true;
break; break;
@ -29,20 +34,22 @@ main(int argc, char *argv[])
sflag = true; sflag = true;
break; break;
case 'w': case 'w':
width = estrtol(optarg, 0); width = estrtol(EARGF(usage()), 0);
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind == argc)
if(argc == 0) {
fold(stdin, width); fold(stdin, width);
else for(; optind < argc; optind++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
fold(fp, width); fold(fp, width);
fclose(fp); fclose(fp);
} }
return EXIT_SUCCESS;
return 0;
} }
void void
@ -96,3 +103,4 @@ foldline(const char *str, long width)
putchar('\n'); putchar('\n');
} while(str[i = n] && str[i] != '\n'); } while(str[i = n] && str[i] != '\n');
} }

32
head.c
View File

@ -7,30 +7,37 @@
static void head(FILE *, const char *, long); static void head(FILE *, const char *, long);
static void
usage(void)
{
eprintf("usage: %s [-n] [FILE...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
long n = 10; long n = 10;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "n:")) != -1) ARGBEGIN {
switch(c) {
case 'n': case 'n':
n = estrtol(optarg, 0); n = estrtol(EARGF(usage()), 0);
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind == argc)
if(argc == 0) {
head(stdin, "<stdin>", n); head(stdin, "<stdin>", n);
else for(; optind < argc; optind++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
head(fp, argv[optind], n); head(fp, argv[0], n);
fclose(fp); fclose(fp);
} }
return EXIT_SUCCESS;
return 0;
} }
void void
@ -47,3 +54,4 @@ head(FILE *fp, const char *str, long n)
if(ferror(fp)) if(ferror(fp))
eprintf("%s: read error:", str); eprintf("%s: read error:", str);
} }

44
kill.c
View File

@ -19,60 +19,62 @@ struct {
#undef SIG #undef SIG
}; };
static void usage(void); static void
usage(void)
{
eprintf("usage: %s [-s signal] [pid...]\n"
" %s -l [signum]\n", argv0, argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool lflag = false; bool lflag = false;
char c, *end; char *end, *v;
int sig = SIGTERM; int sig = SIGTERM;
pid_t pid; pid_t pid;
size_t i; size_t i;
while((c = getopt(argc, argv, "ls:")) != -1) ARGBEGIN {
switch(c) {
case 'l': case 'l':
lflag = true; lflag = true;
break; break;
case 's': case 's':
sig = strtol(optarg, &end, 0); v = EARGF(usage());
sig = strtol(v, &end, 0);
if(*end == '\0') if(*end == '\0')
break; break;
for(i = 0; i < LEN(sigs); i++) for(i = 0; i < LEN(sigs); i++) {
if(!strcasecmp(optarg, sigs[i].name)) { if(!strcasecmp(v, sigs[i].name)) {
sig = sigs[i].sig; sig = sigs[i].sig;
break; break;
} }
}
if(i == LEN(sigs)) if(i == LEN(sigs))
eprintf("%s: unknown signal\n", optarg); eprintf("%s: unknown signal\n", v);
break; break;
default: default:
usage(); usage();
} } ARGEND;
if(optind < argc-1)
if(argc < 2)
usage(); usage();
if(lflag) { if(lflag) {
sig = (optind == argc) ? 0 : estrtol(argv[optind], 0); sig = (argc > 0) ? 0 : estrtol(argv[0], 0);
if(sig > 128) if(sig > 128)
sig = WTERMSIG(sig); sig = WTERMSIG(sig);
for(i = 0; i < LEN(sigs); i++) for(i = 0; i < LEN(sigs); i++)
if(sigs[i].sig == sig || sig == 0) if(sigs[i].sig == sig || sig == 0)
putword(sigs[i].name); putword(sigs[i].name);
putchar('\n'); putchar('\n');
} } else for(; argc > 0; argc--, argv++) {
else for(; optind < argc; optind++) { pid = estrtol(argv[0], 0);
pid = estrtol(argv[optind], 0);
if(kill(pid, sig) == -1) if(kill(pid, sig) == -1)
eprintf("kill %d:", pid); eprintf("kill %d:", pid);
} }
return EXIT_SUCCESS;
return 0;
} }
void
usage(void)
{
eprintf("usage: %s [-s signal] [pid...]\n"
" %s -l [signum]\n", argv0, argv0);
}

23
ln.c
View File

@ -11,13 +11,17 @@ static int ln(const char *, const char *);
static bool sflag = false; static bool sflag = false;
static bool fflag = false; static bool fflag = false;
static void
usage(void)
{
eprintf("usage: %s [-fs] target linkname\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c; ARGBEGIN {
while((c = getopt(argc, argv, "fs")) != -1)
switch(c) {
case 'f': case 'f':
fflag = true; fflag = true;
break; break;
@ -25,10 +29,12 @@ main(int argc, char *argv[])
sflag = true; sflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
enmasse(argc - optind, &argv[optind], ln);
return EXIT_SUCCESS; enmasse(argc, &argv[0], ln);
return 0;
} }
int int
@ -45,3 +51,4 @@ ln(const char *s1, const char *s2)
} }
return -1; return -1;
} }

29
ls.c
View File

@ -35,15 +35,20 @@ static bool tflag = false;
static bool first = true; static bool first = true;
static bool many; static bool many;
static void
usage(void)
{
eprintf("usage: %s [-adlt] [FILE...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
int i, n; int i, n;
Entry *ents; Entry *ents;
while((c = getopt(argc, argv, "adlt")) != -1) ARGBEGIN {
switch(c) {
case 'a': case 'a':
aflag = true; aflag = true;
break; break;
@ -57,22 +62,25 @@ main(int argc, char *argv[])
tflag = true; tflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
many = (argc > optind+1);
if((n = argc - optind) > 0) { many = (argc > 1);
if((n = argc) > 0) {
if(!(ents = malloc(n * sizeof *ents))) if(!(ents = malloc(n * sizeof *ents)))
eprintf("malloc:"); eprintf("malloc:");
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
mkent(&ents[i], argv[optind+i]); mkent(&ents[i], argv[i]);
qsort(ents, n, sizeof *ents, (int (*)(const void *, const void *))entcmp); qsort(ents, n, sizeof *ents,
(int (*)(const void *, const void *))entcmp);
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
ls(ents[i].name); ls(ents[i].name);
} }
else else
ls("."); ls(".");
return EXIT_SUCCESS;
return 0;
} }
int int
@ -229,3 +237,4 @@ output(Entry *ent)
} }
putchar('\n'); putchar('\n');
} }

29
mc.c
View File

@ -14,32 +14,37 @@ static struct linebuf b = EMPTY_LINEBUF;
static long n_columns; static long n_columns;
static long n_rows; static long n_rows;
static void
usage(void)
{
eprintf("usage: %s [-c chars] [FILE...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c;
long i, l, col; long i, l, col;
size_t maxlen = 0; size_t maxlen = 0;
char *space; char *space;
FILE *fp; FILE *fp;
while ((c = getopt(argc, argv, "c:")) != -1) ARGBEGIN {
switch (c) {
case 'c': case 'c':
chars = estrtol(optarg, 0); chars = estrtol(EARGF(usage()), 0);
if(chars < 3) if(chars < 3)
eprintf("%d: too few character columns"); eprintf("%d: too few character columns");
break; break;
default: default:
exit(2); usage();
} } ARGEND;
/* XXX librarify this chunk, too? only useful in sponges though */ /* XXX librarify this chunk, too? only useful in sponges though */
if(optind == argc) if(argc == 0) {
getlines(stdin, &b); getlines(stdin, &b);
else for(; optind < argc; optind++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
getlines(fp, &b); getlines(fp, &b);
fclose(fp); fclose(fp);
} }
@ -59,7 +64,7 @@ main(int argc, char *argv[])
for(l = 0; l < b.nlines; ++l) { for(l = 0; l < b.nlines; ++l) {
fputs(b.lines[l], stdout); fputs(b.lines[l], stdout);
} }
return EXIT_SUCCESS; return 0;
} }
if(!(space = malloc(maxlen + 2))) if(!(space = malloc(maxlen + 2)))
@ -78,6 +83,6 @@ main(int argc, char *argv[])
fputs("\n", stdout); fputs("\n", stdout);
} }
return EXIT_SUCCESS; return 0;
} }

30
mkdir.c
View File

@ -10,26 +10,35 @@
static void mkdirp(char *); static void mkdirp(char *);
static void
usage(void)
{
eprintf("usage: %s [-p] directory...\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool pflag = false; bool pflag = false;
char c;
while((c = getopt(argc, argv, "p")) != -1) ARGBEGIN {
switch(c) {
case 'p': case 'p':
pflag = true; pflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} ARGEND;
for(; argc > 0; argc--, argv++) {
if(pflag) {
mkdirp(argv[0]);
} else if(mkdir(argv[0], S_IRWXU|S_IRWXG|S_IRWXO) == -1) {
eprintf("mkdir %s:", argv[0]);
} }
for(; optind < argc; optind++) }
if(pflag)
mkdirp(argv[optind]); return 0;
else if(mkdir(argv[optind], S_IRWXU|S_IRWXG|S_IRWXO) == -1)
eprintf("mkdir %s:", argv[optind]);
return EXIT_SUCCESS;
} }
void void
@ -46,3 +55,4 @@ mkdirp(char *path)
*p = '/'; *p = '/';
} while(p); } while(p);
} }

View File

@ -5,13 +5,26 @@
#include <sys/stat.h> #include <sys/stat.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s name...\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
while(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(EXIT_FAILURE); default:
for(; optind < argc; optind++) usage();
if(mkfifo(argv[optind], S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH) == -1) } ARGEND;
eprintf("mkfifo %s:", argv[optind]); for(; argc > 0; argc--, argv++) {
return EXIT_SUCCESS; if(mkfifo(argv[0], S_IRUSR|S_IWUSR|S_IRGRP|\
S_IWGRP|S_IROTH|S_IWOTH) == -1) {
eprintf("mkfifo %s:", argv[0]);
} }
}
return 0;
}

23
mv.c
View File

@ -9,17 +9,28 @@
int mv(const char *, const char *); int mv(const char *, const char *);
static void
usage(void)
{
eprintf("usage: %s source... dest\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct stat st; struct stat st;
if(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(EXIT_FAILURE); default:
usage();
} ARGEND;
if(argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode))) if(argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]); eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc - optind, &argv[optind], mv); enmasse(argc, &argv[0], mv);
return EXIT_SUCCESS;
return 0;
} }
int int
@ -27,8 +38,7 @@ mv (const char *s1, const char *s2)
{ {
if (rename(s1, s2) == 0) if (rename(s1, s2) == 0)
return 0; return 0;
if (errno == EXDEV) if (errno == EXDEV) {
{
cp_rflag = true; cp_rflag = true;
rm_rflag = true; rm_rflag = true;
cp(s1, s2); cp(s1, s2);
@ -37,3 +47,4 @@ mv (const char *s1, const char *s2)
} }
return -1; return -1;
} }

57
nl.c
View File

@ -14,39 +14,50 @@ static const char *sep = "\t";
static long incr = 1; static long incr = 1;
static regex_t preg; static regex_t preg;
static void
usage(void)
{
eprintf("usage: %s [-b style] [-i increment] [-s sep] [FILE...]\n",
argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
FILE *fp; FILE *fp;
char *r;
while((c = getopt(argc, argv, "b:i:s:")) != -1) ARGBEGIN {
switch(c) {
case 'b': case 'b':
mode = optarg[0]; r = EARGF(usage());
if(optarg[0] == 'p') mode = r[0];
regcomp(&preg, &optarg[1], REG_NOSUB); if(r[0] == 'p') {
else if(!strchr("ant", optarg[0]) || optarg[1] != '\0') regcomp(&preg, &r[1], REG_NOSUB);
eprintf("usage: %s [-b mode] [-i increment] [-s separator] [file...]\n", argv[0]); } else if(!strchr("ant", mode)) {
usage();
}
break; break;
case 'i': case 'i':
incr = estrtol(optarg, 0); incr = estrtol(EARGF(usage()), 0);
break; break;
case 's': case 's':
sep = optarg; sep = EARGF(usage());
break; break;
default: default:
exit(2); usage();
} } ARGEND;
if(optind == argc)
if(argc == 0) {
nl(stdin); nl(stdin);
else for(; optind < argc; optind++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
nl(fp); nl(fp);
fclose(fp); fclose(fp);
} }
return EXIT_SUCCESS;
return 0;
} }
void void
@ -56,12 +67,16 @@ nl(FILE *fp)
long n = 0; long n = 0;
size_t size = 0; size_t size = 0;
while(afgets(&buf, &size, fp)) while(afgets(&buf, &size, fp)) {
if((mode == 'a') if((mode == 'a')
|| (mode == 'p' && !regexec(&preg, buf, 0, NULL, 0)) || (mode == 'p'
|| (mode == 't' && buf[0] != '\n')) && !regexec(&preg, buf, 0, NULL, 0))
|| (mode == 't' && buf[0] != '\n')) {
printf("%6ld%s%s", n += incr, sep, buf); printf("%6ld%s%s", n += incr, sep, buf);
else } else {
printf(" %s", buf); printf(" %s", buf);
}
}
free(buf); free(buf);
} }

29
nohup.c
View File

@ -9,21 +9,34 @@
enum { Error = 127, Found = 126 }; enum { Error = 127, Found = 126 };
static void
usage(void)
{
eprintf("usage: %s command [argument...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int fd; int fd;
if(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(Error); default:
if(optind == argc) usage();
enprintf(Error, "usage: %s command [argument...]\n", argv[0]); } ARGEND;
if(argc == 0)
usage();
if(signal(SIGHUP, SIG_IGN) == SIG_ERR) if(signal(SIGHUP, SIG_IGN) == SIG_ERR)
enprintf(Error, "signal HUP:"); enprintf(Error, "signal HUP:");
if(isatty(STDOUT_FILENO)) { if(isatty(STDOUT_FILENO)) {
if((fd = open("nohup.out", O_APPEND|O_CREAT, S_IRUSR|S_IWUSR)) == -1) if((fd = open("nohup.out", O_APPEND|O_CREAT,
S_IRUSR|S_IWUSR)) == -1) {
enprintf(Error, "open nohup.out:"); enprintf(Error, "open nohup.out:");
}
if(dup2(fd, STDOUT_FILENO) == -1) if(dup2(fd, STDOUT_FILENO) == -1)
enprintf(Error, "dup2:"); enprintf(Error, "dup2:");
close(fd); close(fd);
@ -32,7 +45,9 @@ main(int argc, char *argv[])
if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1) if(dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
enprintf(Error, "dup2:"); enprintf(Error, "dup2:");
execvp(argv[optind], &argv[optind]); execvp(argv[0], &argv[0]);
enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[optind]); enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]);
return Error; return Error;
} }

48
paste.c
View File

@ -13,42 +13,44 @@ typedef struct {
const char *name; const char *name;
} Fdescr; } Fdescr;
static void eusage(void);
static size_t unescape(wchar_t *); static size_t unescape(wchar_t *);
static wint_t in(Fdescr *); static wint_t in(Fdescr *);
static void out(wchar_t); static void out(wchar_t);
static void sequential(Fdescr *, int, const wchar_t *, size_t); static void sequential(Fdescr *, int, const wchar_t *, size_t);
static void parallel(Fdescr *, int, const wchar_t *, size_t); static void parallel(Fdescr *, int, const wchar_t *, size_t);
static void
usage(void)
{
eprintf("usage: %s [-s] [-d list] file...\n", argv0);
exit(1);
}
int int
main(int argc, char **argv) { main(int argc, char *argv[])
{
const char *adelim = NULL; const char *adelim = NULL;
bool seq = false; bool seq = false;
wchar_t *delim; wchar_t *delim;
size_t len; size_t len;
Fdescr *dsc; Fdescr *dsc;
int i, c; int i;
setlocale(LC_CTYPE, ""); setlocale(LC_CTYPE, "");
while((c = getopt(argc, argv, "sd:")) != -1) ARGBEGIN {
switch(c) {
case 's': case 's':
seq = true; seq = true;
break; break;
case 'd': case 'd':
adelim = optarg; adelim = EARGF(usage());
break; break;
case '?':
default: default:
eusage(); usage();
break; } ARGEND;
}
argc -= optind;
argv += optind;
if(argc == 0) if(argc == 0)
eusage(); usage();
/* populate delimeters */ /* populate delimeters */
if(!adelim) if(!adelim)
@ -98,16 +100,14 @@ main(int argc, char **argv) {
free(delim); free(delim);
free(dsc); free(dsc);
return 0; return 0;
} }
static void
eusage(void) {
eprintf("usage: paste [-s][-d list] file...\n");
}
static size_t static size_t
unescape(wchar_t *delim) { unescape(wchar_t *delim)
{
wchar_t c; wchar_t c;
size_t i; size_t i;
size_t len; size_t len;
@ -140,7 +140,8 @@ unescape(wchar_t *delim) {
} }
static wint_t static wint_t
in(Fdescr *f) { in(Fdescr *f)
{
wint_t c = fgetwc(f->fp); wint_t c = fgetwc(f->fp);
if(c == WEOF && ferror(f->fp)) if(c == WEOF && ferror(f->fp))
@ -150,14 +151,16 @@ in(Fdescr *f) {
} }
static void static void
out(wchar_t c) { out(wchar_t c)
{
putwchar(c); putwchar(c);
if(ferror(stdout)) if(ferror(stdout))
eprintf("write error:"); eprintf("write error:");
} }
static void static void
sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) { sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
{
int i; int i;
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
@ -185,7 +188,8 @@ sequential(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) {
} }
static void static void
parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt) { parallel(Fdescr *dsc, int len, const wchar_t *delim, size_t cnt)
{
int last; int last;
do { do {

23
pwd.c
View File

@ -7,24 +7,32 @@
static const char *getpwd(const char *cwd); static const char *getpwd(const char *cwd);
static void
usage(void)
{
eprintf("usage: %s [-LP]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char *cwd, c; char *cwd;
char mode = 'L'; char mode = 'L';
while((c = getopt(argc, argv, "LP")) != -1) ARGBEGIN {
switch(c) {
case 'L': case 'L':
case 'P': case 'P':
mode = c; mode = ARGC();
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
cwd = agetcwd(); cwd = agetcwd();
puts((mode == 'L') ? getpwd(cwd) : cwd); puts((mode == 'L') ? getpwd(cwd) : cwd);
return EXIT_SUCCESS;
return 0;
} }
const char * const char *
@ -42,3 +50,4 @@ getpwd(const char *cwd)
else else
return cwd; return cwd;
} }

24
rm.c
View File

@ -7,13 +7,17 @@
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-fr] FILE...\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c; ARGBEGIN {
while((c = getopt(argc, argv, "fr")) != -1)
switch(c) {
case 'f': case 'f':
rm_fflag = true; rm_fflag = true;
break; break;
@ -21,9 +25,11 @@ main(int argc, char *argv[])
rm_rflag = true; rm_rflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
for(; optind < argc; optind++) for(; argc > 0; argc--, argv++)
rm(argv[optind]); rm(argv[0]);
return EXIT_SUCCESS;
return 0;
} }

52
seq.c
View File

@ -11,43 +11,56 @@ static int digitsright(const char *);
static double estrtod(const char *); static double estrtod(const char *);
static bool validfmt(const char *); static bool validfmt(const char *);
static void
usage(void)
{
eprintf("usage: %s [-f format] [-s sep] [-w width] first"
" [inc [last]]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n"; const char *starts = "1", *steps = "1", *ends = "1", *sep = "\n";
bool wflag = false; bool wflag = false;
char c, ftmp[BUFSIZ], *fmt = ftmp; char ftmp[BUFSIZ], *fmt = ftmp;
double start, step, end, out, dir; double start, step, end, out, dir;
while((c = getopt(argc, argv, "f:s:w")) != -1) ARGBEGIN {
switch(c) {
case 'f': case 'f':
if(!validfmt(optarg)) if(!validfmt(EARGF(usage())))
eprintf("%s: invalid format\n", optarg); eprintf("%s: invalid format\n", ARGF());
fmt = optarg; fmt = ARGF();
break; break;
case 's': case 's':
sep = optarg; sep = EARGF(usage());
break; break;
case 'w': case 'w':
wflag = true; wflag = true;
break; break;
} default:
usage();
} ARGEND;
switch(argc-optind) { switch(argc) {
case 3: case 3:
starts = argv[optind++]; starts = argv[0];
steps = argv[optind++]; argv++;
ends = argv[optind++]; steps = argv[0];
argv++;
ends = argv[0];
argv++;
break; break;
case 2: case 2:
starts = argv[optind++]; starts = argv[0];
argv++;
/* fallthrough */ /* fallthrough */
case 1: case 1:
ends = argv[optind++]; ends = argv[0];
break; break;
default: default:
eprintf("usage: %s [-w] [-f fmt] [-s separator] [start [step]] end\n", argv[0]); usage();
} }
start = estrtod(starts); start = estrtod(starts);
step = estrtod(steps); step = estrtod(steps);
@ -65,9 +78,9 @@ main(int argc, char *argv[])
if(wflag) { if(wflag) {
int left = MAX(digitsleft(starts), digitsleft(ends)); int left = MAX(digitsleft(starts), digitsleft(ends));
snprintf(ftmp, sizeof ftmp, "%%0%d.%df", right+left+(right != 0), right); snprintf(ftmp, sizeof ftmp, "%%0%d.%df",
} right+left+(right != 0), right);
else } else
snprintf(ftmp, sizeof ftmp, "%%.%df", right); snprintf(ftmp, sizeof ftmp, "%%.%df", right);
} }
for(out = start; out * dir <= end * dir; out += step) { for(out = start; out * dir <= end * dir; out += step) {
@ -77,7 +90,7 @@ main(int argc, char *argv[])
} }
printf("\n"); printf("\n");
return EXIT_SUCCESS; return 0;
} }
int int
@ -155,3 +168,4 @@ format:
return false; return false;
} }
} }

23
sleep.c
View File

@ -3,18 +3,29 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s seconds\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
unsigned int seconds; unsigned int seconds;
if(getopt(argc, argv, "") != -1) ARGBEGIN {
exit(EXIT_FAILURE); default:
if(optind != argc-1) usage();
eprintf("usage: %s seconds\n", argv[0]); } ARGEND;
seconds = estrtol(argv[optind], 0); if(argc < 1)
usage();
seconds = estrtol(argv[0], 0);
while((seconds = sleep(seconds)) > 0) while((seconds = sleep(seconds)) > 0)
; ;
return EXIT_SUCCESS; return 0;
} }

38
sort.c
View File

@ -14,15 +14,20 @@ static bool uflag = false;
static struct linebuf linebuf = EMPTY_LINEBUF; static struct linebuf linebuf = EMPTY_LINEBUF;
static void
usage(void)
{
eprintf("usage: %s [-ru] [file...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
long i; long i;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "ru")) != -1) ARGBEGIN {
switch(c) {
case 'r': case 'r':
rflag = true; rflag = true;
break; break;
@ -30,22 +35,28 @@ main(int argc, char *argv[])
uflag = true; uflag = true;
break; break;
default: default:
exit(2); usage();
} } ARGEND;
if(optind == argc)
if(argc == 0) {
getlines(stdin, &linebuf); getlines(stdin, &linebuf);
else for(; optind < argc; optind++) { } else for(; argc > 0; argc--, argv++) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
getlines(fp, &linebuf); getlines(fp, &linebuf);
fclose(fp); fclose(fp);
} }
qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines, (int (*)(const void *, const void *))linecmp); qsort(linebuf.lines, linebuf.nlines, sizeof *linebuf.lines,
(int (*)(const void *, const void *))linecmp);
for(i = 0; i < linebuf.nlines; i++) for(i = 0; i < linebuf.nlines; i++) {
if(!uflag || i == 0 || strcmp(linebuf.lines[i], linebuf.lines[i-1]) != 0) if(!uflag || i == 0 || strcmp(linebuf.lines[i],
linebuf.lines[i-1]) != 0) {
fputs(linebuf.lines[i], stdout); fputs(linebuf.lines[i], stdout);
return EXIT_SUCCESS; }
}
return 0;
} }
int int
@ -53,3 +64,4 @@ linecmp(const char **a, const char **b)
{ {
return strcmp(*a, *b) * (rflag ? -1 : +1); return strcmp(*a, *b) * (rflag ? -1 : +1);
} }

35
tail.c
View File

@ -9,36 +9,40 @@
static void dropinit(FILE *, const char *, long); static void dropinit(FILE *, const char *, long);
static void taketail(FILE *, const char *, long); static void taketail(FILE *, const char *, long);
static void
usage(void)
{
eprintf("usage: %s [-n lines] [file]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
long n = 10; long n = 10;
FILE *fp; FILE *fp;
void (*tail)(FILE *, const char *, long) = taketail; void (*tail)(FILE *, const char *, long) = taketail;
while((c = getopt(argc, argv, "n:")) != -1) ARGBEGIN {
switch(c) {
case 'n': case 'n':
n = abs(estrtol(optarg, 0)); n = abs(estrtol(EARGF(usage()), 0));
if(optarg[0] == '+') if(optarg[0] == '+')
tail = dropinit; tail = dropinit;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(optind == argc) if(argc == 0) {
tail(stdin, "<stdin>", n); tail(stdin, "<stdin>", n);
else if(optind == argc-1) { } else if(argc == 1) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
tail(fp, argv[optind], n); tail(fp, argv[0], n);
fclose(fp); fclose(fp);
} } else
else usage();
eprintf("usage: %s [-n lines] [file]\n", argv[0]);
return EXIT_SUCCESS; return 0;
} }
void void
@ -76,3 +80,4 @@ taketail(FILE *fp, const char *str, long n)
free(ring); free(ring);
free(size); free(size);
} }

33
tee.c
View File

@ -5,38 +5,49 @@
#include <unistd.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-a] [file...]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool aflag = false; bool aflag = false;
char buf[BUFSIZ], c; char buf[BUFSIZ];
int i, nfps = 1; int i, nfps = 1;
size_t n; size_t n;
FILE **fps; FILE **fps;
while((c = getopt(argc, argv, "a")) != -1) ARGBEGIN {
switch(c) {
case 'a': case 'a':
aflag = true; aflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(!(fps = malloc(sizeof *fps))) if(!(fps = malloc(sizeof *fps)))
eprintf("malloc:"); eprintf("malloc:");
fps[nfps-1] = stdout; fps[nfps-1] = stdout;
for(; optind < argc; optind++) { for(; argc > 0; argc--, argv++) {
if(!(fps = realloc(fps, ++nfps * sizeof *fps))) if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
eprintf("realloc:"); eprintf("realloc:");
if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w"))) if(!(fps[nfps-1] = fopen(argv[0], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
} }
while((n = fread(buf, 1, sizeof buf, stdin)) > 0) while((n = fread(buf, 1, sizeof buf, stdin)) > 0) {
for(i = 0; i < nfps; i++) for(i = 0; i < nfps; i++) {
if(fwrite(buf, 1, n, fps[i]) != n) if(fwrite(buf, 1, n, fps[i]) != n)
eprintf("%s: write error:", buf); eprintf("%s: write error:", buf);
}
}
if(ferror(stdin)) if(ferror(stdin))
eprintf("<stdin>: read error:"); eprintf("<stdin>: read error:");
return EXIT_SUCCESS;
return 0;
} }

27
touch.c
View File

@ -14,26 +14,32 @@ static void touch(const char *);
static bool cflag = false; static bool cflag = false;
static time_t t; static time_t t;
static void
usage(void)
{
eprintf("usage: %s [-c] [-t stamp] file...\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char c;
t = time(NULL); t = time(NULL);
while((c = getopt(argc, argv, "ct:")) != -1)
switch(c) { ARGBEGIN {
case 'c': case 'c':
cflag = true; cflag = true;
break; break;
case 't': case 't':
t = estrtol(optarg, 0); t = estrtol(EARGF(usage()), 0);
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
for(; optind < argc; optind++) for(; argc > 0; argc--, argv++)
touch(argv[optind]); touch(argv[0]);
return EXIT_SUCCESS;
return 0;
} }
void void
@ -60,3 +66,4 @@ touch(const char *str)
close(fd); close(fd);
touch(str); touch(str);
} }

19
uname.c
View File

@ -6,6 +6,13 @@
#include <sys/utsname.h> #include <sys/utsname.h>
#include "util.h" #include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-amnrsv]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -14,11 +21,9 @@ main(int argc, char *argv[])
bool rflag = false; bool rflag = false;
bool sflag = false; bool sflag = false;
bool vflag = false; bool vflag = false;
char c;
struct utsname u; struct utsname u;
while((c = getopt(argc, argv, "amnrsv")) != -1) ARGBEGIN {
switch(c) {
case 'a': case 'a':
mflag = nflag = rflag = sflag = vflag = true; mflag = nflag = rflag = sflag = vflag = true;
break; break;
@ -38,8 +43,8 @@ main(int argc, char *argv[])
vflag = true; vflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); usage();
} } ARGEND;
if(uname(&u) == -1) if(uname(&u) == -1)
eprintf("uname:"); eprintf("uname:");
@ -54,5 +59,7 @@ main(int argc, char *argv[])
if(mflag) if(mflag)
putword(u.machine); putword(u.machine);
putchar('\n'); putchar('\n');
return EXIT_SUCCESS;
return 0;
} }

30
uniq.c
View File

@ -19,14 +19,19 @@ static bool uflag = false;
static char *prev_line = NULL; static char *prev_line = NULL;
static long prev_line_count = 0; static long prev_line_count = 0;
static void
usage(void)
{
eprintf("usage: %s [-cdu] [input]]\n", argv0);
exit(1);
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int c;
FILE *fp; FILE *fp;
while((c = getopt(argc, argv, "cdu")) != -1) ARGBEGIN {
switch(c) {
case 'c': case 'c':
countfmt = "%7ld "; countfmt = "%7ld ";
break; break;
@ -37,21 +42,21 @@ main(int argc, char *argv[])
uflag = true; uflag = true;
break; break;
default: default:
exit(2); usage();
} } ARGEND;
if(optind == argc) if(argc == 0) {
uniq(stdin, "<stdin>"); uniq(stdin, "<stdin>");
else if(optind == argc - 1) { } else if(argc == 1) {
if(!(fp = fopen(argv[optind], "r"))) if(!(fp = fopen(argv[0], "r")))
eprintf("fopen %s:", argv[optind]); eprintf("fopen %s:", argv[0]);
uniq(fp, argv[optind]); uniq(fp, argv[0]);
fclose(fp); fclose(fp);
} else } else
enprintf(2, "too many arguments\n"); usage();
uniq_finish(); uniq_finish();
return EXIT_SUCCESS; return 0;
} }
void void
@ -96,3 +101,4 @@ uniq_finish()
{ {
uniq_line(NULL); uniq_line(NULL);
} }