Fedora Linux Support Community & Resources Center
Old 21st July 2010, 01:54 PM
merkava's Avatar
merkava Offline
Registered User
Join Date: Jul 2010
Location: Tabriz, Iran
Age: 27
Posts: 77
getgroups function

Hi guys.

I have wrote a simple program to test getgroups functionality:
Here is my group configuration:

test_user is member of all groups. and here is the program:

int main(void)
//I get number of supplementary groups
int number_of_groups = getgroups(0, NULL);
printf("%d\n", number_of_groups);

This program is running with user id of test_user. But the problem is this:

'number_of_groups' is always '1'
Best Regards
Reply With Quote
Old 22nd July 2010, 09:24 AM
giulix's Avatar
giulix Offline
Join Date: Oct 2005
Location: CE(S)T
Posts: 3,242
Re: getgroups function

If you access the info documentation from the command line for getgroups, you'll get a usage example, from which the following
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct s S_STR;
#define S_STR struct s
struct s {
    int counter;
    gid_t *g;

S_STR *read_all_groups();

int main(int argc, char *argv[]) {

    int i = 0;
    S_STR *p = NULL;

    if((p = (S_STR *)read_all_groups()) == NULL) {
        perror("Could not determine process group(s)");

    for(i = 0; i < p->counter; p->g++, i++)
        printf("Group: %d\n", *p->g);

    return 0;

S_STR *read_all_groups(void) {

    S_STR *p = NULL;
    int ngroups = getgroups(0, NULL);

    if((p = (S_STR *)malloc(sizeof(S_STR))) == NULL) {
        perror("Error allocating memory for S_STR struct");
        p->g = NULL;

    if((p->g = (gid_t *)malloc(ngroups * sizeof(gid_t))) == NULL) {
        perror("Error allocating memory for groups array");
    p->counter = getgroups(ngroups, p->g);
    if(p->counter < 0) {
        if(p->g != NULL) {
            p->g = NULL;
        return NULL;

    return p;
Asus K55VD, i5 3230M - Productivity/Programming (F24)
Asus M32CD - i7-6700, Asus STRIX-GTX970-DC2OC-4GD, 1x8 GB Sk Hynix 2133 MHz DDR4 - Gaming (W10)
HP Proliant DL 360 G5 - Heavy lifting (CentOS 7.2)

Last edited by giulix; 22nd July 2010 at 05:13 PM.
Reply With Quote

function, getgroups

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
fc8 Function keys Fittersman Using Fedora 1 23rd November 2007 08:54 AM
keyboard not function irbadguyzs Using Fedora 3 6th November 2007 01:19 AM
MP3 and DVD does not function Rezwan Mahbub Using Fedora 0 7th March 2007 06:51 PM
some function the curse Programming & Packaging 2 26th November 2006 08:01 AM

Current GMT-time: 07:48 (Monday, 25-09-2017)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat