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