Thread: popen for linux cmd output

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    18

    popen for linux cmd output

    Hi,

    The following C program on Linux, does a set of operations based on whether user is "root" or an ordinary user.

    Using popen() function, I've tried to manipulate Linux cmd output in the program:



    Code:
         int main()
    {
    	FILE *fp;
    	char ch[130],user[]="root";
    	int i;
    
    	fp = popen("id -u -n","r");
    
    	while (fgets(ch,sizeof ch,fp))
    	{
    	printf("\n String from fgets = %s \n String in user = %s",ch,user);
    
    	i=strcmp(user,ch);
    	printf("\n i= %d\n",i);
    	i=strlen(user);
    	printf("\n i= %d\n",i);
    	i=strlen(ch);
            printf("\n i= %d\n",i);
    
    	if ((strcmp(user,ch))==0)
    		printf("\n Welcome to Administrator Login \n");
    	else	
    		printf("\n Welcome to User Login \n");
    	}
    	pclose(fp);
    	return 0;
    }
    Even, the user is root, but still its giving wrong output.

    # icc chusr.c -o chusr

    # ./chusr

    String from fgets = root

    String in user = root
    i= -1

    i= 4

    i= 5

    Welcome to User Login

    I'm not getting, why strcmp function gave "-1" value here.

    Is there a function to read word by word from fp?
    What is the best method to manipulate data between program to OS & vice-versa.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sangamesh View Post
    I'm not getting, why strcmp function gave "-1" value here.
    Because ch and user are different lengths.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Why don't you do the thing id does by yourself? It will be quite a lot simpler and cleaner. getuid(2) and geteuid(2).

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    18
    Thanks fronty for your suggestion. Its working with the function getuid(). Is root's uid (0) same in all linux flavours?

    Also can some one let's know, why length of ch is 5? why not 4?

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    fgets() reads in at most one less than size characters from stream and
    stores them into the buffer pointed to by s. Reading stops after an
    EOF or a newline. If a newline is read, it is stored into the buffer.
    A '\0' is stored after the last character in the buffer.
    There from man fgets.

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by sangamesh View Post
    Is root's uid (0) same in all linux flavours?
    Super user's uid is 0 on all posix-ish operating systems.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  4. Control different DA output value!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-13-2003, 12:11 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM