xref: /dragonfly/games/random/randomize_fd.c (revision 0db87cb7)
1 /*-
2  * Copyright (C) 2003 Sean Chittenden <seanc@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/games/random/randomize_fd.c,v 1.2.2.1 2003/02/15 10:34:35 seanc Exp $
27  * $DragonFly: src/games/random/randomize_fd.c,v 1.2 2003/06/17 04:25:24 dillon Exp $
28  */
29 
30 #include <sys/types.h>
31 #include <sys/param.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "randomize_fd.h"
41 
42 static struct rand_node *rand_root;
43 static struct rand_node *rand_tail;
44 
45 static struct rand_node *
46 rand_node_allocate(void)
47 {
48 	struct rand_node *n;
49 
50 	n = (struct rand_node *)malloc(sizeof(struct rand_node));
51 	if (n == NULL)
52 		err(1, "malloc");
53 
54 	n->len = 0;
55 	n->cp = NULL;
56 	n->next = NULL;
57 	return(n);
58 }
59 
60 static void
61 rand_node_free(struct rand_node *n)
62 {
63 	if (n != NULL) {
64 		if (n->cp != NULL)
65 			free(n->cp);
66 
67 		free(n);
68 	}
69 }
70 
71 static void
72 rand_node_free_rec(struct rand_node *n)
73 {
74 	if (n != NULL) {
75 		if (n->next != NULL)
76 			rand_node_free_rec(n->next);
77 
78 		rand_node_free(n);
79 	}
80 }
81 
82 static void
83 rand_node_append(struct rand_node *n)
84 {
85 	if (rand_root == NULL)
86 		rand_root = rand_tail = n;
87 	else {
88 		rand_tail->next = n;
89 		rand_tail = n;
90 	}
91 }
92 
93 int
94 randomize_fd(int fd, int type, int unique, double denom)
95 {
96 	u_char *buf, *p;
97 	u_int numnode, j, selected, slen;
98 	struct rand_node *n, *prev;
99 	int bufleft, eof, fndstr, ret;
100 	size_t bufc, buflen, i;
101 	ssize_t len;
102 
103 	rand_root = rand_tail = NULL;
104 	bufc = i = 0;
105 	bufleft = eof = fndstr = numnode = ret = 0;
106 
107 	if (type == RANDOM_TYPE_UNSET)
108 		type = RANDOM_TYPE_LINES;
109 
110 	buflen = sizeof(u_char) * MAXBSIZE;
111 	buf = (u_char *)malloc(buflen);
112 	if (buf == NULL)
113 		err(1, "malloc");
114 
115 	while (!eof) {
116 		/* Check to see if we have bits in the buffer */
117 		if (bufleft == 0) {
118 			len = read(fd, buf, buflen);
119 			if (len == -1)
120 				err(1, "read");
121 			else if (len == 0) {
122 				eof++;
123 				break;
124 			} else if ((size_t)len < buflen)
125 				buflen = (size_t)len;
126 
127 			bufleft = (int)len;
128 		}
129 
130 		/* Look for a newline */
131 		for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
132 			if (i == buflen) {
133 				if (fndstr) {
134 					if (!eof) {
135 						memmove(buf, &buf[bufc], i - bufc);
136 						i -= bufc;
137 						bufc = 0;
138 						len = read(fd, &buf[i], buflen - i);
139 						if (len == -1)
140 							err(1, "read");
141 						else if (len == 0) {
142 							eof++;
143 							break;
144 						} else if (len < (ssize_t)(buflen - i))
145 							buflen = i + (size_t)len;
146 
147 						bufleft = (int)len;
148 						fndstr = 0;
149 					}
150 				} else {
151 					p = (u_char *)realloc(buf, buflen * 2);
152 					if (p == NULL)
153 						err(1, "realloc");
154 
155 					buf = p;
156 					if (!eof) {
157 						len = read(fd, &buf[i], buflen);
158 						if (len == -1)
159 							err(1, "read");
160 						else if (len == 0) {
161 							eof++;
162 							break;
163 						} else if (len < (ssize_t)(buflen - i))
164 							buflen = (size_t)len;
165 
166 						bufleft = (int)len;
167 					}
168 
169 					buflen *= 2;
170 				}
171 			}
172 
173 			if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
174 			    (type == RANDOM_TYPE_WORDS && isspace((int)buf[i])) ||
175 			    (eof && i == buflen - 1)) {
176 			make_token:
177 				n = rand_node_allocate();
178 				if (-1 != (int)i) {
179 					slen = i - (u_long)bufc;
180 					n->len = slen + 2;
181 					n->cp = (u_char *)malloc(slen + 2);
182 					if (n->cp == NULL)
183 						err(1, "malloc");
184 
185 					memmove(n->cp, &buf[bufc], slen);
186 					n->cp[slen] = buf[i];
187 					n->cp[slen + 1] = '\0';
188 					bufc = i + 1;
189 				}
190 				rand_node_append(n);
191 				fndstr = 1;
192 				numnode++;
193 			}
194 		}
195 	}
196 
197 	(void)close(fd);
198 
199 	/* Necessary evil to compensate for files that don't end with a newline */
200 	if (bufc != i) {
201 		i--;
202 		goto make_token;
203 	}
204 
205 	for (i = numnode; i > 0; i--) {
206 		selected = ((int)denom * random())/(((double)RAND_MAX + 1) / numnode);
207 
208 		for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
209 			if (j == selected) {
210 				if (n->cp == NULL)
211 					break;
212 
213 				ret = printf("%.*s", (int)n->len - 1, n->cp);
214 				if (ret < 0)
215 					err(1, "printf");
216 				if (unique) {
217 					if (n == rand_root)
218 						rand_root = n->next;
219 					if (n == rand_tail)
220 						rand_tail = prev;
221 
222 					prev->next = n->next;
223 					rand_node_free(n);
224 					numnode--;
225 					break;
226 				}
227 			}
228 		}
229 	}
230 
231 	fflush(stdout);
232 
233 	if (!unique)
234 		rand_node_free_rec(rand_root);
235 
236 	return(0);
237 }
238