strings.c revision 1ba99545c96fd4f92872df411096dac283cd316e
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
* All Rights Reserved
*/
/*
* Copyright (c) 1987, 1988 Microsoft Corporation
* All Rights Reserved
*/
/*
* Copyright (c) 1979 Regents of the University of California
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include "a.out.h"
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include <libelf.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <widec.h>
#include <gelf.h>
#include <errno.h>
#define NOTOUT 0
#define AOUT 1
#define ELF 4
/* used to maintain a list of program sections to look in */
typedef struct sec_name {
char *name;
} sec_name_t;
/*
* function prototypes
*/
static void Usage();
static void find(long);
static int dirt(int, int);
/*
* Strings - extract strings from an object file for whatever
*
* The algorithm is to look for sequences of "non-junk" characters
* The variable "minlen" is the minimum length string printed.
* This helps get rid of garbage.
* Default minimum string length is 4 characters.
*
*/
#define DEF_MIN_STRING 4
static int tflg;
static char t_format;
static int aflg;
static int minlength = 0;
static int isClocale = 0;
static int rc = 0; /* exit code */
/*
* Returns 0 when sections have been successfully looked through,
* otherwise returns 1.
*/
static int
{
int found_sec;
int rc = 0;
return (1);
}
scn = 0;
found_sec = 0;
elf_errmsg(-1));
rc = 1;
continue;
}
if (seclistptr != NULL) {
char *scn_name;
/* Only look in the specified section(s). */
elf_errmsg(-1));
rc = 1;
continue;
} else {
found_sec = 1;
break;
}
}
}
} else {
/*
* Look through program sections that are
* loaded in memory.
*/
found_sec = 1;
}
}
if (found_sec == 1) {
}
}
return (rc);
}
int
int argc;
char *argv[];
{
int hsize;
int htype;
char *locale;
int opt;
int i;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
isClocale = 1;
}
/* check for non-standard "-" option */
for (i = 1; i < argc; i++) {
aflg++;
while (i < argc) {
i++;
}
argc--;
}
}
/* get options */
switch (opt) {
case 'a':
aflg++;
break;
case 'n':
10);
break;
case 'N':
== NULL)) {
"Cannot allocate memory: "
exit(1);
}
if (seclistptr == NULL) {
seclistptr = sptr;
} else {
}
break;
case 'o':
tflg++;
t_format = 'd';
break;
case 't':
tflg++;
t_format != 'x')
{
gettext("Invalid format\n"));
Usage();
}
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
minlength *= 10;
break;
default:
Usage();
}
}
/* if min string not specified, use default */
if (!minlength)
/* dynamic allocation of char buffer array */
exit(1);
}
/* for each file operand */
do {
rc = 1;
optind++;
continue;
}
optind++;
} else
aflg++;
if (aflg)
else {
}
switch (htype) {
case AOUT:
continue;
case ELF:
/*
* Will take care of COFF M32 and i386 also
* As well as ELF M32, i386 and Sparc (32-
* and 64-bit)
*/
continue;
case NOTOUT:
default:
if (!aflg)
continue;
}
return (rc);
}
static void
long cnt;
{
int c;
int cc;
int cr;
cc = 0;
if (tflg) {
switch (t_format) {
case 'd':
(void) printf("%7ld ",
break;
case 'o':
(void) printf("%7lo ",
break;
case 'x':
(void) printf("%7lx ",
break;
}
}
else
}
cc = 0;
}
}
}
static int
int c;
int cc;
{
int r_val;
if (isascii(c)) {
if (isprint(c)) {
/*
* If character count is greater than dynamic
* char buffer size, then increase char buffer size.
*/
gettext("Cannot allocate memory: %s\n"),
rc = 1;
return (0);
} else {
}
} else {
return (0);
}
}
return (1);
}
return (0);
}
if (isClocale)
return (0);
r_val = 0;
mbuf[0] = c;
if ((signed char)
break;
}
len1 = 1;
goto _unget;
}
gettext("Cannot allocate memory: %s\n"),
rc = 1;
return (0);
}
} else {
return (0);
}
}
}
return (r_val);
}
static int
int hsize;
{
case A_MAGIC1:
case A_MAGIC2:
case A_MAGIC3:
case A_MAGIC4:
return (NOTOUT);
else
return (AOUT);
default:
break;
}
}
static int
{
int fd;
return (NOTOUT);
}
return (NOTOUT);
}
case ELF_K_AR:
/*
* This should try to run strings on each element
* of the archive. For now, just search entire
* file (-a), as strings has always done
* for archives.
*/
case ELF_K_NONE:
return (NOTOUT);
}
return (NOTOUT);
}
return (NOTOUT);
}
return (ELF);
}
static void
Usage()
{
"Usage: strings [-a | -] [-t format | -o] [-n number | -number]"
"\n\t[-N name] [file]...\n"));
exit(1);
}