summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-04 17:15:20 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-04 17:15:20 -0400
commit1a520f3c6af55686787656f561548d96a82d01cb (patch)
treeb10349e78f318cc011c062a21d70468399db030e
parent708abfc1b9422e7dcff6c58a0db5d55536bd077b (diff)
update license and clean up a bit
-rw-r--r--strings.c95
1 files 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 <jakob@kaivo.net>
- *
- * 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 <jkk@ung.org>
+ *
+ * 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
-#include <getopt.h>
+#include <unistd.h>
-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;
}