Works, but is a bit slower than expected (Linux bug?).

This commit is contained in:
hjp 2000-10-08 21:33:04 +00:00
parent 50a51a6093
commit 4b63fa3513
3 changed files with 119 additions and 0 deletions

36
slowcat/.vimrc Normal file
View File

@ -0,0 +1,36 @@
version 5.0
set nocompatible
let cpo_save=&cpo
set cpo=B
map! <xHome> <Home>
map! <xEnd> <End>
map! <S-xF4> <S-F4>
map! <S-xF3> <S-F3>
map! <S-xF2> <S-F2>
map! <S-xF1> <S-F1>
map! <xF4> <F4>
map! <xF3> <F3>
map! <xF2> <F2>
map! <xF1> <F1>
map <F8> :cn
map <xHome> <Home>
map <xEnd> <End>
map <S-xF4> <S-F4>
map <S-xF3> <S-F3>
map <S-xF2> <S-F2>
map <S-xF1> <S-F1>
map <xF4> <F4>
map <xF3> <F3>
map <xF2> <F2>
map <xF1> <F1>
map!  }I\begin{yyplcwendO
map!  >I<yypa/O
let &cpo=cpo_save
unlet cpo_save
set autoindent
set exrc
set number
set ruler
set shiftwidth=4
set showmatch
set textwidth=72

13
slowcat/GNUmakefile Normal file
View File

@ -0,0 +1,13 @@
include GNUmakevars
include GNUmakerules
all: slowcat
install: $(BINDIR)/slowcat
clean:
rm -f *.bak *.o core slowcat
distclean: clean
rm -f *.d
-include *.d

70
slowcat/slowcat.c Normal file
View File

@ -0,0 +1,70 @@
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
char *cmnd;
static void usage(void) {
fprintf(stderr, "Usage: %s [-d delay] file ...\n", cmnd);
exit(1);
}
static void do_cat(FILE *fp, double delay) {
int c;
while ((c = getc(fp)) != EOF) {
putchar(c);
usleep(delay * 1E6);
}
}
int main(int argc, char **argv) {
int rc = 0;
int c;
double delay = 0.5;
cmnd = argv[0];
while ((c = getopt(argc, argv, "d:")) != EOF) {
char *p;
switch (c) {
case 'd':
p = NULL;
delay = strtod(optarg, &p);
if (!p || *p) usage();
if (delay < 0 || delay > ULONG_MAX / 1E6) usage();
break;
case '?':
usage();
default:
assert(0);
}
}
setvbuf(stdout, NULL, _IONBF, 0);
if (optind == argc) {
do_cat(stdin, delay);
} else {
int i;
for (i = optind; i < argc; i++) {
FILE *fp = fopen(argv[i], "r");
if (fp) {
do_cat(fp, delay);
fclose(fp);
} else {
fprintf(stderr, "%s: cannot open %s for reading: %s\n",
argv[0], argv[i], strerror(errno));
rc++;
}
}
}
return rc;
}