forked from tburette/randtype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
randtype.c
662 lines (550 loc) · 13.9 KB
/
randtype.c
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/* Copyright (C) 1999-2001 bjk <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#ifdef HAVE_ZLIB
#include <zlib.h>
#endif
#include "randtype.h"
static void cleanup(void)
{
REPLACE *cur, *next;
if (replace) {
for (cur = replace; cur; cur = next) {
next = cur->next;
free(cur->find);
free(cur->replace);
free(cur);
}
}
return;
}
/* What to do with the caught signals. */
static void catch_signal(int signal)
{
switch (signal) {
case SIGALRM:
raise(SIGTERM);
break;
case SIGTERM:
break;
case SIGINT:
break;
default:
raise(SIGTERM);
break;
}
cleanup();
exit(signal);
}
/* Show help text. */
static void usage(const char *me)
{
fprintf(stderr,
"Usage: %s [-hvl] [-d ,|.<string> [-k]] [-n <string>] "
"[-t <ms,mult>]"
"\n\t\t [-w <string> [-c ms,mult]] [-r s1,s2[:...]] "
"[-m <int>]\n\t\t [-q <int>] [file ...]\n\n", me);
fprintf(stderr,
" -t ms,mult\tSpecify randomness where `ms' is "
"the maximum microseconds\n\t\tbetween characters and `mult' "
"is the multiplier of `ms'.\n\t\tDefault is %i,%i.\n",
DEF_MS, DEF_MULT);
fprintf(stderr,
" -d string\tA string beginning with either ',' (left) or '.' "
"(right) which\n\t\tis output immediately.\n");
fprintf(stderr,
" -k\t\tKill the dump string (-d) when printing the line.\n");
fprintf(stderr,
" -r s1,s2\tReplace s1 with s2. Separate more than one with "
"':'.\n");
fprintf(stderr,
" -n chars\tPrint these characters immediately.\n");
fprintf(stderr,
" -w chars\tThe opposite of the -n option, these "
"characters are delayed by\n\t\tthe time specified by "
"the -c option.\n");
fprintf(stderr,
" -c ms,mult\tFor use with the -w option, this option "
"has the same value\n\t\tformat as the -t option or the "
"same default.\n");
fprintf(stderr,
" -l\t\tThe -t option will apply to outputting "
"lines rather than\n\t\tcharacters, all options other than -q are "
"ignored.\n");
fprintf(stderr, " -m int\tGenerate mistakes on output with a frequency "
"of <int>. The\n\t\tfrequency is the comparison of a random "
"alphanumeric character\n\t\tand the next character.\n");
fprintf(stderr,
" -q int\tTerminate program after specified amount of seconds.\n");
fprintf(stderr, " -h\t\tYour looking at it.\n");
fprintf(stderr, " -v\t\tShow version information.\n");
exit(-1);
}
/* See if the character matches any character in the --wait/--nowait string. */
static char chk_special(const char *str, char c)
{
register char i;
while ((i = *str++)) {
if (i == '\\' && *str) {
switch (i = *str++) {
case 'a':
i = '\007';
break;
case 'b':
i = '\b';
break;
case 'f':
i = '\f';
break;
case 'n':
i = '\n';
break;
case 'r':
i = '\r';
break;
case 't':
i = '\t';
break;
case 'v':
i = (int) 0x0B;
break;
case '\\':
i = '\\';
break;
default:
break;
}
}
if (i == c)
break;
}
return i;
}
static void randsleep(float ms, unsigned int mult)
{
static struct timeval tv;
long i;
#ifdef HAVE_RANDOM
i = (1 + (int) (ms * random() / (RAND_MAX + 1.0))) * mult;
#else
i = (1 + (int) (ms * rand() / (RAND_MAX + 1.0))) * mult;
#endif
/* FreeBSD likes one less */
if (i > 999999L) {
tv.tv_sec = i / 1000000;
tv.tv_usec = i % 1000000;
}
else
tv.tv_usec = i;
/* select() is unbelievably more efficient than usleep(). */
select(0, NULL, NULL, NULL, &tv);
return;
}
static void randchar(int c)
{
int i;
do {
#ifdef HAVE_RANDOM
i = (1 + (int) (94.0 * random() / (RAND_MAX + 1.0))) + 32;
#else
i = (1 + (int) (94.0 * rand() / (RAND_MAX + 1.0))) + 32;
#endif
} while (!isalpha(i));
fprintf(stdout, "%c", (islower(c) ? tolower(i) : i));
randsleep(tms * 2, tmult);
fprintf(stdout, "\b \b");
randsleep(tms * 2, tmult);
return;
}
static int randint(float max)
{
#ifdef HAVE_RANDOM
return((1 + (int) (max * random() / (RAND_MAX + 1.0))));
#else
return((1 + (int) (max * rand() / (RAND_MAX + 1.0))));
#endif
}
static int randtype(char *str)
{
int n = 0, p;
register unsigned int i = 0;
register char c;
while ((i = *str++)) {
for (p = 0; p < mistakes; p++) {
if (!isalpha(i))
break;
if (randint(strlen(str)) == n) {
randchar(i);
break;
}
}
n++;
c = chk_special(_nowait, i);
if (i == c) {
fprintf(stdout, "%c", i);
continue;
}
c = chk_special(_wait, i);
if (i == c) {
randsleep(cms, cmult);
fprintf(stdout, "%c", i);
continue;
}
fprintf(stdout, "%c", i);
randsleep(tms, tmult);
}
return 0;
}
/* Check the -t and -c argument format. */
static int time_split(const char *str, float *ms, unsigned int *mult)
{
register int n, i;
char *s, *tmp;
tmp = strdup(str);
/* Only 2 values needed so n<=1. Others are ignored. */
for (n = 0; n <= 1; n++) {
if (n == 0)
s = strtok(tmp, ",");
else
s = strtok(NULL, ",");
if (s == NULL)
return 1;
for (i = 0; i < strlen(s); i++) {
if (isdigit(s[i]) == 0)
return 1;
}
if (n == 0)
*ms = atof(s);
else
*mult = atoi(s);
}
free(tmp);
return 0;
}
/* this function was written by Dan Stubbs ([email protected])
* and leeched from a post on comp.lang.c. it searches a given string for a
* token and replaces it with another. */
static char *strrep(char *str, char *find, char *repl)
{
char *p0, *p1, *p2;
size_t find_len = strlen(find);
size_t repl_len = strlen(repl);
size_t dist = 0;
long diff = repl_len - find_len;
if (!find_len || (p0 = strstr(str ,find)) == NULL)
return str;
if (diff > 0) { /* If extra space needed--insert it.*/
for (p1 = p0 + 1; p1 != NULL; p1 = strstr(++p1, find))
dist += diff;
memmove (p0 + dist, p0, strlen(str) - (p0 - str) + 1);
}
p1 = (diff > 0) ? p0 + dist + find_len : p0 + find_len;
p2 = strstr(p1, find);
while (p2) { /* While there is another string to replace.*/
memcpy (p0, repl, repl_len); /* Insert replacement.*/
p0 += repl_len;
memmove (p0, p1, p2-p1); /* Move just the right segment.*/
p0 += (p2 - p1);
p1 = p2 + find_len;
p2 = strstr(p1, find);
}
memcpy (p0, repl, repl_len); /* Final replacement.*/
if (diff < 0) { /* If there is a gap at the end of str.*/
p0 += repl_len;
p2 = strchr(p1, '\0');
memmove (p0, p1, p2 - p1 + 1);
}
return (str);
}
/* Open the file, read lines, check for dump string, pass to other
* functions then close file. */
int splitter(const char *filename, int kill)
{
#ifdef HAVE_ZLIB
gzFile fp = NULL;
#else
FILE *fp = NULL;
#endif
char *s, *src, *tmp;
register unsigned int i;
if ((s = malloc(LINE_MAX)) == NULL) {
perror("malloc(LINE_MAX)");
return errno;
}
if ((src = calloc(0, 1)) == NULL) {
perror("calloc()");
return errno;
}
if (filename[0] == '-' && filename[1] == 0) {
#ifdef HAVE_ZLIB
if ((fp = gzdopen(0, "rb")) == NULL) {
perror(filename);
return errno;
}
#else
;
#endif
}
else {
#ifdef HAVE_ZLIB
if ((fp = gzopen(filename, "rb")) == NULL) {
#else
if ((fp = fopen(filename, "r")) == NULL) {
#endif
perror(filename);
return errno;
}
}
#ifdef HAVE_ZLIB
while ((s = gzgets(fp, s, LINE_MAX)) != NULL) {
#else
while ((s = fgets(s, LINE_MAX, fp ? fp : stdin)) != NULL) {
#endif
REPLACE *rnode;
char *p;
if (replace) {
rnode = replace;
while (rnode->next != NULL) {
char *x;
x = strrep(s, rnode->find, rnode->replace);
strncpy(s, x, LINE_MAX - 1);
rnode = rnode->next;
}
}
if (dolines) {
randsleep(tms, tmult);
fprintf(stdout, "%s", s);
continue;
}
if (dumpstr[0]) {
if (dir == ',') {
if ((tmp = strstr(s, dumpstr)) != NULL) {
for (i = 0; i < (strlen(s) - strlen(tmp)); i++)
fprintf(stdout, "%c", s[i]);
if ((s = calloc(1, LINE_MAX)) == NULL) {
perror("calloc");
return errno;
}
if (kill) {
for (i = strlen(dumpstr); i < strlen(tmp); i++) {
sprintf(src, "%c", tmp[i]);
strncat(s, src, LINE_MAX - 1);
}
}
else {
while ((i = *tmp++)) {
sprintf(src, "%c", i);
strncat(s, src, LINE_MAX - 1);
}
}
}
}
else {
p = strdup(s);
if ((tmp = strtok(s, dumpstr)) != NULL) {
randtype(tmp);
if (kill)
i = strlen(tmp) + strlen(dumpstr);
else
i = strlen(tmp);
for (;i < strlen(p); i++)
fprintf(stdout, "%c", p[i]);
}
free(p);
}
}
if (dir != '.')
randtype(s);
}
#ifdef HAVE_ZLIB
gzclose(fp);
#else
fclose(fp);
#endif
if (!dolines)
free(src);
free(s);
return 0;
}
static int parse_replace(const char *str)
{
REPLACE *rnode;
char *tmp, *s, *ss;
if ((replace = (REPLACE *) malloc(sizeof(REPLACE))) == NULL) {
perror("malloc");
exit(errno);
}
rnode = replace;
tmp = strdup(str);
while ((s = strtok_r(tmp, ":", &ss)) != NULL) {
char *find, *repl;
int i;
if ((find = strtok(s, ",")) == NULL)
return 1;
i = strlen(find) + 1;
if ((rnode->find = (char *) malloc(i)) == NULL) {
perror("malloc");
exit(errno);
}
snprintf(rnode->find, i, "%s", find);
if ((repl = strtok(NULL, ",")) == NULL)
return 1;
i = strlen(repl) + 1;
if ((rnode->replace = (char *) malloc(i)) == NULL) {
perror("malloc");
exit(errno);
}
snprintf(rnode->replace, i, "%s", repl);
if ((rnode->next = (REPLACE *) malloc(sizeof(REPLACE))) == NULL) {
perror("malloc");
exit(errno);
}
rnode = rnode->next;
tmp = ss;
}
rnode->next = NULL;
tmp = NULL;
free(tmp);
return 0;
}
int main(int argc, char *argv[])
{
static unsigned int i, opt, kill, quit;
char *time = NULL, *chartime = NULL, *replacestr = NULL;
char progname[NAME_MAX], tmp[NAME_MAX];
struct sigaction my_sigaction;
bzero(&my_sigaction, sizeof(my_sigaction));
bzero(progname, sizeof(progname));
snprintf(progname, sizeof(progname), "%s", argv[0]);
/* Set defaults. */
tms = cms = DEF_MS;
tmult = cmult = DEF_MULT;
_nowait[0] = _wait[0] = '\0';
while ((opt = getopt(argc, argv, "m:vhr:q:t:n:w:d:c:kl")) != EOF) {
tmp[0] = 0;
switch (opt) {
case 0:
break;
case 'm':
snprintf(tmp, sizeof(tmp), "%s", optarg);
for (i = 0; i < strlen(tmp); i++) {
if (isdigit(tmp[i]) == 0)
usage(progname);
}
mistakes = atoi(tmp);
break;
case 'n':
snprintf(_nowait, sizeof(_nowait), "%s", optarg);
break;
case 'w':
snprintf(_wait, sizeof(_wait), "%s", optarg);
break;
case 't':
time = optarg;
break;
case 'r':
replacestr = optarg;
break;
case 'd':
if ((optarg[0] != ',' && optarg[0] != '.') || strlen(optarg) < 2)
usage(progname);
dir = optarg[0];
while ((*optarg++))
tmp[i++] = *optarg;
tmp[i] = 0;
snprintf(dumpstr, sizeof(dumpstr), "%s", tmp);
break;
case 'c':
chartime = optarg;
break;
case 'k':
kill = 1;
break;
case 'l':
dolines = 1;
break;
case 'q':
snprintf(tmp, sizeof(tmp), "%s", optarg);
for (i = 0; i < strlen(tmp); i++) {
if (isdigit(tmp[i]) == 0)
usage(progname);
}
quit = atoi(tmp);
break;
case 'v':
fprintf(stdout, "%s\n%s\n", VERSION, COPYRIGHT);
exit(0);
case 'h':
default:
usage(progname);
break;
}
}
if (replacestr)
if (parse_replace(replacestr) != 0)
usage(progname);
if (time)
if (time_split(time, &tms, &tmult) != 0)
usage(progname);
if (!dolines) {
if (chartime != NULL && _wait[0] != '\0') {
if (time_split(chartime, &cms, &cmult) != 0)
usage(progname);
}
/* STDOUT is line buffered by default, we need to change this to
* deal with characters. */
setvbuf(stdout, 0, _IONBF, BUFSIZ);
}
/* Setup the signal handler. */
sigemptyset(&my_sigaction.sa_mask);
sigfillset(&my_sigaction.sa_mask);
my_sigaction.sa_handler = catch_signal;
/* These are the signals that will be caught. All others are ignored when
* possible. */
sigdelset(&my_sigaction.sa_mask, SIGTERM);
sigdelset(&my_sigaction.sa_mask, SIGALRM);
sigdelset(&my_sigaction.sa_mask, SIGINT);
sigdelset(&my_sigaction.sa_mask, SIGTSTP);
sigprocmask(SIG_BLOCK, &my_sigaction.sa_mask, NULL);
sigaction(SIGINT, &my_sigaction, NULL);
sigaction(SIGTERM, &my_sigaction, NULL);
sigaction(SIGALRM, &my_sigaction, NULL);
#ifdef HAVE_RANDOM
srandom(getpid());
#else
srand(getpid());
#endif
/* Start the quitting timer. */
alarm(quit);
/* Keep reading until last non-option argument is read. */
if (argc == optind)
err |= splitter("-", kill);
else {
for (i = optind; i < argc; i++) {
err |= splitter(argv[i], kill);
}
}
cleanup();
exit(err);
}