"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "littleutils/randomize.c" between
littleutils-1.2.4.tar.lz and littleutils-1.2.5.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

randomize.c  (littleutils-1.2.4.tar.lz):randomize.c  (littleutils-1.2.5.tar.lz)
/* randomize: Randomizes lines from a file or from stdin. /* randomize: Randomizes lines from a file or from stdin.
Copyright (C) 2004-2020 by Brian Lindholm. Copyright (C) 2004-2021 by Brian Lindholm.
This file is part of the littleutils utility set. This file is part of the littleutils utility set.
The randomize utility is free software; you can redistribute it and/or The randomize utility is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option) any the Free Software Foundation; either version 3, or (at your option) any
later version. later version.
The randomize utility is distributed in the hope that it will be useful, but The randomize utility is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details. more details.
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
the littleutils. If not, see <https://www.gnu.org/licenses/>. */ the littleutils. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h> #include <config.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H #ifdef HAVE_STDLIB_H
# include <stdlib.h> # include <stdlib.h>
#endif #endif
#ifdef HAVE_STRING_H #ifdef HAVE_STRING_H
# include <string.h> # include <string.h>
#endif #endif
#include <time.h> #ifdef HAVE_SYS_RANDOM_H
# include <sys/random.h>
#endif
#ifdef HAVE_SYS_STAT_H #ifdef HAVE_SYS_STAT_H
# include <sys/stat.h> # include <sys/stat.h>
#endif #endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
# define OPTEND -1 # define OPTEND -1
#else #else
# define OPTEND EOF # define OPTEND EOF
#endif #endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#include "rand_funcs.h"
#ifdef __MINGW32__ #ifdef __MINGW32__
extern int getopt (int argc, char * const *argv, const char *optstring); extern int getopt (int argc, char * const *argv, const char *optstring);
extern char *optarg; extern char *optarg;
extern int optind; extern int optind;
#endif #endif
#ifdef DJGPP #ifdef DJGPP
/* speed up stat command for DJGPP */ /* speed up stat command for DJGPP */
unsigned short _djstat_flags = 63; unsigned short _djstat_flags = 63;
skipping to change at line 73 skipping to change at line 79
/* help function */ /* help function */
static void static void
help (FILE *where) help (FILE *where)
{ {
fprintf (where, fprintf (where,
"randomize " PACKAGE_VERSION "\n" "randomize " PACKAGE_VERSION "\n"
"usage: randomize [-0/-z(ero_terminated)] [file...]\n"); "usage: randomize [-0/-z(ero_terminated)] [file...]\n");
} }
/* random functions */
static void
rand_seed ()
{
int URANDOM, urandom_worked = 1;
ssize_t bytes;
time_t junk;
unsigned short seed[3];
/* pre-populate arracy for seed using /dev/urandom */
URANDOM = open("/dev/urandom", O_RDONLY);
if (URANDOM < 0) {
urandom_worked = 0;
}
else {
/* use read instead of buffered fread to avoid grabbing extra bytes */
bytes = read(URANDOM, seed, sizeof seed);
if (bytes < 6) {
urandom_worked = 0;
}
}
close(URANDOM);
/* fall back to time and PID combo if /dev/urandom failed */
if (urandom_worked == 0) {
(void) time (&junk);
seed[0] = (unsigned short) (junk & 0xFFFF);
seed[1] = (unsigned short) (getpid () & 0xFFFF);
seed[2] = (unsigned short) ((junk >> 16) & 0xFFFF);
}
/* initialize random number generator seed */
#ifdef HAVE_LRAND48
(void) seed48 (seed);
#elif defined(HAVE_RANDOM)
srandom (((unsigned int) seed[0] << 16) + (unsigned int) seed[1]);
#else
srand (((unsigned int) seed[0] << 16) + (unsigned int) seed[1]);
#endif
}
static size_t
rand_int (size_t n)
{
size_t x;
#if defined(HAVE_LRAND48)
x = (size_t) ((double) n * drand48 ());
#elif defined(HAVE_RANDOM)
x = (size_t) ((double) n * ((double) random () / (double) 2147483648.0));
#else
x = (size_t) ((double) n * ((double) rand () / ((double) RAND_MAX + 1.0)));
#endif
if (x > (n - 1))
x = 0;
return (x);
}
/* read stdin into a buffer allocated here, returning number of bytes read */ /* read stdin into a buffer allocated here, returning number of bytes read */
static size_t static size_t
load_stdin (char **buffer, char delimiter) load_stdin (char **buffer, char delimiter)
{ {
char *tmp_buffer; char *tmp_buffer;
size_t buffer_size, last_read, size; size_t buffer_size, last_read, size;
/* set up initial block */ /* set up initial block */
buffer_size = BLOCKSIZE; buffer_size = BLOCKSIZE;
skipping to change at line 207 skipping to change at line 154
fprintf (stderr, "randomize error: out of memory C!\n"); fprintf (stderr, "randomize error: out of memory C!\n");
exit (4); exit (4);
} }
*buffer = tmp_buffer; *buffer = tmp_buffer;
} }
/* read a file into a pre-allocated buffer, returning number of bytes read /* read a file into a pre-allocated buffer, returning number of bytes read
from file */ from file */
static size_t static size_t
load_file (char *buffer, char *filename, size_t size, size_t index, load_file (char *buffer, char *filename, size_t size, size_t index, char delimit
char delimiter) er)
{ {
size_t qty_read; size_t qty_read;
FILE *input_file; FILE *input_file;
input_file = fopen (filename, "r"); input_file = fopen (filename, "r");
if (input_file == NULL) if (input_file == NULL)
{ {
fprintf (stderr, "randomize error: can't open %s!\n", filename); fprintf (stderr, "randomize error: can't open %s!\n", filename);
exit (3); exit (3);
} }
skipping to change at line 370 skipping to change at line 316
delimiter_qty = 0; delimiter_qty = 0;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
if (buffer[i] == delimiter) if (buffer[i] == delimiter)
delimiter_qty++; delimiter_qty++;
/* create access arrays for index and length */ /* create access arrays for index and length */
scan_buffer (buffer, &indices, &lengths, size, delimiter_qty, delimiter); scan_buffer (buffer, &indices, &lengths, size, delimiter_qty, delimiter);
rand_seed (); rand_seed ();
/* shuffle the list twice */ /* shuffle the list twice */
for (k = 0; k < 2; k++) for (k = 0; k < 2; k++)
for (i = 0; i < delimiter_qty; i++) for (i = 0; i < delimiter_qty; i++)
{ {
j = rand_int (delimiter_qty); j = rand_int (delimiter_qty);
index = indices[i]; index = indices[i];
indices[i] = indices[j]; indices[i] = indices[j];
indices[j] = index; indices[j] = index;
length = lengths[i]; length = lengths[i];
lengths[i] = lengths[j]; lengths[i] = lengths[j];
lengths[j] = length; lengths[j] = length;
 End of changes. 8 change blocks. 
68 lines changed or deleted 14 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)