#include #include #include #include char *malloc(); void exit(); #define TMP "/tmp/unsort" int main(int argc, char *argv[]) { FILE *fp, *fopen(); char fname[1024]; pid_t getpid(); int c; int i, k, ccnt, scnt; int seed; char **thestrings; char *tmp, *ptr; int rand(); void srand(unsigned int); int atoi(const char *); if( argc > 1 && strcmp(argv[1], "-s") == 0 ) { if( argc < 3 ) { fprintf(stderr, "Usage: unsort [ -s seed ] [ file ] \n"); exit(1); } seed = atoi(argv[2]); argc -= 2; argv += 2; } else seed = 73802; srand(seed); if( argc == 1 ) /* reading from standard input */ { sprintf(fname, "%s.%d", TMP, (int)getpid()); fp = fopen(fname, "w"); while( ( c = getchar() ) != EOF ) fputc(c, fp); fclose(fp); fp = fopen(fname, "r"); } else { if( ( fp = fopen(argv[1], "r") ) == NULL ) { fprintf(stderr, "%s: file not found.\n", argv[1]); exit(1); } } /* how big is the file, how many strings? */ for( ccnt=scnt=0 ; ( c = getc(fp) ) != EOF ; ccnt++ ) { if( c=='\n' ) scnt++; } /* load the strings */ thestrings = (char **) malloc(sizeof(char *) * ( scnt+1 )); tmp = (char *) malloc(sizeof(char) * ccnt); rewind(fp); fread(tmp, sizeof(char), ccnt, fp); thestrings[0] = tmp; for( k=i=0 ; i %d lost??\n", i); else printf("%s\n", thestrings[i]); unlink(fname); exit(0); }