update mappings to reflect recent changes
[AROS.git] / compiler / clib / chown.c
blob96a9b9d4c33f5c1194bc924520a460f23c2d9424
1 /*
2 Copyright © 1995-2012, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
8 #include <proto/dos.h>
10 #include <errno.h>
12 #include "__arosc_privdata.h"
13 #include "__upath.h"
15 /*****************************************************************************
17 NAME */
18 #include <unistd.h>
20 int chown(
22 /* SYNOPSIS */
23 const char *path,
24 uid_t owner,
25 gid_t group)
27 /* FUNCTION
28 Change the user and group ownership of a file.
30 INPUTS
31 path - the path to file
32 owner - new owner ID
33 group - new group ID
35 RESULT
36 0 on success and -1 on error. If an error occurred, the global
37 variable errno is set.
39 NOTES
40 This implementation was done by looking at Olaf 'Olsen' Barthels
41 clib2.
43 EXAMPLE
45 BUGS
47 SEE ALSO
49 INTERNALS
51 ******************************************************************************/
53 struct aroscbase *aroscbase = __aros_getbase_aroscbase();
54 int res = -1;
55 BPTR lock = BNULL;
56 struct FileInfoBlock *fib = NULL;
57 BOOL changed = TRUE;
59 /* check for empty path before potential conversion from "." to "" */
60 if (aroscbase->acb_doupath && path && *path == '\0')
62 errno = ENOENT;
63 goto out;
66 path = __path_u2a(path);
67 if (path == NULL)
68 goto out;
70 /* should some id not be changed */
71 if (owner == -1 || group == -1)
73 if (!(fib = AllocDosObject(DOS_FIB, NULL)))
75 errno = __arosc_ioerr2errno(IoErr());
76 goto out;
79 if (!(lock = Lock(path, SHARED_LOCK)) || !Examine(lock, fib))
81 errno = __arosc_ioerr2errno(IoErr());
82 goto out;
85 /* set to previous id */
86 if (owner == -1)
87 owner = fib->fib_OwnerUID;
89 if (group == -1)
90 group = fib->fib_OwnerGID;
92 if (owner == fib->fib_OwnerUID && group == fib->fib_OwnerGID)
93 changed = FALSE;
96 if (owner > 65535 || group > 65535)
98 errno = EINVAL;
99 goto out;
102 if (changed && !SetOwner(path, owner << 16 | group))
104 errno = __arosc_ioerr2errno(IoErr());
105 goto out;
108 res = 0;
110 out:
112 if (fib)
113 FreeDosObject(DOS_FIB, fib);
115 if (lock)
116 UnLock(lock);
118 return res;