User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,698 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,500 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Jun 28th, 2005
Views: 3,061
This Program gives the UserDetails of the logged in User.
a) The RealName
b) Login Name ( Why do we need this?)
c) Home Directory
d) Default Login Shell

Additional Purpose of this code would be to invoke/ learn at man getpwuid and about struct passswd
c Syntax | 4 stars
  1. /* Program to get the Details of the user like User's Real Name, Username and Shell */
  2.  
  3. #include <pwd.h> /* getpwdid */
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <stdio.h>
  7.  
  8. void user_details(void);
  9.  
  10. int main(void)
  11. {
  12. user_details();
  13.  
  14. return 0;
  15. }
  16.  
  17. void user_details(void)
  18. {
  19. struct passwd *passwd; /* man getpwuid */
  20. passwd = getpwuid ( getuid()); /* Get the uid of the running processand use it to get a record from /etc/passwd */
  21.  
  22. printf("\n The Real User Name is %s ", passwd->pw_gecos);
  23. printf("\n The Login Name is %s ", passwd->pw_name);
  24. printf("\n The Home Directory is %s", passwd->pw_dir);
  25. printf("\n The Login Shell is %s ", passwd->pw_shell);
  26. printf("\n The Passwd is %s ", getpwuid(getuid()) >pw_passwd);
  27. printf("\n The uid is %lu ", (unsigned long) getpwuid(getuid())->pw_uid);
  28. printf("\n The gid is %lu \n\n", (unsigned long) getpwuid(getuid())->pw_gid);
  29. }
  30.  
  31. /* Program to get the Details of the user like User's Real Name, Username and Shell */
  32.  
  33. #include <pwd.h> /* getpwdid */
  34. #include <sys/types.h>
  35. #include <unistd.h>
  36. #include <stdio.h>
  37.  
  38. void user_details(void);
  39.  
  40. int main(void)
  41. {
  42. user_details();
  43.  
  44. return 0;
  45. }
  46.  
  47. void user_details(void)
  48. {
  49. struct passwd *passwd; /* man getpwuid */
  50. passwd = getpwuid ( getuid()); /* Get the uid of the running processand use it to get a record from /etc/passwd */
  51.  
  52. printf("\n The Real User Name is %s ", passwd->pw_gecos);
  53. printf("\n The Login Name is %s ", passwd->pw_name);
  54. printf("\n The Home Directory is %s", passwd->pw_dir);
  55. printf("\n The Login Shell is %s ", passwd->pw_shell);
  56. printf("\n The Passwd is %s ", getpwuid(getuid())->pw_passwd);
  57. printf("\n The uid is %lu ", (unsigned long) getpwuid(getuid())->pw_uid);
  58. printf("\n The gid is %lu \n\n", (unsigned long) getpwuid(getuid())->pw_gid);
  59. }
  60.  
  61.  
Comments (Newest First)
bumsfeld | Posting Shark | Aug 17th, 2005
Twice is better? You must be shiddin us!
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 1:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC