home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / chmod.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  81 lines

  1. /* chmod -- change the permissions of a file */
  2. /* chown -- change the owner and group of a file */
  3. /* written by Eric R. Smith and placed in the public domain */
  4.  
  5. #include <types.h>
  6. #include <stat.h>
  7. #include <osbind.h>
  8. #include <mintbind.h>
  9. #include <limits.h>
  10. #include <errno.h>
  11. #include <unistd.h>
  12. #include "lib.h"
  13.  
  14. extern int __mint;
  15.  
  16. int chmod(_path, mode)
  17.        const char *_path;
  18.        int   mode;
  19. {
  20.        int  dosattrib = 0, r;
  21.        char path[PATH_MAX];
  22.        struct stat stb;
  23.  
  24.        (void)_unx2dos(_path, path);
  25.  
  26.     if (__mint >= 9) {    /* use MiNT Fchmod function */
  27.         r = (int)Fchmod(path, mode);
  28.         if (r) {
  29.             errno = -r;
  30.             return -1;
  31.         }
  32.         return 0;
  33.     }
  34.  
  35. /* The following lines ensure that the archive bit isn't cleared */
  36.     r = Fattrib(path, 0, dosattrib);
  37.      if (r > 0 && (r & FA_CHANGED))
  38.         dosattrib |= FA_CHANGED;
  39.  
  40. #if 0
  41.        if (!(mode & S_IREAD))
  42.                dosattrib |= FA_HIDDEN;
  43. #endif
  44.        if (!(mode & S_IWRITE))
  45.                dosattrib |= FA_RDONLY;
  46.        r = Fattrib(path, 1, dosattrib);
  47.        if (r < 0) {
  48. /* GEMDOS doesn't allow chmod on a directory, so pretend it worked */
  49.                if (!stat(_path, &stb)) {
  50.                 if ( (stb.st_mode & S_IFMT) == S_IFDIR )
  51.             return 0;
  52.                }
  53.                errno = -r;
  54.                return -1;
  55.        }
  56.        return 0;
  57. }
  58.  
  59. /*
  60.  * chown: this is faked if MiNT is not running
  61.  */
  62.  
  63. int chown(_name, uid, gid)
  64.        const char *_name;
  65.        int uid, gid;
  66. {
  67.     int r;
  68.     char name[PATH_MAX];
  69.  
  70.     if (__mint >= 9) {
  71.         (void)_unx2dos(_name, name);
  72.         r = (int)Fchown(name, uid, gid);
  73.         if (r) {
  74.             errno = -r;
  75.             return -1;
  76.         }
  77.         return 0;
  78.     }
  79.     return 0;
  80. }
  81.