LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2009, 12:45 AM   #1
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Rep: Reputation: 15
Need some modification in program.....


Respected Sir,


Below is a program:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#define N_BITS 3

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

unsigned int i, mask = 0700;
struct stat buff;
struct tm* lastMtime;
static char *perm[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
static char *mnth[] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct", "Nov", "Dec"};
if (argc > 1 ) {
if ((stat(argv[1], &buff) != -1)) {
lastMtime = localtime(&buff.st_mtime);
for (i=3; i; --i) {
printf("%3s", perm[(buff.st_mode & mask) >> (i-1)*N_BITS]);
mask >>= N_BITS;
}
printf(" %d", buff.st_nlink);
printf(" %d", buff.st_uid);
printf(" %d", buff.st_gid);
printf(" %d", buff.st_size);
printf(" %s %d", mnth[lastMtime->tm_mon], lastMtime->tm_mday);
printf(" %d:%d", lastMtime->tm_hour, lastMtime->tm_min);
printf(" %s", argv[1]);
putchar('\n');
} else {
perror(argv[1]);
exit(1);
}
} else {
fprintf(stderr, "Usage: %s a_file\n", *argv);
}
}

Output of this program is going to be like this:

rwxr-xr-x 1 1533 20 853 Jan 31 21:43 ppp.c

I want to modify this program in such as way that instead of numbers - 1533 and 20 it should print user's name and group's name.

Thanks.
 
Old 02-25-2009, 12:52 AM   #2
slackert
LQ Newbie
 
Registered: Dec 2007
Posts: 6

Rep: Reputation: 1
Maybe getpwnam or getpwuid?
 
Old 02-25-2009, 12:13 PM   #3
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by slackert View Post
Maybe getpwnam or getpwuid?
Thanks for the help sir, but it is not working. Its giving 0 and 0.
 
Old 02-25-2009, 12:26 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Where is the zero? Is that the return of getpwuid or is that the pointer to the user-name string? If it's the return of getpwuid, that normally means the user ID doesn't exist or you don't have permission to read the passwd file. Print strerror(errno) immediately after getpwuid and tell us what it says.
Kevin Barry

PS You need getgrgid to get the group name. Also, you'll sometimes encounter an external file system with unknown IDs because the files were created on other machines. That means you'll have to deal with unknown users. Extracting archives can do this, also.

Last edited by ta0kira; 02-25-2009 at 12:37 PM.
 
Old 02-25-2009, 12:32 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
This works for me:
Code:
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>

int main()
{
  struct passwd* pwd = getpwuid( getuid() );
  struct group*  grp = getgrgid(pwd->pw_gid);

  printf("user's name  = %s\n", pwd->pw_name);
  printf("user's group = %s\n", grp->gr_name);
}
Of course, I left off the error checking as an exercise for the OP to complete.
 
Old 02-25-2009, 01:03 PM   #6
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ta0kira View Post
Where is the zero? Is that the return of getpwuid or is that the pointer to the user-name string? If it's the return of getpwuid, that normally means the user ID doesn't exist or you don't have permission to read the passwd file. Print strerror(errno) immediately after getpwuid and tell us what it says.
Kevin Barry

PS You need getgrgid to get the group name. Also, you'll sometimes encounter an external file system with unknown IDs because the files were created on other machines. That means you'll have to deal with unknown users. Extracting archives can do this, also.

Thanks Ta0kira,

yes you are right, it seems like I dont have permission rights to access passwd file as I tried /etc/passwd and I got a message saying permission denied.

However, those two 0's were the return values.

I have also tried getgrgid and it printing group id but I want to print a group name of the group ID.
like my user id is 34008 and user name is LinuxInfo so I want to print User Name.

Thanks.
 
Old 02-25-2009, 01:05 PM   #7
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dwhitney67 View Post
This works for me:
Code:
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <stdio.h>

int main()
{
  struct passwd* pwd = getpwuid( getuid() );
  struct group*  grp = getgrgid(pwd->pw_gid);

  printf("user's name  = %s\n", pwd->pw_name);
  printf("user's group = %s\n", grp->gr_name);
}
Of course, I left off the error checking as an exercise for the OP to complete.
Thanks dwhitne,

I tried the above code and I am getting two warning and 1 error. Dont know how to debug. Below are the warning and error I am getting:

cc: Warning: Lab2b.c, line 14: In the initializer for pwd, "getpwuid(...)" of type "int", is being converted to "pointer to struct passwd".
struct passwd* pwd = getpwuid( getuid() );
------------------------^
cc: Warning: Lab2b.c, line 15: In the initializer for grp, "pwd" is a pointer to an incomplete struct or union and should not be used as the left operand of
mber dereference.
struct group* grp = getgrgid(pwd->pw_gid);
---------------------------------^
cc: Error: Lab2b.c, line 15: In the initializer for grp, "pw_gid" is not a member of "pwd".
struct group* grp = getgrgid(pwd->pw_gid);
---------------------------------^
 
Old 02-25-2009, 01:30 PM   #8
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
LinuxInfo

The program I posted compiles/links on my Linux system that has GCC 4.2.0.

If you are using a different OS (e.g. Solaris, Windoze, etc.), then you may want to check if those library functions are available. Reference the man-page(s) or other documentation for your system.

Btw, I used the following statement to compile the program:
Code:
/usr/bin/gcc -Wall -ansi -pedantic -std=gnu99 userinfo.c

Last edited by dwhitney67; 02-25-2009 at 01:33 PM.
 
Old 02-25-2009, 01:39 PM   #9
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dwhitney67 View Post
LinuxInfo

The program I posted compiles/links on my Linux system that has GCC 4.2.0.

If you are using a different OS (e.g. Solaris, Windoze, etc.), then you may want to check if those library functions are available. Reference the man-page(s) or other documentation for your system.

Btw, I used the following statement to compile the program:
Code:
/usr/bin/gcc -Wall -ansi -pedantic -std=gnu99 userinfo.c
Hi dwhitney,

I figured out the problem and its perfectly printing my user name but for the group name its giving core dump. So, if I ignore the printf statement for user's group its working fine but when I include printf statement for user's group I am getting core dump.

any idea how to fix this.

printf("user's name = %s\n", pwd->pw_name);
/*printf("user's group = %s\n", grp->gr_name);*/


Thanks and I really apprecitae everyone's help.
 
Old 02-25-2009, 01:48 PM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
LinuxInfo

If you are unable to compile/link with the following:
Code:
...
struct group*  grp = getgrgid(pwd->pw_gid);
...
then I do not know what to say. You should always verify that getgrgid() returns a non-null pointer before attempting to dereference the pointer.

However, if you cannot build your application with the statement above, the pointer issue is moot.

Does the man-page (on your system) for getpwuid() show any related functions (in the SEE ALSO section)?
 
Old 02-25-2009, 01:54 PM   #11
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by dwhitney67 View Post
LinuxInfo

If you are unable to compile/link with the following:
Code:
...
struct group*  grp = getgrgid(pwd->pw_gid);
...
then I do not know what to say. You should always verify that getgrgid() returns a non-null pointer before attempting to dereference the pointer.

However, if you cannot build your application with the statement above, the pointer issue is moot.

Does the man-page (on your system) for getpwuid() show any related functions (in the SEE ALSO section)?
No, only thing I know is integer is being converted into pointer and I think that is the problem I am facing right now.
 
Old 02-25-2009, 01:57 PM   #12
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Take a look here: http://linux.die.net/man/3/getgrgid
 
Old 02-25-2009, 02:50 PM   #13
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Dwhitney67 is correct on all counts.

I suspect you're seeing "0" simply because your system doesn't happen to have a user "1533" or a group "20". Or perhaps your login doesn't have permission to access the user database.

Either way, here's DWhitney67's example With (rudimentary) error checking:
Code:
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

int main()
{
  /* We read these values from "stat()" */
  int user_id = 1533;
  int group_id = 20;

  /* We're going to get these strings from getpwuid() and getgrgid () */
  struct passwd* pwd = getpwuid( getuid() );
  struct group*  grp = getgrgid(pwd->pw_gid);

  if ((pwd = getpwuid (1533)) == NULL)
    perror ("Unable to read user info");
  else
    printf("user's name  = %s\n", pwd->pw_name);
  if ((grp = getgrgid (group_id)) == NULL)
    perror ("Unable to read group info");
  else
    printf("user's group = %s\n", grp->gr_name);
  return 0;
}
My output is:
Quote:
Unable to read user info!: No such file or directory
user's group = games
Another problem is if you create a file created on a system where group id 20 happens to be "games", then copy that file to a second system, there's no guarantee that second system will have a group "games", nor that "games" will happen to be group ID 20.

In other words, the group id's and file id's in a file are completely non-portable between different hosts. Unless you "wrap" the files in a container like "tar", "cpio" or "zip" that contains owner and group info as "metadata".

'Hope that helps .. PSM

Last edited by paulsm4; 02-25-2009 at 02:52 PM.
 
Old 02-25-2009, 03:13 PM   #14
LinuxInfo
LQ Newbie
 
Registered: Sep 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Hi Paul and Dwhitney67,

Thanks a lot for all your help. Even I think the problem is with permissions. But, it was really great that I atleast found the way of doing it. I really appreciate your help.

Thanks again.
 
Old 02-25-2009, 07:36 PM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
LinuxInfo

The program I posted earlier worked under my account, which is not root (or sudo) privileged.

You may want to take a look at the /etc/passwd and /etc/group files on your system; both files should have the following permissions:
Code:
-rw-r--r--  1 root     root        947 2009-01-05 10:55 group
-rw-r--r--  1 root     root       2157 2009-01-05 10:55 passwd
Also, it would be nice if you would specify which flavor of Linux you are using.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LQ Profile Modification?? ajeetraina LQ Suggestions & Feedback 4 06-01-2008 03:24 AM
Scheduler modification LinuxNewbieX Programming 3 05-02-2005 08:23 AM
RAID modification jdt Linux - General 0 11-16-2003 03:54 PM
Jpeg Modification Endy Programming 0 02-17-2003 04:13 PM
modification to .Xmodmap butthead Programming 0 02-01-2002 08:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration