1
0

independ str(1)

This commit is contained in:
dtb
2022-09-13 23:54:18 -04:00
parent 97c96d1063
commit 278f8f57a7
4 changed files with 47 additions and 15 deletions

28
str/Makefile Normal file
View File

@@ -0,0 +1,28 @@
all: str
clean:
rm -rf ../dist/str ../dist/str.tar ../dist/str.tar.gz str
dist: ../dist/str.tar.gz
sane: str.c ../include/sysexits.h
$(CC) -DDONT_USE_SYSTEM_SYSEXITS -o str str.c
str: str.c
$(CC) -o str str.c
../dist/str: str
mkdir -p ../dist/str.tmp/bin/ ../dist/str.tmp/share/man/man1/
cp str ../dist/str.tmp/bin/str
cp str.1 ../dist/str.tmp/share/man/man1/str.1
mv ../dist/str.tmp ../dist/str
../dist/str.tar: ../dist/str
cd ../dist/str && pax -w -x ustar . >../str.tar.tmp
mv ../dist/str.tar.tmp ../dist/str.tar
../dist/str.tar.gz: ../dist/str.tar
gzip -c <../dist/str.tar >../dist/str.tar.gz.tmp
mv ../dist/str.tar.gz.tmp ../dist/str.tar.gz
.PHONY: all clean sane

35
str/str.1 Normal file
View File

@@ -0,0 +1,35 @@
.TH STRIS 1
.SH NAME
str \(en test the character types of string arguments
.SH SYNOPSIS
str
.RB [ type ]
.RB [ string... ]
.SH DESCRIPTION
Str tests each character in an arbitrary quantity of string arguments against the function of the same name within ctype(3).
.SH DIAGNOSTICS
Strexits successfully if all tests pass and unsuccessfully if a test failed.
.PP
Str will print a message to standard error and exit unsuccessfully if used improperly.
.SH BUGS
There's no way of knowing which argument failed the test without re-testing arguments individually.
.PP
If a character in a string isn't valid ASCII the behavior of this program is undefined.
.SH SEE ALSO
ctype(3), ascii(7)
.SH COPYRIGHT
Public domain.

53
str/str.c Normal file
View File

@@ -0,0 +1,53 @@
#include <ctype.h>
#include <stddef.h> /* NULL */
#include <stdio.h> /* fprintf(3) */
#include <string.h> /* strcmp(3) */
#ifdef DONT_USE_SYSTEM_SYSEXITS
# include "../include/sysexits.h" /* EX_USAGE */
#else
# include <sysexits.h> /* EX_USAGE */
#endif
static char *program_name = "str";
static struct {
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 }
};
int main(int argc, char *argv[]){
int ctype;
int i;
if(argc < 3){
usage: fprintf(stderr, "Usage: %s [type] [string...]\n",
argv[0] == NULL ? program_name : argv[0]
);
return EX_USAGE;
}
for(ctype = 0; ctype < (sizeof(ctypes)/sizeof(ctypes[0])); ++ctype)
if(strcmp(argv[1], ctypes[ctype].name) == 0)
goto pass;
goto usage;
pass: for(argv += 2; *argv != NULL; ++argv)
for(i = 0; argv[0][i] != '\0'; ++i)
if(!ctypes[ctype].f(argv[0][i]))
return 1;
return 0;
}