blob: 49f2cf31e040de59cc926651a09d39c22bba5dde [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
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
Denis Vlasenko06af2162007-02-03 17:28:39 +000021int strings_main(int argc, char **argv);
Eric Andersen65ddf772003-01-13 23:19:31 +000022int strings_main(int argc, char **argv)
Eric Andersenef5e8f82002-11-07 02:09:37 +000023{
Rob Landley16cd02e2005-06-07 03:21:20 +000024 int n, c, i = 0, status = EXIT_SUCCESS;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000025 unsigned opt;
Rob Landley16cd02e2005-06-07 03:21:20 +000026 unsigned long count;
Glenn L McGrathe16ab472003-09-15 14:22:37 +000027 FILE *file = stdin;
Rob Landley16cd02e2005-06-07 03:21:20 +000028 char *string;
29 const char *fmt = "%s: ";
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000030 const char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000031
Denis Vlasenko67b23e62006-10-03 21:00:06 +000032 opt = getopt32(argc, argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000033 /* -a is our default behaviour */
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000034
Eric Andersenef5e8f82002-11-07 02:09:37 +000035 argc -= optind;
36 argv += optind;
37
Denis Vlasenko13858992006-10-08 12:49:22 +000038 n = xatoul_range(n_arg, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000039 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000040 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000042 if (argc == 0) {
Rob Landley16cd02e2005-06-07 03:21:20 +000043 fmt = "{%s}: ";
44 *argv = (char *)bb_msg_standard_input;
45 goto PIPE;
Eric Andersen65ddf772003-01-13 23:19:31 +000046 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000047
Rob Landley16cd02e2005-06-07 03:21:20 +000048 do {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000049 file = fopen_or_warn(*argv, "r");
Denis Vlasenko13858992006-10-08 12:49:22 +000050 if (file) {
Rob Landley16cd02e2005-06-07 03:21:20 +000051PIPE:
52 count = 0;
53 do {
54 c = fgetc(file);
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000055 if (isprint(c) || c == '\t') {
Rob Landley16cd02e2005-06-07 03:21:20 +000056 if (i <= n) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000057 string[i] = c;
Rob Landley16cd02e2005-06-07 03:21:20 +000058 } else {
59 putchar(c);
60 }
61 if (i == n) {
62 if (opt & PRINT_NAME) {
63 printf(fmt, *argv);
64 }
65 if (opt & PRINT_OFFSET) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000066 printf("%7lo ", count - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000067 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000068 printf("%s", string);
69 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000070 i++;
Rob Landley16cd02e2005-06-07 03:21:20 +000071 } else {
72 if (i > n) {
Glenn L McGrathbb136242003-08-30 12:38:13 +000073 putchar('\n');
Rob Landley16cd02e2005-06-07 03:21:20 +000074 }
75 i = 0;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000076 }
77 count++;
Rob Landley16cd02e2005-06-07 03:21:20 +000078 } while (c != EOF);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000079 fclose_if_not_stdin(file);
Rob Landley16cd02e2005-06-07 03:21:20 +000080 } else {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000081 status = EXIT_FAILURE;
Rob Landley16cd02e2005-06-07 03:21:20 +000082 }
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000083 } while (--argc > 0);
Rob Landley658d2cf2005-09-08 03:11:58 +000084
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000085 if (ENABLE_FEATURE_CLEAN_UP)
86 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000087
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000088 fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000089}