prism2.device: fix strict aliasing issue detected with gcc 4.8.3
[AROS.git] / compiler / posixc / chown.c
blob733eee062650a06cc3b5ddf5a63b81c086510d63
1 /*
2 Copyright © 1995-2013, The AROS Development Team. All rights reserved.
3 $Id$
5 POSIX.1-2008 chown() function
6 */
8 #include <aros/debug.h>
10 #include <proto/dos.h>
12 #include <errno.h>
14 #include "__posixc_intbase.h"
15 #include "__upath.h"
17 /*****************************************************************************
19 NAME */
20 #include <unistd.h>
22 int chown(
24 /* SYNOPSIS */
25 const char *path,
26 uid_t owner,
27 gid_t group)
29 /* FUNCTION
30 Change the user and group ownership of a file.
32 INPUTS
33 path - the path to file
34 owner - new owner ID
35 group - new group ID
37 RESULT
38 0 on success and -1 on error. If an error occurred, the global
39 variable errno is set.
41 NOTES
42 This implementation was done by looking at Olaf 'Olsen' Barthels
43 clib2.
45 EXAMPLE
47 BUGS
49 SEE ALSO
51 INTERNALS
53 ******************************************************************************/
55 struct PosixCIntBase *PosixCBase =
56 (struct PosixCIntBase *)__aros_getbase_PosixCBase();
57 int res = -1;
58 BPTR lock = BNULL;
59 struct FileInfoBlock *fib = NULL;
60 BOOL changed = TRUE;
62 /* check for empty path before potential conversion from "." to "" */
63 if (PosixCBase->doupath && path && *path == '\0')
65 errno = ENOENT;
66 goto out;
69 path = __path_u2a(path);
70 if (path == NULL)
71 goto out;
73 /* should some id not be changed */
74 if (owner == -1 || group == -1)
76 if (!(fib = AllocDosObject(DOS_FIB, NULL)))
78 errno = __stdc_ioerr2errno(IoErr());
79 goto out;
82 if (!(lock = Lock(path, SHARED_LOCK)) || !Examine(lock, fib))
84 errno = __stdc_ioerr2errno(IoErr());
85 goto out;
88 /* set to previous id */
89 if (owner == -1)
90 owner = fib->fib_OwnerUID;
92 if (group == -1)
93 group = fib->fib_OwnerGID;
95 if (owner == fib->fib_OwnerUID && group == fib->fib_OwnerGID)
96 changed = FALSE;
99 if (owner > 65535 || group > 65535)
101 errno = EINVAL;
102 goto out;
105 if (changed && !SetOwner(path, owner << 16 | group))
107 errno = __stdc_ioerr2errno(IoErr());
108 goto out;
111 res = 0;
113 out:
115 if (fib)
116 FreeDosObject(DOS_FIB, fib);
118 if (lock)
119 UnLock(lock);
121 return res;