home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / rmdir.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  140 lines

  1. /***
  2. *rmdir.c - remove directory
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _rmdir() - remove a directory
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <oscalls.h>
  15. #include <internal.h>
  16. #include <direct.h>
  17. #include <tchar.h>
  18.  
  19. /***
  20. *int _rmdir(path) - remove a directory
  21. *
  22. *Purpose:
  23. *       deletes the directory speicifed by path.  The directory must
  24. *       be empty, and it must not be the current working directory or
  25. *       the root directory.
  26. *
  27. *Entry:
  28. *       _TSCHAR *path - directory to remove
  29. *
  30. *Exit:
  31. *       returns 0 if successful
  32. *       returns -1 and sets errno if unsuccessful
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. int __cdecl _trmdir (
  39.         const _TSCHAR *path
  40.         )
  41. {
  42.         ULONG dosretval;
  43.  
  44.         /* ask OS to remove directory */
  45.  
  46.         if (!RemoveDirectory((LPTSTR)path))
  47.             dosretval = GetLastError();
  48.         else
  49.             dosretval = 0;
  50.  
  51.         if (dosretval) {
  52.             /* error occured -- map error code and return */
  53.             _dosmaperr(dosretval);
  54.             return -1;
  55.         }
  56.  
  57.         return 0;
  58. }
  59.  
  60. #else  /* _MAC */
  61.  
  62. #include <cruntime.h>
  63. #include <internal.h>
  64. #include <direct.h>
  65. #include <string.h>
  66. #include <errno.h>
  67. #include <macos\osutils.h>
  68. #include <macos\files.h>
  69. #include <macos\errors.h>
  70.  
  71. #include <macos\types.h>
  72.  
  73. /***
  74. *int _rmdir(path) - remove a directory
  75. *
  76. *Purpose:
  77. *       deletes the directory speicifed by path.  The directory must
  78. *       be empty, and it must not be the current working directory or
  79. *       the root directory.
  80. *
  81. *Entry:
  82. *       char *path -    directory to remove
  83. *
  84. *Exit:
  85. *       returns 0 if successful
  86. *       returns -1 and sets errno if unsuccessful
  87. *
  88. *Exceptions:
  89. *
  90. *******************************************************************************/
  91.  
  92. int __cdecl _rmdir (
  93.         const char *path
  94.         )
  95. {
  96.         ParamBlockRec parm;
  97.         HParamBlockRec hparm;
  98.  
  99.         OSErr osErr;
  100.         char stPath[256];
  101.  
  102.         if (!*path || strlen(path) > 255)
  103.         {
  104.             errno = ENOENT;
  105.             return -1;
  106.         }
  107.  
  108.         strcpy(stPath, path);
  109.         _c2pstr(stPath);
  110.  
  111.         memset(&hparm, '\0', sizeof(HParamBlockRec));
  112.         hparm.fileParam.ioNamePtr = stPath;
  113.  
  114.         /* hparm.fileParam.ioVRefNum = 0; */
  115.         /* hparm.fileParam.ioFDirIndex = 0; */
  116.  
  117.         if ((osErr = PBHGetFInfoSync(&hparm)) == fnfErr)
  118.         {
  119.             memset(&parm, '\0', sizeof(ParamBlockRec));
  120.             parm.fileParam.ioNamePtr = stPath;
  121.             /* parm.fileParam.ioVRefNum = 0; */
  122.             osErr = PBDeleteSync(&parm);
  123.         }
  124.         else if (!osErr)
  125.         {
  126.             osErr = fnfErr;  /* Can't rmdir a file */
  127.         }
  128.  
  129.         if (osErr)
  130.         {
  131.             /* error occured -- map error code and return */
  132.             _dosmaperr(osErr);
  133.             return -1;
  134.         }
  135.         return 0;
  136.  
  137. }
  138.  
  139. #endif  /* _MAC */
  140.