Update TODO. Update Makefile. Add sleep. Add wc.

This commit is contained in:
Mid Favila 2022-10-01 14:38:29 -04:00
parent b7ac76440a
commit d8bb1fcc3e
5 changed files with 284 additions and 96 deletions

194
TODO
View File

@ -1,90 +1,108 @@
Write a Makefile. Write a Makefile. [done]
Add octal escape support to echo. Add octal escape support to echo.
Rework yes(1) to buffer input. This achieves greater throughput per system call. Rework yes(1) to buffer output. This achieves greater throughput per system call.
Write the following utilities: -----------------------------------------------------
ar PROGS
at/atd _____________________________________________________
basename Simple commands (only a handful of parameters at most, with simple premises)
batch(?) -sleep [done]
bc -true [done]
cal -yes [done]
cat (done) -basename/dirname
chgrp -echo [in-progress, SUS]
chmod -wc [in-progress]
chown -tee
cksum -cat [done]
cmp -pwd
comm -cksum
compress(?) -cal
cp
crond/crontab Filesystem modifiers
csplit -rm
cut -mv
dd -cp
df -mkdir
diff -rmdir
dirname -mktemp
du -chgrp
ed/x -chown
expand(?) -chmod
expr -mkfifo
file -mknod
find -touch
fold -ln
fuser -link(?)
grep -ls
head -find
iconv -file
id
join Filters
kill -sed
link(?) -nl
ln -tr
locale -expand/unexpand
localedef -fold
logname -head/tail
lp(?) -sort
ls -grep
mesg -uniq
mkdir -cut
mkfifo -paste
mknod -csplit
mktemp -iconv
more
mv Sorting, searching and testing
newgrp(?) -test
nice -cmp
nl -comm
paste -diff
pathchk(?) -tsort
pax -du
printf -df
ps -join
pwd -split
renice
rm System interfaces
rmdir -uname
sed -tput
sh -who
sleep -ps
sort -kill
split -renice
strings -nice
tabs -newgrp
tail -pathchk
talk(?) -fuser
tee -id
test -tabs
touch -lp
tput -locale
tr -localedef
tsort -at/atd
uname -batch
uncompress(?) -crond
unexpand -crontab
uniq -logname
wc [in progress]
who Binary utilities
write(?) -ar
xargs -tar
-pax
-od
-dd
-compress
-expand
-strings
Interactive utilities
-ed
-bc
-sh
Miscellaneous
-xargs
-mesg [maybe]
-write [maybe]
-printf
-expr

View File

@ -6,16 +6,18 @@ LDFLAGS = -static
LDLIBS = LDLIBS =
PREFIX = usr/local PREFIX = usr/local
DESTDIR = DESTDIR =
BIN = echo yes true false cat BIN = cat\
false\
sleep\
true\
yes\
echo\
wc\
all: ${BIN} all: ${BIN}
echo: echo.c false: true
wc: wc.c
yes: yes.c
true: true.c
false:
ln -s true false ln -s true false
cat: cat.c
install: all install: all
install -Dm0755 ${BIN} ${DESTDIR}/${PREFIX}/bin install -Dm0755 ${BIN} ${DESTDIR}/${PREFIX}/bin

36
src/sleep.c Normal file
View File

@ -0,0 +1,36 @@
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include "support.h"
int main(int argc, char **argv)
{
int i;
i = 0;
if(argc != 2)
{
throw(NEEDARG_FAIL, mastrcat(argv[0], " int"));
}
i = (int) (strtol(argv[1], (char **) NULL, 10));
if(errno == ERANGE)
{
throw(TIME_FAIL, mastrcat(argv[0], " ERANGE error."));
}
if(i < 0)
{
throw(MISC_FAIL, mastrcat(argv[0], ": need positive number."));
}
sleep(i);
exit(0);
}

View File

@ -11,7 +11,9 @@ enum {
MALLOC_FAIL, MALLOC_FAIL,
NEEDARG_FAIL, NEEDARG_FAIL,
FOPEN_FAIL, FOPEN_FAIL,
WRITE_FAIL WRITE_FAIL,
TIME_FAIL,
MISC_FAIL
}; };
@ -54,6 +56,10 @@ void throw(long unsigned int condition, void *info)
exit(FOPEN_FAIL); exit(FOPEN_FAIL);
case WRITE_FAIL: printf("failed to write: %s\n", (char *) info); case WRITE_FAIL: printf("failed to write: %s\n", (char *) info);
exit(WRITE_FAIL); exit(WRITE_FAIL);
case TIME_FAIL: printf("time error: %s\n", (char *) info);
exit(TIME_FAIL);
case MISC_FAIL: printf("%s\n", (char *) info);
exit(MISC_FAIL);
default: printf("You shouldn't be seeing this.\nSomething is very wrong.\n"); default: printf("You shouldn't be seeing this.\nSomething is very wrong.\n");
exit(-256); exit(-256);
} }

126
src/wc.c Normal file
View File

@ -0,0 +1,126 @@
/* wc.c -- program to count and display the number of lines, words and one of bytes or characters in the named files */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "support.h"
enum
{
C,
M,
L,
W,
OUTWORD,
INWORD,
/* character or byte */
COB = 0,
WORDS = 1,
LINES = 2,
};
int main(int argc, char **argv)
{
char state;
int c, i;
char par[4] = {1};
/* M and C conflict; M counts actual characters (theoretically), so we should go with that as the default */
par[C] = 0;
int count[3] = {0};
int total[3] = {0};
FILE *fd;
state = OUTWORD;
c = i = 0;
fd = 0;
/* process our parameters */
for(; (c = getopt(argc, argv, "cmlw")) != -1; c = 0)
{
switch(c)
{
case 'c': par[C] = 1;
par[M] = 0;
break;
case 'm': par[M] = 1;
par[C] = 0;
break;
case 'l': par[L] = 1;
break;
case 'w': par[W] = 1;
break;
/* Fall-through to default from ':' */
case ':':
case '?':
default: throw(NEEDARG_FAIL, mastrcat(argv[0], "[-c|-m] [-lw] [file ...]"));
break;
}
}
/* for every file we're given... */
for(fd = 0, i = optind; i < argc; fclose(fd), i++)
{
/* ...open our file... */
if( !(fd = fopen(argv[i], "r")))
{
printf("%d %d\n", i, optind);
exit(1);
}
/* ...then read until EOF... */
for(c = 0; (c = fgetc(fd)) != EOF;)
{
/* ...and count the goods as we go. */
if(!(isblank(c) || c == '\n'))
{
if(state == OUTWORD)
{
count[WORDS]++;
}
state = INWORD;
}
else
{
if(c == '\n')
{
count[LINES]++;
}
state = OUTWORD;
}
count[COB]++;
}
/* ...then print our total and reset. */
printf("%d %d %d %s\n", count[LINES], count[WORDS], count[COB], argv[i]);
total[COB] += count[COB];
total[WORDS] += count[WORDS];
total[LINES] += count[LINES];
count[COB] = 0;
count[WORDS] = 0;
count[LINES] = 0;
}
if((argc-optind) > 1)
{
printf("%d %d %d total\n", total[LINES], total[WORDS], total[COB]);
}
exit(0);
}