home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / GET_DIRS.C < prev    next >
C/C++ Source or Header  |  1991-12-01  |  2KB  |  81 lines

  1. /*
  2. **  GET_DIRS.C - build a linked list of directories on a drive
  3. **
  4. **  public domain by Mike Gillen
  5. */
  6.  
  7. struct dirs
  8. {
  9.       char path[MAXPATH];                /* directory name         */
  10.       struct dirs *nxtdir, *prvdir;      /* next and prev pointers */
  11. };
  12.  
  13. typedef struct dirs DIRS, *DIRPTR;
  14. DIRPTR dfirst, dprev, dnow, dlast;
  15.  
  16. /*
  17. ** get_dirs() is called to find all of the directories on the current
  18. ** drive.  It in turn calls gdir() after creating the first structure
  19. ** that coresponds to the root directory.
  20. */
  21.  
  22. void get_dirs( void )
  23. {
  24.       char work_dir[MAXPATH];
  25.  
  26.       getcwd( work_dir, MAXPATH );
  27.       chdir( "\\" );
  28.       if( (dnow = (DIRPTR) malloc( sizeof( DIRS ) ) ) == NULL )
  29.             mem_err( 52 );                /* memory error function */
  30.       dfirst = dlast = dnow->prvdir = dprev = dnow;
  31.       dnow->nxtdir = (DIRPTR) NULL;
  32.       strcpy( dnow->path, " :\\" );
  33.       dnow->path[0] = work_dir[0];
  34.       gdir( 1 );
  35.       dlast = dnow;
  36.       dnow->nxtdir = dnow;
  37.       dnow = dfirst;
  38.       dnow->prvdir = dnow;
  39.       chdir( work_dir );
  40. } /* end get_dirs() */
  41.  
  42. /*
  43. ** gdir() uses recursion to find all of the directories on the current
  44. ** drive.  After building the linked list of directories, it returns
  45. ** to get_dirs().
  46. */
  47.  
  48. void gdir( int lev )
  49. {
  50.       struct ffblk dirinfo;
  51.  
  52.       if( findfirst( "*.*", &dirinfo, FA_DIREC | FA_HIDDEN ) )
  53.       {
  54.             chdir( ".." );
  55.             return;
  56.       }
  57.       do
  58.       {
  59.             if( !( dirinfo.ff_attrib & FA_DIREC ) )
  60.                   continue;
  61.             if( dirinfo.ff_name[0] == '.' )     /* optional to skip . dirs */
  62.                   continue;
  63.             if( (dnow = (DIRPTR) malloc( sizeof( DIRS ) ) ) == NULL )
  64.                   mem_err( 53 );                /* memory error function */
  65.             dprev->nxtdir = dnow;
  66.             dnow->prvdir = dprev;
  67.             dnow->nxtdir = (DIRPTR) NULL;
  68.             getcwd( dnow->path, MAXPATH );
  69.             strcpy( dnow->path, dnow->path );
  70.             if( dnow->path[ strlen( dnow->path ) - 1 ] != '\\' )
  71.                   strcat( dnow->path, "\\" );
  72.             strcat( dnow->path, dirinfo.ff_name );
  73.             dprev = dnow;
  74.             chdir( dnow->path );
  75.             gdir( lev + 1 );  /* recursive call */
  76.       }
  77.       while( !findnext( &dirinfo ) );
  78.       if( lev != 1 )
  79.             chdir( ".." );
  80. } /* end gdir() */
  81.