Advertisement
Guest User

Untitled

a guest
Nov 3rd, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include <pwd.h>
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11.  
  12. #define osErrorFatal(userMsg) osErrorFatalImpl((userMsg), __FILE__, __func__, __LINE__)
  13. #define osAssert(expr, userMsg) \
  14.     do { \
  15.         if (!(expr)) \
  16.             osErrorFatal(userMsg); \
  17.     } while(0)
  18.  
  19. static const char* osUsage = "Usage: ./userinfo user_name";
  20.  
  21. void osErrorFatalImpl(const char *userMsg, const char *fileName,
  22.                       const char *functionName, const int lineNum);
  23. bool osWriteUserInfo(const char* username, FILE* out);
  24.  
  25. int main(int argc, char** argv)
  26. {
  27.     osAssert(argc == 2, osUsage);
  28.     osAssert(osWriteUserInfo(argv[1], stdout),"Writing info failed");
  29.  
  30.     exit(EXIT_SUCCESS);
  31. }
  32.  
  33. bool osWriteUserInfo(const char* username, FILE* out)
  34. {
  35.     struct passwd* userInfo = getpwnam(username);
  36.     if (username == NULL)
  37.         return false;
  38.  
  39.     fprintf(out, "Username: %s\n", username);
  40.     //fprintf(out, "Password: %s\n", userInfo->pw_passwd);
  41.     fprintf(out, "Home directory: %s\n", userInfo->pw_dir);
  42.     fprintf(out, "User ID: %jd\n", (intmax_t)userInfo->pw_uid);
  43.     fprintf(out, "Group ID: %jd\n", (intmax_t)userInfo->pw_gid);
  44.     fprintf(out, "User information: %s\n", userInfo->pw_gecos);
  45.     //fprintf(out, "Shell program: %s\n", userInfo->pw_shell);
  46.  
  47.     return !ferror(out);
  48. }
  49.  
  50. void osErrorFatalImpl(const char *userMsg, const char *fileName,
  51.                       const char *functionName, const int lineNum) {
  52.     perror(userMsg);
  53.     fprintf(stderr, "File: '%s'\nFunction: '%s'\nLine: '%d'\n", fileName, functionName, lineNum);
  54.  
  55.     exit(EXIT_FAILURE);
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement