home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / HPFSLS.ZOO / lsdir.c < prev    next >
C/C++ Source or Header  |  1992-02-19  |  3KB  |  105 lines

  1. /*
  2.  * Modulo di scansione delle directory.
  3.  * (C)1990 by redmax@alessia.dei.unipd.it (Massimo A. Santin)
  4.  * $Log:    lsdir.c $
  5.  * Revision 1.2  91/05/11  00:11:47  Unknown
  6.  * Aggiunta del messaggio che riferisce che la versione DOS non e` 
  7.  * ancora stata implementata.
  8.  * 
  9.  * Revision 1.1  91/05/08  20:00:35  Unknown
  10.  * Initial revision
  11.  * 
  12.  */
  13.  
  14. #include "ls.h"
  15.  
  16. static char rcsid[] = "$Header: d:/usr/utils/ls/rcs/lsdir.c 1.2 91/05/11 00:11:47 Unknown Exp $";
  17.  
  18. /* Scandisce il contenuto della directory */
  19. long ls_dir(char* name, unsigned attr, option_type opt, int level)
  20. {
  21.    HDIR hdir;
  22.    FILEFINDBUF ffbuf;
  23.    USHORT csearch = 1;
  24.    char *buf, *p, *cwd;
  25.    int j;
  26.    long file_counter = 0;
  27.    option_type opt2;
  28.    BYTE mode;
  29.  
  30.    DosGetMachineMode(&mode);
  31.    if (mode == MODE_PROTECTED)
  32.       hdir = HDIR_CREATE;
  33.    else 
  34.       hdir = HDIR_SYSTEM;
  35.    if (DosFindFirst(name, &hdir, attr, &ffbuf, sizeof ffbuf, &csearch, 0L)) {
  36.       fprintf(stderr, "LS: error in open %s.\n", name);
  37.       exit(1);
  38.    }
  39.    do {
  40.       file_counter += print_name(&ffbuf, opt, level);
  41.       if (opt.recursive && ffbuf.attrFile & FILE_DIRECTORY) {
  42.          if (STRCMP(ffbuf.achName, ".") != 0 && STRCMP(ffbuf.achName, "..") != 0) {
  43.             cwd = (char *) malloc(CCHMAXPATHCOMP);
  44.             buf = (char *) malloc(CCHMAXPATHCOMP);
  45.             getcwd(cwd, CCHMAXPATHCOMP);
  46.             buf = (char*) malloc(CCHMAXPATHCOMP);
  47.  
  48.             strcpy(buf, name);
  49.             p = &buf[strlen(name) - 1];
  50.             while (*p == '*' || *p == '?') {
  51.                *p = '\0';
  52.                --p;
  53.             }
  54.             strcat(buf, ffbuf.achName);
  55.             chdir(buf);
  56.             file_counter += ls_dir("*", attr, opt, level + 1);
  57.  
  58.             free((void *) buf);
  59.             chdir(cwd);
  60.             free((void *) cwd);
  61.          }
  62.       }
  63.    }
  64.    while (DosFindNext(hdir, &ffbuf, sizeof ffbuf, &csearch) == 0);
  65.    DosFindClose(hdir);
  66.    return file_counter;
  67. } /* ls_dir */
  68.  
  69. /* Prepara il nome per la scansione (anche ricorsiva) */
  70. void ls(char *name, unsigned attr, option_type opt)
  71. {
  72.    struct stat st;
  73.    char buffer[CCHMAXPATHCOMP];
  74.    int l;
  75.    char *fname;
  76.    long file_counter = 0;
  77.  
  78.    strcpy(buffer, name);
  79.    if (buffer[l = (strlen(buffer) - 1)] == ':')
  80.       buffer[l + 1] = '.';
  81.  
  82.    if ((stat(buffer, &st) != -1) && (st.st_mode & S_IFDIR)) {
  83.       ls_chdir(buffer);
  84.       if (opt.volume)
  85.          ls_volume();
  86.       file_counter = ls_dir("*", attr, opt, 0);
  87.    }
  88.    else {
  89.       fname = strrchr(buffer, '\\');
  90.       if (fname == NULL)
  91.          fname = strchr(buffer, '/');
  92.       if (fname == NULL)
  93.          fname = buffer;
  94.       else {
  95.          *fname++ = '\0';
  96.          ls_chdir(buffer);
  97.       }
  98.       if (opt.volume)
  99.          ls_volume();
  100.       file_counter = ls_dir(fname, attr, opt, 0);
  101.    }
  102.  
  103.    printf("%ld files\n", file_counter);
  104. } /* ls */
  105.