jac@yoko.rutgers.edu (Jonathan A. Chandross) (12/02/90)
Submitted-by: NONE Posting-number: Volume 1, Source:17 Archive-name: util/sort Architecture: ANY_2 Version-number: 1.00 This C utility sorts text files in memory. Both normal and reverse sorting are supported. Enjoy. =sort.c -/* - * sort.c - * - * Sort text files in memory. - * - * Usage: - * sort [-r] [file_1] [file_2] [file_3] [...] - * - * Options: - * -r sort in reverse order - * - * If no file is specified, sort reads from standard in. - * - * Contributed Anonymously. Written: November 1983 - * - * Version 1.00 - * - */ - -#include "stdio.h" - -#define TRUE 1 -#define FALSE 0 -#define NL '\n' -#define EOS '\0' -#define MAXTEXT 16384 -#define MAXPTR 1024 - -char linebuf[MAXTEXT] ; -char *lineptr[MAXPTR] ; -int reverse ; - -main(argc, argv) -int argc ; -char *argv[] ; -{ - char *s ; - FILE *input ; - - - reverse = FALSE ; - while( --argc>0 && **++argv == '-' ) - for( s=&argv[0][1] ; *s != EOS ; s++ ) - switch( *s ) { - case 'r': - reverse = TRUE ; - break ; - default: - fprintf(stderr, "sort: unknown option %c\n", *s ) ; - argc = -1 ; - break ; - } - - if( argc < 0 ) { - fprintf(stderr, "usage: sort files\n"); - exit(1) ; - } - - if( argc == 0 ) - sort( stdin, "stdin" ) ; - else - for( ; argc>0 ; argc--,argv++) - if( (input=fopen(*argv,"r")) == NULL ) { - fprintf(stderr, "sort: can't open %s\n", *argv) ; - exit(1) ; - } - else { - sort( input, *argv ) ; - fclose( input ) ; - } - - exit(0) ; - -} /* end main */ - - -/* sort - sort text files in memory */ - -sort( in, fname ) -FILE *in ; -char *fname ; -{ - int nlines ; - - if( (nlines=readlines(in)) > 0 ) { - sortlines( 0, nlines-1 ) ; - printlines( stdout, nlines ) ; - } - else if( nlines < 0 ) - fprintf(stderr, "sort: file is too large: %s\n", fname ) ; - -} /* end sort */ - - -/* readlines - store text in linebuf */ - -readlines( in ) -FILE *in ; -{ - int nlines, len ; - char *lbptr, *endbuffer ; - - nlines = 0 ; - lbptr = &linebuf[0] ; - endbuffer = &linebuf[MAXTEXT-1] ; - - do { - if( (len = getline(lbptr, in)) == EOF ) - return nlines ; - lineptr[ nlines ] = lbptr ; - nlines++ ; - lbptr += len + 1 ; /* 1 for EOS */ - } while( lbptr < endbuffer && nlines < MAXPTR ) ; - - return EOF ; - -} /* end readlines */ - - -/* sortlines - quicksort on pointers */ - -sortlines( left, right ) -int left, right ; -{ - int l, r, middle ; - char *pivot, *temp ; - l = left ; r = right ; - middle = (l + r) / 2 ; - pivot = lineptr[ middle ] ; - - do { - - while( strcmp(lineptr[l],pivot) < 0 ) - l++ ; - while( strcmp(pivot,lineptr[r]) < 0 ) - r-- ; - - if( l <= r ) { - temp = lineptr[l] ; - lineptr[l] = lineptr[r] ; - lineptr[r] = temp ; - l++ ; - r-- ; - } - - } while( l <= r ) ; - - if( left < r ) - sortlines( left, r ) ; - if( l < right ) - sortlines( l, right ) ; - -} /* end sortlines */ - -/* printlines */ - -printlines( out, nlines ) -FILE *out ; -int nlines ; -{ - int i ; - - if( reverse ) - for( i=nlines-1 ; i >= 0 ; i-- ) - fprintf( out, "%s", lineptr[i] ) ; - else - for( i=0 ; i < nlines ; i++ ) - fprintf( out, "%s", lineptr[i] ) ; - -} /* end printlines */ - - -/* getline - read input line */ - -getline( ptr, input ) -char *ptr ; -FILE *input ; -{ - int c ; - char *bufptr ; - - bufptr = ptr ; - - while( (c=agetc(input)) != NL ) { - if( c == EOF ) - return EOF ; - *bufptr++ = c ; - } - - *bufptr++ = NL ; - *bufptr++ = EOS ; - return bufptr - ptr ; - -} /* end getline */ - + END OF ARCHIVE