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