busybox/miscutils/strings.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * strings implementation for busybox
   4 *
   5 * Copyright 2003 Tito Ragusa <farmatito@tiscali.it>
   6 *
   7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
   8 */
   9
  10#include "libbb.h"
  11
  12#define WHOLE_FILE              1
  13#define PRINT_NAME              2
  14#define PRINT_OFFSET    4
  15#define SIZE                    8
  16
  17int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  18int strings_main(int argc UNUSED_PARAM, char **argv)
  19{
  20        int n, c, status = EXIT_SUCCESS;
  21        unsigned count;
  22        off_t offset;
  23        FILE *file;
  24        char *string;
  25        const char *fmt = "%s: ";
  26        const char *n_arg = "4";
  27
  28        getopt32(argv, "afon:", &n_arg);
  29        /* -a is our default behaviour */
  30        /*argc -= optind;*/
  31        argv += optind;
  32
  33        n = xatou_range(n_arg, 1, INT_MAX);
  34        string = xzalloc(n + 1);
  35        n--;
  36
  37        if (!*argv) {
  38                fmt = "{%s}: ";
  39                *--argv = (char *)bb_msg_standard_input;
  40        }
  41
  42        do {
  43                file = fopen_or_warn_stdin(*argv);
  44                if (!file) {
  45                        status = EXIT_FAILURE;
  46                        continue;
  47                }
  48                offset = 0;
  49                count = 0;
  50                do {
  51                        c = fgetc(file);
  52                        if (isprint(c) || c == '\t') {
  53                                if (count > n) {
  54                                        bb_putchar(c);
  55                                } else {
  56                                        string[count] = c;
  57                                        if (count == n) {
  58                                                if (option_mask32 & PRINT_NAME) {
  59                                                        printf(fmt, *argv);
  60                                                }
  61                                                if (option_mask32 & PRINT_OFFSET) {
  62                                                        printf("%7"OFF_FMT"o ", offset - n);
  63                                                }
  64                                                fputs(string, stdout);
  65                                        }
  66                                        count++;
  67                                }
  68                        } else {
  69                                if (count > n) {
  70                                        bb_putchar('\n');
  71                                }
  72                                count = 0;
  73                        }
  74                        offset++;
  75                } while (c != EOF);
  76                fclose_if_not_stdin(file);
  77        } while (*++argv);
  78
  79        if (ENABLE_FEATURE_CLEAN_UP)
  80                free(string);
  81
  82        fflush_stdout_and_exit(status);
  83}
  84