Linux 哪个system 函数可以得到我自己 和 什么 tty

acange 2007-08-30 04:23:11
Linux 哪个system C 函数可以得到我自己的login名字 和 什么 tty.
我可以用 "who am i" 在shell 下得到:
jack pts/3 Aug 27 13:51 (machine name).

有没有对应的 system 函数 可以来调用得到:
1:) 我的名字. 比如: jack
2:) 哪个 tty. 比如: pts/3

我不想用getenv("USER")之类的通过env变量, 因为这些变量很有可能没有设好.

谢谢了.
...全文
71 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
acange 2007-08-30
  • 打赏
  • 举报
回复
tthyname(0) to get the tty

stat(proc/pid) to get stat strtuctm, and get uid from stat_str.st_uid,
then getpwuid(uid)
plusboy 2007-08-30
  • 打赏
  • 举报
回复
如果想要在C语言中用,可以用popen()调用uname -n
当然更好的方法是用getpwent()函数返回一个passwd结构,查找pw_uid与getuid()返回值相同的passswd结构,
然后取其pw_name成员。
plusboy 2007-08-30
  • 打赏
  • 举报
回复
uname -n
tty
hzcpig 2007-08-30
  • 打赏
  • 举报
回复
#include <stdio.h>

int main()
{
printf("Log name: \n");
system("who am i | awk '{print $1}'");

printf("Tty : \n");
system("who am i | awk '{print $2}'");

return 0;
}
cceczjxy 2007-08-30
  • 打赏
  • 举报
回复
#include <unistd.h>
#include <stdio.h>
int main()
{
char buf[1024],*ptr;
memset(buf,0,sizeof(buf));

ptr=getlogin();
printf("%s\n",ptr);

ptr=getlogin_r(buf, sizeof(buf));
printf("%s,%s\n",ptr,buf);
memset(buf,0,sizeof(buf));

ptr=cuserid(buf);
printf("%s,%s\n",ptr,buf);
}



这个是找用户名的

23,110

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧