Renamed include file aros/_timeval.h to aros/types/timeval_s.h.
[AROS.git] / compiler / clib / chown.c
blobdcd9e36ab9947144915de5a55180ef1c49fa9f7b
1 /*
2 Copyright © 1995-2009, 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 "__errno.h"
14 #include "__upath.h"
16 /*****************************************************************************
18 NAME */
19 #include <unistd.h>
21 int chown(
23 /* SYNOPSIS */
24 const char *path,
25 uid_t owner,
26 gid_t group)
28 /* FUNCTION
29 Change the user and group ownership of a file.
31 INPUTS
32 path - the path to file
33 owner - new owner ID
34 group - new group ID
36 RESULT
37 0 on success and -1 on error. If an error occurred, the global
38 variable errno is set.
40 NOTES
41 This implementation was done by looking at Olaf 'Olsen' Barthels
42 clib2.
44 EXAMPLE
46 BUGS
48 SEE ALSO
50 INTERNALS
52 ******************************************************************************/
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 (__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 = IoErr2errno(IoErr());
76 goto out;
79 if (!(lock = Lock(path, SHARED_LOCK)) || !Examine(lock, fib))
81 errno = 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 = 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;