sampson@killer.UUCP (Steve Sampson) (12/27/87)
Speaking of strings programs, here's a bare bones one:
/*
* S T R I N G S . C
*
* This program is used to display strings in binary files
*
* Public Domain (p) November 1987
* S. R. Sampson, No Rights Reserved
*/
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#define FALSE 0
#define TRUE ~FALSE
#define MAXLINE 1024
main(argc, argv)
int argc;
char *argv[];
{
int i, ercode, lptr, nchars, fd, in_string;
char *fname, inbuff[MAXLINE], outbuff[MAXLINE];
nchars = 3; /* Default string length */
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: string [n] <filename>\n");
fprintf(stderr, "Where n is the minimum width (default is 3)\n");
exit(0);
}
if (argc == 3) {
nchars = abs(atoi(argv[1]));
if (nchars < 1 || nchars > MAXLINE - 1) {
fprintf(stderr, "string: Maximum width is %d\n", MAXLINE - 1);
exit(1);
}
fname = argv[2];
}
else
fname = argv[1];
if ((fd = open(fname, O_RDONLY)) == -1) {
fprintf(stderr, "string: File `%s' Not Found\n", fname);
exit(1);
}
lptr = 0; /* Index into outbuff */
in_string = FALSE; /* Not in a string yet */
while ((ercode = read(fd, inbuff, MAXLINE)) > 0) {
for (i = 0; i < ercode; i++) {
/* Search entire buffer */
if (in_string && isprint(inbuff[i]))
/* Was in a string */
outbuff[lptr++] = inbuff[i];
else if (isprint(inbuff[i])) {
/* Start of a new string */
outbuff[lptr++] = inbuff[i];
in_string = TRUE; /* We're in a string now */
}
else if (in_string) {
/* End of string */
if (lptr >= nchars) {
/* Found a long-enough string */
outbuff[lptr] = '\0';
/* Terminate the string and print */
printf("%s\n", outbuff);
}
lptr = 0; /* Start at beginning again */
}
}
}
}
/* EOF */