《C++ Primer第五版》读书笔记(15)--Specialized Library Facilities
时间:2014-03-25 21:51:01
收藏:0
阅读:601
System Data files and information
Password File
POSIX.1 defines two functions to fetch entries from the password file. These functions allow us to look up an entry given a user’s login name or numerical user ID.
#include <pwd.h> struct passwd *getpwuid(uid_t uid); struct passwd *getpwnam(const char *name); Both return: pointer if OK, NULL on error
在pwd.h里面定义了passwd结构体
/* The passwd structure. */
struct passwd
{
char *pw_name; /* Username. */
char *pw_passwd; /* Password. */
__uid_t pw_uid; /* User ID. */
__gid_t pw_gid; /* Group ID. */
char *pw_gecos; /* Real name. */
char *pw_dir; /* Home directory. */
char *pw_shell; /* Shell program. */
};
linux 的passwd结构体有些变量和UNix有点不一样,少了
user access class
char *pw_class
next time to change password time_t pw_change
account expiration time
time_t pw_expire
#include <pwd.h>
#include <stdio.h>
int main()
{
struct passwd* p_passwd = NULL;
p_passwd = getpwnam("liuzjian");
printf("the user name:%s\n",p_passwd->pw_name);
printf("the numberical user ID:%u\n",p_passwd->pw_uid);
printf("the numberical group ID:%u\n",p_passwd->pw_gid);
printf("the comment field:%s\n",p_passwd->pw_gecos);
printf("the initial working directory:%s\n",p_passwd->pw_dir);
printf("the initial shell:%s\n",p_passwd->pw_shell);
/* printf("the user access class:%s\n",p_passwd->pw_class);
printf("the next time to change password:%d\n",p_passwd->pw_change);
printf("the account expiration time:%d\n",p_passwd->pw_expire);
*/ return 0;
}test result:root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
the user name:liuzjian
the numberical user ID:1000
the numberical group ID:1000
the comment field:liuzjian,,,
the initial working directory:/home/liuzjian
the initial shell:/bin/bash
#include <pwd.h> struct passwd *getpwent(void); Returns: pointer if OK,NULL on error or end of file void setpwent(void); void endpwent(void);
getpwent()是获得执行该程序的用户的passwd 结构体
#include <pwd.h>
#include <stdio.h>
int main()
{
struct passwd* p_passwd = NULL;
setpwent();
p_passwd = getpwent();
printf("the user name:%s\n",p_passwd->pw_name);
printf("the numberical user ID:%u\n",p_passwd->pw_uid);
printf("the numberical group ID:%u\n",p_passwd->pw_gid);
printf("the comment field:%s\n",p_passwd->pw_gecos);
printf("the initial working directory:%s\n",p_passwd->pw_dir);
printf("the initial shell:%s\n",p_passwd->pw_shell);
endpwent();
return 0;
}
The function setpwent re winds whatever files it uses, and endpwent closes these files. When using getpwent , we must always be sure to close these files by calling endpwent when we’re through. Although getpwent is
smart enough to know when it has to open its files (the first time we call it), it never knows when we’re through.
test result:
root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./b.out
the user name:root
the numberical user ID:0
the numberical group ID:0
the comment field:root
the initial working directory:/root
the initial shell:/bin/bash
the user name:root
the numberical user ID:0
the numberical group ID:0
the comment field:root
the initial working directory:/root
the initial shell:/bin/bash
APUE 给出的getpwnam的一个实现:
/************************************************************
just a implementation of getpwname function
*************************************************************/
#include "pwd.h"
#include "stdef.h"
#include "string.h"
struct passwd*
getpwname(const char*name)
{
struct passwd* ptr = NULL;
setpwent();
while((ptr = getpwent()) != NULL)
{
if((strcmp(name,ptr->pw_name)) == 0)
{
break;
}
}
endpwent(); return ptr;}
Shadow Passwords
linux下spwd的结构体定义
/* Structure of the password file. */
struct spwd
{
char *sp_namp; /* Login name. */
char *sp_pwdp; /* Encrypted password. */
long int sp_lstchg; /* Date of last change. */
long int sp_min; /* Minimum number of days between changes. */
long int sp_max; /* Maximum number of days between changes. */
long int sp_warn; /* Number of days to warn user to change
the password. */
long int sp_inact; /* Number of days the account may be
inactive. */
long int sp_expire; /* Number of days since 1970-01-01 until
account expires. */
unsigned long int sp_flag; /* Reserved. */
};#include <shadow.h> struct spwd *getspnam(const char *name); struct spwd *getspent(void);Both return: pointer if OK, NULL on error
void setspent(void); void endspent(void);
test code:
#include <stdio.h> #include <shadow.h> int main() { struct spwd* p_spwd = NULL; if((p_spwd = getspnam("liuzjian")) == NULL) { printf("getspnam error\n"); } printf("user login name :%s\n",p_spwd->sp_namp); printf("encrypted password: %s\n",p_spwd->sp_pwdp); printf("day since Epoch of last password change :%ld\n",p_spwd->sp_lstchg); printf("days until change allowed :%ld\n",p_spwd->sp_min); printf("days before change required:%ld\n",p_spwd->sp_max); printf("days warning for expiration:%ld\n",p_spwd->sp_warn); printf("days before account inactive:%ld\n",p_spwd->sp_inact); printf("days since Epoch when account expires:%ld\n",p_spwd->sp_expire); printf("reserved :%lu\n",p_spwd->sp_flag); return 0; }test result:
root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
user login name :jasonleaster
encrypted password: $1$EI.fFLyC$3ogyqNY162jmeWntAMdVE1
day since Epoch of last password change :15896
days until change allowed :0
days before change required:99999
days warning for expiration:7
days before account inactive:-1
days since Epoch when account expires:-1
reserved :18446744073709551615
user login name :jasonleaster
encrypted password: $1$EI.fFLyC$3ogyqNY162jmeWntAMdVE1
day since Epoch of last password change :15896
days until change allowed :0
days before change required:99999
days warning for expiration:7
days before account inactive:-1
days since Epoch when account expires:-1
reserved :18446744073709551615
Group File
linux 下grp.h 中 group结构体的定义
/* The group structure. */ struct group { char *gr_name; /* Group name. */ char *gr_passwd; /* Password. */ __gid_t gr_gid; /* Group ID. */ char **gr_mem; /* Member list. */ };
These fields are contained in a group structure that is defined in <grp.h>.
#include <grp.h> struct group *getgrgid(gid_t gid); struct group *getgrnam(const char *name);Both return: pointer if OK, NULL on error
#include <grp.h> struct group *getgrent(void); Returns: pointer if OK,NULL on error or end of file void setgrent(void); void endgrent(void);
The setgrent function opens the group file, if it’s not already open, and rewinds it. The getgrent function reads the next entry from the group file, opening the file first, if it’s not already open. The endgrent function closes the group file.
#include <stdio.h> #include <grp.h> int main() { struct group* p_group = NULL; if((p_group = getgrnam("liuzjian")) == NULL) { printf("getgrnam error\n"); } printf("group name :%s\n",p_group->gr_name); printf("encrypted passwd:%s\n",p_group->gr_passwd); printf("numberical group ID:%d\n",p_group->gr_gid); printf("array of pointers of individual user names :%s\n",*(p_group->gr_mem)); return 0; }
test result:
root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
group name :jasonleaster
encrypted passwd:x
numberical group ID:1000
array of pointers of individual user names :(null)
group name :jasonleaster
encrypted passwd:x
numberical group ID:1000
array of pointers of individual user names :(null)
The field gr_mem is an array of pointers to the user names that belong to this group. This array is terminated by a null pointer
Other Data Files
The general principle is that every data file has at least three functions:
1.A get function that reads the next record, opening the file if necessary .
2.A set function that opens the file, if not already open, and rewinds the file.
3.An end entry that closes the data file.
System Identification
linux下面ustname 结构体的定义
/* Structure describing the system and machine. */
struct utsname
{
/* Name of the implementation of the operating system. */
char sysname[_UTSNAME_SYSNAME_LENGTH];
/* Name of this node on the network. */
char nodename[_UTSNAME_NODENAME_LENGTH];
/* Current release level of this implementation. */
char release[_UTSNAME_RELEASE_LENGTH];
/* Current version level of this release. */
char version[_UTSNAME_VERSION_LENGTH];
/* Name of the hardware type the system is running on. */
char machine[_UTSNAME_MACHINE_LENGTH];
#if _UTSNAME_DOMAIN_LENGTH - 0
/* Name of the domain of this node on the network. */
# ifdef __USE_GNU
char domainname[_UTSNAME_DOMAIN_LENGTH];
# else
char __domainname[_UTSNAME_DOMAIN_LENGTH];
# endif
#endif
};
Each string is null terminated. The maximum name lengths, including the terminating null byte.
/***************************************************************************
code writer :EOF
code date : 2014.03.25
e-mail : jasonleaster@gmail.com
code purpose:
just a demo for uname function. I would like to share my code with
you. If you find something thing wrong with my code, please touche me
by e-mail. Thank you!
****************************************************************************/
#include <stdio.h>
#include <sys/utsname.h>
#include <stdlib.h>
int main()
{
struct utsname* p_utsname = NULL;
if((p_utsname = (struct utsname*)malloc(sizeof(struct utsname))) == NULL)
{
printf("malloc error\nprocess end\n");
return 0;
}
if((uname(p_utsname)) < 0)
{
printf("uname error\n");
}
else
{
printf("name of the operating system:%s\n",p_utsname->sysname);
printf("name of this node:%s\n",p_utsname->nodename);
printf("current release of operating system:%s\n",p_utsname->release);
printf("current version of this release:%s\n",p_utsname->version);
printf("name of hard ware type:%s\n",p_utsname->machine);
}
free(p_utsname);
return 0;
}
test result:root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
name of the operating system:Linux
name of this node:ubuntu
current release of operating system:3.8.0-19-generic
current version of this release:#29-Ubuntu SMP Wed Apr 17 18:16:28 UTC 2013
name of hard ware type:x86_64
hostname function:
#include <unistd.h>
int gethostname(char *name,int namelen);
Returns: 0 if OK,?1 on error
test code:
/***************************************************************************
code writer :EOF
code date : 2014.03.25
e-mail : jasonleaster@gmail.com
code purpose:
just a demo for gethostname function. I would like to share my code with
you. If you find something thing wrong with my code, please touche me
by e-mail. Thank you!
****************************************************************************/
#include <unistd.h>
#include <stdio.h>
int main()
{
char buffer[BUFSIZ];
if(gethostname(buffer,BUFSIZ) != 0)
{
printf("gethostname error\n");
}
else
{
printf("host name:%s\n",buffer);
}
return 0;
}
test result:root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
host name:ubuntu
host name:ubuntu
Time and Date Routines
/* Used by other time functions. */ struct tm { int tm_sec; /* Seconds. [0-60] (1 leap second) */ int tm_min; /* Minutes. [0-59] */ int tm_hour; /* Hours. [0-23] */ int tm_mday; /* Day. [1-31] */ int tm_mon; /* Month. [0-11] */ int tm_year; /* Year - 1900. */ int tm_wday; /* Day of week. [0-6] */ int tm_yday; /* Days in year.[0-365] */ int tm_isdst; /* DST. [-1/0/1]*/ # ifdef __USE_BSD long int tm_gmtoff; /* Seconds east of UTC. */ const char *tm_zone; /* Timezone abbreviation. */ # else long int __tm_gmtoff; /* Seconds east of UTC. */ const char *__tm_zone; /* Timezone abbreviation. */ # endif };
#define __TIME_T_TYPE __SYSCALL_SLONG_TYPE
__STD_TYPE __TIME_T_TYPE __time_t; /* Seconds since the Epoch. */
__STD_TYPE __SYSCALL_SLONG_TYPE __syscall_slong_t;
在linux里面找了几个文件,发现上面这些“蛛丝马迹”,找到syscall_slong_t 就找不到下文了。。。但是可以很明显的看出time_t 是个长整型数据。。。
#include <stdio.h> #include <time.h> int main() { time_t buffer[BUFSIZ]; printf("%ld\n",time(buffer)); return 0; }
root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
1395738306
我始终没明白,这串数字神马意思,有大神路过帮忙解释一下。。。
#include <sys/time.h> int clock_gettime(clockid_t clock_id,struct timespec *tsp); Returns: 0 if OK,?1 on error
#include <sys/time.h> int clock_getres(clockid_t clock_id,struct timespec *tsp); Returns: 0 if OK,?1 on error
#include <sys/time.h> int clock_settime(clockid_t clock_id,const struct timespec *tsp); Returns: 0 if OK,?1 on error
#include <sys/time.h> int gettimeofday(struct timeval *restrict tp ,void *restrict tzp); Returns: 0 always
#include <time.h> struct tm *gmtime(const time_t *calptr); struct tm *localtime(const time_t *calptr); Both return: pointer to broken-down time, NULL on error
#include <time.h> time_t mktime(struct tm *tmptr); Returns: calendar time if OK, ?1 on error
#include <time.h> size_t strftime(char *restrictbuf,size_tmaxsize, const char *restrict format , const struct tm *restrict tmptr); size_t strftime_l(char *restrict buf,size_tmaxsize, const char *restrict format , const struct tm *restrict tmptr,locale_t locale); Both return: number of characters stored in array if room, 0 otherwise
strtime 函数参数列表中变量 format的选项。。。下表
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
time_t buffer[BUFSIZ];
struct tm* p_tm = NULL;
time(buffer);
if((p_tm = (struct tm*)malloc(sizeof(struct tm))) == NULL)
{
printf("malloc error\nprocess end\n");
return 0;
}
mktime(p_tm);
printf("%s\n",asctime(p_tm));
printf("%s\n",ctime(buffer));
free(p_tm);
return 0;
}root@ubuntu:/Ad_Pro_in_Unix/chapter_6# ./a.out
Sun Dec 31 00:00:00 1899
Tue Mar 25 17:55:44 2014
我始终觉得这个输出有问题,但是布吉岛为什吗。。。是1899年。。。非常欢迎feedback。。。
Sun Dec 31 00:00:00 1899
Tue Mar 25 17:55:44 2014
我始终觉得这个输出有问题,但是布吉岛为什吗。。。是1899年。。。非常欢迎feedback。。。
《C++ Primer第五版》读书笔记(15)--Specialized Library Facilities,布布扣,bubuko.com
原文:http://blog.csdn.net/davidwang9527/article/details/22082657
评论(0)