home *** CD-ROM | disk | FTP | other *** search
/ Zodiac Super OZ / MEDIADEPOT.ISO / FILES / 16 / FREEDOS.ZIP / FD_A4PRE.ZIP / SOURCE / SORT.ZIP / SORT.C < prev    next >
C/C++ Source or Header  |  1995-05-23  |  4KB  |  157 lines

  1. /*
  2. *    SORT - reads line of a file and sorts them in order
  3. *    Copyright  1995  Jim Lynch
  4. *
  5. *    This program is free software; you can redistribute it and/or modify
  6. *    it under the terms of the GNU General Public License as published by
  7. *    the Free Software Foundation; either version 2 of the License, or
  8. *    (at your option) any later version.
  9. *
  10. *    This program is distributed in the hope that it will be useful,
  11. *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *    GNU General Public License for more details.
  14. *
  15. *    You should have received a copy of the GNU General Public License
  16. *    along with this program; if not, write to the Free Software
  17. *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <malloc.h>
  23. #ifndef MAXPATH
  24. #define MAXPATH 80
  25. #endif
  26. #define MAXRECORDS  10000    /* maximum number of records that can be
  27.                  * sorted */
  28. #define MAXLEN    1023        /* maximum record length */
  29.  
  30. int nrec;
  31. int             rev;        /* reverse flag */
  32. int             help;        /* help flag */
  33. int             sortcol;    /* sort column */
  34. int             err = 0;    /* error counter */
  35. char           *list[MAXRECORDS];
  36.  
  37. int
  38. cmpr(void *a, void *b)
  39. {
  40.     char           *A, *B;
  41.  
  42.     A = *(char **) a;
  43.     B = *(char **) b;
  44.  
  45.     if (sortcol > 0) {
  46.     if (strlen(A) > sortcol)
  47.         A += sortcol;
  48.     else
  49.         A = "";
  50.     if (strlen(B) > sortcol)
  51.         B += sortcol;
  52.     else
  53.         B = "";
  54.     }
  55.     if (!rev)
  56.     return strcmp(A, B);
  57.     else
  58.     return strcmp(B, A);
  59. }
  60.  
  61. void
  62. usage(void)
  63. {
  64.     fputs("SORT: V1.0 R0\n", stderr);
  65.     if (err)
  66.     fputs("Invalid parameter\n", stderr);
  67.     fputs("    SORT [/R] [/+num] [/?]\n", stderr);
  68.     fputs("    /R    Reverse order\n", stderr);
  69.     fputs("    /+num    start sorting with column num, 1 based\n", stderr);
  70.     fputs("    /?    help\n", stderr);
  71. }
  72.  
  73. int main(int argc, char **argv)
  74. {
  75.     char            filename[MAXPATH];
  76.     char            temp[MAXLEN + 1];
  77.     char           *cp;        /* option character pointer */
  78.     int             nr;
  79.     int             l,i;
  80.     FILE           *fi;        /* file descriptor */
  81.  
  82.  
  83.     sortcol = 0;
  84.     strcpy(filename, "");
  85.     rev = 0;
  86.     while (--argc) {
  87.     if (*(cp = *++argv) == '/') {
  88.         switch (cp[1]) {
  89.         case 'R':
  90.         case 'r':
  91.         rev = 1;
  92.         break;
  93.         case '?':
  94.         case 'h':
  95.         case 'H':
  96.         help = 1;
  97.         break;
  98.         case '+':
  99.         sortcol = atoi(cp + 1);
  100.         if (sortcol)
  101.             sortcol--;
  102.         break;
  103.         default:
  104.         err++;
  105.         }
  106.     } else {        /* must be a file name */
  107.         strcpy(filename, *argv);
  108.     }
  109.     }
  110.     if (err || help) {
  111.     usage();
  112.     exit(1);
  113.     }
  114.     fi = stdin;            /* just in case */
  115.     if (strlen(filename)) {
  116.     if ((fi = fopen(filename, "r")) == NULL) {
  117.         fprintf(stderr, "SORT: Can't open %s for read\n", filename);
  118.         exit(2);
  119.     }
  120.     }
  121.     for (nr = 0; nr < MAXRECORDS; nr++) {
  122.         list[nr]=NULL;
  123.     }
  124.     for (nr = 0; nr < MAXRECORDS; nr++) {
  125.     char *p;
  126.     nrec=nr;
  127.     if(nrec==900) {
  128.         nrec=nr;
  129.         p=(char *) malloc(1000);
  130.     }
  131.     if (fgets(temp, MAXLEN, fi) == NULL)
  132.         break;
  133.     if(strlen(temp))
  134.         temp[strlen(temp)-1]='\0';
  135.     else
  136.         fputs("SORT: Internal error\n",stderr);
  137.     l=strlen(temp);
  138.     p = (char *) malloc(l + 8);
  139.     if (p == NULL) {
  140.         fputs("SORT: Insufficient memory\n", stderr);
  141.         exit(3);
  142.     }
  143.     list[nr] = p;
  144.     strcpy(list[nr], temp);
  145.     }
  146.     if (nr == MAXRECORDS) {
  147.     fputs("SORT: number of records exceeds maximum\n", stderr);
  148.     exit(4);
  149.     }
  150.     qsort((void *) list, nr, sizeof(char *), cmpr);
  151.     for (i = 0; i < nr; i++) {
  152.     fputs(list[i], stdout);
  153.     fputs("\n",stdout);
  154.     }
  155.     return 0;
  156. }
  157.