Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdint.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #define osErrorFatal(userMsg) osErrorFatalImpl((userMsg), __FILE__, __func__, __LINE__)
- #define osAssert(expr, userMsg) \
- do { \
- if (!(expr)) \
- osErrorFatal(userMsg); \
- } while(0)
- static const char* osUsage = "Usage: ./userinfo user_name";
- void osErrorFatalImpl(const char *userMsg, const char *fileName,
- const char *functionName, const int lineNum);
- bool osWriteUserInfo(const char* username, FILE* out);
- int main(int argc, char** argv)
- {
- osAssert(argc == 2, osUsage);
- osAssert(osWriteUserInfo(argv[1], stdout),"Writing info failed");
- exit(EXIT_SUCCESS);
- }
- bool osWriteUserInfo(const char* username, FILE* out)
- {
- struct passwd* userInfo = getpwnam(username);
- if (username == NULL)
- return false;
- fprintf(out, "Username: %s\n", username);
- //fprintf(out, "Password: %s\n", userInfo->pw_passwd);
- fprintf(out, "Home directory: %s\n", userInfo->pw_dir);
- fprintf(out, "User ID: %jd\n", (intmax_t)userInfo->pw_uid);
- fprintf(out, "Group ID: %jd\n", (intmax_t)userInfo->pw_gid);
- fprintf(out, "User information: %s\n", userInfo->pw_gecos);
- //fprintf(out, "Shell program: %s\n", userInfo->pw_shell);
- return !ferror(out);
- }
- void osErrorFatalImpl(const char *userMsg, const char *fileName,
- const char *functionName, const int lineNum) {
- perror(userMsg);
- fprintf(stderr, "File: '%s'\nFunction: '%s'\nLine: '%d'\n", fileName, functionName, lineNum);
- exit(EXIT_FAILURE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement