From 1a520f3c6af55686787656f561548d96a82d01cb Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 4 Aug 2019 17:15:20 -0400 Subject: update license and clean up a bit --- strings.c | 95 +++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 59 insertions(+), 36 deletions(-) diff --git a/strings.c b/strings.c index f4245b1..02158f5 100644 --- a/strings.c +++ b/strings.c @@ -1,33 +1,45 @@ /* * UNG's Not GNU - * - * Copyright (c) 2011, Jakob Kaivo - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * + * Copyright (c) 2011-2019, Jakob Kaivo + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ +#define _POSIX_C_SOURCE 2 #include #include #include #include -#include +#include -const char *strings_desc = "find printable strings in files"; -const char *strings_inv = "strings [-a] [-t format] -[n number] [file...]"; - -static int strings(FILE * f, int number, char format) +static int strings(const char *path, int number, char format) { + FILE *f = stdin; + if (path && strcmp(path, "-")) { + f = fopen(path, "rb"); + } + + if (f == NULL) { + return 1; + } + char buf[BUFSIZ]; char tbuf[BUFSIZ]; int nread; @@ -67,50 +79,61 @@ static int strings(FILE * f, int number, char format) offset++; } } + + if (f != stdin) { + fclose(f); + } + return 0; } -int main(int argc, char **argv) +int main(int argc, char *argv[]) { int c; int number = 4; char format = 0; char *end; - while ((c = getopt(argc, argv, ":an:t:")) != -1) { + while ((c = getopt(argc, argv, "an:t:")) != -1) { switch (c) { case 'a': - // Always scan the entire file + /* Ignored. Always scan the whole file. */ break; + case 'n': number = strtol(optarg, &end, 10); if (end != NULL && strlen(end) > 0) return 1; break; + case 't': - if (strlen(optarg) != 1) + if (strlen(optarg) != 1) { + fprintf(stderr, "strings: invalid format %s\n", optarg); return 1; + } + switch (optarg[0]) { + case 'd': + case 'o': + case 'x': + /* OK */ + break; + + default: + fprintf(stderr, "strings: invalid format %s\n", optarg); + } format = optarg[0]; break; + default: return 1; } } - if ((format != 0 && format != 'd' && format != 'o' && format != 'x') - || number < 1) - return 1; - - if (optind >= argc) { - strings(stdin, number, format); - } + int ret = 0; - while (optind < argc) { - FILE *f = fopen(argv[optind], "r"); - strings(f, number, format); - fclose(f); - optind++; - } + do { + ret |= strings(argv[optind], number, format); + } while (optind++ < argc); return 0; } -- cgit v1.2.1