home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 December / simtel1292_SIMTEL_1292_Walnut_Creek.iso / msdos / c / help.arc / HELPDIR.C < prev    next >
C/C++ Source or Header  |  1988-03-10  |  4KB  |  196 lines

  1. /* Help: (C) Copyright 1986 Michael A. Shiels
  2.    
  3.    This program is Copyright by Michael Shiels and may be given away and
  4.    used in other programs as long as the Copyright notices are not removed
  5.    or altered.  No profit can be made from the distribution of this product
  6.    except by the author.
  7. */
  8.  
  9. #define LINT_ARGS
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <dos.h>
  13.  
  14. #include    "masdos.h"
  15.  
  16. #include "helpdir.h"
  17.  
  18. #include    "cpy.x"
  19. #include    "next.x"
  20. #include    "cptolow.x"
  21. #include    "ssort.x"
  22.  
  23. #define iswhite(c)    ((c) == ' ' || (c) == '\t')
  24.  
  25. int    haswild(s)
  26. register char    *s;
  27. {
  28.     for( ; *s ; s++)
  29.         if( *s == '*' || *s == '?' )
  30.             return 1;
  31.     return 0;
  32. }
  33.  
  34. /*----------------------------------------------------------------------*/
  35.  
  36. static int  dirtoa( target, infop )
  37. register char        *target   ;
  38. register struct    FILEINFO  *infop    ;
  39. {
  40.     char *startstr = target;
  41.  
  42.     target = cptodlower( target, infop->fi_name );
  43.  
  44.     return( target - startstr );
  45. }
  46.  
  47. /*----------------------------------------------------------------------*/
  48.  
  49. static int    add_entry( infop, dp )
  50. struct    FILEINFO        *infop    ;
  51. register HDIRECTORY    *dp    ;
  52. {
  53.     char        buf[128] ;
  54.     register int    len   ;
  55.  
  56.     if( IS_HIDDEN(infop->fi_attrib) || *infop->fi_name == '.' )
  57.         return 1;
  58.  
  59.     if( dp->maxdirs <= 0  )        /* No more room in dirv. return    */
  60.         return 0;        /* error status            */
  61.  
  62.     if( IS_SUBDIR(infop->fi_attrib) )    
  63.     {
  64.         if( dp->dirs )
  65.             dp->ndirs++  ;
  66.         else
  67.             return 1;
  68.     }
  69.     else
  70.     {
  71.         if( dp->files )
  72.             dp->nfiles++  ;
  73.         else
  74.             return 1;
  75.     }
  76.  
  77.     len = dirtoa( buf, infop );
  78.  
  79.     if( len > dp->width )
  80.         dp->width = len ;
  81.         
  82.     if( *dp->lastdir = malloc(len + 1) )
  83.     {
  84.         strcpy( *dp->lastdir++, buf )    ;
  85.         --dp->maxdirs;
  86.         return 1;
  87.     }
  88.  
  89.     return 0;
  90. }
  91.  
  92. static  int  cmp( pp1, pp2 )
  93. char    **pp1, **pp2;
  94. {
  95.     register char    *p1 = *pp1;
  96.     register char    *p2 = *pp2;
  97.     int        num1, num2;
  98.  
  99.     while( (num1 = (isdigit(*p1) && isdigit(*p2))) || (*p1 == *p2 && *p1) )
  100.     {
  101.         if( !num1 )
  102.         {
  103.             p1++;
  104.             p2++;
  105.         }
  106.         else
  107.         {
  108.             num1 = num2 = 0;
  109.  
  110.             do {
  111.                 num1 = (num1 * 10) + ( *p1++ - '0');
  112.             } while( isdigit(*p1) );
  113.  
  114.             do {
  115.                 num2 = (num2 * 10) + ( *p2++ - '0');
  116.             } while( isdigit(*p2) );
  117.  
  118.             if( num1 != num2 )
  119.                 return( num1 - num2 );
  120.         }
  121.     }
  122.  
  123.     return( *p1 - *p2 );
  124. }
  125.  
  126. /*----------------------------------------------------------------------*/
  127.  
  128. HDIRECTORY  *mk_hdir( size )
  129. register unsigned size;
  130. {
  131.     register HDIRECTORY  *dp;
  132.     register unsigned   amt_mem;
  133.  
  134.     amt_mem = sizeof(HDIRECTORY) + (size * sizeof(char *));
  135.  
  136.     if( !( dp = (HDIRECTORY *) malloc(amt_mem) ))
  137.         return 0;
  138.  
  139.     memset( dp, 0, amt_mem );    /* Initialize *dp to zeros */
  140.  
  141.     dp->maxdirs = size ;
  142.     dp->lastdir = dp->dirv;
  143.     return dp;
  144. }
  145.  
  146. /*----------------------------------------------------------------------*/
  147.  
  148. del_hdir( dp )
  149. register HDIRECTORY    *dp;
  150. {
  151.     register char    **v;
  152.  
  153.     for( v = dp->dirv; v < dp->lastdir ; free( *v++ ) )
  154.         ;
  155.  
  156.     free( dp );
  157. }
  158.  
  159. /*----------------------------------------------------------------------*/
  160.  
  161. hdir( spec, dp )
  162. char       *spec;
  163. HDIRECTORY  *dp;
  164. {
  165.     struct    FILEINFO     info;
  166.     char          **firstdir;
  167.  
  168.     fchgdta( &info );
  169.  
  170.     firstdir = dp->lastdir;
  171.  
  172.     if( !ffind_first( spec, A_ALL)  )
  173.     {
  174.         if( !add_entry(&info, dp ) )    
  175.             goto aborted;
  176.  
  177.         if( haswild(spec) )
  178.         {
  179.             while( !ffind_next() )
  180.                 if( !add_entry(&info, dp ) )
  181.                     goto aborted;
  182.         }
  183.     }
  184.  
  185.     if( dp->sort )
  186.         ssort( (char *)firstdir, dp->lastdir - firstdir,
  187.                     sizeof(char*), cmp);
  188.     goto abort;
  189.  
  190. aborted:
  191.     E( "Help:hdir: unable to add entries - increase the appropriate    parameter\n" );
  192. abort:
  193.     frstdta();            /* Restore the original dta    */
  194.  
  195. }
  196.