a version of strings(1)
that isn’t dependent on a linker. Dumps all strings of any given file.
Based off code provided by toybox
a version of strings(1)
that isn’t dependent on a linker. Dumps all strings of any given file.
Based off code provided by toybox
TARGET := strings | |
SRC := src/strings.c | |
all: $(TARGET) | |
$(TARGET): $(SRC) | |
$(CC) $(LDFLAGS) $^ -o $@ | |
clean: | |
$(RM) $(TARGET) |
// from https://github.com/landley/toybox/blob/master/toys/posix/strings.c | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#define BUFSIZE 4096 | |
long n = 4; | |
char buf[BUFSIZE]; | |
void do_strings(char *filename) { | |
int fd = open(filename, O_RDONLY); | |
if (fd < 0) { | |
perror(filename); | |
exit(EXIT_FAILURE); | |
} | |
int nread, i, wlen = n, count = 0; | |
off_t offset = 0; | |
char *string = 0; | |
string = calloc(wlen+1, sizeof(char)); | |
if (!string) { | |
perror("calloc"); | |
exit(EXIT_FAILURE); | |
} | |
for (i = nread = 0; ;i++) { | |
if (i >= nread) { | |
nread = read(fd, buf, sizeof(buf)); | |
i = 0; | |
if (nread < 0) { | |
perror(filename); | |
exit(EXIT_FAILURE); | |
} | |
if (nread < 1) { | |
if (count) goto flush; | |
break; | |
} | |
} | |
offset++; | |
if ((buf[i]>=32 && buf[i]<=126) || buf[i]=='\t') { | |
if (count == wlen) fputc(buf[i], stdout); | |
else { | |
string[count++] = buf[i]; | |
if (count == wlen) { | |
printf("%s", string); | |
} | |
} | |
continue; | |
} | |
flush: | |
if (count == wlen) putchar('\n'); | |
count = 0; | |
} | |
close(fd); | |
free(string); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "Usage: %s [file ...]\n", argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
for (int i = 1; i < argc; i++) { | |
do_strings(argv[i]); | |
} | |
return 0; | |
} |