ftp.nice.ch/pub/next/unix/hack/hackkit.2.N.bs.tar.gz#/freebies/source/strings.c

This is strings.c in view mode; [Download] [Up]

#define BUFSIZE 0x1000
char buff[BUFSIZE];
int fd,numleft;
char *cptr;
int block = 0;
#define TRUE 1
#define FALSE 0

/*	The printing routines are a bit strange because this was written for
	MSDOS, where there was significant savings in code size if you didn't
	call printf.
*/

void ps(char *p) {while (*p) putchar(*p++);}

main(int argc, char **argv) {
    int c;
    char part[100];
    int length,afterstring;
    if (argc < 2) {
	ps("Locate strings in a program:\nstrings <filename>\n");
	exit(1);
	}
    if ((fd = open(argv[1],0)) < 0) {
	ps("Unable to open "); ps(argv[1]); ps("\n");
	exit(1);
	}
    numleft = 0;
    length = 0; afterstring = FALSE;
    while (1) {
	if (!numleft) {
	    cptr++;
	    numleft = read(fd,buff,BUFSIZE);
	    if (numleft <= 0) {if (length) print(part,length); break;}
	    block++;
	    cptr = buff;
	    }
	numleft--;
	c = *cptr++;
	/* see if we have the end of a string: */
	if ((c == 0 || c == '\n') &&
	    length > (afterstring?1:part[0]=='N' ? 12:4)) {
	    part[length++] = c;
	GOTONE:
	    print(part,length);
	    length = 0; afterstring = TRUE;
	    }
	/* skip common 68000 garbage: */
	else if (c=='N' && length && length<10 && part[length-1]!=' ') goto GARBAGE;
	/* continue a string that is started: */
	else if ((length || afterstring) &&
		 (c>=' ' && c<'\177' ||
		  (length>10 && c>'\240' && c<'\377') ||
		  c=='\n' || c=='\r' || c=='\t')) {
	    part[length++] = c;
	    if (length >= 70) goto GOTONE;
	    }
	/* start a string: */
	else if (c>='A' && c<='Z' || c>='a' && c<='z' || c=='%' || c=='#')
	    part[length++] = c;
	else {
	GARBAGE:
	    if (length > 10) {part[length++] = c; print(part,length);}
	    if (c) afterstring = FALSE;
	    length = 0;
	    }
	}
    close(fd);
    }

_PutNum(unsigned value,int width) {
    if (value>=16) _PutNum(value/16,width-1);
    else while (--width > 0) putchar('0');
    value %= 16;
    putchar(value+((value>9)?'A'-10:'0'));
    }

_PutOct(int value) {
    putchar(((value>>6)&3)+'0');
    putchar(((value>>3)&7)+'0');
    putchar((value&7)+'0');
    }

print(str,len)
     char *str;
     int len;
{
    int x;
    x = (cptr-buff)-len;
    if (x < 0) {_PutNum(block-2,0); x += 0x1000;}
    else _PutNum(block-1,0);
    _PutNum(x,3);
    ps(" : ");
    while (len--) {
	if (*str&128) {putchar('\\'); _PutOct(*str);}
	else switch(*str) {
	case '\n': ps("\\n"); break;
	case '\r': ps("\\r"); break;
	case '\t': ps("\\t"); break;
	case '\\': ps("\\\\"); break;
	case '^': ps("\\^"); break;
	case 0: if (!len) break;
	default:
	    if (*str < 32 || *str > 126) {putchar('^'); *str ^= 64;}
	    putchar(*str);
	    }
	str++;
	}
    putchar('\n');
    }

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.