home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD2.mdf
/
tools
/
exe2bin
/
exe2com
/
strings.c
< prev
Wrap
Text File
|
1987-12-30
|
3KB
|
119 lines
/*
* S T R I N G S . C
*
* This program is used to display strings in binary files
*
* Steve Sampson, Public Domain (p) November 1987
*
* Updated 12/29/87 - P. Lyall (76703,4230)
*
* - bug fix: in_string was previously never reset to FALSE,
* resulting in erroneous marking of string start.
*
* - hex offset of each string start is now provided to ease
* hacking/patching of files.
*/
#include <stdio.h>
#include <ctype.h>
#ifdef MSDOS
#include <fcntl.h>
#define READ O_RDONLY
#define pflinit()
#else
#include <lowio.h>
#endif
#define FALSE 0
#define TRUE ~FALSE
#define DEFCHARS 3
#define MAXLINE 256
main(argc, argv)
int argc;
char *argv[];
{
int i, ercode, lptr, nchars = DEFCHARS, fd, in_string;
char *fname, inbuff[MAXLINE], outbuff[MAXLINE];
long sofar = 0L, sstart;
pflinit();
if (argc < 2 || argc > 3)
{
fprintf(stderr, "Usage: strings [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, "strings: Maximum width is %d\n", MAXLINE - 1);
exit(1);
}
fname = argv[2];
}
else
fname = argv[1];
if ((fd = open(fname, READ)) == -1)
{
fprintf(stderr, "strings: 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]))
/* We are in a string and are continuing */
outbuff[lptr++] = inbuff[i];
else
if (isprint(inbuff[i])) /* would this char start one ? */
{
outbuff[lptr++] = inbuff[i]; /* start new one */
in_string = TRUE; /* We're in a string now */
sstart = i+sofar; /* mark string start */
}
else
if (in_string) /* if in one, would this end it? */
{
/* End of string */
if (lptr >= nchars) /* enough to tell about? */
{
/* Found a long-enough string */
outbuff[lptr] = '\0';
/* Terminate the string and print */
printf("%6lx: %s\n", sstart, outbuff);
}
in_string = FALSE; /* PWL */
lptr = 0; /* Start at beginning again */
}
}
sofar += (long)ercode; /* update how far we've read into buffers */
}
close(fd);
}