Subversion Repositories SvarDOS

Rev

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

Rev 2059 Rev 2060
Line 579... Line 579...
579
  struct SUBDIRINFO * parent; /* points to parent subdirectory                */
579
  struct SUBDIRINFO * parent; /* points to parent subdirectory                */
580
  char *currentpath;    /* Stores the full path this structure represents     */
580
  char *currentpath;    /* Stores the full path this structure represents     */
581
  char *subdir;         /* points to last subdir within currentpath           */
581
  char *subdir;         /* points to last subdir within currentpath           */
582
  char *dsubdir;        /* Stores a display ready directory name              */
582
  char *dsubdir;        /* Stores a display ready directory name              */
583
  long subdircnt;       /* Initially a count of how many subdirs in this dir  */
583
  long subdircnt;       /* Initially a count of how many subdirs in this dir  */
584
  struct FFDTA *findnexthnd; /* The handle returned by findfirst, used in findnext */
584
  struct find_t *findnexthnd; /* The handle returned by findfirst, used in findnext */
585
  struct DIRDATA ddata; /* Maintain directory information, eg attributes      */
585
  struct DIRDATA ddata; /* Maintain directory information, eg attributes      */
586
} SUBDIRINFO;
586
} SUBDIRINFO;
587
 
587
 
588
 
588
 
589
/**
589
/**
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
  struct FFDTA findData;
597
  struct find_t findData;
598
  char buffer[PATH_MAX + 2];
598
  char buffer[PATH_MAX + 4];
599
  int hasSubdirs = 0;
599
  int hasSubdirs = 0;
600
 
600
 
601
  /* get the handle to start with (using wildcard spec) */
601
  /* get the handle to start with (using wildcard spec) */
602
  strcpy(buffer, path);
602
  strcpy(buffer, path);
603
  strcat(buffer, "*");
603
  strcat(buffer, "*.*");
604
 
604
 
605
  /* Use FindFirstFileEx when available (falls back to FindFirstFile).
-
 
606
   * Allows us to limit returned results to just directories
-
 
607
   * if supported by underlying filesystem.
-
 
608
   */
-
 
609
  if (FindFirstFile(buffer, &findData) != 0) {
605
  if (_dos_findfirst(buffer, 0x37, &findData) != 0) {
610
    showInvalidPath(path); /* Display error message */
606
    showInvalidPath(path); /* Display error message */
611
    return(-1);
607
    return(-1);
612
  }
608
  }
613
 
609
 
614
  /*  cycle through entries counting directories found until no more entries */
610
  /*  cycle through entries counting directories found until no more entries */
Line 618... Line 614...
618
         (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0 || dspAll) ) {
614
         (FILE_A_HIDDEN | FILE_A_SYSTEM)) == 0 || dspAll) ) {
619
      if (findData.name[0] != '.') { /* ignore '.' and '..' */
615
      if (findData.name[0] != '.') { /* ignore '.' and '..' */
620
        hasSubdirs++;      /* subdir of initial path found, so increment counter */
616
        hasSubdirs++;      /* subdir of initial path found, so increment counter */
621
      }
617
      }
622
    }
618
    }
623
  } while(FindNextFile(&findData) == 0);
619
  } while(_dos_findnext(&findData) == 0);
624
 
620
 
625
  /* prevent resource leaks, close the handle. */
621
  /* prevent resource leaks, close the handle. */
626
  FindClose(&findData);
622
  _dos_findclose(&findData);
627
 
623
 
628
  if (ddata != NULL)  // don't bother if user doesn't want them
624
  if (ddata != NULL)  // don't bother if user doesn't want them
629
  {
625
  {
630
    /* The root directory of a volume (including non root paths
626
    /* The root directory of a volume (including non root paths
631
       corresponding to mount points) may not have a current (.) and
627
       corresponding to mount points) may not have a current (.) and
Line 845... Line 841...
845
 * Returns -1 on error,
841
 * Returns -1 on error,
846
 *          0 if no files, but no errors either,
842
 *          0 if no files, but no errors either,
847
 *      or  1 if files displayed, no errors.
843
 *      or  1 if files displayed, no errors.
848
 */
844
 */
849
static int displayFiles(const char *path, char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
845
static int displayFiles(const char *path, char *padding, int hasMoreSubdirs, DIRDATA *ddata) {
850
  char buffer[PATH_MAX + 2];
846
  char buffer[PATH_MAX + 4];
851
  struct FFDTA entry;   /* current directory entry info    */
847
  struct find_t entry;   /* current directory entry info    */
852
  unsigned long filesShown = 0;
848
  unsigned long filesShown = 0;
853
 
849
 
854
  /* get handle for files in current directory (using wildcard spec) */
850
  /* get handle for files in current directory (using wildcard spec) */
855
  strcpy(buffer, path);
851
  strcpy(buffer, path);
856
  strcat(buffer, "*");
852
  strcat(buffer, "*.*");
857
  if (FindFirstFile(buffer, &entry) != 0) return(-1);
853
  if (_dos_findfirst(buffer, 0x37, &entry) != 0) return(-1);
858
 
854
 
859
  addPadding(padding, hasMoreSubdirs);
855
  addPadding(padding, hasMoreSubdirs);
860
 
856
 
861
  /* cycle through directory printing out files. */
857
  /* cycle through directory printing out files. */
862
  do
858
  do
Line 887... Line 883...
887
      /* print filename */
883
      /* print filename */
888
      pprintf("%s\n", entry.name);
884
      pprintf("%s\n", entry.name);
889
 
885
 
890
      filesShown++;
886
      filesShown++;
891
    }
887
    }
892
  } while(FindNextFile(&entry) == 0);
888
  } while(_dos_findnext(&entry) == 0);
893
 
889
 
894
  if (filesShown)
890
  if (filesShown)
895
  {
891
  {
896
    pprintf("%s\n", padding);
892
    pprintf("%s\n", padding);
897
  }
893
  }
Line 911... Line 907...
911
 * was found, and if so copies appropriate data into subdir and dsubdir.
907
 * was found, and if so copies appropriate data into subdir and dsubdir.
912
 * It will repeat until a valid subdirectory is found or no more
908
 * It will repeat until a valid subdirectory is found or no more
913
 * are found, at which point it closes the FindFile search handle and
909
 * are found, at which point it closes the FindFile search handle and
914
 * return NULL.  If successful, returns FindFile handle.
910
 * return NULL.  If successful, returns FindFile handle.
915
 */
911
 */
916
static struct FFDTA *cycleFindResults(struct FFDTA *entry, char *subdir, char *dsubdir) {
912
static struct find_t *cycleFindResults(struct find_t *entry, char *subdir, char *dsubdir) {
917
  /* cycle through directory until 1st non . or .. directory is found. */
913
  /* cycle through directory until 1st non . or .. directory is found. */
918
  for (;;) {
914
  for (;;) {
919
    /* skip files & hidden or system directories */
915
    /* skip files & hidden or system directories */
920
    if ((((entry->attrib & FILE_A_SUBDIR) == 0) ||
916
    if ((((entry->attrib & FILE_A_SUBDIR) == 0) ||
921
         ((entry->attrib &
917
         ((entry->attrib &
922
          (FILE_A_HIDDEN | FILE_A_SYSTEM)) != 0  && !dspAll) ) ||
918
          (FILE_A_HIDDEN | FILE_A_SYSTEM)) != 0  && !dspAll) ) ||
923
        (entry->name[0] == '.')) {
919
        (entry->name[0] == '.')) {
924
      if (FindNextFile(entry) != 0) {
920
      if (_dos_findnext(entry) != 0) {
925
        FindClose(entry);      // prevent resource leaks
921
        _dos_findclose(entry);      // prevent resource leaks
926
        return(NULL); // no subdirs found
922
        return(NULL); // no subdirs found
927
      }
923
      }
928
    } else {
924
    } else {
929
      /* set display name */
925
      /* set display name */
930
      strcpy(dsubdir, entry->name);
926
      strcpy(dsubdir, entry->name);
Line 947... Line 943...
947
 * findclose when directory has finished processing, and can be
943
 * findclose when directory has finished processing, and can be
948
 * passed to findnextsubdir to find subsequent subdirectories.
944
 * passed to findnextsubdir to find subsequent subdirectories.
949
 * Returns NULL on error.
945
 * Returns NULL on error.
950
 * currentpath must end in \
946
 * currentpath must end in \
951
 */
947
 */
952
static struct FFDTA *findFirstSubdir(char *currentpath, char *subdir, char *dsubdir) {
948
static struct find_t *findFirstSubdir(char *currentpath, char *subdir, char *dsubdir) {
953
  char buffer[PATH_MAX];
949
  char buffer[PATH_MAX + 4];
954
  struct FFDTA *dir;         /* Current directory entry working with      */
950
  struct find_t *dir;         /* Current directory entry working with      */
955
 
951
 
956
  dir = malloc(sizeof(struct FFDTA));
952
  dir = malloc(sizeof(struct find_t));
957
  if (dir == NULL) return(NULL);
953
  if (dir == NULL) return(NULL);
958
 
954
 
959
  /* get handle for files in current directory (using wildcard spec) */
955
  /* get handle for files in current directory (using wildcard spec) */
960
  strcpy(buffer, currentpath);
956
  strcpy(buffer, currentpath);
961
  strcat(buffer, "*");
957
  strcat(buffer, "*.*");
962
 
958
 
963
  if (FindFirstFile(buffer, dir) != 0) {
959
  if (_dos_findfirst(buffer, 0x37, dir) != 0) {
964
    showInvalidPath(currentpath);
960
    showInvalidPath(currentpath);
965
    return(NULL);
961
    return(NULL);
966
  }
962
  }
967
 
963
 
968
  /* clear result path */
964
  /* clear result path */
Line 977... Line 973...
977
 * dsubdir is the name to display
973
 * dsubdir is the name to display
978
 * currentpath must end in \
974
 * currentpath must end in \
979
 * If a subdirectory is found, returns 0, otherwise returns 1
975
 * If a subdirectory is found, returns 0, otherwise returns 1
980
 * (either error or no more files).
976
 * (either error or no more files).
981
 */
977
 */
982
static int findNextSubdir(struct FFDTA *findnexthnd, char *subdir, char *dsubdir) {
978
static int findNextSubdir(struct find_t *findnexthnd, char *subdir, char *dsubdir) {
983
  /* clear result path */
979
  /* clear result path */
984
  subdir[0] = 0;
980
  subdir[0] = 0;
985
 
981
 
986
  if (FindNextFile(findnexthnd) != 0) return(1); // no subdirs found
982
  if (_dos_findnext(findnexthnd) != 0) return(1); // no subdirs found
987
 
983
 
988
  if (cycleFindResults(findnexthnd, subdir, dsubdir) == NULL) {
984
  if (cycleFindResults(findnexthnd, subdir, dsubdir) == NULL) {
989
    return 1;
985
    return 1;
990
  }
986
  }
991
  return 0;
987
  return 0;
Line 1070... Line 1066...
1070
      /* Remove the padding for this directory, all but initial path. */
1066
      /* Remove the padding for this directory, all but initial path. */
1071
      if (sdi->parent != NULL)
1067
      if (sdi->parent != NULL)
1072
        removePadding(padding);
1068
        removePadding(padding);
1073
 
1069
 
1074
      /* Prevent resource leaks, by ending findsearch and freeing memory. */
1070
      /* Prevent resource leaks, by ending findsearch and freeing memory. */
1075
      FindClose(sdi->findnexthnd);
1071
      _dos_findclose(sdi->findnexthnd);
1076
      if (sdi != NULL)
1072
      if (sdi != NULL)
1077
      {
1073
      {
1078
        if (sdi->currentpath != NULL)
1074
        if (sdi->currentpath != NULL)
1079
          free(sdi->currentpath);
1075
          free(sdi->currentpath);
1080
        free(sdi);
1076
        free(sdi);