在给出用户登录名或数值用户ID后,这两个函数就能查看相关项。

#include <sys/types.h>
#include <pwd.h> struct passwd *getpwuid(uid_t uid);
struct passwd *getpwnam(const char *name);
两个函数返回值:成功返回指针,出错返回NULL
uid:需要获取信息的uid号

getpwuid例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
uid_t my_uid; struct passwd *my_info;
my_info = getpwuid(getuid()); printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

getpwuid

getpwnam例程

 #include <pwd.h>
#include <sys/types.h>
#include <stdio.h> int main()
{
char *username = "pi";
struct passwd *my_info;
my_info = getpwnam(username);
if(!my_info)
{
perror("getpwnam error");
exit();
} printf("my name = [%s]\n", my_info->pw_name);
printf("my passwd = [%s]\n", my_info->pw_passwd);
printf("my uid = [%d]\n", my_info->pw_uid);
printf("my gid = [%d]\n", my_info->pw_gid);
printf("my gecos = [%s]\n", my_info->pw_gecos);
printf("my dir = [%s]\n", my_info->pw_dir);
printf("my shell = [%s]\n", my_info->pw_shell); return ;
}

getpwnam

也有些程序要查看整个口令文件。

#include <sys/types.h>
#include <pwd.h> struct passwd *getpwent(void);
返回值:成功指针,出错或达文件尾端,返回NULL
void setpwent(void);
void endpwent(void);

apue例程

getpwent

有另一组函数可用于访问阴影口令文件

#include <shadow.h>

struct spwd *getspnam(const char *name);
struct spwd *getspent(void);
两个函数返回值:成功返回指针,出错NULL
void setspent(void);
vpid endspent(void);

查看组名或数值组ID

#include <sys/types.h>
#include <grp.h> struct group *getgrgid(gid_t gid);
struct group *getgrnam(const char *name);
返回值:成功指针,出错NULL

针对口令文件的3个函数

#include <sys/types.h>
#include <grp.h> struct group *getgrent(void);
返回值:成功指针,出错或达到文件尾端,返回NULL
void setgrent(void);
void endgrent(void);

获取和设置附属组ID

#include <sys/types.h>
#include <unistd.h>
int getgroups(int size, gid_t list[]);
返回值:成功返回附属组ID数量,出错-
#include <grp.h>
int setgroups(size_t size, const gid_t *list);
两个函数的返回值:成功0,出错-

返回与主机和操作系统有关的信息

#include <sys/utsname.h>

int uname(struct utsname *buf);
返回值:成功非负值,出错-

返回主机名

#include <unistd.h>

int gethostname(char *name, int namelen);
返回值:成功0,出错-

时间部分

time函数返回当前时间和日期

#include <time.h>

time_t time(time_t *t);
返回值:成功返回时间值,出错-

把时间表示为秒和纳秒

#include <time.h>

int clock_gettime(clockid_t clk_id, struct timespec *tp);
返回值:成功0,出错-

clock_getres函数把参数

#include <time.h>

int clock_getres(clockid_t clk_id, struct timespec *res);
返回值:成功0,出错-

要对特定的时钟设置时间,可以调用clock_settime函数

#include <time.h>

int clock_settime(clockid_t clk_id, const struct timespec *tp);
返回值:成功0,出错-

SUSv4指定gettimeofday已经弃用,然而一些程序仍然使用这个函数,因为与time相比,提供了更高的精度

#include <sys/time.h>

int gettimeofday(struct timeval *tv, struct timezone *tz);
返回值:总是0
#include <time.h>

struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
返回值:指向分解的tm结构的指针,出错NULL

localtime和gmtime之间的区别是,localtime是转换成本地时间,gmtime是将时间结构分解成年月日时分秒周日。

以本地时间的年、月、日等作为参数,将其变换成time_t值

#include <time.h>

time_t mktime(struct tm *tm);
返回值:成功返回日历时间,出错-

类似printf的时间值的函数,通过多个参数来定制产生的字符串

#include <time.h>

size_t strftime(char s, size_t max, const char *format, const struct tm *tm);
返回值:若有空间,返回存入数组的字符数,否则返回0

字符串时间转换成分解时间

#include <time.h>

char *strptime(const char *s, const char *format, struct tm *tm);
返回值:指向上次解析的字符的下一个字符的指针,否则返回NULL

最新文章

  1. 20145334赵文豪 《Java程序设计》第4周学习总结
  2. cocos2d-x 3.2 listview scorllview 等容器在小米华为等部分手机显示泛白解决
  3. Linux-第一天
  4. H2Database数据类型
  5. bash shell命令行选项与修传入参数处理
  6. JavaWeb与Asp.net工作原理比较分析
  7. 深入理解final和static关键字
  8. 正则验证,match()与test()函数的区别?
  9. 采用Global.asax的Application_BeginRequest事件过滤敏感字符
  10. Java - 数组排序 -- 浅析稳定性与复杂度
  11. spring4笔记----web.xml中2.4以上版本Listener的配置
  12. Andrew NG 机器学习编程作业6 Octave
  13. 关于http以及aphace配置https
  14. stark组件开发之添加功能实现
  15. perl _DATA_ 文件句柄
  16. SQLSTATE[42000]
  17. Java并发编程的艺术(五)——中断
  18. Spring 开发环境搭建(二)
  19. [转]oracle在删除表\表空间\用户时,如何释放磁盘空间
  20. hdu-4023-博弈(模拟)

热门文章

  1. 【BZOJ3473&amp;BZOJ3277】字符串(广义后缀自动机)
  2. paper 164: Siamese网络--相似度量方法
  3. 防止NSTimer和调用对象之间的循环引用
  4. 【系统架构理论】一篇文章精通:Spring Cloud Netflix Eureka
  5. arcpy 常用操作
  6. jenkins clone代码时间太长怎么办?
  7. zabbix配置-模板
  8. C++怎样通过嵌入汇编写一个函数
  9. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ
  10. HBase最佳实践-读性能优化策略