#include <pwd.h> struct passwd *getpwuid(uid_t uid); struct passwd *getpwnam(const char *name); Both return: pointer if OK, NULL on error
/* 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. */
};
#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:#include <pwd.h> struct passwd *getpwent(void); Returns: pointer if OK,NULL on error or end of file void setpwent(void); void endpwent(void);
#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;
}
/************************************************************
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;}
/* 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);
#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:
/* 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. */ };
#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);
#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; }
/* 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
};
/***************************************************************************
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:#include <unistd.h>
int gethostname(char *name,int namelen);
Returns: 0 if OK,?1 on error
/***************************************************************************
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:/* 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 };
#include <stdio.h> #include <time.h> int main() { time_t buffer[BUFSIZ]; printf("%ld\n",time(buffer)); return 0; }
#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
#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;
}《C++ Primer第五版》读书笔记(15)--Specialized Library Facilities,布布扣,bubuko.com
《C++ Primer第五版》读书笔记(15)--Specialized Library Facilities
原文:http://blog.csdn.net/davidwang9527/article/details/22082657