home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxasyn20.zip / FILEFUNC.C < prev    next >
Text File  |  1994-12-26  |  15KB  |  431 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /*  MODULE         FILEFUNC.C                                                */
  4. /*                                                                           */
  5. /*  VERSION        Version 2.0 - 26th Dec 1994                               */
  6. /*                                                                           */
  7. /*  COPYRIGHT      Copyright (c) 1993, 1994 by Crucial Applications          */
  8. /*                             All rights reserved.                          */
  9. /*                                                                           */
  10. /*  DESCRIPTION    File System Functions                                     */
  11. /*                                                                           */
  12. /*  FUNCTIONS                                                                */
  13. /*                                                                           */
  14. /*    fFileExists  - Test for file existence                                 */
  15. /*    fFileDelete  - Delete specified file                                   */
  16. /*    fFileCopy    - Copy specified file                                     */
  17. /*    fFileRename  - Rename specified file                                   */
  18. /*    fFileMove    - Move specified file                                     */
  19. /*    FileNameIs   - Return name component of specified file                 */
  20. /*    FilePathIs   - Return path component of specified file                 */
  21. /*    FileChgDir   - Change current drive/directory                          */
  22. /*    FileGetDir   - Query current drive/directory                           */
  23. /*                                                                           */
  24. /*****************************************************************************/
  25.  
  26. /*********************************************************************/
  27. /* Includes needed by this module                                    */
  28. /*********************************************************************/
  29.  
  30. #define  _MT
  31. #define  _DLL
  32. #define  INCL_DOS
  33. #define  INCL_ERRORS
  34.  
  35. #include <os2.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39.  
  40. #include "miscfunc.h"
  41. #include "filefunc.h"
  42.  
  43. /*********************************************************************/
  44. /* fFileExists()                                                     */
  45. /*********************************************************************/
  46. BOOL fFileExists( PSZ pszFileName )
  47. {
  48.    /*---------------------------------------------------------------*/
  49.    /* Note: It is normally best to return TRUE (exists) if the find */
  50.    /*       test fails for some unknown reason.  Thus any operation */
  51.    /*       expecting the file to exist will be performed and that  */
  52.    /*       will then fail/suceed.  If one was to return FALSE then */
  53.    /*       operations expecting it not to exist ie.  create/copy   */
  54.    /*       etc.  could possibly overwrite a file which does exist. */
  55.    /*---------------------------------------------------------------*/
  56.  
  57.    BOOL         fExists;
  58.    APIRET       rc;
  59.    HDIR         hdirFindHandle;
  60.    FILEFINDBUF  finfFileInfo;
  61.    ULONG        ulFindCount;
  62.  
  63.    hdirFindHandle = HDIR_CREATE;
  64.    ulFindCount    = 1;
  65.    fExists        = TRUE;
  66.  
  67.    rc = DosFindFirst( (PSZ)    pszFileName,
  68.                       (PHDIR)  &hdirFindHandle,
  69.                       (ULONG)  FILE_NORMAL,
  70.                       (PVOID)  &finfFileInfo,
  71.                       (ULONG)  sizeof(finfFileInfo),
  72.                       (PULONG) &ulFindCount,
  73.                       (ULONG)  FIL_STANDARD );
  74.  
  75.    if ( (rc == NO_ERROR) || (rc == ERROR_NO_MORE_FILES) ) {
  76.  
  77.       if ( rc == ERROR_NO_MORE_FILES ) {
  78.          fExists = FALSE;
  79.       }
  80.  
  81.       if ( (rc == NO_ERROR) || (rc == ERROR_EAS_DIDNT_FIT) ) {
  82.          rc = DosFindClose( hdirFindHandle );
  83.          if ( rc != NO_ERROR ) {
  84.             fprintf( stdout, "DosFindClose() failed with RC=%d.\r\n", rc );
  85.          }
  86.       }
  87.  
  88.    } else {
  89.       fprintf( stdout, "DosFindFirst('%s') failed with RC=%d.\r\n", pszFileName, rc );
  90.    }
  91.  
  92.    return( fExists );
  93.  
  94. } /* end fFileExists() */
  95.  
  96. /*********************************************************************/
  97. /* fFileDelete()                                                     */
  98. /*********************************************************************/
  99. BOOL fFileDelete( PSZ pszFileName )
  100. {
  101.    /*-----------------------------------------------------------------*/
  102.    /* Note: If it didn't exist then it may as well have been deleted. */
  103.    /*-----------------------------------------------------------------*/
  104.  
  105.    BOOL   fDeleted;
  106.    APIRET rc;
  107.  
  108.    fDeleted = FALSE;
  109.  
  110.    rc = DosDelete( pszFileName );
  111.    if ( rc == NO_ERROR ) {
  112.       fDeleted = TRUE;
  113.    } else {
  114.  
  115.       if ( rc == ERROR_FILE_NOT_FOUND ) {
  116.          fDeleted = TRUE;
  117.       } else {
  118.          fprintf( stdout, "DosDelete('%s') failed with RC=%d.\r\n", pszFileName, rc );
  119.       }
  120.    }
  121.  
  122.    return( fDeleted );
  123.  
  124. } /* end fFileDelete() */
  125.  
  126. /*********************************************************************/
  127. /* fFileCopy()                                                       */
  128. /*********************************************************************/
  129. BOOL fFileCopy( PSZ pszFromName, PSZ pszToName )
  130. {
  131.    BOOL         fCopied;
  132.    APIRET       rc;
  133.  
  134.    fCopied = FALSE;
  135.  
  136.    rc = DosCopy( pszFromName, pszToName, (ULONG) DCPY_EXISTING );
  137.    if ( rc == NO_ERROR ) {
  138.       fCopied = TRUE;
  139.    } else {
  140.  
  141.       if ( rc != ERROR_FILE_NOT_FOUND ) {
  142.          fprintf( stdout, "DosCopy('%s','%s') failed with RC=%d.\r\n", pszFromName, pszToName, rc );
  143.       }
  144.    }
  145.  
  146.    return( fCopied );
  147.  
  148. } /* end fFileCopy() */
  149.  
  150. /*********************************************************************/
  151. /* fFileRename()                                                     */
  152. /*********************************************************************/
  153. BOOL fFileRename( PSZ pszFromName, PSZ pszToName )
  154. {
  155.    BOOL   fRenamed;
  156.    APIRET rc;
  157.    PSZ    pszNameBuff;                 /* Pointer to name buffer     */
  158.    UCHAR  ucNameBuff[_MAX_DIR];        /* File name buffer           */
  159.    PSZ    pszPath;
  160.    PSZ    pszName;
  161.    PSZ    pszColon;
  162.    PSZ    pszSlash;
  163.  
  164.    fRenamed = FALSE;
  165.  
  166.    // Place the FromName into the file name buffer
  167.    pszNameBuff = &ucNameBuff[0];
  168.    strcpy( pszNameBuff, pszFromName );
  169.  
  170.    // Point to the start of the File component of FromName in buffer
  171.    pszPath  = pszNameBuff;
  172.    pszColon = strrchr( pszPath, DRIVE_DELIM_CH );
  173.    pszSlash = strrchr( pszPath, PATH_DELIM_CH );
  174.    if (pszSlash != NULL) {
  175.       pszColon = pszSlash;
  176.    }
  177.    if (pszColon != NULL) {
  178.       pszColon++;
  179.       pszPath = pszColon;
  180.    }
  181.  
  182.    // Point to the start of the File component of ToName
  183.    pszName  = pszToName;
  184.    pszColon = strrchr( pszName, DRIVE_DELIM_CH );
  185.    pszSlash = strrchr( pszName, PATH_DELIM_CH );
  186.    if (pszSlash != NULL) {
  187.       pszColon = pszSlash;
  188.    }
  189.    if (pszColon != NULL) {
  190.       pszColon++;
  191.       pszName = pszColon;
  192.    }
  193.  
  194.    // Copy the ToName File component into the buffer
  195.    strcpy( pszPath, pszName );
  196.  
  197.    rc = DosMove( pszFromName, pszNameBuff );
  198.    if ( rc == NO_ERROR ) {
  199.       fRenamed = TRUE;
  200.    } else {
  201.  
  202.       if ( rc != ERROR_FILE_NOT_FOUND ) {
  203.          fprintf( stdout, "DosMove('%s', '%s') failed with RC=%d.\r\n", pszFromName, pszToName, rc );
  204.       }
  205.    }
  206.  
  207.    return( fRenamed );
  208.  
  209. } /* end fFileRename() */
  210.  
  211. /*********************************************************************/
  212. /* fFileMove()                                                       */
  213. /*********************************************************************/
  214. BOOL fFileMove( PSZ pszFromName, PSZ pszToName )
  215. {
  216.    /*----------------------------------------------------------------*/
  217.    /* Note: Doing it this way means we can move files across drives. */
  218.    /*----------------------------------------------------------------*/
  219.  
  220.    BOOL   fMoved;
  221.    APIRET rc;
  222.  
  223.    fMoved = FALSE;
  224.  
  225.    if ( !fFileExists( pszToName ) ) {
  226.       if ( fFileCopy( pszFromName, pszToName ) ) {
  227.  
  228.          if ( fFileDelete( pszFromName ) ) {
  229.             fMoved = TRUE;
  230.          } else {
  231.             fFileDelete( pszToName );
  232.          }
  233.       }
  234.    }
  235.  
  236.    return( fMoved );
  237.  
  238. } /* end fFileMove() */
  239.  
  240. /*********************************************************************/
  241. /* FileNameIs()                                                      */
  242. /*********************************************************************/
  243. VOID FileNameIs( PSZ pszFile, PSZ pszName )
  244. {
  245.    /*--------------------------------------------*/
  246.    /* Note: Caller must allocate buffer pszName. */
  247.    /*--------------------------------------------*/
  248.  
  249.    PSZ pszColon;
  250.    PSZ pszSlash;
  251.  
  252.    pszName[0] = EOSTR_CH;
  253.  
  254.    if ( pszFile != NULL ) {
  255.       if ( strlen(pszFile) > 0 ) {
  256.  
  257.          pszColon = strrchr( pszFile, DRIVE_DELIM_CH );
  258.          pszSlash = strrchr( pszFile, PATH_DELIM_CH );
  259.  
  260.          if (pszSlash != NULL) {
  261.             pszColon = pszSlash;
  262.          }
  263.  
  264.          if (pszColon != NULL) {
  265.             pszColon++;
  266.             strcpy( pszName, pszColon );
  267.          } else {
  268.             strcpy( pszName, pszFile );
  269.          }
  270.       }
  271.    }
  272.  
  273.    return;
  274.  
  275. } /* end FileNameIs() */
  276.  
  277. /*********************************************************************/
  278. /* FilePathIs()                                                      */
  279. /*********************************************************************/
  280. VOID FilePathIs( PSZ pszFile, PSZ pszPath )
  281. {
  282.    /*--------------------------------------------*/
  283.    /* Note: Caller must allocate buffer pszPath. */
  284.    /*--------------------------------------------*/
  285.  
  286.    PSZ pszColon;
  287.    PSZ pszSlash;
  288.  
  289.    pszPath[0] = EOSTR_CH;
  290.  
  291.    if ( pszFile != NULL ) {
  292.       if ( strlen(pszFile) > 0 ) {
  293.  
  294.          strcpy( pszPath, pszFile );
  295.  
  296.          pszColon = strrchr( pszPath, DRIVE_DELIM_CH );
  297.          pszSlash = strrchr( pszPath, PATH_DELIM_CH );
  298.  
  299.          if (pszSlash != NULL) {
  300.             pszColon = pszSlash;
  301.          }
  302.  
  303.          if (pszColon != NULL) {
  304.             pszColon++;
  305.          } else {
  306.             pszColon = pszPath;
  307.          }
  308.  
  309.          pszColon[0] = EOSTR_CH;
  310.       }
  311.    }
  312.  
  313.    return;
  314.  
  315. } /* end FilePathIs() */
  316.  
  317. /*********************************************************************/
  318. /* FileChgDir()                                                      */
  319. /*********************************************************************/
  320. VOID FileChgDir( PSZ pszNewDir )
  321. {
  322.    APIRET  rc;               /* API return code                      */
  323.    ULONG   ulDrive;          /* Default drive number (A=1,..,Z=26)   */
  324.    ULONG   ulPathLen;        /* Directory path buffer length         */
  325.    PSZ     pszPath;          /* Pointer to path buffer               */
  326.    UCHAR   ucPath[_MAX_DIR]; /* Directory path buffer                */
  327.  
  328.    rc = NO_ERROR;
  329.  
  330.    pszPath = &ucPath[0];
  331.  
  332.    if ( strlen(pszNewDir) > 0 ) {
  333.  
  334.       strcpy( pszPath, pszNewDir );
  335.  
  336.       ulPathLen = strlen( pszPath );
  337.  
  338.       /* remove trailing backslash if present and not root */
  339.       if ( ulPathLen > 1 ) {
  340.          if (   (pszPath[ulPathLen-1] == PATH_DELIM_CH)
  341.              && (pszPath[ulPathLen-2] != DRIVE_DELIM_CH) ) {
  342.             pszPath[ulPathLen-1] = EOSTR_CH;
  343.          }
  344.       }
  345.  
  346.       /* change to new drive if specified */
  347.       if ( pszPath[1] == DRIVE_DELIM_CH ) {
  348.          ulDrive = (ULONG)( (BYTE)pszPath[0] - (BYTE)'A' ) + 1;
  349.          DosError( FERR_DISABLEHARDERR | FERR_DISABLEEXCEPTION );
  350.          rc = DosSetDefaultDisk( ulDrive );
  351.          DosError( FERR_ENABLEHARDERR | FERR_ENABLEEXCEPTION );
  352.       }
  353.  
  354.       if ( rc != NO_ERROR ) {
  355.          fprintf( stdout, "DosSetDefaultDisk('%c:') failed with RC=%d.\r\n", pszPath[0], rc );
  356.       } else {
  357.  
  358.          /* change to new directory */
  359.          rc = DosSetCurrentDir( pszPath );
  360.          if ( rc != NO_ERROR ) {
  361.             fprintf( stdout, "DosSetCurrentDir('%s') failed with RC=%d.\r\n", pszPath, rc );
  362.          }
  363.       }
  364.    }
  365.  
  366.    return;
  367.  
  368. } /* end FileChgDir() */
  369.  
  370. /*********************************************************************/
  371. /* FileGetDir()                                                      */
  372. /*********************************************************************/
  373. VOID FileGetDir( PSZ pszCurDir )
  374. {
  375.    /*----------------------------------------------*/
  376.    /* Note: Caller must allocate buffer pszCurDir. */
  377.    /*----------------------------------------------*/
  378.  
  379.    APIRET  rc;               /* API return code                      */
  380.    ULONG   ulDrive;          /* Default drive number (A=1,..,Z=26)   */
  381.    ULONG   ulMap;            /* Drive map (bits 0-25 indicate valid) */
  382.    ULONG   ulPathLen;        /* Directory path buffer length         */
  383.    UCHAR   ucPath[_MAX_DIR]; /* Directory path buffer                */
  384.    PSZ     pszPath;          /* Pointer to path buffer               */
  385.  
  386.    pszPath = &ucPath[0];
  387.  
  388.    pszCurDir[0] = EOSTR_CH;
  389.  
  390.    rc = DosQueryCurrentDisk( &ulDrive, &ulMap );
  391.    if ( rc != NO_ERROR ) {
  392.       fprintf( stdout, "DosQueryCurrentDisk() failed with RC=%d.\r\n", rc );
  393.    } else {
  394.  
  395.       pszPath[0] = EOSTR_CH;
  396.       ulPathLen = _MAX_DIR;
  397.  
  398.       rc = DosQueryCurrentDir( ulDrive, pszPath, &ulPathLen );
  399.       if ( rc != NO_ERROR ) {
  400.          fprintf( stdout, "DosQueryCurrentDir() failed with RC=%d.\r\n", rc );
  401.       } else {
  402.  
  403.          /* return drive spec */
  404.          pszCurDir[0] = (CHAR)( (BYTE)'A' + (BYTE)(ulDrive-1) );
  405.          pszCurDir[1] = DRIVE_DELIM_CH;
  406.          pszCurDir[2] = PATH_DELIM_CH;
  407.          pszCurDir[3] = EOSTR_CH;
  408.  
  409.          /* append path if any */
  410.          if (strlen( pszPath ) > 0) {
  411.             strcat( pszCurDir, pszPath );
  412.             ulPathLen = strlen( pszCurDir );
  413.  
  414.             /* append trailing backslash if room */
  415.             if (ulPathLen < _MAX_DIR) {
  416.                pszCurDir[ulPathLen] = PATH_DELIM_CH;
  417.                ulPathLen++;
  418.                pszCurDir[ulPathLen] = EOSTR_CH;
  419.             }
  420.          }
  421.       }
  422.    }
  423.  
  424.    return;
  425.  
  426. } /* end FileGetDir() */
  427.  
  428. /*********************************************************************/
  429. /* END MODULE                                                        */
  430. /*********************************************************************/
  431.