blob: 0d5576e9b2aea464567bb8540cbec3f7c70e6e22 [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
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;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000024 unsigned opt;
Rob Landley16cd02e2005-06-07 03:21:20 +000025 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
Denis Vlasenko67b23e62006-10-03 21:00:06 +000031 opt = getopt32(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
Denis Vlasenko13858992006-10-08 12:49:22 +000037 n = xatoul_range(n_arg, 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 {
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000048 file = fopen_or_warn(*argv, "r");
Denis Vlasenko13858992006-10-08 12:49:22 +000049 if (file) {
Rob Landley16cd02e2005-06-07 03:21:20 +000050PIPE:
51 count = 0;
52 do {
53 c = fgetc(file);
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000054 if (isprint(c) || c == '\t') {
Rob Landley16cd02e2005-06-07 03:21:20 +000055 if (i <= n) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000056 string[i] = c;
Rob Landley16cd02e2005-06-07 03:21:20 +000057 } else {
58 putchar(c);
59 }
60 if (i == n) {
61 if (opt & PRINT_NAME) {
62 printf(fmt, *argv);
63 }
64 if (opt & PRINT_OFFSET) {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000065 printf("%7lo ", count - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000066 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000067 printf("%s", string);
68 }
Eric Andersen92b7e7b2003-03-13 18:49:45 +000069 i++;
Rob Landley16cd02e2005-06-07 03:21:20 +000070 } else {
71 if (i > n) {
Glenn L McGrathbb136242003-08-30 12:38:13 +000072 putchar('\n');
Rob Landley16cd02e2005-06-07 03:21:20 +000073 }
74 i = 0;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000075 }
76 count++;
Rob Landley16cd02e2005-06-07 03:21:20 +000077 } while (c != EOF);
Denis Vlasenkoddec5af2006-10-26 23:25:17 +000078 fclose_if_not_stdin(file);
Rob Landley16cd02e2005-06-07 03:21:20 +000079 } else {
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000080 status = EXIT_FAILURE;
Rob Landley16cd02e2005-06-07 03:21:20 +000081 }
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000082 } while (--argc > 0);
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}