Every file (or directory, or device entry, or whatever) in the filesystem has an owner and group. The owner and group of a file determine to whom the owner and group permissions apply (read, write, and/or execute). The owner and group of a file are determined at the time the file is created, but under certain circumstances, you can
change them. (The exact circumstances depend on the particular flavor of UNIX you are running: see the
chown
manpage for details.)
The
chown
function takes a user ID number (UID), a group ID number (GID), and a list of filenames, and attempts to change the ownership of each of the listed files as specified. A success is indicated by a nonzero return value equal to the number of files successfully changed - just like
chmod
or
unlink
. Note that you are changing both the owner and the group at once. Use -1 instead of an actual user or group ID if you do not want to change the ID. Also note that you must use the numeric UID and GID, not the corresponding symbolic names (even though the
chmod
command accepts the names). For example, if
fred
is UID 1234 and
fred
's default group
stoners
is GID 35, then the following command makes the files
slate
and
granite
belong to
fred
and his default group:
chown(1234, 35, "slate", "granite"); # same as:
# chown fred slate granite
# chgrp stoners slate granite
In
Chapter 16,
System Database Access
, you'll learn how to convert
fred
to
1234
and
stoners
to
35
.