home *** CD-ROM | disk | FTP | other *** search
- /* Help: (C) Copyright 1986 Michael A. Shiels
-
- This program is Copyright by Michael Shiels and may be given away and
- used in other programs as long as the Copyright notices are not removed
- or altered. No profit can be made from the distribution of this product
- except by the author.
- */
-
- #define LINT_ARGS
- #include <stdio.h>
- #include <ctype.h>
- #include <dos.h>
-
- #include "masdos.h"
-
- #include "helpdir.h"
-
- #include "cpy.x"
- #include "next.x"
- #include "cptolow.x"
- #include "ssort.x"
-
- #define iswhite(c) ((c) == ' ' || (c) == '\t')
-
- int haswild(s)
- register char *s;
- {
- for( ; *s ; s++)
- if( *s == '*' || *s == '?' )
- return 1;
- return 0;
- }
-
- /*----------------------------------------------------------------------*/
-
- static int dirtoa( target, infop )
- register char *target ;
- register struct FILEINFO *infop ;
- {
- char *startstr = target;
-
- target = cptodlower( target, infop->fi_name );
-
- return( target - startstr );
- }
-
- /*----------------------------------------------------------------------*/
-
- static int add_entry( infop, dp )
- struct FILEINFO *infop ;
- register HDIRECTORY *dp ;
- {
- char buf[128] ;
- register int len ;
-
- if( IS_HIDDEN(infop->fi_attrib) || *infop->fi_name == '.' )
- return 1;
-
- if( dp->maxdirs <= 0 ) /* No more room in dirv. return */
- return 0; /* error status */
-
- if( IS_SUBDIR(infop->fi_attrib) )
- {
- if( dp->dirs )
- dp->ndirs++ ;
- else
- return 1;
- }
- else
- {
- if( dp->files )
- dp->nfiles++ ;
- else
- return 1;
- }
-
- len = dirtoa( buf, infop );
-
- if( len > dp->width )
- dp->width = len ;
-
- if( *dp->lastdir = malloc(len + 1) )
- {
- strcpy( *dp->lastdir++, buf ) ;
- --dp->maxdirs;
- return 1;
- }
-
- return 0;
- }
-
- static int cmp( pp1, pp2 )
- char **pp1, **pp2;
- {
- register char *p1 = *pp1;
- register char *p2 = *pp2;
- int num1, num2;
-
- while( (num1 = (isdigit(*p1) && isdigit(*p2))) || (*p1 == *p2 && *p1) )
- {
- if( !num1 )
- {
- p1++;
- p2++;
- }
- else
- {
- num1 = num2 = 0;
-
- do {
- num1 = (num1 * 10) + ( *p1++ - '0');
- } while( isdigit(*p1) );
-
- do {
- num2 = (num2 * 10) + ( *p2++ - '0');
- } while( isdigit(*p2) );
-
- if( num1 != num2 )
- return( num1 - num2 );
- }
- }
-
- return( *p1 - *p2 );
- }
-
- /*----------------------------------------------------------------------*/
-
- HDIRECTORY *mk_hdir( size )
- register unsigned size;
- {
- register HDIRECTORY *dp;
- register unsigned amt_mem;
-
- amt_mem = sizeof(HDIRECTORY) + (size * sizeof(char *));
-
- if( !( dp = (HDIRECTORY *) malloc(amt_mem) ))
- return 0;
-
- memset( dp, 0, amt_mem ); /* Initialize *dp to zeros */
-
- dp->maxdirs = size ;
- dp->lastdir = dp->dirv;
- return dp;
- }
-
- /*----------------------------------------------------------------------*/
-
- del_hdir( dp )
- register HDIRECTORY *dp;
- {
- register char **v;
-
- for( v = dp->dirv; v < dp->lastdir ; free( *v++ ) )
- ;
-
- free( dp );
- }
-
- /*----------------------------------------------------------------------*/
-
- hdir( spec, dp )
- char *spec;
- HDIRECTORY *dp;
- {
- struct FILEINFO info;
- char **firstdir;
-
- fchgdta( &info );
-
- firstdir = dp->lastdir;
-
- if( !ffind_first( spec, A_ALL) )
- {
- if( !add_entry(&info, dp ) )
- goto aborted;
-
- if( haswild(spec) )
- {
- while( !ffind_next() )
- if( !add_entry(&info, dp ) )
- goto aborted;
- }
- }
-
- if( dp->sort )
- ssort( (char *)firstdir, dp->lastdir - firstdir,
- sizeof(char*), cmp);
- goto abort;
-
- aborted:
- E( "Help:hdir: unable to add entries - increase the appropriate parameter\n" );
- abort:
- frstdta(); /* Restore the original dta */
-
- }
-