home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / GRAPHICS / MISC / EEDRW23S.ZIP / DIRECTOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-13  |  6.1 KB  |  188 lines

  1. /******************************************************************************
  2. * Procedures to get/set current working directory and get file names in a     *
  3. * given directory.                                  *
  4. *                                          *
  5. *                Ver 0.1        Gershon Elber          Oct. 89 *
  6. ******************************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <process.h>
  11. #include <dir.h>
  12. #include <dos.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <float.h>
  17. #include "Program.h"
  18. #include "Director.h"
  19. #include "Ctrl-Brk.h"
  20. #include "EERedraw.h"
  21.  
  22. #ifndef LINE_LEN
  23. #define LINE_LEN 128
  24. #endif
  25.  
  26. static void DosReleaseResources(void);
  27. static void DosReclaimResources(void);
  28.  
  29. /******************************************************************************
  30. * Procedure to create a file name list of the files in the given directory    *
  31. * that match the given mask.                              *
  32. * Returns an array of names allocated dynamically, or NULL if no match.          *
  33. ******************************************************************************/
  34. FileNameType *GetFileNamesDir(char *Mask, char *Directory, int *NumOfFiles)
  35. {
  36.     int FileNamesArraySize,
  37.     FileNameCount = 0;
  38.     char FullName[FULL_PATH_LEN];
  39.     struct ffblk ffblk;
  40.     FileNameType *FileNames, *TempFileNames;
  41.  
  42.     FileNamesArraySize = 10;       /* Start with 10 and realloc as required. */
  43.     FileNames = (FileNameType *)
  44.     MyMalloc(sizeof(FileNameType) * FileNamesArraySize);
  45.  
  46.     if (Mask == NULL) Mask = DEFAULT_FILE_TO_DIR;
  47.     if (Directory == NULL) Directory = DEFAULT_DIR_TO_DIR;
  48.     sprintf(FullName, "%s\\%s", Directory, Mask);
  49.  
  50.     if (!findfirst(FullName, &ffblk, 0))
  51.     {
  52.     do {
  53.         if (FileNameCount >= FileNamesArraySize) {
  54.         /* Need to realloc the array: */
  55.         TempFileNames = (FileNameType *)
  56.             MyMalloc(sizeof(FileNameType) * (FileNamesArraySize << 1));
  57.         GEN_COPY(TempFileNames, FileNames,
  58.              sizeof(FileNameType) * FileNamesArraySize);
  59.         MyFree((VoidPtr) FileNames);
  60.         FileNames = TempFileNames;
  61.         FileNamesArraySize <<= 1;
  62.         }
  63.         strncpy(FileNames[FileNameCount++], ffblk.ff_name,
  64.                             FILE_NAME_LEN - 1);
  65.     }
  66.     while (!findnext(&ffblk));
  67.  
  68.     *NumOfFiles = FileNameCount;
  69.     return FileNames;
  70.     }
  71.     else {
  72.     MyFree((VoidPtr) FileNames);
  73.  
  74.     *NumOfFiles = 0;
  75.     return NULL;
  76.     }
  77. }
  78.  
  79. /*****************************************************************************
  80. *   Routine to get current directory                         *
  81. *****************************************************************************/
  82. char *GetDir(void)
  83. {
  84.     static char cwd[FULL_PATH_LEN];
  85.  
  86.     return getcwd(cwd, FULL_PATH_LEN - 1);
  87. }
  88.  
  89. /*****************************************************************************
  90. *   Routine to change current directory                         *
  91. *****************************************************************************/
  92. ChDirErrorType ChangeDir(char *Directory)
  93. {
  94.     char *cwd, *Path;
  95.  
  96.     if (Directory[1] == ':')
  97.     Path = &Directory[2];            /* Path points on path only. */
  98.     else
  99.         Path = Directory;
  100.  
  101.     cwd = GetDir();
  102.  
  103.     if (strlen(Path) != 0 && chdir(Directory))
  104.     return CD_NO_SUCH_DIR;
  105.     else if (Directory[1] == ':') {
  106.     if (islower(Directory[0])) Directory[0] = toupper(Directory[0]);
  107.     setdisk(Directory[0] - 'A');            /* Move to the new disk. */
  108.     if (getdisk() != Directory[0] - 'A') return CD_NO_SUCH_DISK;
  109.     }
  110.  
  111.     if (getcwd(Directory, LINE_LEN-1) == NULL) { /* Test if directory valid! */
  112.     /* Restore old working directory: */
  113.     if (strlen(&cwd[2]) != 0) chdir(cwd); /* If directory is not root... */
  114.     if (islower(cwd[0])) cwd[0] = toupper(cwd[0]);
  115.     setdisk(cwd[0] - 'A');               /* Move to the original disk. */
  116.     return CD_INVALID_DIR;
  117.     }
  118.  
  119.     return CD_OK;
  120. }
  121.  
  122. /******************************************************************************
  123. * Procedure to execute the command processor as defined by COMSPEC          *
  124. * environment variable and enter it as a child process.                  *
  125. ******************************************************************************/
  126. void DosSystem(void)
  127. {
  128.     char *FullPath,
  129.         *Err = NULL;
  130.     IntrCursorShapeStruct Cursor;
  131.  
  132.     Cursor.CursorType = INTR_CURSOR_ARROW;
  133.  
  134.     if ((FullPath = getenv("COMSPEC")) == NULL) {
  135.     IntrQueryContinue("No COMSPEC environment variable, cannt escape to system",
  136.                           EEPopUpFrameColor, EEPopUpBackColor,
  137.                           EEPopUpForeColor, EEPopUpXorColor,
  138.                           EEWindowsFrameWidth, &Cursor, INTR_WNDW_PLACE_CENTER);
  139.     return;
  140.     }
  141.  
  142.     DosReleaseResources();
  143.  
  144.     if (spawnl(P_WAIT, FullPath, FullPath, NULL) == -1) {       /* Do it! */
  145.     /* Error had occured - trace it: */
  146.     switch (errno) {
  147.         case ENOENT:
  148.         Err = "No command processor found in COMSPEC path specified.";
  149.         break;
  150.         case ENOMEM:
  151.         Err = "Not enough memory to escape to system.";
  152.         break;
  153.         default:
  154.         Err = "Undefined error in attempt to escape to system.";
  155.     }
  156.     }
  157.  
  158.     DosReclaimResources();
  159.  
  160.     if (Err != NULL) IntrQueryContinue(Err,
  161.                                EEPopUpFrameColor, EEPopUpBackColor,
  162.                                EEPopUpForeColor, EEPopUpXorColor,
  163.                                        EEWindowsFrameWidth, &Cursor,
  164.                                        INTR_WNDW_PLACE_CENTER);
  165. }
  166.  
  167. /******************************************************************************
  168. * Procedure to release all resources used by the program.              *
  169. ******************************************************************************/
  170. static void DosReleaseResources(void)
  171. {
  172.     IntrClose();                /* Close the graphic driver. */
  173.  
  174.     RestoreCtrlBrk();                  /* Restore ctrl-brk interrupt. */
  175. }
  176.  
  177. /******************************************************************************
  178. * Procedure to reclaim all resources used by the program.              *
  179. ******************************************************************************/
  180. static void DosReclaimResources(void)
  181. {
  182.     _fpreset();           /* Reset the floating point package to a known state. */
  183.  
  184.     IntrInit();                  /* Initiate the graphic driver. */
  185.  
  186.     IntrWndwRedrawAll();
  187. }
  188.