70 lines
1.8 KiB
C
70 lines
1.8 KiB
C
#include <ctype.h>
|
|
#include <stddef.h> /* NULL */
|
|
#include <stdio.h> /* fprintf(3) */
|
|
#include <string.h> /* strcmp(3) */
|
|
#if !defined EX_USAGE
|
|
# include <sysexits.h>
|
|
#endif
|
|
#if defined INCLUDE_ISVALUE_C
|
|
# include "isvalue.c"
|
|
/* This is a special addition to the command that lets `str isvalue "$input"`
|
|
* function as a lightweight replacement to the common `test -n "$input"` or
|
|
* `[ -n "$input" ]`. This will speed up your shellscript execution by a tad
|
|
* ONLY if test(1) isn't already built into your shell (most of the time, it
|
|
* is, and saves you the overhead of spawning a new process, which is greater
|
|
* than the savings of switching from test(1) to this program). */
|
|
#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 }
|
|
#if defined _isvalue_c
|
|
, { "isvalue", isvalue }
|
|
#endif
|
|
};
|
|
|
|
int main(int argc, char *argv[]){
|
|
int ctype;
|
|
int i;
|
|
int r;
|
|
|
|
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, r = 1; *argv != NULL; ++argv)
|
|
for(i = 0; argv[0][i] != '\0'; ++i)
|
|
/* First checks if argv[0][i] is valid ASCII; ctypes(3)
|
|
* don't handle non-ASCII.
|
|
* This is bad. */
|
|
if(argv[0][i] < 0x80 && !ctypes[ctype].f(argv[0][i]))
|
|
return 1;
|
|
else
|
|
r = 0;
|
|
|
|
return r;
|
|
}
|