This commit is contained in:
Connor Lane Smith 2011-05-24 01:13:34 +01:00
parent 687e5411ee
commit 9714d7b1d3
8 changed files with 103 additions and 90 deletions

25
date.c
View File

@ -1,29 +1,30 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h>
#include "util.h" #include "util.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
char buf[BUFSIZ]; char buf[BUFSIZ], c;
char *fmt = "%c"; char *fmt = "%c";
int i;
struct tm *now = NULL; struct tm *now = NULL;
time_t t; time_t t;
t = time(NULL); t = time(NULL);
for(i = 1; i < argc; i++) while((c = getopt(argc, argv, "d:")) != -1)
if(!strncmp("+", argv[i], 1)) switch(c) {
fmt = &argv[i][1]; case 'd':
else if(!strcmp("-d", argv[i]) && i+1 < argc) t = strtol(optarg, NULL, 0);
t = strtol(argv[++i], NULL, 0); break;
else default:
eprintf("usage: %s [-d time] [+format]\n", argv[0]); exit(EXIT_FAILURE);
now = localtime(&t); }
if(!now) if(optind < argc && argv[optind][0] == '+')
fmt = &argv[optind][1];
if(!(now = localtime(&t)))
eprintf("localtime failed\n"); eprintf("localtime failed\n");
strftime(buf, sizeof buf, fmt, now); strftime(buf, sizeof buf, fmt, now);

18
echo.c
View File

@ -2,19 +2,25 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <unistd.h>
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool nflag = false; bool nflag = false;
int i; char c;
if(argc > 1 && !strcmp(argv[1], "-n")) while((c = getopt(argc, argv, "n")) != -1)
switch(c) {
case 'n':
nflag = true; nflag = true;
for(i = nflag ? 2 : 1; i < argc; i++) { break;
fputs(argv[i], stdout); default:
if(i+1 < argc) exit(EXIT_FAILURE);
}
for(; optind < argc; optind++) {
fputs(argv[optind], stdout);
if(optind+1 < argc)
fputc(' ', stdout); fputc(' ', stdout);
} }
if(!nflag) if(!nflag)

7
grep.1
View File

@ -3,12 +3,7 @@
grep \- search files for a pattern grep \- search files for a pattern
.SH SYNOPSIS .SH SYNOPSIS
.B grep .B grep
.RB [ \-c ] .RB [ \-cilnqv ]
.RB [ \-i ]
.RB [ \-l ]
.RB [ \-n ]
.RB [ \-q ]
.RB [ \-v ]
.I pattern .I pattern
.RI [ file ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION

54
grep.c
View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <unistd.h>
static void grep(FILE *, const char *, regex_t *); static void grep(FILE *, const char *, regex_t *);
@ -16,46 +16,48 @@ static char mode = 0;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i, flags = 0; char c;
int flags = 0;
regex_t preg; regex_t preg;
FILE *fp; FILE *fp;
for(i = 1; i < argc; i++) while((c = getopt(argc, argv, "cilnqv")) != -1)
if(!strcmp(argv[i], "-c")) switch(c) {
mode = 'c'; case 'c':
else if(!strcmp(argv[i], "-i")) case 'l':
iflag = true; case 'n':
else if(!strcmp(argv[i], "-l")) case 'q':
mode = 'l'; mode = c;
else if(!strcmp(argv[i], "-n"))
mode = 'n';
else if(!strcmp(argv[i], "-q"))
mode = 'q';
else if(!strcmp(argv[i], "-v"))
vflag = true;
else
break; break;
case 'i':
if(i == argc) { iflag = true;
fprintf(stderr, "usage: %s [-c] [-i] [-l] [-n] [-v] pattern [files...]\n", argv[0]); break;
case 'v':
vflag = true;
break;
default:
exit(EXIT_FAILURE);
}
if(optind == argc) {
fprintf(stderr, "usage: %s [-cilnqv] pattern [files...]\n", argv[0]);
exit(2); exit(2);
} }
if(mode == 'c') if(mode == 'c')
flags |= REG_NOSUB; flags |= REG_NOSUB;
if(iflag) if(iflag)
flags |= REG_ICASE; flags |= REG_ICASE;
regcomp(&preg, argv[i++], flags); regcomp(&preg, argv[optind++], flags);
many = (argc > i+1); many = (argc > optind+1);
if(i == argc) if(optind == argc)
grep(stdin, "<stdin>", &preg); grep(stdin, "<stdin>", &preg);
else for(; i < argc; i++) { else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[i], "r"))) { if(!(fp = fopen(argv[optind], "r"))) {
fprintf(stderr, "fopen %s: ", argv[i]); fprintf(stderr, "fopen %s: ", argv[optind]);
perror(NULL); perror(NULL);
exit(2); exit(2);
} }
grep(fp, argv[i], &preg); grep(fp, argv[optind], &preg);
fclose(fp); fclose(fp);
} }
return match ? 0 : 1; return match ? 0 : 1;

18
tee.c
View File

@ -2,29 +2,35 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <unistd.h>
#include "util.h" #include "util.h"
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool aflag = false; bool aflag = false;
char buf[BUFSIZ]; char buf[BUFSIZ], c;
int i, nfps = 1; int i, nfps = 1;
size_t n; size_t n;
FILE **fps; FILE **fps;
if(argc > 1 && !strcmp(argv[1], "-a")) while((c = getopt(argc, argv, "a")) != -1)
switch(c) {
case 'a':
aflag = true; aflag = true;
break;
default:
exit(EXIT_FAILURE);
}
if(!(fps = malloc(sizeof *fps))) if(!(fps = malloc(sizeof *fps)))
eprintf("malloc:"); eprintf("malloc:");
fps[nfps-1] = stdout; fps[nfps-1] = stdout;
for(i = aflag ? 2 : 1; i < argc; i++) { for(; optind < argc; optind++) {
if(!(fps = realloc(fps, ++nfps * sizeof *fps))) if(!(fps = realloc(fps, ++nfps * sizeof *fps)))
eprintf("realloc:"); eprintf("realloc:");
if(!(fps[nfps-1] = fopen(argv[i], aflag ? "a" : "w"))) if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w")))
eprintf("fopen %s:", argv[i]); eprintf("fopen %s:", argv[optind]);
} }
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++)

22
touch.c
View File

@ -3,7 +3,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <utime.h> #include <utime.h>
@ -18,19 +17,22 @@ static time_t t;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
int i; char c;
t = time(NULL); t = time(NULL);
for(i = 1; i < argc; i++) while((c = getopt(argc, argv, "ct:")) != -1)
if(!strcmp(argv[i], "-c")) switch(c) {
case 'c':
cflag = true; cflag = true;
else if(!strcmp(argv[i], "-t") && i+1 < argc)
t = strtol(argv[++i], NULL, 0);
else
break; break;
case 't':
for(; i < argc; i++) t = strtol(optarg, NULL, 0);
touch(argv[i]); break;
default:
exit(EXIT_FAILURE);
}
for(; optind < argc; optind++)
touch(argv[optind]);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

5
wc.1
View File

@ -3,10 +3,7 @@
wc \- word count wc \- word count
.SH SYNOPSIS .SH SYNOPSIS
.B wc .B wc
.RB [ \-c ] .RB [ \-clmw ]
.RB [ \-l ]
.RB [ \-m ]
.RB [ \-w ]
.RI [ file ...] .RI [ file ...]
.SH DESCRIPTION .SH DESCRIPTION
.B wc .B wc

40
wc.c
View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <unistd.h>
#include "util.h" #include "util.h"
static void output(const char *, long, long, long); static void output(const char *, long, long, long);
@ -18,28 +18,32 @@ int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool many; bool many;
int i; char c;
FILE *fp; FILE *fp;
for(i = 1; i < argc; i++) while((c = getopt(argc, argv, "clmw")) != -1)
if(!strcmp(argv[i], "-c")) switch(c) {
cmode = 'c'; case 'c':
else if(!strcmp(argv[i], "-l")) case 'm':
lflag = true; cmode = c;
else if(!strcmp(argv[i], "-m"))
cmode = 'm';
else if(!strcmp(argv[i], "-w"))
wflag = true;
else
break; break;
many = (argc > i+1); case 'l':
lflag = true;
break;
case 'w':
wflag = true;
break;
default:
exit(EXIT_FAILURE);
}
many = (argc > optind+1);
if(i == argc) if(optind == argc)
wc(stdin, NULL); wc(stdin, NULL);
else for(; i < argc; i++) { else for(; optind < argc; optind++) {
if(!(fp = fopen(argv[i], "r"))) if(!(fp = fopen(argv[optind], "r")))
eprintf("fopen %s:", argv[i]); eprintf("fopen %s:", argv[optind]);
wc(fp, argv[i]); wc(fp, argv[optind]);
fclose(fp); fclose(fp);
} }
if(many) if(many)