1 /*
2  * fake - make up random lines resembling history-file entries, reproducibly
3  *
4  * -Log-
5  */
6 
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 #define	MAXSTR	500		/* For sizing strings -- DON'T use BUFSIZ! */
15 #define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)
16 
17 #ifndef lint
18 static char RCSid[] = "$Header$";
19 #endif
20 
21 int midonly = 0;		/* just message ids, rest not realistic */
22 int tag = 0;			/* tag lines with random digit for later use */
23 int expired = -1;		/* percentage of lines to be expired */
24 
25 int debug = 0;
26 char *progname;
27 
28 char *inname;				/* filename for messages etc. */
29 long lineno;				/* line number for messages etc. */
30 
31 void doline(char *buf);
32 void addchars(char *buf, int len);
33 extern void seed(long n);
34 int range(int min, int max);
35 
36 /*
37  - main - parse arguments and handle options
38  */
main(argc,argv)39 main(argc, argv)
40 int argc;
41 char *argv[];
42 {
43 	int c;
44 	int errflg = 0;
45 	FILE *in;
46 	struct stat statbuf;
47 	extern int optind;
48 	extern char *optarg;
49 	void process();
50 	register long no;
51 	char line[MAXSTR];
52 
53 	progname = argv[0];
54 
55 	while ((c = getopt(argc, argv, "ms:te:d")) != EOF)
56 		switch (c) {
57 		case 'm':	/* message-ids only */
58 			midonly = 1;
59 			break;
60 		case 's':	/* seed */
61 			seed(atol(optarg));
62 			break;
63 		case 't':	/* tag lines with a random digit */
64 			tag = 1;
65 			break;
66 		case 'e':	/* percentage to be expired */
67 			expired = atoi(optarg);
68 			break;
69 		case 'd':	/* Debugging. */
70 			debug++;
71 			break;
72 		case '?':
73 		default:
74 			errflg++;
75 			break;
76 		}
77 	if (errflg || optind != argc - 1) {
78 		fprintf(stderr, "usage: %s ", progname);
79 		fprintf(stderr, "[-m] [-s seed] length\n");
80 		exit(2);
81 	}
82 
83 	for (no = atol(argv[optind]); no > 0; no--) {
84 		doline(line);
85 		puts(line);
86 	}
87 	exit(0);
88 }
89 
90 /*
91  - doline - generate random history pseudo-line
92  */
93 void
doline(buf)94 doline(buf)
95 char *buf;
96 {
97 	char tagch[2];
98 
99 	(void) strcpy(buf, "<");
100 	addchars(buf, range(4, 20));
101 	(void) strcat(buf, "@");
102 	addchars(buf, range(8, 20));
103 	if (midonly)
104 		(void) strcat(buf, ">\tx");
105 	else {
106 		if (tag) {
107 			tagch[0] = "1234567890"[range(0,9)];
108 			tagch[1] = '\0';
109 			(void) strcat(buf, ">\t");
110 			(void) strcat(buf, tagch);
111 			(void) strcat(buf, "00000000~-");
112 		} else
113 			(void) strcat(buf, ">\t1234567890~-");
114 	}
115 	if (range(1, 100) > expired) {
116 		if (midonly)
117 			(void) strcat(buf, "\tx");
118 		else {
119 			(void) strcat(buf, "\t");
120 			addchars(buf, range(10, 30));
121 		}
122 	}
123 }
124 
125 /*
126  - addchars - generate n random characters suitable for history file
127  */
128 void
addchars(buf,len)129 addchars(buf, len)
130 char *buf;
131 int len;
132 {
133 	register int i;
134 	register char *p = buf + strlen(buf);
135 	static char vocab[] = "1234567890.abcde.fghij.klmno.pqrst.uvwxyz.\
136 1234567890.ABCDE.FGHIJ.KLMNO.PQRST.UVWXYZ.1234567890.\
137 1234567890.abcde.fghij.klmno.pqrst.uvwxyz.1234567890";
138 
139 	for (i = len; i > 0; i--)
140 		*p++ = vocab[range(0, sizeof(vocab)-2)];
141 	*p++ = '\0';
142 }
143