R
Robert Manea
Hello everyone,
I wrote, simply as an exercise, a small piece of code to find 'strings'
(defined as an amount of at least 3 ASCII characters followed by a non
ASCII character) in binary files.
The purpose of the program is to serve as a facile 'strings' (Unix
command) replacement and to be 100% ANSI C. Unfortunatelly it operates
notedly slower than the original 'strings' from the fileutils package.
Maybe someone has some hints on how to improve performance and keep the
code at the same time pure ANSI C.
Any other remarks to obvoius or not so obvoius errors are highly
appreciated, too.
#v+
/* Seek for ASCII-strings in binary streams and output them including
* their byte-position in the stream
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXSTRING 128
#define ISSTRSIZE 3
int main(int argc, char *argv[])
{
FILE *inp;
size_t i=0, j=MAXSTRING;
int ch;
char *buf;
switch(argc) {
case 0:
case 1:
if( !(inp=fdopen(fileno(stdin), "r")) ) {
perror("Error");
return EXIT_FAILURE;
}
break;
case 2:
if( !(inp=fopen(argv[1], "rb")) ) {
perror("Error");
return EXIT_FAILURE;
}
break;
default:
fprintf(stderr, "Syntax: %s [File]\n", argv[0]);
return EXIT_FAILURE;
}
if( !(buf = malloc(MAXSTRING)) )
return EXIT_FAILURE;
while( !feof(inp) ) {
ch = fgetc(inp);
if ( isprint(ch) ) {
if( i>j ) {
buf = realloc(buf, j*2);
j *= 2;
}
buf[i++] = (char) ch;
}
else {
if( i>ISSTRSIZE ) {
#ifdef POSITION
printf("%6lu: ", ftell(inp)-i-1);
#endif
buf = '\0';
puts(buf);
}
i=0;
}
}
free(buf);
return EXIT_SUCCESS;
}
#v-
TIA & Greets, Rob
I wrote, simply as an exercise, a small piece of code to find 'strings'
(defined as an amount of at least 3 ASCII characters followed by a non
ASCII character) in binary files.
The purpose of the program is to serve as a facile 'strings' (Unix
command) replacement and to be 100% ANSI C. Unfortunatelly it operates
notedly slower than the original 'strings' from the fileutils package.
Maybe someone has some hints on how to improve performance and keep the
code at the same time pure ANSI C.
Any other remarks to obvoius or not so obvoius errors are highly
appreciated, too.
#v+
/* Seek for ASCII-strings in binary streams and output them including
* their byte-position in the stream
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXSTRING 128
#define ISSTRSIZE 3
int main(int argc, char *argv[])
{
FILE *inp;
size_t i=0, j=MAXSTRING;
int ch;
char *buf;
switch(argc) {
case 0:
case 1:
if( !(inp=fdopen(fileno(stdin), "r")) ) {
perror("Error");
return EXIT_FAILURE;
}
break;
case 2:
if( !(inp=fopen(argv[1], "rb")) ) {
perror("Error");
return EXIT_FAILURE;
}
break;
default:
fprintf(stderr, "Syntax: %s [File]\n", argv[0]);
return EXIT_FAILURE;
}
if( !(buf = malloc(MAXSTRING)) )
return EXIT_FAILURE;
while( !feof(inp) ) {
ch = fgetc(inp);
if ( isprint(ch) ) {
if( i>j ) {
buf = realloc(buf, j*2);
j *= 2;
}
buf[i++] = (char) ch;
}
else {
if( i>ISSTRSIZE ) {
#ifdef POSITION
printf("%6lu: ", ftell(inp)-i-1);
#endif
buf = '\0';
puts(buf);
}
i=0;
}
}
free(buf);
return EXIT_SUCCESS;
}
#v-
TIA & Greets, Rob