gpt4 book ai didi

c - 输入无效用户时 getpwnam() 使程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:11 25 4
gpt4 key购买 nike

getuserinfo() 中,如果行 struct passwd *theUser = getpwnam(username); 尝试使用不存在的用户名,程序将以-1 的误差。它永远不会到达函数的错误处理部分。它没有返回到 main,我不确定为什么。

它应该返回 NULL 并打印出错误消息,然后让程序的其余部分尝试运行。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>


//awk 'BEGIN { FS=":"; print "User\t\tUID\n--------------------"; } { print $1,"\t\t",$3;} END { print "--------------------\nAll Users and UIDs Printed!" }' /etc/passwd
#define SHELLSCRIPT "\
#/bin/bash \n\
awk 'BEGIN { FS=\":\"; print \"User\t\tUID\n--------------------\"; } { print $1,\"\t\t\",$3;} END { print \"--------------------\nAll Users and UIDs Printed!\" }' /etc/passwd \n\
"



struct passwd *getuserinfo(char *username)
{
//Set errno to 0 so we can use it
errno = 0;

//create a variable to store the user info in
struct passwd *theUser = getpwnam(username);

//error check getpwnam()
if(theUser == NULL)
{
printf("getpwnam() ERROR, errno = %d", errno);
return NULL;
}

return theUser;
}

struct group *getgroupinfo(long int groupid)
{
//Set errno to 0 so we can use it
errno = 0;

//create a variable to store the group info in
struct group *theGroup = getgrgid(groupid);

//error check getgrgid()
if(theGroup == NULL)
{
printf("getgrgid() ERROR, errno = %d", errno);
return NULL;
}

return theGroup;
}

void displayusers()
{
system(SHELLSCRIPT);
}

/*
*
* 4. A main function which will
* Call getuserinfo passing a hard-coded username, and using the return object, display the user id. (10 points)
* Call getgroupinfo passing the return object from getuserinfo, and using the return object, display the user’s group name. (10 points)
* Call displayusers. (10 points)
*
* My main function does those things but I don't pass objects since the function prototypes you gave us
* don't accept that input, they want ints and strings
*/

int main(int argc, char **argv)
{
//Take in the username we want to look up
char username[50];
printf("Enter username: ");
scanf("%s", username);


//look up the user and get their info
struct passwd *theUser = getuserinfo(username);
//use the user's gid and look up its info
struct group *theGroup = getgroupinfo(theUser->pw_gid);

printf("User: %s\nUser ID: %d\nGroup Name: %s\n\n\n", theUser->pw_name, theUser->pw_uid, theGroup->gr_name);

displayusers();


return 0;
}

最佳答案

错误检查应该是这样的:

struct passwd *getuserinfo(char *username)
{


//define errno so we can use it
errno = 0;
//create a variable to store the user info in
struct passwd *theUser = getpwnam(username);

//error check getpwnam()
if(errno != 0)
{
perror("ERROR: getpwnam() FAIL");
}
//check if we got user data/if the user existed
else if(theUser == NULL)
{
fprintf(stderr, "ERROR: User does not exist!\n");
return NULL;
}

return theUser;
}

需要使用使用 stderr 的 fprintf 来确保始终显示错误消息。它在 printf 缓冲区中丢失了,keithmo 关于使用 fflush(NULL) 的建议解决了这个问题。

errno 用于检查调用是否成功。然后我们确保 theUser != NULL 因为即使缺少用户,调用也会成功。如果它为 NULL,我们会向用户打印一条错误消息,以便他们知道。

我们现在根据 t0mm13b 在主函数中调用 getgroupinfo 之前还会进行错误检查

//look up the user and get their info
struct passwd *theUser = getuserinfo(username);
//use the user's gid and look up its info
struct group *theGroup;
if(theUser != NULL)
{
//get the user's group id
theGroup = getgroupinfo(theUser->pw_gid);
//print the user and group info
printf("User: %s\nUser ID: %d\nGroup Name: %s\n\n\n", theUser->pw_name, theUser->pw_uid, theGroup->gr_name);
}
else
{
fprintf(stderr, "ERROR: No group info, missing user!\n");
}


displayusers();

关于c - 输入无效用户时 getpwnam() 使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com