Tenth chapter System level I/O
Cp1
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h > #define BUFFERSIZE 4096//define memory capacity # Copymode 0644//Define the length of the copy void oops (char *, char *); int main (int argc, char *Argv[]) {int in_fd, OUT_FD, n_chars;//three descriptor values char buf[buffersize];//memory location/*CP parameters are two, respectively, the file to be copied, and the destination directory, so altogether there should be three operations Number so first check whether the value of ARGC is three, if not, return the standard error */if (argc! = 3{fprintf (stderr, "Usage:%s source destination\n", *ARGV); Exit (1); }/* Check the first parameter of the CP, the file to be copied, opens with open, IN_FD returns the descriptor for open if return-1, which indicates that the opening failed, prompt error */if ((in_fd = open (argv[1), o_rdonly) = = 1) oops ("Cannot open", argv[1]); /* Check the second parameter of the CP, copy the destination address, create a new file in the destination address with Create, OUT_FD returns the descriptor for open if return-1, represents the creation failure, prompt error */if ((out_fd = creat (argv[2], Copymode) ) = =-1) oops ("Cannot creat", argv[2]); the action of the/*CP instruction is to read the contents of a file to the memory, create a blank file at the new address, and then write the content from the memory to the new file. Here the success of the replication is determined: if it can read smoothly, and read the number of bits and the number of bits written is a write error, if the read failed, is a read error. */while ((N_chars = Read (IN_FD, buf, buffersize)) > 0) if (write (OUT_FD, buf, n_chars)! = n_chars) oops ("Write Error to ", Argv[2]); if (N_chars = =-1) oops ("Read error from", Argv[1]); /* Here is the action to close the file, in_fd and out_fd two file descriptor pointing to the file as long as there is a close error, prompt to close the error. */if (Close (in_fd) = =-1 | | | close (OUT_FD) = =-1) oops ("Error Closing Files", "");} /* This is the function used to output the error message */void oops (char *s1, char *s2) {fprintf (stderr, "error:%s", S1); Perror (S2);//used to output the cause of an error in the previous function to the standard device (stderr) exit (1);}
echostate
#include <stdio.h>#include <stdlib.h>#include <termios.h>int main () { struct termios info; int RV; RV = tcgetattr (0, &info); /* Read values from driver */ if (rv = 1 ) {perror ("tcgetattr"); exit (1);} if (Info.c_lflag & Echo) printf ("Echo is on, since it bit is 1\n") and Else printf ("Echo is OFF, since it bit is 0\n "); return 0;}
Checks whether the prompt on the command line is displayed, if it is displayed, the command entered is visible, and the command that indicates the input is not visible.
Fileinfo.c
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>void show_stat_info (char *, struct stat *); int main (int argc, char *argv[]) { struct stat info; if (argc>1) {if (stat (argv[1], &info)! =-1 ) {show_stat_info (argv[1], &info); return 0;} else perror (argv[1]);} return 1;} void Show_stat_info (char *fname, struct stat *buf) {printf ("mode:%o\n", buf->St_mode); printf ("Links:%d\n ", buf->St_nlink); printf ("User:%d\n", buf->st_uid); printf ("Group:%d\n", buf->St_gid); printf ("Size:%d\n", (int) buf-& Gt st_size); printf ("Modtime:%d\n", (int) buf->st_mtime); printf ("Name:%s\n", fname);}
Used to implement display file information.
Filesize.c
#include <stdio.h>#include <sys/stat.h>int main () { struct stat infobuf; if (Stat ("/etc/passwd", &infobuf) = =-1 ) perror ("/etc/passwd"); else printf ("The size of/etc/passwd is%d\n", infobuf.st_size);}
Calculates the size of a file's bytes.
Ls1
#include <stdio.h>#include <sys/types.h>#include <dirent.h>void Do_ls ( Char []); int main (int argc, char *argv[]) {/ * If the operand is only 1, it indicates that the LS is not followed by a parameter, the default is the current directory,. Represents the current directory. */ if (argc = = 1 ) Do_ls ("." ); /* If there are parameters behind LS, read the parameters into the argv. */Else while (--argc) {printf ("%s:\n", *++argv); Do_ls (*argv);} return 0;} /* Because LS and dir function similarly, use DIR to implement Ls*/void Do_ls (char dirname[]) {dir *dir_ptr; struct dirent *direntp;/* If there is no point to the address, error */if ((Dir_ptr = Opendir (dirname)) = = NULL) fprintf (stderr, "Ls1:cannot open%s\n", dirname ); else {/* recursive way to read */while ((DIRENTP = Readdir (dir_ptr)) = NULL) printf ("%s\n", direntp->d_name); Closedir (DIR_PTR); }}
Ls2
#include <stdio.h>#include <string.h>#include <sys/types.h>#include <dirent.h>#include <sys/stat.h>void Do_ls (char[]); void Dostat (char *); void Show_file_info (char *, struct stat *); void mode_to_letters (int, Char[]); char *Uid_to_name (uid_t); char *Gid_to_name (gid_t); int main (int argc, char *Argv[]) {if (argc = = 1) Do_ls ("."); else while (--ARGC) {printf ("%s:\n", *++ARGV); Do_ls (*ARGV); } return 0;} void Do_ls (charDirname[]) {DIR *Dir_ptr; struct Dirent *DIRENTP; if ((Dir_ptr = Opendir (dirname)) = =NULL) fprintf (stderr, "Ls1:cannot open%s\n", dirname); Else{while (DIRENTP = Readdir (dir_ptr))! =NULL) Dostat (direntp->D_name); Closedir (DIR_PTR); }}void Dostat (char *FileName) {structStat info; if (stat (filename, &info) = =-1) perror (filename); ElseShow_file_info (filename, &info);} void Show_file_info (char *filename, struct stat *info_p) {char *uid_to_name (), *ctime (), *gid_to_name (), *FileMode (); voidMode_to_letters (); Char modestr[11]; Mode_to_letters (info_p->St_mode, MODESTR); printf ("%s", MODESTR); printf ("%4d", (int) info_p->St_nlink); printf ("%-8s", Uid_to_name (info_p->ST_UID)); printf ("%-8s", Gid_to_name (info_p->St_gid)); printf ("%8ld", (long) info_p->St_size); printf ("%.12s", 4+ctime (&info_p->St_mtime)); printf ("%s\n", filename);} void Mode_to_letters (int mode, charStr[]) {strcpy (str, "----------")); if (S_isdir (mode)) str[0] = ' d '; if (S_ISCHR (mode)) str[0] = ' C '; if (s_isblk (mode)) str[0] = ' B '; if (Mode & S_IRUSR) str[1] = ' R '; if (Mode & S_IWUSR) str[2] = ' W '; if (Mode & S_IXUSR) str[3] = ' x '; if (Mode & S_IRGRP) str[4] = ' R ' , if (Mode & S_IWGRP) str[5] = ' W ' ; if (Mode & S_IXGR P) Str[6] = ' x ' , if (Mode & S_iroth) str[7] = ' R ' ; if (Mode & S_iwoth) str[8] = ' W ' ; if (m Ode & S_ixoth) str[9] = ' x ' ;} #include <pwd.h>char * uid_to_name (uid_t uid) {struct passwd *getpwuid (), * pw_ptr; static Char numstr[1 0 ]; if (pw_ptr = Getpwuid (uid)) = = NULL) {sprintf (Numstr, "%d" , UID); return Numstr;} else return pw_ Ptr-> Pw_name;} #include <grp.h>char * gid_to_name (gid_t gid) {struct Group *getgrgid (), * grp_ptr; static Char numstr[1 0 ]; if (grp_ptr = Getgrgid (gid)) = = NULL) {sprintf (Numstr, "%d" , GID); return Numstr;} else return grp_ Ptr-> gr_name;}
Used to display details about a file, user name, size, creation time, and so on.
Setecho.c
#include <stdio.h>#include <stdlib.h>#include <termios.h> #define Oops (s,x) {perror (s); exit (x);} int main (int argc, char *argv[]) { struct termios info; if (argc = = 1 ) exit (0), if (tcgetattr (0,&info) = = 1 ) oops ("Tcgettattr", 1); if (argv[1] [0] = = ' Y ' ) info.c_lflag |= ECHO;/* Open prompt * /else info.c_lflag &= ~echo;/* Hide Prompt */if (tcsetattr (0,tcsanow, &info) = =-1 ) oops ("Tcsetattr", 2); return 0;}
Whether to display instructions entered by the user.
Spwd.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>ino_t Get_inode (char *); voidPrintpathto (ino_t); void Inum_to_name (ino_t, char *, int); intMain () {Printpathto (Get_inode (".")) ); Putchar (' \ n '); return 0;} voidPrintpathto (ino_t this_inode) {ino_t my_inode; charIts_name[bufsiz]; if (Get_inode ("..")! =This_inode) {chdir ("..")); Inum_to_name (This_inode,its_name,bufsiz); My_inode = Get_inode (".") ); Printpathto (My_inode); printf ("/%s", Its_name);}} void Inum_to_name (ino_t inode_to_find, char *namebuf, int buflen) {DIR *dir_ptr; struct dirent *direntp; dir_ ptr = Opendir (".") ); if (dir_ptr = = NULL) {perror (".") ); Exit (1);} while ((DIRENTP = Readdir (dir_ptr)) = NULL) if (Direntp->d_ino = = inode_to_find) {str ncpy (Namebuf, direntp->d_name, Buflen); namebuf[buflen-1] = ' n '; Closedir (dir_ptr); return;} fprint F (stderr, "error looking for Inum%d\n", (int) inode_to_find); exit (1);} ino_t Get_inode (char *fname) {struct stat info; if (stat (fname, &info) = =-1 ) {fprintf (stderr, " Cannot stat "); Perror (fname); Exit (1);} return Info.st_ino;}
The function is to list the current directory.
testioctl.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h> int Main () { struct winsize size; if (Isatty (stdout_fileno) = = 0) exit (1), if (IOCTL (Stdout_fileno, Tiocgwinsz, &size) < 0) {perror ("io CTL Tiocgwinsz Error "); Exit (1);} printf ("%d rows%d columns\n", Size.ws_row, Size.ws_col); return 0;}
who1.c
#include <stdio.h>#include <stdlib.h> #include <utmp.h> #include <fcntl.h> #include <unistd.h> #define Showhost int Show_info (struct utmp * utbufp) {printf ("%-8.8s", Utbufp-> ut_name); printf ("" ); printf ("%-8.8s", Utbufp-> ut_line); printf ("" ); printf ("%10ld", Utbufp-> ut_time); printf ("" ); #ifdef showhost printf ("(%s)", Utbufp-> ut_host); #endif printf ("\ n" ); return 0 ;} int Main () {struct utmp current_record; int utmpfd; int reclen = sizeof (current_record); if (UTM PFD = open (Utmp_file, o_rdonly)) = =-1 ) {perror (utmp_file); exit (1 );} while (Read (UTMPFD, ¤t_re Cord, reclen) = = Reclen) show_info (& Current_record); close (UTMPFD); return 0 ;}
Read the desired information from the Utmp_file file to the memory, then print it to the screen with the standard output function and close the file finally.
who2.cThe same problems as Who1
1. Run chapter Tenth code CSAPP.H
Workaround: Add the header file library
Reference: http://group.cnblogs.com/topic/73278.html
Resources
1. Teaching Material: Tenth chapter, detailed study instruction: http://group.cnblogs.com/topic/73069.html
2. Course Materials: https://www.shiyanlou.com/courses/413 Experiment 10, Course invitation code: W7FQKW4Y
3.20135202 Shang-Blog Park http://www.cnblogs.com/20135202yjx/
System level I/O week eighth 11.9~11.15