Finish 6.

This commit is contained in:
Saturneric 2020-12-09 18:08:25 +08:00
parent dbf45fa592
commit 9a22d818b0
7 changed files with 167 additions and 0 deletions

25
6/etc/group.c Normal file
View File

@ -0,0 +1,25 @@
#include <grp.h>
#include <stdio.h>
int main(void){
struct group *p_gp = getgrnam("sudo");
if(p_gp == NULL) {
printf("getgrgid error\n");
return -1;
}
printf("%d %s", p_gp->gr_gid, p_gp->gr_name);
char **p_men = p_gp->gr_mem;
while(*p_men != NULL){
printf(" %s", *p_men);
p_men++;
}
printf("\n");
return 0;
}

19
6/etc/pwd.c Normal file
View File

@ -0,0 +1,19 @@
#include <pwd.h>
#include <stdio.h>
int main(void) {
struct passwd *p_pwd = getpwnam("eric");
printf("%d %d %s %s %s\n", p_pwd->pw_uid, p_pwd->pw_gid, p_pwd->pw_name, p_pwd->pw_shell, p_pwd->pw_passwd);
while((p_pwd = getpwent()) != NULL) {
printf("%d %d %s %s\n", p_pwd->pw_uid, p_pwd->pw_gid, p_pwd->pw_name, p_pwd->pw_shell);
}
endpwent();
return 0;
}

14
6/etc/spwd.c Normal file
View File

@ -0,0 +1,14 @@
#include <shadow.h>
#include <stdio.h>
int main(void) {
struct spwd *p_spwd = getspnam("eric");
printf("%s %s %ld %lu\n", p_spwd->sp_namp, p_spwd->sp_pwdp, p_spwd->sp_flag, p_spwd->sp_max);
printf("%ld %ld %ld\n", p_spwd->sp_min, p_spwd->sp_inact, p_spwd->sp_warn);
endspent();
return 0;
}

15
6/etc/uname.c Normal file
View File

@ -0,0 +1,15 @@
#include <sys/utsname.h>
#include <stdio.h>
int main(void) {
struct utsname utsn;
if(uname(&utsn) < 0) {
printf("uname error\n");
return -1;
}
printf("%s %s %s %s\n", utsn.sysname, utsn.nodename, utsn.machine, utsn.release);
printf("%s\n", utsn.version);
return 0;
}

36
6/time/clock.c Normal file
View File

@ -0,0 +1,36 @@
#include <sys/time.h>
#include <time.h>
#include <stdio.h>
int main(void) {
struct timespec tsp;
if(clock_gettime(CLOCK_REALTIME, &tsp) < 0) {
printf("clock_gettime error\n");
return -1;
}
printf("real time: %lu %lu\n", tsp.tv_sec, tsp.tv_nsec);;
if(clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tsp) < 0) {
printf("clock_gettime error\n");
return -1;
}
printf("cpu time: %lu %lu\n", tsp.tv_sec, tsp.tv_nsec);;
if(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &tsp) < 0) {
printf("clock_gettime error\n");
return -1;
}
printf("thread time: %lu %lu\n", tsp.tv_sec, tsp.tv_nsec);;
if(clock_getres(CLOCK_REALTIME, &tsp) < 0) {
printf("clock_getres error\n");
return -1;
}
printf("real time res: %lu %lu\n", tsp.tv_sec, tsp.tv_nsec);;
return 0;
}

50
6/time/format.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <time.h>
void printTm(struct tm *ptm) {
printf("%d-%d-%d\n", 1900 + ptm->tm_year, 1 + ptm->tm_mon, ptm->tm_mday);
printf("%d:%d:%d\n", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
printf("\n");
}
int main(void) {
time_t t = time(NULL);
struct tm * ptm = gmtime(&t);
printTm(ptm);
ptm = localtime(&t);
printTm(ptm);
t = mktime(ptm);
printf("time: %lu\n", t);
char buf[1024];
size_t wbit = strftime(buf , sizeof(buf), "%D %T %r %Z", ptm);
if(wbit == 0) {
printf("strftime error\n");
return -1;
}
printf("format: %s\n", buf);
sprintf(buf, "09/04/00 1:00:00");
strptime((const char *)buf, "%x %X", ptm);
wbit = strftime(buf , sizeof(buf), "%D %T %r %Z", ptm);
if(wbit == 0) {
printf("strftime error\n");
return -1;
}
printf("check format: %s\n", buf);
return 0;
}

8
6/time/time.c Normal file
View File

@ -0,0 +1,8 @@
#include <time.h>
#include <stdio.h>
int main(void) {
time_t t = time(NULL);
printf("%lu\n", t);
return 0;
}