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.