Add strings(1)

This commit is contained in:
sin 2013-08-14 10:41:55 +01:00
parent cd592c9f23
commit 0ed2a55003
3 changed files with 67 additions and 0 deletions

View File

@ -70,6 +70,7 @@ SRC = \
sort.c \ sort.c \
split.c \ split.c \
sponge.c \ sponge.c \
strings.c \
sync.c \ sync.c \
tail.c \ tail.c \
tar.c \ tar.c \

10
strings.1 Normal file
View File

@ -0,0 +1,10 @@
.TH STRINGS 1 sbase\-VERSION
.SH NAME
strings \- print the strings of printable characters in files
.SH SYNOPSIS
.B strings
.IR [file...]
.SH DESCRIPTION
.B strings
prints the printable character sequences that are at least 6 characters
long. If no files are given then it uses stdin.

56
strings.c Normal file
View File

@ -0,0 +1,56 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "util.h"
static void dostrings(FILE *fp, const char *fname);
static void
usage(void)
{
eprintf("usage: %s file\n", argv0);
}
int
main(int argc, char *argv[])
{
FILE *fp;
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc > 0) {
if (!(fp = fopen(argv[0], "r")))
eprintf("open %s:", argv[0]);
dostrings(fp, argv[0]);
fclose(fp);
} else {
dostrings(stdin, "<stdin>");
}
return 0;
}
static void
dostrings(FILE *fp, const char *fname)
{
unsigned char buf[BUFSIZ];
int c, i = 0;
off_t offset = 0;
do {
offset++;
if (isprint(c = getc(fp)))
buf[i++] = c;
if ((!isprint(c) && i >= 6) || i == sizeof(buf) - 1) {
buf[i] = '\0';
printf("%8ld: %s\n", (long)offset - i - 1, buf);
i = 0;
}
} while (c != EOF);
if (ferror(fp))
eprintf("%s: read error:", fname);
}