-- Debian GNU/Linux 2.2 is out! ( http://www.debian.org/ ) Email: Herbert Xu ~{PmV>HI~} <[EMAIL PROTECTED]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Hi, you know how most free software scratches an itch? Well, I've had an itch for a while - there's no standard text utility for randomizing the lines in a file, which is useful for making multiple, unique random picks from a file (rather than just one pick at a time, which may have repetitions and such). So a while back I took a stab at writing one. This is very, very rudimentary, but it does the job rather well. My main usage for it is generating a unique, purely-random but static-ordered playlist of all my MP3s; for example, I have a shellscript which looks like this to generate a random playlist and use it: #!/bin/sh find ~/mp3s -name '*.mp3' | randline > /tmp/$$.m3u xmms /tmp/$$.m3u rm /tmp/$$.m3u The reason I'm rambling on about this is I'm including the source code for randline, and am hoping you'd see fit to include it in the Debian textutils distribution, and possibly pass it along to whoever is upstream that maintains the standard stuff which textutils maintains. Thanks in advance for your consideration. :) -- Joshua Shagam /"\ ASCII Ribbon Campaign [EMAIL PROTECTED] \ / No HTML/RTF in email www.cs.nmsu.edu/~joshagam X No Word docs in email mp3.com/fluffyporcupine / \ Respect for open standards/* randline.c ** randomize stdio on a per-line basis ** More efficient C version ** ** [EMAIL PROTECTED] for comments. Source released to the public domain. ** Share and enjoy. Whee. */ #include <stdlib.h> #include <stdio.h> #include <time.h> #include <limits.h> static FILE *crypto = NULL; void initrand(void) { crypto = fopen("/dev/urandom", "r"); if (!crypto) srand(time(NULL)); } int myrand(void) { int stdlib = 0, blah; if (!crypto) return rand(); fread(&blah, sizeof(blah), 1, crypto); return blah & INT_MAX; } int main(void) { char **lines; int nlines = 0; while (!feof(stdin)) { lines = (char **)realloc(lines, (nlines + 1)*sizeof(char *)); lines[nlines] = (char *)malloc(1024); fgets(lines[nlines], 1023, stdin); nlines++; } initrand(); while (nlines) { int i = myrand()%nlines; if (strlen(lines[i])) fputs(lines[i], stdout); free(lines[i]); lines[i] = lines[nlines - 1]; nlines--; } free(lines); return 0; }
