source: MondoRescue/branches/2.2.5/mindi-busybox/miscutils/strings.c@ 1765

Last change on this file since 1765 was 1765, checked in by Bruno Cornec, 16 years ago

Update to busybox 1.7.2

File size: 1.6 KB
RevLine 
[821]1/* vi: set sw=4 ts=4: */
2/*
3 * strings implementation for busybox
4 *
[1765]5 * Copyright Tito Ragusa <farmatito@tiscali.it>
[821]6 *
7 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 */
9
10#include <getopt.h>
11
[1765]12#include "libbb.h"
13
[821]14#define WHOLE_FILE 1
15#define PRINT_NAME 2
16#define PRINT_OFFSET 4
17#define SIZE 8
18
[1765]19int strings_main(int argc, char **argv);
[821]20int strings_main(int argc, char **argv)
21{
[1765]22 int n, c, status = EXIT_SUCCESS;
23 unsigned opt;
24 unsigned count;
25 off_t offset;
[821]26 FILE *file = stdin;
27 char *string;
28 const char *fmt = "%s: ";
[1765]29 const char *n_arg = "4";
[821]30
[1765]31 opt = getopt32(argv, "afon:", &n_arg);
[821]32 /* -a is our default behaviour */
[1765]33 /*argc -= optind;*/
[821]34 argv += optind;
35
[1765]36 n = xatou_range(n_arg, 1, INT_MAX);
[821]37 string = xzalloc(n + 1);
38 n--;
39
[1765]40 if (!*argv) {
[821]41 fmt = "{%s}: ";
[1765]42 *--argv = (char *)bb_msg_standard_input;
[821]43 goto PIPE;
44 }
45
46 do {
[1765]47 file = fopen_or_warn(*argv, "r");
48 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) {
59 putchar(c);
60 } else {
61 string[count] = c;
62 if (count == n) {
[821]63 if (opt & PRINT_NAME) {
64 printf(fmt, *argv);
65 }
66 if (opt & PRINT_OFFSET) {
[1765]67 printf("%7"OFF_FMT"o ", offset - n);
[821]68 }
[1765]69 fputs(string, stdout);
[821]70 }
[1765]71 count++;
[821]72 }
[1765]73 } else {
74 if (count > n) {
75 putchar('\n');
76 }
77 count = 0;
78 }
79 offset++;
80 } while (c != EOF);
81 fclose_if_not_stdin(file);
82 } while (*++argv);
[821]83
84 if (ENABLE_FEATURE_CLEAN_UP)
85 free(string);
86
[1765]87 fflush_stdout_and_exit(status);
[821]88}
Note: See TracBrowser for help on using the repository browser.