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

  1. /***
  2. *chmod.c - change file attributes
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       Defines _chmod() - change file attributes
  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 <sys\types.h>
  18. #include <sys\stat.h>
  19. #include <tchar.h>
  20.  
  21. /***
  22. *int _chmod(path, mode) - change file mode
  23. *
  24. *Purpose:
  25. *       Changes file mode permission setting to that specified in
  26. *       mode.  The only XENIX mode bit supported is user write.
  27. *
  28. *Entry:
  29. *       _TSCHAR *path - file name
  30. *       int mode - mode to change to
  31. *
  32. *Exit:
  33. *       returns 0 if successful
  34. *       returns -1 and sets errno if not successful
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.  
  40. int __cdecl _tchmod (
  41.         const _TSCHAR *path,
  42.         int mode
  43.         )
  44. {
  45.         DWORD attr;
  46.  
  47.         attr = GetFileAttributes((LPTSTR)path);
  48.         if (attr  == 0xffffffff) {
  49.                 /* error occured -- map error code and return */
  50.                 _dosmaperr(GetLastError());
  51.                 return -1;
  52.         }
  53.  
  54.         if (mode & _S_IWRITE) {
  55.                 /* clear read only bit */
  56.                 attr &= ~FILE_ATTRIBUTE_READONLY;
  57.         }
  58.         else {
  59.                 /* set read only bit */
  60.                 attr |= FILE_ATTRIBUTE_READONLY;
  61.         }
  62.  
  63.         /* set new attribute */
  64.         if (!SetFileAttributes((LPTSTR)path, attr)) {
  65.                 /* error occured -- map error code and return */
  66.                 _dosmaperr(GetLastError());
  67.                 return -1;
  68.         }
  69.  
  70.         return 0;
  71. }
  72.  
  73. #else  /* _MAC */
  74.  
  75.  
  76. #include <cruntime.h>
  77. #include <internal.h>
  78. #include <io.h>
  79. #include <sys\types.h>
  80. #include <sys\stat.h>
  81. #include <string.h>
  82. #include <errno.h>
  83. #include <macos\osutils.h>
  84. #include <macos\files.h>
  85. #include <macos\errors.h>
  86.  
  87. /***
  88. *int _chmod(path, mode) - change file mode
  89. *
  90. *Purpose:
  91. *       Changes file mode permission setting to that specified in
  92. *       mode.  The only XENIX mode bit supported is user write.
  93. *
  94. *Entry:
  95. *       char *path -    file name
  96. *       int mode - mode to change to
  97. *
  98. *Exit:
  99. *       returns 0 if successful
  100. *       returns -1 and sets errno if not successful
  101. *
  102. *Exceptions:
  103. *
  104. *******************************************************************************/
  105.  
  106. int __cdecl _chmod (
  107.         const char *path,
  108.         int mode
  109.         )
  110. {
  111.         HParamBlockRec hparamBlock;
  112.         char szPath[256];
  113.         OSErr osErr;
  114.  
  115.         if (!*path)
  116.         {
  117.                 errno = ENOENT;
  118.                 return -1;
  119.         }
  120.  
  121.         strcpy(szPath, path);
  122.         hparamBlock.fileParam.ioNamePtr = _c2pstr(szPath);
  123.         hparamBlock.fileParam.ioVRefNum = 0;
  124.         hparamBlock.fileParam.ioDirID = 0;
  125.  
  126.         if (mode & _S_IWRITE) {
  127.                 /* clear read only/locked bit */
  128.                 osErr = PBHRstFLockSync(&hparamBlock);
  129.         }
  130.         else {
  131.                 /* set read only/locked bit */
  132.                 osErr = PBHSetFLockSync(&hparamBlock);
  133.         }
  134.  
  135.         if (osErr)
  136.         {
  137.             _dosmaperr(osErr);
  138.             return -1;
  139.         }
  140.  
  141.         return 0;
  142. }
  143.  
  144. #endif  /* _MAC */
  145.