2003-02-11 19:32:18 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003 Sean Chittenden <seanc@FreeBSD.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
2008-08-08 02:46:47 +00:00
|
|
|
#include <errno.h>
|
2003-02-15 10:26:10 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
#include "randomize_fd.h"
|
|
|
|
|
|
|
|
static struct rand_node *rand_root;
|
|
|
|
static struct rand_node *rand_tail;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
static struct rand_node *
|
|
|
|
rand_node_allocate(void)
|
2003-02-11 19:32:18 +00:00
|
|
|
{
|
|
|
|
struct rand_node *n;
|
|
|
|
|
2008-08-11 23:24:42 +00:00
|
|
|
n = (struct rand_node *)malloc(sizeof(struct rand_node));
|
2003-02-11 19:32:18 +00:00
|
|
|
if (n == NULL)
|
2008-08-11 23:24:42 +00:00
|
|
|
err(1, "malloc");
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2008-08-11 23:24:42 +00:00
|
|
|
n->len = 0;
|
|
|
|
n->cp = NULL;
|
|
|
|
n->next = NULL;
|
2003-02-11 19:32:18 +00:00
|
|
|
return(n);
|
|
|
|
}
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
static void
|
|
|
|
rand_node_free(struct rand_node *n)
|
2003-02-11 19:32:18 +00:00
|
|
|
{
|
|
|
|
if (n != NULL) {
|
|
|
|
if (n->cp != NULL)
|
|
|
|
free(n->cp);
|
|
|
|
|
|
|
|
free(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
static void
|
|
|
|
rand_node_free_rec(struct rand_node *n)
|
2003-02-11 19:32:18 +00:00
|
|
|
{
|
|
|
|
if (n != NULL) {
|
|
|
|
if (n->next != NULL)
|
|
|
|
rand_node_free_rec(n->next);
|
|
|
|
|
|
|
|
rand_node_free(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
static void
|
|
|
|
rand_node_append(struct rand_node *n)
|
2003-02-11 19:32:18 +00:00
|
|
|
{
|
2003-02-15 10:26:10 +00:00
|
|
|
if (rand_root == NULL)
|
2003-02-11 19:32:18 +00:00
|
|
|
rand_root = rand_tail = n;
|
2003-02-15 10:26:10 +00:00
|
|
|
else {
|
2003-02-11 19:32:18 +00:00
|
|
|
rand_tail->next = n;
|
|
|
|
rand_tail = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
int
|
|
|
|
randomize_fd(int fd, int type, int unique, double denom)
|
2003-02-11 19:32:18 +00:00
|
|
|
{
|
2006-04-14 17:32:27 +00:00
|
|
|
u_char *buf;
|
2008-08-08 02:46:47 +00:00
|
|
|
u_int slen;
|
|
|
|
u_long i, j, numnode, selected;
|
2003-02-11 19:32:18 +00:00
|
|
|
struct rand_node *n, *prev;
|
2003-02-15 10:26:10 +00:00
|
|
|
int bufleft, eof, fndstr, ret;
|
2008-08-08 02:46:47 +00:00
|
|
|
size_t bufc, buflen;
|
2003-02-15 10:26:10 +00:00
|
|
|
ssize_t len;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
|
|
|
rand_root = rand_tail = NULL;
|
2003-02-15 10:26:10 +00:00
|
|
|
bufc = i = 0;
|
2010-06-14 13:03:25 +00:00
|
|
|
bufleft = eof = fndstr = numnode = 0;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
|
|
|
if (type == RANDOM_TYPE_UNSET)
|
|
|
|
type = RANDOM_TYPE_LINES;
|
|
|
|
|
|
|
|
buflen = sizeof(u_char) * MAXBSIZE;
|
|
|
|
buf = (u_char *)malloc(buflen);
|
|
|
|
if (buf == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
|
|
|
|
while (!eof) {
|
|
|
|
/* Check to see if we have bits in the buffer */
|
|
|
|
if (bufleft == 0) {
|
|
|
|
len = read(fd, buf, buflen);
|
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
2003-02-15 10:26:10 +00:00
|
|
|
else if (len == 0) {
|
2003-02-11 19:32:18 +00:00
|
|
|
eof++;
|
2003-02-15 10:26:10 +00:00
|
|
|
break;
|
|
|
|
} else if ((size_t)len < buflen)
|
|
|
|
buflen = (size_t)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
bufleft = (int)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Look for a newline */
|
2003-02-15 10:26:10 +00:00
|
|
|
for (i = bufc; i <= buflen && bufleft >= 0; i++, bufleft--) {
|
2003-02-11 19:32:18 +00:00
|
|
|
if (i == buflen) {
|
|
|
|
if (fndstr) {
|
|
|
|
if (!eof) {
|
|
|
|
memmove(buf, &buf[bufc], i - bufc);
|
2003-02-15 10:26:10 +00:00
|
|
|
i -= bufc;
|
2003-02-11 19:32:18 +00:00
|
|
|
bufc = 0;
|
|
|
|
len = read(fd, &buf[i], buflen - i);
|
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
|
|
|
else if (len == 0) {
|
|
|
|
eof++;
|
|
|
|
break;
|
2003-02-15 10:26:10 +00:00
|
|
|
} else if (len < (ssize_t)(buflen - i))
|
|
|
|
buflen = i + (size_t)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
bufleft = (int)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
fndstr = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2006-04-14 17:32:27 +00:00
|
|
|
buflen *= 2;
|
|
|
|
buf = (u_char *)realloc(buf, buflen);
|
|
|
|
if (buf == NULL)
|
2003-02-11 19:32:18 +00:00
|
|
|
err(1, "realloc");
|
|
|
|
|
|
|
|
if (!eof) {
|
2006-04-14 17:32:27 +00:00
|
|
|
len = read(fd, &buf[i], buflen - i);
|
2003-02-11 19:32:18 +00:00
|
|
|
if (len == -1)
|
|
|
|
err(1, "read");
|
|
|
|
else if (len == 0) {
|
|
|
|
eof++;
|
|
|
|
break;
|
2003-02-15 10:26:10 +00:00
|
|
|
} else if (len < (ssize_t)(buflen - i))
|
2006-04-14 17:32:27 +00:00
|
|
|
buflen = i + (size_t)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
bufleft = (int)len;
|
2003-02-11 19:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((type == RANDOM_TYPE_LINES && buf[i] == '\n') ||
|
2006-04-14 17:32:27 +00:00
|
|
|
(type == RANDOM_TYPE_WORDS && isspace(buf[i])) ||
|
2003-02-11 19:32:18 +00:00
|
|
|
(eof && i == buflen - 1)) {
|
2003-02-15 10:26:10 +00:00
|
|
|
make_token:
|
2008-08-10 11:31:56 +00:00
|
|
|
if (numnode == RANDOM_MAX_PLUS1) {
|
2008-08-08 02:46:47 +00:00
|
|
|
errno = EFBIG;
|
2008-08-10 11:31:56 +00:00
|
|
|
err(1, "too many delimiters");
|
2008-08-08 02:46:47 +00:00
|
|
|
}
|
|
|
|
numnode++;
|
2003-02-11 19:32:18 +00:00
|
|
|
n = rand_node_allocate();
|
2003-02-15 10:26:10 +00:00
|
|
|
if (-1 != (int)i) {
|
|
|
|
slen = i - (u_long)bufc;
|
|
|
|
n->len = slen + 2;
|
|
|
|
n->cp = (u_char *)malloc(slen + 2);
|
|
|
|
if (n->cp == NULL)
|
|
|
|
err(1, "malloc");
|
|
|
|
|
|
|
|
memmove(n->cp, &buf[bufc], slen);
|
|
|
|
n->cp[slen] = buf[i];
|
|
|
|
n->cp[slen + 1] = '\0';
|
|
|
|
bufc = i + 1;
|
|
|
|
}
|
2003-02-11 19:32:18 +00:00
|
|
|
rand_node_append(n);
|
2003-02-15 10:26:10 +00:00
|
|
|
fndstr = 1;
|
2003-02-11 19:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)close(fd);
|
|
|
|
|
2003-02-15 10:26:10 +00:00
|
|
|
/* Necessary evil to compensate for files that don't end with a newline */
|
|
|
|
if (bufc != i) {
|
|
|
|
i--;
|
|
|
|
goto make_token;
|
|
|
|
}
|
|
|
|
|
2003-02-11 19:32:18 +00:00
|
|
|
for (i = numnode; i > 0; i--) {
|
2008-08-08 02:46:47 +00:00
|
|
|
selected = random() % numnode;
|
2003-02-11 19:32:18 +00:00
|
|
|
|
|
|
|
for (j = 0, prev = n = rand_root; n != NULL; j++, prev = n, n = n->next) {
|
|
|
|
if (j == selected) {
|
2003-02-15 10:26:10 +00:00
|
|
|
if (n->cp == NULL)
|
|
|
|
break;
|
|
|
|
|
2008-08-11 23:24:42 +00:00
|
|
|
if ((int)(denom * random() /
|
|
|
|
RANDOM_MAX_PLUS1) == 0) {
|
|
|
|
ret = printf("%.*s",
|
|
|
|
(int)n->len - 1, n->cp);
|
2008-08-08 02:46:47 +00:00
|
|
|
if (ret < 0)
|
|
|
|
err(1, "printf");
|
|
|
|
}
|
2003-02-11 19:32:18 +00:00
|
|
|
if (unique) {
|
|
|
|
if (n == rand_root)
|
|
|
|
rand_root = n->next;
|
|
|
|
if (n == rand_tail)
|
|
|
|
rand_tail = prev;
|
|
|
|
|
|
|
|
prev->next = n->next;
|
|
|
|
rand_node_free(n);
|
|
|
|
numnode--;
|
|
|
|
}
|
2008-08-08 02:46:47 +00:00
|
|
|
break;
|
2003-02-11 19:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
if (!unique)
|
|
|
|
rand_node_free_rec(rand_root);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|