1
0
This commit is contained in:
dtb 2022-06-15 09:07:02 -04:00
parent 50f399b764
commit 86f1667699
2 changed files with 43 additions and 33 deletions

View File

@ -102,10 +102,10 @@ streq: libio streq.o
$(CC) $(CFLAGS) -o bin/streq build/libio.o build/streq.o $(CC) $(CFLAGS) -o bin/streq build/libio.o build/streq.o
str.o: src/str.c str.o: src/str.c
$(CC) $(CFLAGS) -o build/str.o src/str.c $(CC) $(CFLAGS) -c -o build/str.o src/str.c
str: libio str.o str: libio str.o
$(CC) $(CFLAGS) -o bin/str lib/libio.o src/str.o $(CC) $(CFLAGS) -o bin/str build/libio.o build/str.o
sleep.o: libio sysexits src/sleep.c usefulmacros sleep.o: libio sysexits src/sleep.c usefulmacros
$(CC) $(CFLAGS) -c -o build/sleep.o src/sleep.c $(CC) $(CFLAGS) -c -o build/sleep.o src/sleep.c

View File

@ -1,50 +1,60 @@
#include <ctype.h>
#include <stddef.h> #include <stddef.h>
#include <string.h> /* strcmp(3) */
#include <sysexits.h> #include <sysexits.h>
#include <unistd.h> #include <unistd.h> /* write(2) */
#include "libstrnum.h" #include "libio.h" /* fdputs(3) */
#include "usefulmacros.h"
int *typenames[] = { static char *program_name = "str";
(int []){ 'f','l','o','a','t', ASCII_US, STRIS_TYPE_FLOAT, '\0' },
(int []){ 'i','n','t', ASCII_US, STRIS_TYPE_INT, '\0' }, static struct {
(int []){ 'u','i','n','t', ASCII_US, STRIS_TYPE_UINT, '\0' } char *name;
int (*f)(int);
}ctypes[] = {
{ "isalnum", isalnum },
{ "isalpha", isalpha },
{ "isblank", isblank },
{ "iscntrl", iscntrl },
{ "isdigit", isdigit },
{ "isxdigit", isxdigit },
{ "isgraph", isgraph },
{ "islower", islower },
{ "isprint", isprint },
{ "ispunct", ispunct },
{ "isspace", isspace },
{ "isupper", isupper }
}; };
size_t typenames_s = 3;
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int i; int i;
int j; int j;
int k;
enum Strtype t;
for(k = 0; argv[0][k] != '\0'; ++k);
if(argc < 3){ if(argc < 3){
write(2, "Usage: ", 7); error: write(2, "Usage: ", 7);
write(2, argv[0], k); fdputs(2, argv[0] == NULL ? program_name : argv[0]);
write(2, " [type] [string...]\n", 20); write(2, " [type] [string...]\n", 20);
return EX_USAGE; return EX_USAGE;
} }
j = 0; /* We could skip checking the first two characters and replace it with
t = 0; * a single if(argv[1][0] == 'i' && argv[1][1] == 's') or something
for(i = 0; argv[1][i] != '\0'; ++i) * but it's simpler to just waste the cycles. */
if(typenames[j][i] != argv[1][i])
if(++j >= typenames_s){
write(2, argv[0], k);
write(2, ": ", 2);
for(k = 0; argv[1][k] != '\0'; ++k);
write(2, argv[1], k);
write(2, ": No such type\n", 15);
return EX_DATAERR;
}
/* slow but for our purposes this is fine */
for(i = 0; i < ARRAYLEN(ctypes); ++i)
if(strcmp(argv[1], ctypes[i].name) == 0){
i *= -1;
break;
}
if(i > 0)
goto error;
i *= -1;
if(argv[1][i] == '\0' && typenames[j][i] == ASCII_US) for(argv += 2; *argv != NULL; ++argv)
t = typenames[j][i+1]; for(j = 0; argv[0][j] != '\0'; ++j)
if(!ctypes[i].f(argv[0][j]))
for(i = 2; i < argc; ++i) return 1;
if(!stris(t, argv[i]))
return 1;
return 0; return 0;
} }