randomize_fd.c (181412) | randomize_fd.c (181527) |
---|---|
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 --- 11 unchanged lines hidden (view full) --- 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 --- 11 unchanged lines hidden (view full) --- 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> --- 6 unchanged lines hidden (view full) --- 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> --- 6 unchanged lines hidden (view full) --- 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) --- 104 unchanged lines hidden (view full) --- 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) --- 104 unchanged lines hidden (view full) --- 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) --- 21 unchanged lines hidden (view full) --- 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) --- 21 unchanged lines hidden (view full) --- 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) --- 18 unchanged lines hidden --- | 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) --- 18 unchanged lines hidden --- |