第六章 系統資料檔案和資訊:
#include <pwd.h>
struct passwd * getpwuid(uid_t uid);
ret is a static valuable
struct passwd * getpwnam(const char * name);
ret is a static valuable
struct passwd * getpwent(void);
ret is NULL, means EOF or error, and it is a static valuable
void setpwent(void);
void endpwent(void);
struct passwd {
char * pw_name;
char * pw_passwd;
...
};
#include <shadow.h>
struct spwd * getspnam(const char * name);
ret is a static valuable
struct spwd * getspent(void);
ret is NULL, means EOF or error, and it is a static valuable
void setspent(void);
void endspent(void);
struct spwd {
char * sp_namp;
char * sp_pwdp;
...
};
#include <grp.h>
struct group * getgrgid(gid_t gid);
ret is a static valuable
struct group * getgrnam(const char * name);
ret is a static valuable
struct group * getgrent(void);
ret is NULL meams EOF or error, and it is a static valuable
void setgrent(void);
void endgrent(void);
struct group {
char * gr_name;
char * gr_passwd;
int gr_gid;
char ** gr_mem;
};
#include <unistd.h>
int getgroups(int gidsetsize, gid_t grouplist[]);
ret is 0 means error,
if gidsetsize is 0, will return the group-total-size
else copy groups to grouplist, and return copy-counts
#include <grp.h> /* Linux */
#include <unistd.h> /* FreeBSD, Mac OS X, Solaris */
int setgroups(int ngroups, const gid_t grouplist[]);
ret is 0 means success and -1 means error
ngroups must <= NGROUPS_MAX
only superuser can call this function
#include <grp.h> /* Linux, Solaris */
#include <unistd.h> /* FreeBSD, Mac OS X */
int initgroups(const char * username, gid_t basegid);
ret is 0 means success and -1 means error
it call setgroups inside
only superuser can call this function
#include <sys/utsname.h>
int uname(struct utsname * name);
ret is -1 means error
struct utsname {
char sysname[];
char nodename[];
char release[];
char version[];
char machine[];
};
#include <unistd.h>
int gethostname(char * name, int namelen);
ret is 0 means success and -1 means error
HOST_NAME_MAX: 64-65(Linux), 256-257(Others)
#include <time.h>
time_t time(time_t * calptr);
if calptr not NULL, will copy ret to calptr
ret is -1 means error
#include <sys/time.h>
int gettimeofday(struct timeval * restrict tp, void * restrict tzp);
we must pass NULL to tzp
alway return 0
struct timeval {
time_t tv_sec /* seconds */
long tv_usec; /* microseconds */
};
#include <time.h>
struct tm * gmtime(const time_t * calptr);
ret point to a static tm object
struct tm * localtime(const time_t * calptr);
ret point to a static tm object
struct tm {
int tm_sec; /* [0-60] */
int tm_min; /* [0-59] */
int tm_hour; /* [0-23] */
int tm_mday; /* [1-31] */
int tm_mon; /* [0-11] */
int tm_year; /* since 1970 */
int tm_wday; /* [0-6] */
int tm_yday; /* [0-365] */
int tm_isdst; /* <0, 0, >0 */
};
#include <time.h>
time_t mktime(struct tm * tmptr);
ret is -1 means error
#include <time.h>
char * asctime(const struct tm * tmptr);
ret point to a static block
char * ctime(const time_t * calptr);
ret point to a static block
#include <time.h>
size_t strftime(char * restrict buf, size_t maxsize,
const char * restrict format,
const struct tm * restrict tmptr);
ret is conv-size, 0 show failed
示例:
#include <stdio.h>
#include <pwd.h>
struct passwd *
my_getpwnam(const char * name)
{
struct passwd * ptr;
setpwent();
while ((ptr = getpwent()) != NULL) {
if (strcmp(ptr->pw_name, name) == 0) {
break;
}
}
endpwent();
return ptr;
}
void
test(const char * name, const char * funcname,
struct passwd * get(const char *))
{
struct passwd * ptr;
if ((ptr = get(name)) == NULL) {
printf("%s failed\n", funcname);
}
else {
printf("name: %s, password: %s\n", ptr->pw_name,
ptr->pw_passwd == NULL || ptr->pw_passwd[0] == '\0'
? "(null)" : ptr->pw_passwd);
}
}
int
main(void)
{
const char * const name = "adwardink";
test(name, "getpwnam", getpwnam);
test(name, "my_getpwnam", my_getpwnam);
return 0;
}
#include <stdio.h>
#include <shadow.h>
struct spwd *
my_getspnam(const char * name)
{
struct spwd * ptr;
setspent();
while ((ptr = getspnam(name)) != NULL) {
if (strcmp(ptr->sp_namp, name) == 0) {
break;
}
}
endspent();
return ptr;
}
void
test(const char * name, const char * funcname,
struct spwd * get(const char *))
{
struct spwd * ptr;
if ((ptr = get(name)) == NULL) {
printf("%s failed\n", funcname);
}
else {
printf("name: %s, password: %s\n", ptr->sp_namp,
ptr->sp_pwdp == NULL || ptr->sp_pwdp[0] == '\0'
? "(null)" : ptr->sp_pwdp);
}
}
int
main(void)
{
const char * const name = "adwardink";
test(name, "getspnam", getspnam);
test(name, "my_getspnam", my_getspnam);
return 0;
}
#include <stdio.h>
#include <grp.h>
struct group *
my_getgrnam(const char * name)
{
struct group * ptr;
setgrent();
while ((ptr = getgrent()) != NULL) {
if (strcmp(ptr->gr_name, name) == 0) {
break;
}
}
endgrent();
return ptr;
}
void
test(const char * name, const char * funcname,
struct group * get(const char *))
{
struct group * ptr;
if ((ptr = get(name)) == NULL) {
printf("%s failed\n", funcname);
}
else {
printf("name: %s, password: %s\n", ptr->gr_name,
ptr->gr_passwd == NULL || ptr->gr_passwd[0] == '\0'
? "(null)" : ptr->gr_passwd);
}
}
void
my_print(const char * format, const char * info)
{
printf(format, info == NULL || info[0] == '\0' ? "(null)" : info);
}
void
showgroup(void)
{
struct group * ptr;
char ** mem;
setgrent();
while ((ptr = getgrent()) != NULL) {
my_print("group name: %s\n", ptr->gr_name);
my_print("password: %s\n", ptr->gr_passwd);
/* my_print("group id: %d\n", ptr->gr_gid); */
mem = ptr->gr_mem;
while (mem != NULL && *mem != NULL) {
my_print("user name: %s\n", *mem);
++mem;
}
}
endgrent();
}
int
main(void)
{
const char * const name = "adwardink";
test(name, "getgrnam", getgrnam);
test(name, "my_getgrnam", my_getgrnam);
showgroup();
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/utsname.h>
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX (280)
#endif
int
main(void)
{
struct utsname uts;
char hostname[HOST_NAME_MAX];
if (uname(&uts) == -1) {
printf("uname error\n");
}
else {
printf("sysname: %s\n", uts.sysname);
printf("nodename: %s\n", uts.nodename);
printf("release: %s\n", uts.release);
printf("version: %s\n", uts.version);
printf("machine: %s\n", uts.machine);
}
if (gethostname(hostname, HOST_NAME_MAX) == -1) {
printf("gethostname error\n");
}
else {
printf("hostname: %s\n", hostname);
}
return 0;
};
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
void
gettime(void)
{
struct timeval tv;
struct tm * ptm;
gettimeofday(&tv, NULL);
if ((ptm = localtime(&tv.tv_sec)) == NULL) {
printf("localtime error\n");
return;
}
printf("microtime: %02d:%02d:%02d:%ld\n", ptm->tm_hour, ptm->tm_min,
ptm->tm_sec, tv.tv_usec);
}
void
testtimefuncs(void)
{
typedef struct tm * (*Fgettm)(const time_t *);
Fgettm fgtm[2] = { gmtime, localtime };
const char * funcname[2] = { "gmtime", "localtime" };
time_t pt;
struct tm * ptm;
char * ptr;
int i;
if (time(&pt) == -1) {
printf("time error\n");
}
else {
printf("time: %ld\n", pt);
}
if ((ptr = ctime(&pt)) == NULL) {
printf("ctime error\n");
}
else {
printf("ctime: %s", ptr);
}
for (i = 0; i < sizeof(funcname) / sizeof(funcname[0]); ++i) {
if ((ptm = fgtm[i](&pt)) == NULL) {
printf("%s error\n", funcname[i]);
}
else {
printf("%s ok\n", funcname[i]);
if ((ptr = asctime(ptm)) == NULL) {
printf("asctime error\n");
}
else {
printf("asctime: %s", ptr);
}
if (mktime(ptm) == pt) {
printf("mktime(%s(pt)) == pt\n", funcname[i]);
}
else {
printf("mktime(%s(pt)) != pt\n", funcname[i]);
}
}
}
}
int
main(void)
{
gettime();
testtimefuncs();
return 0;
}
#include <stdio.h>
#include <time.h>
int
main(void)
{
time_t caltime;
struct tm * ptm;
const int MAXLINE = 64;
char line[MAXLINE];
if ((caltime = time(NULL)) == -1) {
printf("time error\n");
}
if ((ptm = localtime(&caltime)) == NULL) {
printf("localtime error\n");
}
if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm) == 0) {
printf("strftime error\n");
}
fputs(line, stdout);
return 0;
}
#include <stdio.h>
#include <time.h>
int
main(void)
{
time_t caltime;
struct tm * ptm1;
struct tm * ptm2;
const int MAXLINE = 80;
char line[MAXLINE];
if ((caltime = time(NULL)) == -1) {
printf("time error\n");
}
if ((ptm1 = localtime(&caltime)) == NULL) {
printf("localtime error\n");
}
if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm1) == 0) {
printf("strftime error\n");
}
fputs(line, stdout);
fputs("sleep 2 minutes...\n", stdout);
sleep(2 * 60);
if ((caltime = time(NULL)) == -1) {
printf("time error\n");
}
if ((ptm2 = localtime(&caltime)) == NULL) {
printf("localtime error\n");
}
if (strftime(line, MAXLINE, "%a %b %d %X %Z %Y\n", ptm2) == 0) {
printf("strftime error\n");
}
fputs(line, stdout);
if (ptm1 == ptm2) {
printf("static\n");
}
else {
printf("non-static\n");
}
return 0;
}