home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / dev / gcc / ixemulsrc.lha / ixemul / library / chown.c < prev    next >
C/C++ Source or Header  |  1996-12-11  |  2KB  |  73 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *  Portions Copyright (C) 1996 Jeff Shepherd
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public
  18.  *  License along with this library; if not, write to the Free
  19.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #define _KERNEL
  23. #include "ixemul.h"
  24. #include "kprintf.h"
  25. #include <stdlib.h>
  26.  
  27. int
  28. __chown_func (struct StandardPacket *sp, struct MsgPort *handler,
  29.           BPTR parent_lock,
  30.           BSTR name,
  31.           int ugid, int *no_error)
  32. {
  33.   sp->sp_Pkt.dp_Type = ACTION_SET_OWNER;
  34.   sp->sp_Pkt.dp_Arg1 = 0;
  35.   sp->sp_Pkt.dp_Arg2 = parent_lock;
  36.   sp->sp_Pkt.dp_Arg3 = name;
  37.   sp->sp_Pkt.dp_Arg4 = ugid;
  38.  
  39.   PutPacket (handler, sp);
  40.   __wait_sync_packet (sp);
  41.  
  42.   *no_error = sp->sp_Pkt.dp_Res1 == -1;
  43.   return 1;
  44. }
  45.  
  46. int
  47. chown(const char *name, uid_t uid, gid_t gid)
  48. {
  49.   int user;
  50.   int result;
  51.   struct stat stb;
  52.  
  53.   if (syscall (SYS_lstat, name, &stb) == -1) return -1;
  54.   if (stb.st_mode & 0600)
  55.     {
  56.       if (syscall (SYS_chmod, name, stb.st_mode & ~0176000) == -1) return -1;
  57.     }
  58.  
  59.   if (uid == (uid_t)(-1)) uid = stb.st_uid;
  60.   if (gid == (gid_t)(-1)) gid = stb.st_gid;
  61.  
  62.   user = (__unix2amigaid(uid) << 16) | __unix2amigaid(gid);
  63.  
  64.   result = __plock(name,__chown_func, (void *)user);
  65.   if (result == 0)
  66.     {
  67.       errno = __ioerr_to_errno (IoErr ());
  68.       KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  69.     }
  70.  
  71.   return result == -1 ? 0 : -1;
  72. }
  73.