xref: /dragonfly/games/random/randomize_fd.c (revision 65cc0652)
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  */
28 
29 #include <sys/types.h>
30 #include <sys/param.h>
31 
32 #include <ctype.h>
33 #include <err.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include "randomize_fd.h"
40 
41 static struct rand_node *rand_root;
42 static struct rand_node *rand_tail;
43 
44 static struct rand_node *
45 rand_node_allocate(void)
46 {
47 	struct rand_node *n;
48 
49 	n = (struct rand_node *)malloc(sizeof(struct rand_node));
50 	if (n == NULL)
51 		err(1, "malloc");
52 
53 	n->len = 0;
54 	n->cp = NULL;
55 	n->next = NULL;
56 	return(n);
57 }
58 
59 static void
60 rand_node_free(struct rand_node *n)
61 {
62 	if (n != NULL) {
63 		if (n->cp != NULL)
64 			free(n->cp);
65 
66 		free(n);
67 	}
68 }
69 
70 static void
71 rand_node_free_rec(struct rand_node *n)
72 {
73 	if (n != NULL) {
74 		if (n->next != NULL)
75 			rand_node_free_rec(n->next);
76 
77 		rand_node_free(n);
78 	}
79 }
80 
81 static void
82 rand_node_append(struct rand_node *n)
83 {
84 	if (rand_root == NULL)
85 		rand_root = rand_tail = n;
86 	else {
87 		rand_tail->next = n;
88 		rand_tail = n;
89 	}
90 }
91 
92 int
93 randomize_fd(int fd, int type, int unique, double denom)
94 {
95 	u_char *buf, *p;
96 	u_int numnode, j, selected, slen;
97 	struct rand_node *n, *prev;
98 	int bufleft, eof, fndstr, ret;
99 	size_t bufc, buflen, i;
100 	ssize_t len;
101 
102 	rand_root = rand_tail = NULL;
103 	bufc = i = 0;
104 	bufleft = eof = fndstr = numnode = ret = 0;
105 
106 	if (type == RANDOM_TYPE_UNSET)
107 		type = RANDOM_TYPE_LINES;
108 
109 	buflen = sizeof(u_char) * MAXBSIZE;
110 	buf = (u_char *)malloc(buflen);
111 	if (buf == NULL)
112 		err(1, "malloc");
113 
114 	while (!eof) {
115 		/* Check to see if we have bits in the buffer */
116 		if (bufleft == 0) {
117 			len = read(fd, buf, buflen);
118 			if (len == -1)
119 				err(1, "read");
120 			else if (len == 0) {
121 				eof++;
122 				break;
123 			} else if ((size_t)len < buflen)
124 				buflen = (size_t)len;
125 
126 			bufleft = (int)len;
127 		}
128 
129 		/* Look for a newline */
130 		for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
131 			if (i == buflen) {
132 				if (fndstr) {
133 					if (!eof) {
134 						memmove(buf, &buf[bufc], i - bufc);
135 						i -= bufc;
136 						bufc = 0;
137 						len = read(fd, &buf[i], buflen - i);
138 						if (len == -1)
139 							err(1, "read");
140 						else if (len == 0) {
141 							eof++;
142 							break;
143 						} else if (len < (ssize_t)(buflen - i))
144 							buflen = i + (size_t)len;
145 
146 						bufleft = (int)len;
147 						fndstr = 0;
148 					}
149 				} else {
150 					p = (u_char *)realloc(buf, buflen * 2);
151 					if (p == NULL)
152 						err(1, "realloc");
153 
154 					buf = p;
155 					if (!eof) {
156 						len = read(fd, &buf[i], buflen);
157 						if (len == -1)
158 							err(1, "read");
159 						else if (len == 0) {
160 							eof++;
161 							break;
162 						} else if (len < (ssize_t)(buflen - i))
163 							buflen = (size_t)len;
164 
165 						bufleft = (int)len;
166 					}
167 
168 					buflen *= 2;
169 				}
170 			}
171 
172 			if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
173 			    (type == RANDOM_TYPE_WORDS && isspace((int)buf[i])) ||
174 			    (eof && i == buflen - 1)) {
175 			make_token:
176 				n = rand_node_allocate();
177 				if (-1 != (int)i) {
178 					slen = i - (u_long)bufc;
179 					n->len = slen + 2;
180 					n->cp = (u_char *)malloc(slen + 2);
181 					if (n->cp == NULL)
182 						err(1, "malloc");
183 
184 					memmove(n->cp, &buf[bufc], slen);
185 					n->cp[slen] = buf[i];
186 					n->cp[slen + 1] = '\0';
187 					bufc = i + 1;
188 				}
189 				rand_node_append(n);
190 				fndstr = 1;
191 				numnode++;
192 			}
193 		}
194 	}
195 
196 	close(fd);
197 
198 	/* Necessary evil to compensate for files that don't end with a newline */
199 	if (bufc != i) {
200 		i--;
201 		goto make_token;
202 	}
203 
204 	for (i = numnode; i > 0; i--) {
205 		selected = ((int)denom * random())/(((double)RAND_MAX + 1) / numnode);
206 
207 		for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
208 			if (j == selected) {
209 				if (n->cp == NULL)
210 					break;
211 
212 				ret = printf("%.*s", (int)n->len - 1, n->cp);
213 				if (ret < 0)
214 					err(1, "printf");
215 				if (unique) {
216 					if (n == rand_root)
217 						rand_root = n->next;
218 					if (n == rand_tail)
219 						rand_tail = prev;
220 
221 					prev->next = n->next;
222 					rand_node_free(n);
223 					numnode--;
224 					break;
225 				}
226 			}
227 		}
228 	}
229 
230 	fflush(stdout);
231 
232 	if (!unique)
233 		rand_node_free_rec(rand_root);
234 
235 	return(0);
236 }
237