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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (C) 2002 Red Hat, Inc.
*
* This is free software; you can redistribute it and/or modify it under
* the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <config.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glib.h>
static void
catfile(const char *pathname, long delay, long chunksize)
{
FILE *fp;
struct timeval tv;
char *buf;
int c;
long i;
if (!((pathname == NULL) || (strcmp(pathname, "-") == 0))) {
fp = fopen(pathname, "r");
if (fp == NULL) {
g_warning("Error opening file `%s': %s.\n",
pathname, strerror(errno));
return;
}
} else {
fp = stdin;
}
buf = g_malloc(chunksize);
while (!feof(fp)) {
tv.tv_sec = delay / 1000000;
tv.tv_usec = delay % 1000000;
select(0, NULL, NULL, NULL, &tv);
for (i = 0; i < chunksize; i++) {
c = fgetc(fp);
if (c != EOF) {
buf[i] = c;
} else {
break;
}
}
if (i > 0) {
int bytes;
bytes = write(STDOUT_FILENO, buf, i);
fsync(STDOUT_FILENO);
}
}
g_free(buf);
if (fp != stdin) {
fclose(fp);
}
}
int
main(int argc, char **argv)
{
int i, c;
long delay = 200000, chunksize = 1, tmp;
char *p;
GList *files = NULL, *file;
while ((c = getopt(argc, argv, "t:c:")) != -1) {
switch (c) {
case 't':
tmp = strtol(optarg, &p, 0);
if ((p != NULL) && (*p == '\0')) {
delay = tmp;
}
break;
case 'c':
tmp = strtol(optarg, &p, 0);
if ((p != NULL) && (*p == '\0')) {
chunksize = tmp;
}
break;
default:
g_printerr("Usage: slowcat [-t delay] [-c chunksize] [file ...]\n");
exit(1);
break;
}
}
for (i = optind; i < argc; i++) {
files = g_list_append(files, argv[i]);
}
if (files) {
for (file = files; file != NULL; file = g_list_next(file)) {
catfile((const char*)file->data, delay, chunksize);
}
} else {
catfile(NULL, delay, chunksize);
}
return 0;
}
|