当前位置: 技术问答>linux和unix
哪位高手帮我看看下面linux函数调用问题,谢谢。
来源: 互联网 发布时间:2016-10-30
本文导语: 这边的警告我已经列出来了 getpwnam 函数有两种用法即struct passwd *getpwnam(const char *name); 和 struct passwd *getpwuid(uid_t uid); 为什么当我用struct passwd *getpwuid(uid_t uid);这种申明方式的用法是就会有警告,而且运行结...
这边的警告我已经列出来了 getpwnam 函数有两种用法即struct passwd *getpwnam(const char *name); 和
struct passwd *getpwuid(uid_t uid); 为什么当我用struct passwd *getpwuid(uid_t uid);这种申明方式的用法是就会有警告,而且运行结果只有一句?????
运行结果:Segmentation fault
警告:
//getname.c:26: warning: passing argument 1 of ‘getpwnam’ makes pointer from integer without a cast
#include
#include
#include
#include
#include
int main(void)
{
char *login;
struct passwd *pentry;
uid_t uid;
uid =getuid();
/* Get the login name */
if((login = getlogin()) == NULL) { /* oops */
perror("getlogin");
exit(EXIT_FAILURE);
}
/* Get the password entry for login */
// struct passwd *getpwnam(const char *name);
//struct passwd *getpwuid(uid_t uid);
//if((pentry = getpwnam(login)) == NULL)
if((pentry = getpwnam(uid)) == NULL)
{
perror("getpwnam");
exit(EXIT_FAILURE);
}
/* Display the password entry */
printf("user name: %sn", pentry->pw_name);
printf("UID : %dn", pentry->pw_uid);
printf("GID : %dn", pentry->pw_gid);
printf("gecos : %sn", pentry->pw_gecos);
printf("home dir : %sn", pentry->pw_dir);
printf("shell : %sn", pentry->pw_shell);
exit(EXIT_SUCCESS);
}
struct passwd *getpwuid(uid_t uid); 为什么当我用struct passwd *getpwuid(uid_t uid);这种申明方式的用法是就会有警告,而且运行结果只有一句?????
运行结果:Segmentation fault
警告:
//getname.c:26: warning: passing argument 1 of ‘getpwnam’ makes pointer from integer without a cast
#include
#include
#include
#include
#include
int main(void)
{
char *login;
struct passwd *pentry;
uid_t uid;
uid =getuid();
/* Get the login name */
if((login = getlogin()) == NULL) { /* oops */
perror("getlogin");
exit(EXIT_FAILURE);
}
/* Get the password entry for login */
// struct passwd *getpwnam(const char *name);
//struct passwd *getpwuid(uid_t uid);
//if((pentry = getpwnam(login)) == NULL)
if((pentry = getpwnam(uid)) == NULL)
{
perror("getpwnam");
exit(EXIT_FAILURE);
}
/* Display the password entry */
printf("user name: %sn", pentry->pw_name);
printf("UID : %dn", pentry->pw_uid);
printf("GID : %dn", pentry->pw_gid);
printf("gecos : %sn", pentry->pw_gecos);
printf("home dir : %sn", pentry->pw_dir);
printf("shell : %sn", pentry->pw_shell);
exit(EXIT_SUCCESS);
}
|
getpwnam和getpwuid对于参数要求不同,前者要求是char*,后者要求是uid_t型。
//if((pentry = getpwnam(login)) == NULL)
if((pentry = getpwnam(uid)) == NULL) --> if((pentry = getpwuid(uid)) == NULL)
//if((pentry = getpwnam(login)) == NULL)
if((pentry = getpwnam(uid)) == NULL) --> if((pentry = getpwuid(uid)) == NULL)
|
GETPWNAM(3) Linux Programmer's Manual GETPWNAM(3)楼主你看错了,用getpwuid就行了
NAME
getpwnam, getpwnam_r, getpwuid, getpwuid_r - get password file entry
SYNOPSIS
#include
#include
struct passwd *getpwnam(const char *name);
struct passwd *getpwuid(uid_t uid);
int getpwnam_r(const char *name, struct passwd *pwbuf,
char *buf, size_t buflen, struct passwd **pwbufp);
int getpwuid_r(uid_t uid, struct passwd *pwbuf,
char *buf, size_t buflen, struct passwd **pwbufp);