Subversion Repositories SvarDOS

Rev

Rev 2056 | Rev 2058 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2056 Rev 2057
Line 592... Line 592...
592
 * On error (invalid path) displays message and returns -1L.
592
 * On error (invalid path) displays message and returns -1L.
593
 * Stores additional directory data in ddata if non-NULL
593
 * Stores additional directory data in ddata if non-NULL
594
 * and path is valid.
594
 * and path is valid.
595
 */
595
 */
596
static long hasSubdirectories(char *path, DIRDATA *ddata) {
596
static long hasSubdirectories(char *path, DIRDATA *ddata) {
597
  static struct FFDTA findData;
597
  struct FFDTA findData;
598
  struct FFDTA *hnd;
-
 
599
  static char buffer[PATH_MAX + 2];
598
  char buffer[PATH_MAX + 2];
600
  int hasSubdirs = 0;
599
  int hasSubdirs = 0;
601
 
600
 
602
  /* get the handle to start with (using wildcard spec) */
601
  /* get the handle to start with (using wildcard spec) */
603
  strcpy(buffer, path);
602
  strcpy(buffer, path);
604
  strcat(buffer, "*");
603
  strcat(buffer, "*");
605
 
604
 
606
  /* Use FindFirstFileEx when available (falls back to FindFirstFile).
605
  /* Use FindFirstFileEx when available (falls back to FindFirstFile).
607
   * Allows us to limit returned results to just directories
606
   * Allows us to limit returned results to just directories
608
   * if supported by underlying filesystem.
607
   * if supported by underlying filesystem.
609
   */
608
   */
610
  hnd = FindFirstFile(buffer, &findData);
609
  if (FindFirstFile(buffer, &findData) == NULL) {
611
  if (hnd == NULL) {
-
 
612
    showInvalidPath(path); /* Display error message */
610
    showInvalidPath(path); /* Display error message */
613
    return(-1);
611
    return(-1);
614
  }
612
  }
615
 
613
 
616
  /*  cycle through entries counting directories found until no more entries */
614
  /*  cycle through entries counting directories found until no more entries */
Line 620... Line 618...
620
         (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0 || dspAll) ) {
618
         (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0 || dspAll) ) {
621
      if (findData.ff_name[0] != '.') { /* ignore '.' and '..' */
619
      if (findData.ff_name[0] != '.') { /* ignore '.' and '..' */
622
        hasSubdirs++;      /* subdir of initial path found, so increment counter */
620
        hasSubdirs++;      /* subdir of initial path found, so increment counter */
623
      }
621
      }
624
    }
622
    }
625
  } while(FindNextFile(hnd, &findData) != 0);
623
  } while(FindNextFile(&findData) != 0);
626
 
624
 
627
  /* prevent resource leaks, close the handle. */
625
  /* prevent resource leaks, close the handle. */
628
  FindClose(hnd);
626
  FindClose(&findData);
629
 
627
 
630
  if (ddata != NULL)  // don't bother if user doesn't want them
628
  if (ddata != NULL)  // don't bother if user doesn't want them
631
  {
629
  {
632
    /* The root directory of a volume (including non root paths
630
    /* The root directory of a volume (including non root paths
633
       corresponding to mount points) may not have a current (.) and
631
       corresponding to mount points) may not have a current (.) and
Line 849... Line 847...
849
 *      or  1 if files displayed, no errors.
847
 *      or  1 if files displayed, no errors.
850
 */
848
 */
851
static int displayFiles(const char *path, char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
849
static int displayFiles(const char *path, char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
852
  char buffer[PATH_MAX + 2];
850
  char buffer[PATH_MAX + 2];
853
  struct FFDTA entry;   /* current directory entry info    */
851
  struct FFDTA entry;   /* current directory entry info    */
854
  struct FFDTA *dir;    /* Current directory entry working with      */
-
 
855
  unsigned long filesShown = 0;
852
  unsigned long filesShown = 0;
856
 
853
 
857
  /* get handle for files in current directory (using wildcard spec) */
854
  /* get handle for files in current directory (using wildcard spec) */
858
  strcpy(buffer, path);
855
  strcpy(buffer, path);
859
  strcat(buffer, "*");
856
  strcat(buffer, "*");
860
  dir = FindFirstFile(buffer, &entry);
857
  if (FindFirstFile(buffer, &entry) == NULL) return(-1);
861
  if (dir == NULL) return(-1);
-
 
862
 
858
 
863
  addPadding(padding, hasMoreSubdirs);
859
  addPadding(padding, hasMoreSubdirs);
864
 
860
 
865
  /* cycle through directory printing out files. */
861
  /* cycle through directory printing out files. */
866
  do
862
  do
Line 891... Line 887...
891
      /* print filename */
887
      /* print filename */
892
      pprintf("%s\n", entry.ff_name);
888
      pprintf("%s\n", entry.ff_name);
893
 
889
 
894
      filesShown++;
890
      filesShown++;
895
    }
891
    }
896
  } while(FindNextFile(dir, &entry) != 0);
892
  } while(FindNextFile(&entry) != 0);
897
 
893
 
898
  if (filesShown)
894
  if (filesShown)
899
  {
895
  {
900
    pprintf("%s\n", padding);
896
    pprintf("%s\n", padding);
901
  }
897
  }
902
 
898
 
903
  /* cleanup directory search */
-
 
904
  FindClose(dir);
-
 
905
  /* dir = NULL; */
-
 
906
 
-
 
907
  removePadding(padding);
899
  removePadding(padding);
908
 
900
 
909
  /* store for summary display */
901
  /* store for summary display */
910
  if (ddata != NULL) ddata->fileCnt = filesShown;
902
  if (ddata != NULL) ddata->fileCnt = filesShown;
911
 
903
 
Line 919... Line 911...
919
 * was found, and if so copies appropriate data into subdir and dsubdir.
911
 * was found, and if so copies appropriate data into subdir and dsubdir.
920
 * It will repeat until a valid subdirectory is found or no more
912
 * It will repeat until a valid subdirectory is found or no more
921
 * are found, at which point it closes the FindFile search handle and
913
 * are found, at which point it closes the FindFile search handle and
922
 * return NULL.  If successful, returns FindFile handle.
914
 * return NULL.  If successful, returns FindFile handle.
923
 */
915
 */
924
static struct FFDTA *cycleFindResults(struct FFDTA *findnexthnd, struct FFDTA *entry, char *subdir, char *dsubdir) {
916
static struct FFDTA *cycleFindResults(struct FFDTA *entry, char *subdir, char *dsubdir) {
925
  /* cycle through directory until 1st non . or .. directory is found. */
917
  /* cycle through directory until 1st non . or .. directory is found. */
926
  do
918
  for (;;) {
927
  {
-
 
928
    /* skip files & hidden or system directories */
919
    /* skip files & hidden or system directories */
929
    if ((((entry->ff_attr & FILE_A_DIR) == 0) ||
920
    if ((((entry->ff_attr & FILE_A_DIR) == 0) ||
930
         ((entry->ff_attr &
921
         ((entry->ff_attr &
931
          (FILE_A_HIDDEN | FILE_A_SYSTEM)) != 0  && !dspAll) ) ||
922
          (FILE_A_HIDDEN | FILE_A_SYSTEM)) != 0  && !dspAll) ) ||
932
        (entry->ff_name[0] == '.')) {
923
        (entry->ff_name[0] == '.')) {
933
      if (FindNextFile(findnexthnd, entry) == 0) {
924
      if (FindNextFile(entry) == 0) {
934
        FindClose(findnexthnd);      // prevent resource leaks
925
        FindClose(entry);      // prevent resource leaks
935
        return(NULL); // no subdirs found
926
        return(NULL); // no subdirs found
936
      }
927
      }
937
    } else {
928
    } else {
938
      /* set display name */
929
      /* set display name */
939
      strcpy(dsubdir, entry->ff_name);
930
      strcpy(dsubdir, entry->ff_name);
940
 
931
 
941
      strcpy(subdir, entry->ff_name);
932
      strcpy(subdir, entry->ff_name);
942
      strcat(subdir, "\\");
933
      strcat(subdir, "\\");
-
 
934
      return(entry);
943
    }
935
    }
944
  } while (!*subdir); // while (subdir is still blank)
-
 
-
 
936
  }
945
 
937
 
946
  return findnexthnd;
938
  return entry;
947
}
939
}
948
 
940
 
949
 
941
 
950
/* FindFile buffer used by findFirstSubdir and findNextSubdir only */
-
 
951
static struct FFDTA findSubdir_entry; /* current directory entry info    */
-
 
952
 
-
 
953
/**
942
/**
954
 * Given the current path, find the 1st subdirectory.
943
 * Given the current path, find the 1st subdirectory.
955
 * The subdirectory found is stored in subdir.
944
 * The subdirectory found is stored in subdir.
956
 * subdir is cleared on error or no subdirectories.
945
 * subdir is cleared on error or no subdirectories.
957
 * Returns the findfirst search HANDLE, which should be passed to
946
 * Returns the findfirst search HANDLE, which should be passed to
Line 962... Line 951...
962
 */
951
 */
963
static struct FFDTA *findFirstSubdir(char *currentpath, char *subdir, char *dsubdir) {
952
static struct FFDTA *findFirstSubdir(char *currentpath, char *subdir, char *dsubdir) {
964
  char buffer[PATH_MAX];
953
  char buffer[PATH_MAX];
965
  struct FFDTA *dir;         /* Current directory entry working with      */
954
  struct FFDTA *dir;         /* Current directory entry working with      */
966
 
955
 
-
 
956
  dir = malloc(sizeof(struct FFDTA));
-
 
957
  if (dir == NULL) return(NULL);
-
 
958
 
967
  /* get handle for files in current directory (using wildcard spec) */
959
  /* get handle for files in current directory (using wildcard spec) */
968
  strcpy(buffer, currentpath);
960
  strcpy(buffer, currentpath);
969
  strcat(buffer, "*");
961
  strcat(buffer, "*");
970
 
962
 
971
  dir = FindFirstFile(buffer, &findSubdir_entry);
963
  if (FindFirstFile(buffer, dir) == NULL) {
972
  if (dir == NULL) {
-
 
973
    showInvalidPath(currentpath);
964
    showInvalidPath(currentpath);
974
    return(NULL);
965
    return(NULL);
975
  }
966
  }
976
 
967
 
977
  /* clear result path */
968
  /* clear result path */
978
  strcpy(subdir, "");
969
  strcpy(subdir, "");
979
 
970
 
980
  return cycleFindResults(dir, &findSubdir_entry, subdir, dsubdir);
971
  return cycleFindResults(dir, subdir, dsubdir);
981
}
972
}
982
 
973
 
983
/**
974
/**
984
 * Given a search HANDLE, will find the next subdirectory,
975
 * Given a search HANDLE, will find the next subdirectory,
985
 * setting subdir to the found directory name.
976
 * setting subdir to the found directory name.
Line 988... Line 979...
988
 * If a subdirectory is found, returns 0, otherwise returns 1
979
 * If a subdirectory is found, returns 0, otherwise returns 1
989
 * (either error or no more files).
980
 * (either error or no more files).
990
 */
981
 */
991
static int findNextSubdir(struct FFDTA *findnexthnd, char *subdir, char *dsubdir) {
982
static int findNextSubdir(struct FFDTA *findnexthnd, char *subdir, char *dsubdir) {
992
  /* clear result path */
983
  /* clear result path */
993
  strcpy(subdir, "");
984
  subdir[0] = 0;
994
 
985
 
995
  if (FindNextFile(findnexthnd, &findSubdir_entry) == 0) return 1; // no subdirs found
986
  if (FindNextFile(findnexthnd) == 0) return 1; // no subdirs found
996
 
987
 
997
  if (cycleFindResults(findnexthnd, &findSubdir_entry, subdir, dsubdir) == NULL) {
988
  if (cycleFindResults(findnexthnd, subdir, dsubdir) == NULL) {
998
    return 1;
989
    return 1;
999
  }
990
  }
1000
  return 0;
991
  return 0;
1001
}
992
}
1002
 
993