blob: 3e02b53afd9ec3d1f2f4d8fe3cc619ecaf00db54 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000010#include "libbb.h"
Eric Andersenef5e8f82002-11-07 02:09:37 +000011
Rob Landley16cd02e2005-06-07 03:21:20 +000012#define WHOLE_FILE 1
13#define PRINT_NAME 2
14#define PRINT_OFFSET 4
15#define SIZE 8
16
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000017int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000018int strings_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenef5e8f82002-11-07 02:09:37 +000019{
Denis Vlasenko787d9262007-06-17 12:19:07 +000020 int n, c, status = EXIT_SUCCESS;
Denis Vlasenko67b23e62006-10-03 21:00:06 +000021 unsigned opt;
Denis Vlasenko787d9262007-06-17 12:19:07 +000022 unsigned count;
23 off_t offset;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000024 FILE *file;
Rob Landley16cd02e2005-06-07 03:21:20 +000025 char *string;
26 const char *fmt = "%s: ";
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000027 const char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000028
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000029 opt = getopt32(argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000030 /* -a is our default behaviour */
Denis Vlasenko787d9262007-06-17 12:19:07 +000031 /*argc -= optind;*/
Eric Andersenef5e8f82002-11-07 02:09:37 +000032 argv += optind;
33
Denis Vlasenko787d9262007-06-17 12:19:07 +000034 n = xatou_range(n_arg, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000035 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000036 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000037
Denis Vlasenko787d9262007-06-17 12:19:07 +000038 if (!*argv) {
Rob Landley16cd02e2005-06-07 03:21:20 +000039 fmt = "{%s}: ";
Denis Vlasenko787d9262007-06-17 12:19:07 +000040 *--argv = (char *)bb_msg_standard_input;
Eric Andersen65ddf772003-01-13 23:19:31 +000041 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000042
Rob Landley16cd02e2005-06-07 03:21:20 +000043 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000044 file = fopen_or_warn_stdin(*argv);
Denis Vlasenko787d9262007-06-17 12:19:07 +000045 if (!file) {
46 status = EXIT_FAILURE;
47 continue;
48 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000049 offset = 0;
50 count = 0;
51 do {
52 c = fgetc(file);
53 if (isprint(c) || c == '\t') {
54 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000055 bb_putchar(c);
Denis Vlasenko787d9262007-06-17 12:19:07 +000056 } else {
57 string[count] = c;
58 if (count == n) {
Rob Landley16cd02e2005-06-07 03:21:20 +000059 if (opt & PRINT_NAME) {
60 printf(fmt, *argv);
61 }
62 if (opt & PRINT_OFFSET) {
Denis Vlasenko787d9262007-06-17 12:19:07 +000063 printf("%7"OFF_FMT"o ", offset - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000064 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000065 fputs(string, stdout);
Eric Andersen92b7e7b2003-03-13 18:49:45 +000066 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000067 count++;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000068 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000069 } else {
70 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000071 bb_putchar('\n');
Denis Vlasenko787d9262007-06-17 12:19:07 +000072 }
73 count = 0;
74 }
75 offset++;
76 } while (c != EOF);
77 fclose_if_not_stdin(file);
78 } while (*++argv);
Rob Landley658d2cf2005-09-08 03:11:58 +000079
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000080 if (ENABLE_FEATURE_CLEAN_UP)
81 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000082
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000083 fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000084}