[net.sources] Public domain "strings" command

lee@unmvax.UUCP (04/15/84)

 This is a public domain version of the command "strings" available
in the Berkeley distributions. It is slightly more intellgent than
the aforementioned version. It was written for a friend on a Plexus
P-40 running V7. It works on his machine, but how well does Plexus
conform to V7? I dunno, that is the only example I have. In the 4.2 man
page it says that strings reports addresses in octal. The standard command
does not, so this version does not either. Simple change if you want it to
though...

			--Lee (Ward)
		{ucbvax,gatech,convex}!unmvax!lee

   To unarchive, remove the first line through and
 including the dashed line. Then type:

   % sh <thisfilename>

-------------------------------------------
: Run this with sh NOT csh!
echo extracting strings.l
cat << //*FUNKYSTUFF*// > strings.l
.TH STRINGS L "14 April 1984"
.UC
.SH NAME
strings \\- find printable strings in an object, or other file
.SH SYNOPSIS
.B strings
[
.B \\-
] [
.B \\-o
] [
\\fB\\-\\fInumber\\fR
] file ...
.SH DESCRIPTION
.I Strings
looks for ascii strings in a binary file.
A string is any sequence of 4 or more printing characters ending with
a newline or a null.
Unless the
.B \\-
flag is given,
.I strings
only looks in the initialized data space of object files.
If the
.B \\-o
flag is given, then each string is preceded by its offset in the
file (in decimal).
If the
\\fB\\-\\fInumber\\fR
flag is given then number is used as the minimum string length
rather than 4.
.SH "SEE ALSO"
od(1)
.SH AUTHOR
Lee Ward, University of New Mexico
.SH BUGS
Though the string algorithm is a bit more intelligent than the Berkeley
version, it is still pretty stupid.
//*FUNKYSTUFF*//
echo extracting strings.c
cat << //*FUNKYSTUFF*// > strings.c
/*
 * Public domain strings
 *
 * Conforms (closely) to the Berkeley 4.2 BSD manual page.
 *
 * Lee Ward
 * University of New Mexico
 * 4/14/84
 */

# include <stdio.h>
# include <a.out.h>
# include <ctype.h>

/* Is the below right? Oh well.... */
# ifdef V7
# define	N_BADMAG(x)	!((x).a_magic == A_MAGIC1 || \\
				    (x).a_magic == A_MAGIC2 || \\
				    (x).a_magic == A_MAGIC3 || \\
				    (x).a_magic == A_MAGIC4)
# define	N_TXTOFF(x)	(x).a_magic == A_MAGIC2 || \\
				    (x).a_magic == A_MAGIC3 ? \\
				    8 * 1024 : sizeof(x)
# endif

int	countflag = 0;
int	dataflag = 0;

char	*Usgstr = "[-] [-o] [-minlength] file...";
int	minlength = 4;

extern int errno;

main (argc, argv)
	int argc;
	char *argv[];
{
	int x;
	char *aptr;
	struct exec exhdr;

	argv[argc] = NULL;
	for (x = 1; argc > 1 && *(aptr = argv[x]) == '-'; x++,argc--) {
		++aptr;
		if (*aptr == NULL)
			dataflag++;
		else if (*aptr == 'o')
			countflag++;
		else if (isdigit(*aptr))
			minlength = atoi(aptr);
		else {
			fprintf (stderr, "Usage: %s %s\\n", argv[0], Usgstr);
			exit (1);
		}
	}

	do {
		if (argc > 1)
			if (freopen(argv[x++], "r", stdin) == NULL) {
				perror (argv[x - 1]);
				exit (errno);
			}
		if (!dataflag &&
		    fread(&exhdr, sizeof(exhdr), 1, stdin) == 1 &&
		    !N_BADMAG(exhdr)) {
			fseek(stdin,
			    (long )(N_TXTOFF(exhdr) + exhdr.a_text), 0);
			strings ((long )exhdr.a_data,
			    (long )(N_TXTOFF(exhdr) + exhdr.a_text));
		} else {
			fseek(stdin, 0L, 0);
			strings (-1L, 0L);
		}
	} while (argv[x] != NULL);
}

strings (count, loc)
	long count;
	long loc;
{
	char c, buf[BUFSIZ];
	char *bptr;

	bptr = buf;
	while (count--) {
		c = getchar();
		loc++;
		if (c == NULL || c == '\\n' || bptr >= buf + BUFSIZ - 1) {
			if (bptr - buf > minlength) {
				*bptr = NULL;
				if (countflag)
					printf ("%7D\\t", loc - 1 -
					    (long )( bptr - buf));
				printf ("%s\\n", buf);
			}
			bptr = buf;
		} else
			if (c < 0177 && c >= ' ')
				*bptr++ = c;
			else
				bptr = buf;
		if (feof(stdin) || ferror(stdin))
			break;
	}
}
//*FUNKYSTUFF*//

mark@cbosgd.UUCP (Mark Horton) (04/17/84)

It was really nice of someone to rewrite strings so there would be a
public domain version, but the copy from Berkeley is already in the
public domain.  (It was written at Berkeley, it did not come with 32V.)

It would be nice to have one version that works both on 4BSD and System V.
(It seems that strings uses some handy macros that only Berkeley has that
tell about the headers of a.out files.)  It's possible to tell the difference
using ifdefs, based on, say, L_ctermid from stdio.h, or N_BADMAG from a.out.h.)

ron@brl-vgr.ARPA (Ron Natalie <ron>) (04/18/84)

Warning:  I didn't try the STRINGS programs but PLEXUS's have funny
C compilers and loaders and many programs written on them don't compile
on REAL systems without being worked over.

-Ron

lee@unmvax.UUCP (04/23/84)

 My main concern was that the strings I posted was a bit better than the
Berkeley version. Less garbage. Also, it was written for a v7 system
(from Plexus co.). Maybe it'll work under sys V. Anybody try it? It is true
that I was confused on the "public domain" aspects. I apologize for
any insults you may have suffered....
-- 
			--Lee (Ward)
			{ucbvax,convex,gatech,pur-ee}!unmvax!lee