chown -- Change File Owner or Group (Using Pathname)

SYNOPSIS

#include <sys/types.h> 
#include <unistd.h>

 int chown(const char *pathname, uid_t owner, gid_t group);
 

DESCRIPTION

chown changes the owner or owning group of a file. It can be used with either regular files or special files, such as directories or FIFO files. pathname is the name of the file. owner is the user ID. group is the group ID.

OpenEdition implements a restricted chown for all files. A call to chown can succeed in only one of two ways:

If the S_IXUSR, S_IXGRP, or S_IXOTH bit is set in the file permissions, chown clears the S_ISUID and S_ISGID bits of the permissions.

RETURN VALUE

chown returns 0 if successful and a -1 if not successful.

EXAMPLE

The following example illustrates the use of chown to change a file's owner and group:
  #include <sys/types.h>
  #include <unistd.h>
  #include <grp.h>
  #include <stdlib.h>

  main(int argc, char *argv[])
  {
     struct group *billing;
     int rc;

     billing = getgrnam("BILLING");
     if (!billing) {
        perror("getgrnam error");
        abort();
     }
     if (argc <= 1) {
        printf("No file name specified.n");
        exit(EXIT_FAILURE);
     }
     rc = chown(argv[1], geteuid(), billing->gr_gid);
     if (rc == 0) {
        printf("Ownership of %s assigned to BILLING.n", argv[1]);
        exit(EXIT_SUCCESS);
     }
     else {
        perror("chown error");
        abort();
     }
  }

 

RELATED FUNCTIONS

chmod, fchown


Copyright (c) 1998 SAS Institute Inc. Cary, NC, USA. All rights reserved.