* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1980 Regents of the University of California.\n\
static char sccsid
[] = "@(#)strings.c 5.3 (Berkeley) %G%";
#define DEF_LEN 4 /* default minimum string length */
#define EOS (char)NULL /* end of string */
#define ERR -1 /* general error */
#define ERREXIT 1 /* error exit */
#define NO 0 /* false/no */
#define OK 0 /* ok exit */
#define YES 1 /* true/yes */
#define ISSTR(ch) (isascii(ch) && (isprint(ch) || ch == '\t'))
typedef struct exec EXEC
; /* struct exec cast */
static long foff
; /* offset in the file */
static int hcnt
, /* head count */
head_len
, /* length of header */
read_len
; /* length to read */
static u_char hbfr
[sizeof(EXEC
)]; /* buffer for struct exec */
register int ch
, /* character */
cnt
; /* general counter */
register u_char
*C
; /* bfr pointer */
EXEC
*head
; /* exec header pointer */
int minlen
= DEF_LEN
; /* minimum string length */
short asdata
= NO
, /* look in everything */
oflg
; /* print octal location */
u_char
*bfr
; /* collection buffer */
char *file
, /* file name for error */
* for backward compatibility, allow '-' to specify 'a' flag; no
* longer documented in the man page or usage string.
for (++argv
;*argv
&& **argv
== '-';++argv
) {
for (cnt
= 1;(*argv
)[cnt
];++cnt
)
default: /* getopt message compatible */
if (!isdigit((*argv
)[cnt
])) {
fprintf(stderr
,"strings: illegal option -- %c\nusage: strings [-ao] [-#] [file ... ]\n",(*argv
)[cnt
]);
minlen
= atoi(*argv
+ 1);
if (!(bfr
= (u_char
*)malloc((u_int
)minlen
))) {
fputs("strings: unable to allocate space.\n",stderr
);
if (!freopen(*argv
,"r",stdin
)) {
if ((head_len
= read(fileno(stdin
),(char *)head
,sizeof(EXEC
))) == ERR
) {
if (head_len
== sizeof(EXEC
) && !N_BADMAG(*head
)) {
foff
= N_TXTOFF(*head
) + head
->a_text
;
if (fseek(stdin
,foff
,L_SET
) == ERR
) {
for (cnt
= 0;(ch
= getch()) != EOF
;) {
printf("%07ld %s",foff
- minlen
,bfr
);
fputs((char *)bfr
,stdout
);
while ((ch
= getch()) != EOF
&& ISSTR(ch
))
* get next character from wherever
return((int)hbfr
[hcnt
++]);
if (read_len
== ERR
|| read_len
-- > 0)