Script started on Mon Nov 18 13:05:42 2024 mweeks@air:unix_ch13$ echo 4091 4091 mweeks@air:unix_ch13$ echo error control error control mweeks@air:unix_ch13$ echo errno error number errno error number mweeks@air:unix_ch13$ echo perror print the error perror print the error mweeks@air:unix_ch13$ cat fileIOerror.c #include #include #include int main () { int fd; /* Open a non-existent file to cause an error */ fd = open ("nonexist.txt", O_RDONLY); if (fd == -1) /* fd == -1 =, an error occurred */ { printf ("errno = %d\n", errno); perror ("main"); } /* Force a different error */ fd = open ("/", O_WRONLY); if (fd == -1) { printf ("errno = %d\n", errno); perror ("main"); } /* Execute a successful system call */ fd = open ("nonexist.txt", O_RDONLY | O_CREAT, 0644); /* Display after successful call */ printf ("errno = %d\n", errno); /* will display previous error num (21) */ perror ("main"); errno = 0; /* Manually reset error variable */ perror ("main"); return 0; } mweeks@air:unix_ch13$ mweeks@air:unix_ch13$ ls non* nonexist.txt nonexist2 mweeks@air:unix_ch13$ mv nonexist.txt nonexist.old mweeks@air:unix_ch13$ gcc fileIOerror.c mweeks@air:unix_ch13$ ./a.out errno = 2 main: No such file or directory errno = 21 main: Is a directory errno = 21 main: Is a directory main: Undefined error: 0 mweeks@air:unix_ch13$ echo O_APPEND O_APPEND mweeks@air:unix_ch13$ cat sparse.c #include #include #include #include /**************************************/ int main () { int i, fd; /* Create a sparse file */ fd = open ("sparse.txt", O_CREAT | O_RDWR, 0600); write (fd, "sparse", 6); lseek (fd, 60006, SEEK_SET); write (fd, "file", 4); close (fd); /* Create a normal file */ fd = open ("normal.txt", O_CREAT | O_RDWR, 0600); write (fd, "normal", 6); for (i = 1; i <= 60000; i++) write (fd, "\0", 1); /** Different from book! **/ write (fd, "file", 4); close (fd); return 0; } mweeks@air:unix_ch13$ gcc sparse.c mweeks@air:unix_ch13$ ./a.out mweeks@air:unix_ch13$ ls -l *txt -rw-r--r-- 1 mweeks staff 0 Nov 18 13:25 nonexist.txt -rw-r--r-- 1 mweeks staff 60010 Nov 18 13:37 normal.txt -rw-r--r-- 1 mweeks staff 60010 Nov 18 13:37 sparse.txt -rw-r--r-- 1 mweeks staff 15 Aug 5 23:48 test.txt mweeks@air:unix_ch13$ diff normal.txt sparse.txt Binary files normal.txt and sparse.txt differ mweeks@air:unix_ch13$ echo unlink that deletes a file unlink that deletes a file mweeks@air:unix_ch13$ grep read *.c monitor.c: dp = opendir (dirName); /* Open for reading */ monitor.c: while ( (dep = readdir(dp)) ) /* Read all dir entries */ { monitor_original.c: dp = opendir (dirName); /* Open for reading */ monitor_original.c: while (dep = readdir(dp)) /* Read all dir entries */ { reverse_unix_ch13.c:int standardInput = FALSE; /* Set to true if reading stdin */ reverse_unix_ch13.c: else /* Open named file for reading */ reverse_unix_ch13.c: charsRead = read (fd, buffer, BUFFER_SIZE); reverse_unix_ch13.c: /* Copy line to temporary file if reading from stdin */ reverse_unix_ch13.c: /* If reading from standard input, prepare fd for pass2 */ reverse_unix_ch13.c: lseek (fd, lineStart[i], SEEK_SET); /* Find the line and read it */ reverse_unix_ch13.c: charsRead = read (fd, buffer, lineStart[i+1] - lineStart[i]); mweeks@air:unix_ch13$ more reverse_unix_ch13.c #include /* For file mode definitions */ #include #include #include // for fork, sleep /* Enumerator */ enum { FALSE, TRUE }; /* Standard false and true values */ enum { STDIN, STDOUT, STDERR }; /* Standard I/O channel indices */ /* #define Statements */ #define BUFFER_SIZE 4096 /* Copy buffer size */ #define NAME_SIZE 12 #define MAX_LINES 100000 /* Max lines in file */ /* Globals */ char *fileName = NULL; /* Points to file name */ char tmpName [NAME_SIZE]; int charOption = FALSE; /* Set to true if -c option is used */ int standardInput = FALSE; /* Set to true if reading stdin */ int lineCount = 0; /* Total number of lines in input */ int lineStart [MAX_LINES]; /* Store offsets of each line */ int fileOffset = 0; /* Current position in input */ int fd; /* File descriptor of input */ // function declarations void processOptions (char* str); void usageError (); void trackLines ( char* buffer, int charsRead); void fatalError (); void processLine (int i); void reverseLine ( char* buffer, int size); /* Parse command line arguments */ void parseCommandLine (int argc, char* argv[]) { int i; for (i= 1; i < argc; i++) { if(argv[i][0] == '-') processOptions (argv[i]); else if (fileName == NULL) fileName= argv[i]; else usageError (); /* An error occurred */ } standardInput = (fileName == NULL); } /****************************************************************/ /* Parse options */ void processOptions (char* str) { int j; for (j= 1; str[j] != '\0'; j++) { switch(str[j]) /* Switch on command line flag */ { case'c': charOption = TRUE; break; default: usageError (); break; } } } /****************************************************************/ void usageError () { fprintf (stderr, "Usage: reverse -c [filename]\n"); exit (/* EXITFAILURE */ 1); } /****************************************************************/ /* Perform first scan through file */ void pass1 () { int tmpfd, charsRead, charsWritten; char buffer [BUFFER_SIZE]; if (standardInput) /* Read from standard input */ { ...skipping... else /* Open named file for reading */ { fd = open (fileName, O_RDONLY); if (fd == -1) fatalError (); } lineStart[0] = 0; /* Offset of first line */ while (TRUE) /* Read all input */ { /* Fill buffer */ charsRead = read (fd, buffer, BUFFER_SIZE); if (charsRead == 0) break; /* EOF */ if (charsRead == -1) fatalError (); /* Error */ trackLines (buffer, charsRead); /* Process line */ /* Copy line to temporary file if reading from stdin */ if (standardInput) { charsWritten = write (tmpfd, buffer, charsRead); if(charsWritten != charsRead) fatalError (); } } mweeks@air:unix_ch13$ exit exit Script done on Mon Nov 18 13:45:39 2024