APUE第六章学习笔记

前端之家收集整理的这篇文章主要介绍了APUE第六章学习笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一:口令文件的操作

  1. /*****************************************
  2. struct passwd成员:
  3. struct passwd
  4. {
  5. char* pw_name; // 用户名
  6. char* pw_passwd; //加密口令
  7. uid_t pw_uid; //数值用户ID
  8. gid_t pw_gid; // 数值组ID
  9. char* pw_gecos; //注释字段
  10. char* pw_dir; //初始工作目录
  11. char* pw_shell; //初始shell(用户程序)
  12. /*************下列平台可能没有**************/
  13. char* pw_class; //用户访问类
  14. time_t pw_change; //下次更改口令时间
  15. time_t pw_expire; //账户有效期时间
  16. };
  17. *****************************************/
  1. /******************************************************
  2. 包含头文件: #include <pwd.h>
  3. 函数原型: struct passwd* getpwuid(uid_t uid);
  4. 函数说明: 通过用户ID获取口令文件
  5. 返回值: 若成功,返回指针,若出错,返回NULL
  6. *******************************************************/
  1. /*******************************************************
  2. 包含头文件: #include <pwd.h>
  3. 函数原型: struct passwd* getpwuid(uid_t uid);
  4. 函数说明: 通过用户ID获取口令文件
  5. 返回值: 若成功,返回NULL
  6. ********************************************************/
  1. /**********************************************************
  2. 包含头文件: #include <pwd.h>
  3. 函数原型: struct passwd* getpwnam(const char* name);
  4. 函数说明: 通过用户名获取口令文件
  5. 返回值: 若成功,若失败,返回NULL
  6. **********************************************************/
  7.  
  8. /***********************************************************
  9. 包含头文件: #include <pwd.h>
  10. 函数原型: struct passwd* getpwent(void);
  11. 函数说明: 逐项遍历口令文件,返回当前口令文件
  12. 返回值: 返回当前口令文件项,若到达文件尾,则返回NULL
  13. ***********************************************************/
  1. /***********************************************************
  2. 包含头文件: #include <pwd.h>
  3. 函数原型: void setpwent(void);
  4. 函数说明:回绕到口令文件首项
  5. **********************************************************/
  1. /**********************************************************
  2. 包含头文件: #include <pwd.h>
  3. 函数原型: void endpwent(void);
  4. 函数说明: 关闭口令文件
  5. *************************************************************/

vi 6.1.c

  1. #include <pwd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/stat.h>
  5.  
  6. int main(int argc,char* argv[])
  7. {
  8. if (argc != 2)
  9. {
  10. printf("add <pathname>\n");
  11. exit(0);
  12. }
  13.  
  14. struct stat statbuf;
  15.  
  16. if (lstat(argv[1],&statbuf) < 0)
  17. {
  18. printf("lstat error\n");
  19. exit(0);
  20. }
  21.  
  22. struct passwd* pw;
  23.  
  24. if ((pw = getpwuid(statbuf.st_uid)) == NULL)
  25. {
  26. printf("getpwuid error\n");
  27. exit(0);
  28. }
  29.  
  30. printf("用户名: %s\n",pw->pw_name);
  31. printf("加密口令: %s\n",pw->pw_passwd);
  32. printf("用户ID: %d\n",pw->pw_uid);
  33. printf("组ID: %d\n",pw->pw_gid);
  34. printf("初始工作目录: %s\n",pw->pw_dir);
  35. printf("初始shell: %s\n",pw->pw_shell);
  36. return 0;
  37. }

vi 6.1.1.c

  1. #include <pwd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct passwd* pw;
  8.  
  9. if ((pw = getpwnam("root")) == NULL)
  10. {
  11. printf("getpwnam error\n");
  12. exit(0);
  13. }
  14.  
  15. printf("用户名: %s\n",pw->pw_gid);
  16. printf("注释字段: %s\n",pw->pw_gecos);
  17. printf("初始工作目录: %s\n",pw->pw_shell);
  18. exit(0);
  19. }

vi 6.2.c

  1. #include <pwd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void)
  6. {
  7. setpwent();
  8.  
  9. struct passwd *pw;
  10.  
  11. while ((pw = getpwent()) != NULL)
  12. {
  13. printf("用户名: %s\n",pw->pw_passwd);
  14. printf("用户ID:%d\n",pw->pw_dir);
  15. printf("初始shell: %s\n\n",pw->pw_shell);
  16. }
  17.  
  18. endpwent();
  19. }

二. 阴影口令文件

阴影口令文件项结构体

  1. /***********************************************************
  2. struct spwd
  3. {
  4. char* sp_namp; //用户登录
  5. char* sp_pwdp; //加密口令
  6. int sp_lstchg; //上次更改口令以来经过的时间
  7. int sp_min; //经多少天后允许更改
  8. int sp_max; //要求更改尚余天数
  9. int sp_warn; //超期警告天数
  10. int sp_inact; //账户不活动之前尚余天数
  11. int sp_expire; //账户超期天数
  12. unsigned int sp_flag; //保留
  13. }
  14. *********************************************************/
  1. /***********************************************************
  2. 包含头文件: #include <shadow.h>
  3. 函数原型: struct spwd* getspname(const char* name);
  4. 函数说明: 得到阴影口令文件
  5. 返回值: 若成功,返回指针,返回NULL
  6. **********************************************************/

vi 6.3.c

  1. #include <shadow.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct spwd* spw;
  8.  
  9. if ((spw = getspnam("root")) == NULL)
  10. {
  11. printf("getspnam error\n");
  12. exit(0);
  13. }
  14.  
  15. printf("用户名: %s\n",spw->sp_namp);
  16. printf("加密口令: %s\n",spw->sp_pwdp);
  17. printf("上次口令更改以来经过的时间: %ld\n",spw->sp_lstchg);
  18. printf("经过多少天后允许更改: %ld\n",spw->sp_min);
  19. printf("要求更改尚余天数: %ld\n",spw->sp_warn);
  20. printf("账户不活动前尚余天数: %ld\n",spw->sp_inact);
  21. printf("账户超期天数: %ld\n",spw->sp_expire);
  22. exit(0);
  23. }
  1. /***********************************************************
  2. 包含头文件: #include <shadow.h>
  3. 函数原型: void setspent();
  4. 函数说明: 回绕到文件开头项
  5. *******************************************************/
  1. /*******************************************************
  2. 包含头文件: #include <shadow.h>
  3. 函数原型: struct spwd* getspent();
  4. 函数说明: 打开阴影口令文件,并返回当前阴影口令文件
  5. *******************************************************/
  1. /********************************************************
  2. 包含头文件: #include <shadow.h>
  3. 函数原型: void endspent();
  4. 函数说明: 关闭阴影口令文件
  5. ********************************************************/

vi 6.4.c

  1. #include <shadow.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct spwd* spw;
  8.  
  9. setspent();
  10.  
  11. while ((spw = getspent()) != NULL)
  12. {
  13. printf("用户名: %s\n",spw->sp_pwdp);
  14. printf("上次更改口令以来经过的时间: %ld\n",spw->sp_lstchg);
  15. printf("经过多少天允许更改: %ld\n",spw->sp_max);
  16. printf("超期警告天数: %ld\n",spw->sp_warn);
  17. printf("账户不活动之前尚余天数: %ld\n",spw->sp_expire);
  18.  
  19. }
  20. return 0;
  21. }

三: 组文件

  1. /**********************************************************
  2. 文件结构体:
  3. struct group
  4. {
  5. char* gr_name; //组名
  6. char* gr_passwd; //加密口令
  7. int gr_gid; //数值组ID
  8. char **gr_mem; //指向各用户名指针的数组
  9. }
  10. ***********************************************************/
  1. /*******************************************************
  2. 包含头文件: #include <grp.h>
  3. 函数原型: struct group* getgrgid(gid_t gid);
  4. 函数说明: 通过组ID获得组文件组项
  5. 返回值: 若成功,返回组文件组项指针,返回NULL
  6. ********************************************************/

vi 6.5.c

  1. #include <grp.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. struct group* gr;
  8.  
  9. if ((gr = getgrgid(0)) == NULL)
  10. {
  11. printf("getgrgid error\n");
  12. exit(0);
  13. }
  14.  
  15. printf("组名: %s\n",gr->gr_name);
  16. printf("加密口令: %s\n",gr->gr_passwd);
  17. printf("组ID: %d\n",gr->gr_gid);
  18. printf("该组成员:\n");
  19. int i = 0;
  20.  
  21. while ((gr->gr_mem)[i] != NULL)
  22. {
  23. printf("组内用户: %s\n",(gr->gr_mem[i]));
  24. ++i;
  25. }
  26. return 0;
  27. }
  1. /***********************************************************
  2. 包含头文件: #include <grp.h>
  3. 函数原型: struct group* getgrnam(const char* name);
  4. 函数说明: 通过组ID获得组文件组项指针,返回
  5. NULL
  6. **********************************************************/
  1. #include <grp.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int main()
  6. {
  7. setgrent();
  8. struct group* gr;
  9.  
  10. while ((gr = getgrent()) != NULL)
  11. {
  12. printf("组名: %s\n",gr->gr_passwd);
  13. printf("组ID: %d\n",gr->gr_gid);
  14.  
  15. int i = 0;
  16.  
  17. while ((gr->gr_mem)[i] != NULL)
  18. {
  19. printf("组内用户名: %s\n",(gr->gr_mem[i]));
  20. ++i;
  21. }
  22.  
  23. }
  24. return 0;
  25. }
  1. /*********************************************************
  2. 包含头文件: #include <unistd.h>
  3. 函数原型:int getgroups(int gidsetsize,gid_t grouplist[]);
  4. 函数说明: getgroups将进程所属用户的各附属组ID,填写到数组grouplist中,填写入该数组附属组ID最多为gidsize个,实际填写附属组ID数由函数返回
  5. 作为一种特殊情况,如若gidsize为0,则函数返回附属组ID数,而对数组grouplist不做修改
  6. 返回值: 若成功,返回附属组ID数量,若出错,返回-1
  7. *********************************************************/

vi 6.9.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. int main()
  6. {
  7. gid_t * grouplist;
  8. int size;
  9.  
  10. if ((size = getgroups(0,grouplist)) == -1)
  11. {
  12. printf("getgroups error\n");
  13. exit(0);
  14. }
  15.  
  16. grouplist = (gid_t*)(malloc(size * sizeof(gid_t)));
  17. int get;
  18. if ((get = getgroups(size,grouplist)) == -1)
  19. {
  20. printf("getgroups error\n");
  21. exit(0);
  22. }
  23.  
  24. for (int i = 0; i < get; ++i)
  25. printf("附属组号: %d\n",grouplist[i]);
  26. return 0;
  27. }
  1. /***********************************************************
  2. 包含头文件: #include <grp.h> //on linux
  3. #include <unistd.h> // on FreeBSD,Mac Os X,
  4. and Solaris
  5. 函数原型: int setgroups(int ngroups,const gid_t
  6. grouplist[]);
  7. 函数说明:由超级用户调用以便为调用进程设置附属组ID表
  8. 返回值: 若成功,返回0,若出错,返回-1
  9. *******************************************************/
  1. /*********************************************************
  2. 包含头文件:#include <grp.h> // on linux and Solaris
  3. #include <unistd.h> // on FreeBSD and Mac Os X
  4. 函数原型: int initgroups(const char* username,gid_t basegid);
  5. 函数说明: 为用户初始化附属组ID表
  6. 返回值: 若成功,返回0,若失败,返回-1
  7. ****************************************************/
  1. /**********************************************************
  2. struct utsname
  3. {
  4. char sysname[]; //操作系统名称
  5. char nodename[]; //网络上的名称
  6. char release[]; //当前发布级别
  7. char version[]; //当前发布版本
  8. char machine[]; //当前硬件体系类型
  9. }
  10. **********************************************************/
  1. /***********************************************************
  2. 包含头文件: #include <sys/utsname.h>
  3. 函数原型: int uname(struct utsname* name);
  4. 函数说明: 得到主机和操作系统有关信息
  5. 返回值: 若成功,返回非负值,返回-1
  6. **********************************************************/

vi 6.9.1.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/utsname.h>
  4.  
  5. int main()
  6. {
  7. struct utsname name;
  8.  
  9. if (uname(&name) < 0)
  10. {
  11. printf("uname error\n");
  12. exit(0);
  13. }
  14.  
  15. printf("操作系统名称: %s\n",name.sysname);
  16.  
  17. printf("网络名称%s\n",name.nodename);
  18.  
  19. printf("当前发布级别: %s\n",name.release);
  20.  
  21. printf("当前发布版本: %s\n",name.version);
  22.  
  23. printf("当前硬件体系类型: %s\n",name.machine);
  24.  
  25. exit(0);
  26. }

vi 6.9.2.c

  1. /***********************************************************
  2. 包含头文件: #include <unistd.h>
  3. 函数原型: int gethostname(char* name,int namelen);
  4. 函数说明: 得到主机名,该主机名通常为TCP/IP网络主机的名字
  5. 返回值: 若成功,返回0,若失败,返回-1
  6. **********************************************************/
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define HOST_NAME_MAX 300
  6.  
  7. int main()
  8. {
  9. char hostname[HOST_NAME_MAX];
  10.  
  11. if (gethostname(hostname,HOST_NAME_MAX) < 0)
  12. {
  13. printf("gethostname error\n");
  14. exit(0);
  15. }
  16.  
  17. printf("主机名: %s\n",hostname);
  18. exit(0);
  19. }

四: 时间和例程

  1. /*************************************************************
  2. 包含头文件: #include <time.h>
  3. 函数原型: time_t time(time_t *calptr);
  4. 函数说明: 返回当前时间和日期
  5. 时间值作为函数值返回,如果参数非空,则时间值也存放
  6. 在由calptr指向的单元内
  7. 返回值: 若成功,返回时间值,若出错,返回-1
  8. *************************************************************/

vi 6.10.c

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. #define SIZE 100
  6.  
  7. int main()
  8. {
  9. time_t t = time(NULL);
  10.  
  11. char str[SIZE];
  12.  
  13. strftime(str,SIZE,"%Y %x %X\n",localtime(&t));
  14.  
  15. printf("当前时间: %s\n",str);
  16. return 0;
  17. }
  1. /*************************************************************
  2. 时钟通过clockid_t类型标识:
  3. 标识符 选项 说明
  4. CLOCK_REALTIME 实时系统时间
  5. CLOCK_MONTONIC _ POSIX_MONOTONIC_CLOCK 不带负跳数的实时系统时间
  6. CLOCK_PROCESS_cpuTIME_ID POSIXcpuTIME 调用进程的cpu时间
  7. CLOCK_THREAD_cpuTIME_ID POSIXTHREAD_cpuTIME 调用线程的cpu时间
  8. ***********************************************************/
  1. /*********************************************************
  2. 包含头文件: #include <sys/time.h>
  3. 函数原型: int colck_gettime(clockid_t clock_id,struct timespec *tsp);
  4. 函数说明: 获取指定时钟的时间
  5. 返回值: 若成功,返回-1
  6. *********************************************************/

vi 6.10.1.c

  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. char* get_time(clockid_t clockid,char* s)
  6. {
  7.  
  8. struct timespec ts;
  9.  
  10. if (clock_gettime(clockid,&ts) == -1)
  11. {
  12. printf("clock_gettime error\n");
  13. exit(0);
  14. }
  15.  
  16. time_t t = ts.tv_sec;
  17.  
  18. char str[100];
  19.  
  20. if (strftime(str,100,"%Y %x %X",localtime(&t)) == 0)
  21. {
  22. printf("strftime error\n");
  23. exit(0);
  24. }
  25. s = str;
  26. return s;
  27. }
  28.  
  29. int main()
  30. {
  31. char* s;
  32. printf("实时系统时间: %s\n",get_time(CLOCK_REALTIME,s));
  33. printf("不带负跳数的实时系统时间: %s\n",get_time(CLOCK_MONOTONIC,s));
  34. printf("调用进程的cpu时间: %s\n",get_time(CLOCK_PROCESS_cpuTIME_ID,s));
  35.  
  36. printf("调用线程的cpu时间: %s\n",get_time(CLOCK_THREAD_cpuTIME_ID,s));
  37. return 0;
  38. }
  1. /*********************************************************
  2. 包含头文件: #include <sys/time.h>
  3. 函数原型: int clock_getres(clockid_t clock_id,struct timespec *tsp);
  4. 函数说明:把参数tsp指向的timespec结构初始化为与
  5. clock_id参数对应的时钟精度
  6. ***********************************************************/
  1. /***********************************************************
  2. 包含头文件: #include <sys/time.h>
  3. 函数原型:int clock_settime(clockid_t clock_id,const
  4. struct timespec *tsp);
  5. 函数说明:对特定的时钟设置时间
  6. 返回值:若成功,返回0,若出错,返回-1
  7. ***********************************************************/
  1. /**********************************************************
  2. 包含头文件: #include <sys/time.h>
  3. 函数原型: int gettimeofday(struct timeval* restrict tp,void * restrict tzp);
  4. 函数说明: tzp唯一合法值是NULL,其他值将产生不确定的结果(某些平台支持用tzp说明时区)
  5. gettimeofday函数以距特定时间的秒数的方式将时间存放于tp指向的timeval结构
  6. **********************************************************/

vi 6.10.2

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define BUFSIZE 100
  6.  
  7. int main()
  8. {
  9. char buf[BUFSIZE];
  10. struct timespec* curr_ts = (struct timespec*)(malloc(sizeof(struct timespec)));
  11.  
  12. if (clock_gettime(CLOCK_REALTIME,curr_ts) < 0)
  13. {
  14. printf("clock_gettime error\n");
  15. free(curr_ts);
  16. exit(0);
  17. }
  18.  
  19. time_t curr = curr_ts->tv_sec;
  20.  
  21. strftime(buf,BUFSIZE,"%Y %x %X",localtime(&curr));
  22.  
  23. printf("未修改时的实时系统时间: %s\n",buf);
  24.  
  25. struct timespec *ts = (struct timespec*)(malloc(sizeof(struct timespec)));
  26. ts->tv_sec = curr + 60 * 3;
  27. ts->tv_nsec = curr_ts->tv_nsec;
  28.  
  29. if (clock_settime(CLOCK_REALTIME,ts) < 0)
  30. {
  31. printf("clock_settime error\n");
  32. free(curr_ts);
  33. free(ts);
  34. exit(0);
  35. }
  36.  
  37. time_t now = time(NULL);
  38. strftime(buf,"%Y %x %X\n",localtime(&now));
  39. printf("增加三分钟的实时系统时间: %s\n",buf);
  40. free(curr_ts);
  41. free(ts);
  42.  
  43. return 0;
  44. }
  1. /**********************************************************
  2. 结构体 struct tm
  3. {
  4. int tm_sec; //秒
  5. int tm_min; //分
  6. int tm_hour; //时
  7. int tm_mday; //日 of 月
  8. int tm_mon; //月
  9. int tm_year; //年
  10. int tm_wday; //日 of 周
  11. int tm_yady; //日 of 年
  12. int tm_isdst; //夏令时标识符 若为正,开启标识符,
  13. 若为0,关闭夏令时,若为负,未知
  14. }
  15. ********************************************************/
  1. /**********************************************************
  2. 包含头文件: #include <time.h>
  3. 函数原型: struct tm* localtime(const time_t *calptr);
  4. 函数说明: 将日历时间转换为本地时间
  5. 返回值: 若成功,返回结构体tm指针,若出错,返回NULL
  6. **********************************************************/
  1. /**********************************************************
  2. 包含头文件: #include <time.h>
  3. 函数原型: struct tm* gmtime(const time_t* calptr);
  4. 函数说明:将日历时间转化为协调统一时间
  5. 返回值: 若成功,返回结构体tm指针,返回NULL
  6. ***********************************************************/
  1. /*************************************************************
  2. 包含头文件: #include <time.h>
  3. 函数原型: time_t mktime(struct tm* tmptr);
  4. 函数说明: 将tm结构体指针分解成time_t并返回
  5. 返回值: 若成功,返回日历时间,若失败,返回-1
  6. ************************************************************/

vi 6.11.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define BUFSIZE 100
  6.  
  7. int main()
  8. {
  9. char buf[BUFSIZE];
  10.  
  11. struct tm mytm;
  12.  
  13. mytm.tm_sec = 00;
  14. mytm.tm_min = 59;
  15. mytm.tm_hour = 11;
  16. mytm.tm_mday =01;
  17. mytm.tm_mon = 04;
  18. mytm.tm_year = 98;
  19. mytm.tm_isdst = -1;
  20.  
  21. strftime(buf,&mytm);
  22.  
  23. printf("我的TM: %s\n",buf);
  24.  
  25. return 0;
  26. }

猜你在找的Bash相关文章