blob: fea9edbed57f6ddf5de9f62b1fbf3149f67fa27e [file] [log] [blame]
Eric Andersenef5e8f82002-11-07 02:09:37 +00001/* vi: set sw=4 ts=4: */
2/*
3 * strings implementation for busybox
4 *
Denis Vlasenko4b570942008-11-23 14:58:14 +00005 * Copyright 2003 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 Vlasenko787d9262007-06-17 12:19:07 +000021 unsigned count;
22 off_t offset;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000023 FILE *file;
Rob Landley16cd02e2005-06-07 03:21:20 +000024 char *string;
25 const char *fmt = "%s: ";
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000026 const char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000027
Denis Vlasenko4b570942008-11-23 14:58:14 +000028 getopt32(argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000029 /* -a is our default behaviour */
Denis Vlasenko787d9262007-06-17 12:19:07 +000030 /*argc -= optind;*/
Eric Andersenef5e8f82002-11-07 02:09:37 +000031 argv += optind;
32
Denis Vlasenko787d9262007-06-17 12:19:07 +000033 n = xatou_range(n_arg, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000034 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000035 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000036
Denis Vlasenko787d9262007-06-17 12:19:07 +000037 if (!*argv) {
Rob Landley16cd02e2005-06-07 03:21:20 +000038 fmt = "{%s}: ";
Denis Vlasenko787d9262007-06-17 12:19:07 +000039 *--argv = (char *)bb_msg_standard_input;
Eric Andersen65ddf772003-01-13 23:19:31 +000040 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000041
Rob Landley16cd02e2005-06-07 03:21:20 +000042 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000043 file = fopen_or_warn_stdin(*argv);
Denis Vlasenko787d9262007-06-17 12:19:07 +000044 if (!file) {
45 status = EXIT_FAILURE;
46 continue;
47 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000048 offset = 0;
49 count = 0;
50 do {
51 c = fgetc(file);
52 if (isprint(c) || c == '\t') {
53 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000054 bb_putchar(c);
Denis Vlasenko787d9262007-06-17 12:19:07 +000055 } else {
56 string[count] = c;
57 if (count == n) {
Denis Vlasenko4b570942008-11-23 14:58:14 +000058 if (option_mask32 & PRINT_NAME) {
Rob Landley16cd02e2005-06-07 03:21:20 +000059 printf(fmt, *argv);
60 }
Denis Vlasenko4b570942008-11-23 14:58:14 +000061 if (option_mask32 & PRINT_OFFSET) {
Denis Vlasenko787d9262007-06-17 12:19:07 +000062 printf("%7"OFF_FMT"o ", offset - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000063 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000064 fputs(string, stdout);
Eric Andersen92b7e7b2003-03-13 18:49:45 +000065 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000066 count++;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000067 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000068 } else {
69 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000070 bb_putchar('\n');
Denis Vlasenko787d9262007-06-17 12:19:07 +000071 }
72 count = 0;
73 }
74 offset++;
75 } while (c != EOF);
76 fclose_if_not_stdin(file);
77 } while (*++argv);
Rob Landley658d2cf2005-09-08 03:11:58 +000078
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000079 if (ENABLE_FEATURE_CLEAN_UP)
80 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000081
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000082 fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000083}