-
Notifications
You must be signed in to change notification settings - Fork 7
/
strings.c
61 lines (54 loc) · 1.23 KB
/
strings.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "eprintf.h"
#define MINLEN 100
static char *name = NULL; // program name ofr messages
/* setprogname: set stored name of program */
void setprogname(char *str)
{
name = strdup(str);
}
/* strings: extract printable strings from stream */
void strings(char *name, FILE *fin)
{
int c, i;
char buf[BUFSIZ];
do
{
for (i = 0; (c = getc(fin)) != EOF;)
{
if (!isprint(c))
break;
buf[i++] = c;
if (i >= BUFSIZ)
break;
}
if (i >= MINLEN) /* print if long enough */
printf("%s:%.*s\n", name, i, buf);
} while (c != EOF);
}
/* strings main: find printable strings in files */
int main(int argc, char *argv[])
{
int i;
FILE *fin;
setprogname("strings");
if (argc == 1)
eprintf("usage: strings filenames");
else
{
for (i = 1; i < argc; i++)
{
if ((fin = fopen(argv[i], "rb")) == NULL)
weprintf("can't open %s:", argv[i]);
else
{
strings(argv[i], fin);
fclose(fin);
}
}
}
return 0;
}