blob: 9f5018244445a2b9f6504794afa89e3d677897e1 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenef5e8f82002-11-07 02:09:37 +00008 */
9
Pere Orga5bc8c002011-04-11 03:29:49 +020010//usage:#define strings_trivial_usage
11//usage: "[-afo] [-n LEN] [FILE]..."
12//usage:#define strings_full_usage "\n\n"
13//usage: "Display printable strings in a binary file\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage: "\n -a Scan whole file (default)"
15//usage: "\n -f Precede strings with filenames"
16//usage: "\n -n LEN At least LEN characters form a string (default 4)"
17//usage: "\n -o Precede strings with decimal offsets"
18
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Eric Andersenef5e8f82002-11-07 02:09:37 +000020
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020021#define WHOLE_FILE 1
22#define PRINT_NAME 2
23#define PRINT_OFFSET 4
24#define SIZE 8
Rob Landley16cd02e2005-06-07 03:21:20 +000025
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000026int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000027int strings_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenef5e8f82002-11-07 02:09:37 +000028{
Denis Vlasenko787d9262007-06-17 12:19:07 +000029 int n, c, status = EXIT_SUCCESS;
Denis Vlasenko787d9262007-06-17 12:19:07 +000030 unsigned count;
31 off_t offset;
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000032 FILE *file;
Rob Landley16cd02e2005-06-07 03:21:20 +000033 char *string;
34 const char *fmt = "%s: ";
Denis Vlasenkob6aae0f2007-01-29 22:51:25 +000035 const char *n_arg = "4";
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000036
Denis Vlasenko4b570942008-11-23 14:58:14 +000037 getopt32(argv, "afon:", &n_arg);
Rob Landley16cd02e2005-06-07 03:21:20 +000038 /* -a is our default behaviour */
Denis Vlasenko787d9262007-06-17 12:19:07 +000039 /*argc -= optind;*/
Eric Andersenef5e8f82002-11-07 02:09:37 +000040 argv += optind;
41
Denis Vlasenko787d9262007-06-17 12:19:07 +000042 n = xatou_range(n_arg, 1, INT_MAX);
Rob Landleya6e131d2006-05-29 06:43:55 +000043 string = xzalloc(n + 1);
Rob Landley16cd02e2005-06-07 03:21:20 +000044 n--;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000045
Denis Vlasenko787d9262007-06-17 12:19:07 +000046 if (!*argv) {
Rob Landley16cd02e2005-06-07 03:21:20 +000047 fmt = "{%s}: ";
Denis Vlasenko787d9262007-06-17 12:19:07 +000048 *--argv = (char *)bb_msg_standard_input;
Eric Andersen65ddf772003-01-13 23:19:31 +000049 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000050
Rob Landley16cd02e2005-06-07 03:21:20 +000051 do {
Denis Vlasenko62a90cd2008-03-17 09:07:36 +000052 file = fopen_or_warn_stdin(*argv);
Denis Vlasenko787d9262007-06-17 12:19:07 +000053 if (!file) {
54 status = EXIT_FAILURE;
55 continue;
56 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000057 offset = 0;
58 count = 0;
59 do {
60 c = fgetc(file);
Denys Vlasenko8684cbb2009-11-18 11:34:43 +010061 if (isprint_asciionly(c) || c == '\t') {
Denis Vlasenko787d9262007-06-17 12:19:07 +000062 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000063 bb_putchar(c);
Denis Vlasenko787d9262007-06-17 12:19:07 +000064 } else {
65 string[count] = c;
66 if (count == n) {
Denis Vlasenko4b570942008-11-23 14:58:14 +000067 if (option_mask32 & PRINT_NAME) {
Rob Landley16cd02e2005-06-07 03:21:20 +000068 printf(fmt, *argv);
69 }
Denis Vlasenko4b570942008-11-23 14:58:14 +000070 if (option_mask32 & PRINT_OFFSET) {
Denis Vlasenko787d9262007-06-17 12:19:07 +000071 printf("%7"OFF_FMT"o ", offset - n);
Rob Landley16cd02e2005-06-07 03:21:20 +000072 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000073 fputs(string, stdout);
Eric Andersen92b7e7b2003-03-13 18:49:45 +000074 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000075 count++;
Eric Andersen92b7e7b2003-03-13 18:49:45 +000076 }
Denis Vlasenko787d9262007-06-17 12:19:07 +000077 } else {
78 if (count > n) {
Denis Vlasenko4daad902007-09-27 10:20:47 +000079 bb_putchar('\n');
Denis Vlasenko787d9262007-06-17 12:19:07 +000080 }
81 count = 0;
82 }
83 offset++;
84 } while (c != EOF);
85 fclose_if_not_stdin(file);
86 } while (*++argv);
Rob Landley658d2cf2005-09-08 03:11:58 +000087
Bernhard Reutner-Fischer08173132006-02-27 22:34:41 +000088 if (ENABLE_FEATURE_CLEAN_UP)
89 free(string);
Rob Landley658d2cf2005-09-08 03:11:58 +000090
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000091 fflush_stdout_and_exit(status);
Eric Andersenef5e8f82002-11-07 02:09:37 +000092}