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

  1. /***
  2. *unlink.c - unlink a file
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines unlink() - unlink a file
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13. #include <cruntime.h>
  14. #include <oscalls.h>
  15. #include <internal.h>
  16. #include <io.h>
  17. #include <tchar.h>
  18.  
  19. /***
  20. *int _unlink(path) - unlink(delete) the given file
  21. *
  22. *Purpose:
  23. *       This version deletes the given file because there is no
  24. *       distinction between a linked file and non-linked file.
  25. *
  26. *       NOTE: remove() is an alternative entry point to the _unlink()
  27. *       routine* interface is identical.
  28. *
  29. *Entry:
  30. *       _TSCHAR *path - file to unlink/delete
  31. *
  32. *Exit:
  33. *       returns 0 if successful
  34. *       returns -1 and sets errno if unsuccessful
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.  
  40. int __cdecl _tremove (
  41.         const _TSCHAR *path
  42.         )
  43. {
  44.         ULONG dosretval;
  45.  
  46.         if (!DeleteFile((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. int __cdecl _tunlink (
  61.         const _TSCHAR *path
  62.         )
  63. {
  64.         /* remove is synonym for unlink */
  65.         return _tremove(path);
  66. }
  67.  
  68. #else  /* _MAC */
  69.  
  70.  
  71. #include <cruntime.h>
  72. //#include <oscalls.h>
  73. #include <internal.h>
  74. #include <io.h>
  75. #include <errno.h>
  76. #include <stdlib.h>
  77. #include <macos\files.h>
  78. #include <macos\errors.h>
  79. #include <string.h>
  80.  
  81. /***
  82. *int _unlink(path) - unlink(delete) the given file
  83. *
  84. *Purpose:
  85. *       This version deletes the given file but not a directory
  86. *
  87. *Entry:
  88. *       char *path -    file to unlink/delete
  89. *
  90. *Exit:
  91. *       returns 0 if successful
  92. *       returns -1 and sets errno if unsuccessful
  93. *
  94. *Exceptions:
  95. *
  96. *******************************************************************************/
  97.  
  98. int __cdecl _unlink (
  99.         const char *path
  100.         )
  101. {
  102.         ParamBlockRec parm;
  103.         OSErr osErr;
  104.         char stPath[256];
  105.  
  106.         if (!*path || strlen(path) > 255)
  107.         {
  108.             errno = ENOENT;
  109.             return -1;
  110.         }
  111.  
  112.         strcpy(stPath, path);
  113.         _c2pstr(stPath);
  114.  
  115.         memset(&parm, '\0', sizeof(ParamBlockRec));
  116.         parm.fileParam.ioNamePtr = stPath;
  117.  
  118.         /* parm.fileParam.ioVRefNum = 0; */
  119.         /* parm.fileParam.ioFDirIndex = 0; */
  120.  
  121.         if (!(osErr = PBGetFInfoSync(&parm)))  /* Make sure it's not a dir */
  122.         {
  123.             memset(&parm, '\0', sizeof(ParamBlockRec));
  124.             parm.ioParam.ioNamePtr = stPath;
  125.             /* parm.ioParam.ioVRefNum = 0; */
  126.             osErr = PBDeleteSync(&parm);
  127.         }
  128.  
  129.         if (osErr)
  130.         {
  131.             /* error occured -- map error code and return */
  132.             _dosmaperr(osErr);
  133.             return -1;
  134.         }
  135.  
  136.         return 0;
  137. }
  138.  
  139. #endif  /* _MAC */
  140.