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

20
echo.c
View File

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

7
grep.1
View File

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

54
grep.c
View File

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

20
tee.c
View File

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

22
touch.c
View File

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

5
wc.1
View File

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

40
wc.c
View File

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