#include "qsort.c" #include #include #include #include char *_strs; char *p1; char *(*_ptrs)[]; int p2=0; struct stat sb; char line[1000]; int lines=0; FILE *fin; char *INFILE; int sortcol=0; int cmpfunc_l(const void *data1, const void *data2) { #define str1 ((char*)data1) #define str2 ((char*)data2) int a=strlen(str1), b=strlen(str2), p=0; #define tobool(x) ((x)?1:0) if (tobool(a>sortcol) ^ tobool(b>sortcol)) { return( a>sortcol ? -1 : 1 ); } else { if (a>sortcol) p=sortcol; if ((a=strcmp(str1+p, str2+p))!=0) return(a); return(strcmp((char*)data1, (char*)data2)); } #undef tobool #undef str1 #undef str2 } int cmpfunc(const void *data1, const void *data2) { return(strcmp((char*)data1, (char*)data2)); } void main(int argc, char *argv[]) { int argIN=1; if (argc<2) { fprintf(stderr, "gds/FH production! Large file's sorter. Freeware. 2000\n"); printf("Use: largsort [/+COLUMN] IN > OUT , COLUMN=0..hz\n"); return;} if (argv[1][0]=='/') { if (argv[1][1]=='+') { sortcol=atoi(&(argv[1][2])); argIN++; } } INFILE=argv[argIN]; if (stat(INFILE, &sb)) {printf("can't open file: %s\n", INFILE); return;} fin=fopen(INFILE, "rt"); if (!fin) {printf("can't open file: %s", INFILE); return;} #define fout stdout _strs=malloc(sb.st_size+16); if (!_strs) {fprintf(stderr, "can't allocate %i bytes of memory\n", sb.st_size);return;} p1=_strs; while(fgets(line, sizeof(line)-1, fin)) { // if (line[strlen(line)-1]==0xA) line[strlen(line)-1]=0; strcpy(p1, line); p1=1+strchr(p1, 0); lines++; } *p1=0; _ptrs=malloc(sizeof(void*) * lines + 16); p1=_strs; while(*p1) { (*_ptrs)[p2]=p1; p1=1+strchr(p1, 0); p2++; } #define ptrs (*_ptrs) if (sortcol==0) mysort(*_ptrs, lines, cmpfunc); else mysort(*_ptrs, lines, cmpfunc_l); p2=0; while(lines--) { fputs((*_ptrs)[p2++], fout); fputs("\0xD\0xA", fout); } free(_strs); fclose(fin); /* fclose(fout); */ }