blob: 9a76c85346eb1550de242a0e0a16ee8a11546799 [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>
6 *
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
Bernhard Reutner-Fischerc89982d2006-06-03 19:49:21 +000010#include "busybox.h"
Eric Andersen65ddf772003-01-13 23:19:31 +000011#include <stdio.h>
12#include <stdlib.h>
Eric Andersen65ddf772003-01-13 23:19:31 +000013#include <getopt.h>
Eric Andersenef5e8f82002-11-07 02:09:37 +000014#include <ctype.h>
Eric Andersenef5e8f82002-11-07 02:09:37 +000015
Rob Landley16cd02e2005-06-07 03:21:20 +000016#define WHOLE_FILE 1
17#define PRINT_NAME 2
18#define PRINT_OFFSET 4
19#define SIZE 8
20
Eric Andersen65ddf772003-01-13 23:19:31 +000021int strings_main(int argc, char **argv)
Eric Andersenef5e8f82002-11-07 02:09:37 +000022{
Rob Landley16cd02e2005-06-07 03:21:20 +000023 int n, c, i = 0, status = EXIT_SUCCESS;
24 unsigned long opt;
25 unsigned long count;
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: ";
29 char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000030
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000031 opt = bb_getopt_ulflags(argc, argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000032 /* -a is our default behaviour */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000033
Eric Andersenef5e8f82002-11-07 02:09:37 +000034 argc -= optind;
35 argv += optind;
36
Rob Landley16cd02e2005-06-07 03:21:20 +000037 n = bb_xgetlarg(n_arg, 10, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000038 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000039 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000040
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000041 if (argc == 0) {
Rob Landley16cd02e2005-06-07 03:21:20 +000042 fmt = "{%s}: ";
43 *argv = (char *)bb_msg_standard_input;
44 goto PIPE;
Eric Andersen65ddf772003-01-13 23:19:31 +000045 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000046
Rob Landley16cd02e2005-06-07 03:21:20 +000047 do {
48 if ((file = bb_wfopen(*argv, "r"))) {
49PIPE:
50 count = 0;
51 do {
52 c = fgetc(file);
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000053 if (isprint(c) || c == '\t') {
Rob Landley16cd02e2005-06-07 03:21:20 +000054 if (i <= n) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000055 string[i] = c;
Rob Landley16cd02e2005-06-07 03:21:20 +000056 } else {
57 putchar(c);
58 }
59 if (i == n) {
60 if (opt & PRINT_NAME) {
61 printf(fmt, *argv);
62 }
63 if (opt & PRINT_OFFSET) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000064 printf("%7lo ", count - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000065 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000066 printf("%s", string);
67 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000068 i++;
Rob Landley16cd02e2005-06-07 03:21:20 +000069 } else {
70 if (i > n) {
Glenn L McGrathbb136242003-08-30 12:38:13 +000071 putchar('\n');
Rob Landley16cd02e2005-06-07 03:21:20 +000072 }
73 i = 0;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000074 }
75 count++;
Rob Landley16cd02e2005-06-07 03:21:20 +000076 } while (c != EOF);
Glenn L McGrathe01c5502003-08-29 15:48:37 +000077 bb_fclose_nonstdin(file);
Rob Landley16cd02e2005-06-07 03:21:20 +000078 } else {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000079 status = EXIT_FAILURE;
Rob Landley16cd02e2005-06-07 03:21:20 +000080 }
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000081 } while (--argc > 0);
Rob Landley658d2cf2005-09-08 03:11:58 +000082
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000083 if (ENABLE_FEATURE_CLEAN_UP)
84 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000085
Rob Landley16cd02e2005-06-07 03:21:20 +000086 bb_fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000087}