2023-12-24 17:13:17 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023 DTB <trinity@trinity.moe>
|
2023-12-24 18:09:36 -07:00
|
|
|
* Copyright (c) 2023 Marceline Cramer <mars@tebibyte.media>
|
2023-12-24 17:13:17 -07:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stddef.h> /* NULL */
|
|
|
|
#include <stdio.h> /* fprintf(3) */
|
2023-12-25 15:58:24 -07:00
|
|
|
#include <stdlib.h> /* EXIT_FAILURE */
|
2023-12-24 17:13:17 -07:00
|
|
|
#include <string.h> /* strcmp(3) */
|
|
|
|
#if !defined EX_USAGE
|
2023-12-25 15:58:24 -07:00
|
|
|
# define EX_USAGE EXIT_FAILURE
|
2023-12-24 17:13:17 -07:00
|
|
|
#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;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if(argc >= 3){
|
|
|
|
for(ctype = 0; ctype < (sizeof ctypes) / (sizeof *ctypes);
|
|
|
|
++ctype)
|
|
|
|
if(strcmp(argv[1], ctypes[ctype].name) == 0)
|
|
|
|
goto pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "Usage: %s [type] [string...]\n",
|
|
|
|
argv[0] == NULL ? program_name : argv[0]);
|
|
|
|
|
|
|
|
return EX_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. */
|
2023-12-24 18:01:15 -07:00
|
|
|
if((unsigned char)argv[0][i] < 0x80 && !ctypes[ctype].f(argv[0][i]))
|
2023-12-24 17:13:17 -07:00
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
r = 0;
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|