gtpc2m6eC/C++ Language Support User's Guide

setgid-Set the Group ID to a Specified Value

This function sets one or more of the group IDs (GIDs) for the current process to a specified value.

Format

#include <unistd.h>
int setgid(uid_t gid);

gid
The intended new group ID value.

Normal Return

If successful, the setgid function returns an integer value of 0 and the group ID is set.

Error Return

If unsuccessful, the setgid function returns -1 and sets errno to one of the following:

EINVAL
The value specified for gid is incorrect.

EPERM
The process does not have one of the appropriate privileges to set gid.

Programming Considerations

Examples

The following example changes the effective group ID.

#include <unistd.h>
#include <stdio.h>
 
int main(void) {
 
  printf("Your group id is %d\n", (int) getgid() );
  if ( setgid ( 100 ) != 0 )
      perror ( "setgid() error" );
  else
       printf("Your group id was changed to %d\n",
                              (int) getgid() );
  return 0;
}

Related Information