getlogin() c 函数返回 NULL 和错误“没有这样的文件或目录"; [英] getlogin() c function returns NULL and error "No such file or directory"

查看:31
本文介绍了getlogin() c 函数返回 NULL 和错误“没有这样的文件或目录";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 getlogin() 函数 () 的问题.我尝试使用此函数从 c 程序中获取我帐户的登录名.但该函数返回 NULL.使用 perror 显示错误是没有这样的文件或目录".

I have a question regarding the getlogin() function (). I tried to get the login name of my account from the c program using this function. But the function returns a NULL. Using perror shows that the error is "No such file or directory".

我不明白有什么问题.有没有办法在程序中获取用户登录名.

I don't get what is the problem. Is there a way to get user login name in a program.

这是一个示例代码:

#include <stdio.h>
#include <unistd.h>

int main()
{
  char *name;
  name = getlogin();
  perror("getlogin() error");
  //printf("This is the login info: %s\n", name);
  return 0;
}

这是输出:getlogin() error: No such file or directory

请告诉我如何正确处理.

Please let me know how to get this right.

谢谢.

推荐答案

getlogin 是一种不安全且已弃用的确定登录用户的方法.它可能试图打开登录用户的记录,可能是 utmp 或其他东西.确定您正在运行的用户的正确方法(可能与登录用户不同,但几乎总是更好地使用)是 getpwuid(getuid()).

getlogin is an unsafe and deprecated way of determining the logged-in user. It's probably trying to open a record of logged-in users, perhaps utmp or something. The correct way to determine the user you're running as (which might not be the same as the logged-in user, but is almost always better to use anyway) is getpwuid(getuid()).

这篇关于getlogin() c 函数返回 NULL 和错误“没有这样的文件或目录";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆