blob: b7f5a0b57c4a46d08d0dd5bd6a4045f1d6544a8d [file] [log] [blame]
Eric Andersenef5e8f82002-11-07 02:09:37 +00001/* vi: set sw=4 ts=4: */
2/*
3 * strings implementation for busybox
4 *
Rob Landleyc0525762006-07-31 16:37:57 +00005 * Copyright Tito Ragusa <farmatito@tiscali.it>
Denis Vlasenko9213a9e2006-09-17 16:28:10 +00006 *
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +00007 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenef5e8f82002-11-07 02:09:37 +00008 */
9
Eric Andersen65ddf772003-01-13 23:19:31 +000010#include <getopt.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011
12#include "libbb.h"
Eric Andersenef5e8f82002-11-07 02:09:37 +000013
Rob Landley16cd02e2005-06-07 03:21:20 +000014#define WHOLE_FILE 1
15#define PRINT_NAME 2
16#define PRINT_OFFSET 4
17#define SIZE 8
18
Denis Vlasenko06af2162007-02-03 17:28:39 +000019int strings_main(int argc, char **argv);
Eric Andersen65ddf772003-01-13 23:19:31 +000020int strings_main(int argc, char **argv)
Eric Andersenef5e8f82002-11-07 02:09:37 +000021{
Denis Vlasenko787d9262007-06-17 12:19:07 +000022 int n, c, status = EXIT_SUCCESS;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000023 unsigned opt;
Denis Vlasenko787d9262007-06-17 12:19:07 +000024 unsigned count;
25 off_t offset;
Glenn L McGrathe16ab472003-09-15 14:22:37 +000026 FILE *file = stdin;
Rob Landley16cd02e2005-06-07 03:21:20 +000027 char *string;
28 const char *fmt = "%s: ";
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000029 const char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000031 opt = getopt32(argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000032 /* -a is our default behaviour */
Denis Vlasenko787d9262007-06-17 12:19:07 +000033 /*argc -= optind;*/
Eric Andersenef5e8f82002-11-07 02:09:37 +000034 argv += optind;
35
Denis Vlasenko787d9262007-06-17 12:19:07 +000036 n = xatou_range(n_arg, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000037 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000038 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000039
Denis Vlasenko787d9262007-06-17 12:19:07 +000040 if (!*argv) {
Rob Landley16cd02e2005-06-07 03:21:20 +000041 fmt = "{%s}: ";
Denis Vlasenko787d9262007-06-17 12:19:07 +000042 *--argv = (char *)bb_msg_standard_input;
Rob Landley16cd02e2005-06-07 03:21:20 +000043 goto PIPE;
Eric Andersen65ddf772003-01-13 23:19:31 +000044 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000045
Rob Landley16cd02e2005-06-07 03:21:20 +000046 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000047 file = fopen_or_warn(*argv, "r");
Denis Vlasenko787d9262007-06-17 12:19:07 +000048 if (!file) {
49 status = EXIT_FAILURE;
50 continue;
51 }
52 PIPE:
53 offset = 0;
54 count = 0;
55 do {
56 c = fgetc(file);
57 if (isprint(c) || c == '\t') {
58 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000059 bb_putchar(c);
Denis Vlasenko787d9262007-06-17 12:19:07 +000060 } else {
61 string[count] = c;
62 if (count == n) {
Rob Landley16cd02e2005-06-07 03:21:20 +000063 if (opt & PRINT_NAME) {
64 printf(fmt, *argv);
65 }
66 if (opt & PRINT_OFFSET) {
Denis Vlasenko787d9262007-06-17 12:19:07 +000067 printf("%7"OFF_FMT"o ", offset - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000068 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000069 fputs(string, stdout);
Eric Andersen92b7e7b2003-03-13 18:49:45 +000070 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000071 count++;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000072 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000073 } else {
74 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000075 bb_putchar('\n');
Denis Vlasenko787d9262007-06-17 12:19:07 +000076 }
77 count = 0;
78 }
79 offset++;
80 } while (c != EOF);
81 fclose_if_not_stdin(file);
82 } while (*++argv);
Rob Landley658d2cf2005-09-08 03:11:58 +000083
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000084 if (ENABLE_FEATURE_CLEAN_UP)
85 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000086
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000087 fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000088}