1 /*
2    This code randomizes the order of lines in a file. Based on a tool
3    'randomize' by Jef Poskanzer <jef@acme.com>.
4 
5    I tweaked it a bit to work better with evilfinder. All lines that
6    do not start with digit or * and digit are ignored, short lines are
7    ignored, max line length is 1024, randomness is taken from /dev/urandom.
8 
9  */
10 
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 
18 #define MAXLINE 1024
19 
20 
21 int rf=-1;
22 
get_random(void)23 unsigned int get_random(void) {
24   int val;
25   if (rf<0) {
26     rf=open("/dev/urandom",O_RDONLY);
27     if (rf<0) { perror("/dev/urandom"); exit(1); }
28   }
29   read(rf,&val,4);
30   return val;
31 }
32 
33 
main(void)34 int main(void) {
35   int    i, j, maxlines, nlines;
36   char   line[MAXLINE];
37   char** lines;
38   char*  t;
39 
40   maxlines = 500;
41   lines = (char**) malloc( maxlines * sizeof(char*) );
42   nlines = 0;
43 
44   while (fgets(line,MAXLINE,stdin)) {
45 
46     if (strlen(line)<5) continue;
47     if (!(isdigit(line[0]) || (line[0]=='*' && isdigit(line[1])))) continue;
48 
49     if (nlines>=maxlines) {
50       maxlines += 10000;
51       lines = (char**)realloc((char*)lines,maxlines*sizeof(char*));
52     }
53 
54     lines[nlines] = (char*)malloc(strlen(line)+1);
55     strcpy(lines[nlines],line);
56     nlines++;
57 
58   }
59 
60   for (i=0;i<nlines;++i) {
61     j=get_random() % nlines;
62     t=lines[j];
63     lines[j]=lines[i];
64     lines[i]=t;
65   }
66 
67   for (i=0;i<nlines;++i) fputs(lines[i],stdout);
68 
69   exit(0);
70 
71 }
72