1. About Man commandThe man command uses option-K to search online help based on keywords, but only one keyword lookup is supported. The online manual is divided into a number of summaries, man followed by a summary number to see the different chapters of Help content, such as: Man 3 read
2. System calls to use
1> Open
2> Read
3> Close
4> about Unix time
①time_t Data Typetypedef long int time_t; Used to store the number of seconds from 1970 to the present.
②timeval Structural Bodystruct Timeval
{
Long tv_sec; /* SEC * *
Long tv_usec; /* microsecond * *
};
Structure struct Timeval, it is precise to subtle.
③tm Structural Bodystruct TM
{
int tm_sec; /* seconds, normal range 0-59, but allowed to 61*/
int tm_min; * * minutes, 0-59*/
int tm_hour; * * hours, 0-23*/
int tm_mday; /* Day, that is, the first day of one months, 1-31*/
int Tm_mon; /* month, from January, 0-11*/1+p->tm_mon;
int tm_year; * * year, from 1900 so far has been many years * * * 1900+ p->tm_year;
int tm_wday; * * Week, the first day of the week, from Sunday, 0-6*/
int tm_yday; * * FROM January 1 this year to the current number of days, range 0-365*/
int tm_isdst; * * Daylight Saving Time flag/
};
It is particularly noteworthy that the year has been years since 1900, rather than direct storage such as 2011, the month starting from 0, 0 means January, week is also starting from 0, 0 for Sunday, 1 for Monday.
④ commonly used time functions:#include <time.h>
1. Asctime
Char *asctime (const struct tm* timeptr); The time that the information in the structure is converted to the real world, displayed as a string
2. CTime
Char *ctime (const time_t* TIMEP); Convert TIMEP to really world time, in string display, it differs from asctime in the form of parameters passed in, converted format: "Wed June 21:49:08 1993\n"
3. Difftime
Double Difftime (time_t time1, time_t time2); Returns the number of seconds that differ by two times
4. Gettimeofday int gettimeofday (struct timeval* tv,struct timezone*);
Returns the number of seconds and subtleties in the current distance of 1970, and the TZ is the time zone, generally not
5. Gmtime
struct tm* gmtime (const time_t* TIMEP); Converts the time represented by time_t to UTC without time zone conversion, and is a struct TM structure pointer
6. LocalTime
struct tm* localtime (const time_t* TIMEP); Similar to Gmtime, but it is time to transition through time zone.
7. Mktime
time_t mktime (struct tm* timeptr); Converts the time of the struct TM structure to the number of seconds from 1970 to the present
8. Time
time_t time (time_t* t); Gets the number of seconds since January 1, 1970. 9. Strftime
size_t strftime (char *s, size_t max, const char *format, const struct TM *TM); Converts the TM structure body to a string. Strptime
Char *strptime (const char *s, const char *format, struct TM *tm); Converts a string to a TM structure body. ⑤ a piece of code:
#define _xopen_source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main (void)
{
struct TM TM;
Char buf[255];
memset (&tm, 0, sizeof (struct TM));
Strptime ("2001-11-12 18:31:01", "%y-%m-%d%h:%m:%s", &TM);
Strftime (buf, sizeof (BUF), "%d%b%Y%h:%m", &TM);
Puts (BUF);
Exit (exit_success);
}
5> creat
6> Write
7> Lseek
3. Who's preparation
/* Who2.c-read/etc/utmp and list info therein *-Suppresses empty records *-formats time nicely * #include <stdio.h> #include <unistd.h> #include <utmp.h> #include <FCN
tl.h> #include <time.h>/* #define Showhost/void Showtime (Long);
void Show_info (struct utmp *); int main () {struct utmp utbuf; /* Read info into here/int utmpfd; * Read from this descriptor/if ((UTMPFD = open (Utmp_file, o_rdonly)) = = 1) {perror (Utmp_fi
LE);
Exit (1);
While read (UTMPFD, &utbuf, sizeof (utbuf)) = = sizeof (UTBUF)) Show_info (&UTBUF);
Close (UTMPFD);
return 0; } * * Show info () * Displays the contents of the utmp struct * in human Readable form * * Displays nothing if record has no User name/void show_info (struct utmp *utbufp) {if (Utbufp->ut_type!= user_process)
Return printf ("%-8.8s", utbufp->ut_name); * * the logname */printf (""); /* a space */printf ("%-8.8s", utbufp->ut_line); /* The TTY */printf (""); /* A space * * Showtime (Utbufp->ut_time); /* Display time/#ifdef showhost if (utbufp->ut_host[0]!= ' and ') printf ("(%s)", Utbufp-> Ut_host);/* The host */#endif printf ("\ n"); /* NewLine/} void Showtime (Long timeval) * * Displays time in a format fit for human consumption * Uses CTime to builds a string then picks parts out of it * Note:%12.12s prints a string chars wide and LIMITS *
it to 12chars. * * {char *cp; /* To hold address of * * CP = CTime (&timeval);
/* Convert time to string///////////* looks like /* Mon Feb 4 00:46:40 EST 1991 * * 0123456789012345 . */printf ("%12.12s", cp+4); /* Pick chars from POS 4 */}
4. CP Writing
/** cp1.c * Version 1 of cp-uses read and write with tunable buffer size * * USAGE:CP1 src dest * * #include <stdio.h> #include <unistd.h> #include <fcntl.h> #define BUFFERSIZE 4096 #de
Fine Copymode 0644 void oops (char *, char *);
Main (int AC, char *av[]) {int in_fd, OUT_FD, N_chars;
Char Buf[buffersize];
/* Check args */if (AC!= 3) {fprintf (stderr, "Usage:%s source destination\n", *av);
Exit (1);
}/* Open files/if (In_fd=open (av[1), o_rdonly) = = 1) oops ("Cannot open", av[1));
if ((Out_fd=creat (av[2], copymode)) = = 1) oops ("Cannot creat", av[2)); /* Copy files/while (N_chars = Read (IN_FD, buf, buffersize)) > 0) if (write (OUT_FD, b
UF, N_chars)!= n_chars) oops ("Write error to", Av[2]); if (N_chars = = 1) oops ("Read error from", Av[1]); /* Close Files */if (Close (in_fd) = = 1 | | close (OUT_FD) = = 1) oops ("Error Closing Files", "")
;
} void Oops (char *s1, char *s2) {fprintf (stderr, "Error:%s", S1);
Perror (S2);
Exit (1); }
5. Write Terminal logoff code
#include <utmp.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h >
#include <string.h>
int logout_tty (char * line)
{
int fd;
struct UTMP rec;
int len = sizeof (struct utmp);
int retval =-1;
if (fd = open (Utmp_file, o_rdwr)) = = 1)
return-1;
while (read (FD, &rec, Len) ==len)
if (strncmp (rec.ut_line, line, sizeof (rec.ut_line)) = = 0)
{
Rec.ut _type = dead_process;
if (Lseek (FD,-len, Seek_cur)!=-1)
if (write (FD, &rec, len) = len)
retval = 0;
break;
if (Close (FD) = = 1)
retval =-1;
return retval;
}