00001 /* 00002 Error handling examples 00003 AUP2, Sec. 1.4 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 #include <netdb.h> 00023 #include <errno.h> 00024 00025 static bool fcn(void) 00026 { 00027 char buf[100], filein[20], fileout[20]; 00028 /*[cleanup3]*/ 00029 char *p = NULL; 00030 int fdin = -1, fdout = -1; 00031 00032 ec_null( p = malloc(sizeof(buf)) ) 00033 ec_neg1( fdin = open(filein, O_RDONLY) ) 00034 ec_neg1( fdout = open(fileout, O_WRONLY) ) 00035 00036 return true; 00037 00038 EC_CLEANUP_BGN 00039 free(p); 00040 if (fdin != -1) 00041 (void)close(fdin); 00042 if (fdout != -1) 00043 (void)close(fdout); 00044 return false; 00045 EC_CLEANUP_END 00046 /*[]*/ 00047 } 00048 00049 int main(void) 00050 { 00051 00052 { 00053 int amt, fd = -1, numbyte = 10, r; 00054 char buf[100], filein[20], fileout[20]; 00055 char *p = NULL; 00056 int fdin = -1, fdout = -1; 00057 00058 /*[syserrmsg]*/ 00059 if ((amt = read(fd, buf, numbyte)) == -1) { 00060 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00061 "Call to read function failed", errno)); 00062 exit(EXIT_FAILURE); 00063 } 00064 /*[]*/ 00065 /*[cleanup3call]*/ 00066 ec_false( fcn() ) 00067 00068 /* other stuff here */ 00069 00070 exit(EXIT_SUCCESS); 00071 00072 EC_CLEANUP_BGN 00073 exit(EXIT_FAILURE); 00074 EC_CLEANUP_END 00075 /*[]*/ 00076 { 00077 /*[cleanup2]*/ 00078 char *p = NULL; 00079 int fdin = -1, fdout = -1; 00080 00081 if ((p = malloc(sizeof(buf))) == NULL) { 00082 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00083 "malloc failed", errno)); 00084 goto cleanup; 00085 } 00086 if ((fdin = open(filein, O_RDONLY)) == -1) { 00087 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00088 "open (input) failed", errno)); 00089 goto cleanup; 00090 } 00091 if ((fdout = open(fileout, O_WRONLY)) == -1) { 00092 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00093 "open (output) failed", errno)); 00094 goto cleanup; 00095 } 00096 return true; 00097 00098 cleanup: 00099 free(p); 00100 if (fdin != -1) 00101 (void)close(fdin); 00102 if (fdout != -1) 00103 (void)close(fdout); 00104 return false; 00105 /*[cleanup1]*/ 00106 } 00107 if ((p = malloc(sizeof(buf))) == NULL) { 00108 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00109 "malloc failed", errno)); 00110 return false; 00111 } 00112 if ((fdin = open(filein, O_RDONLY)) == -1) { 00113 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00114 "open (input) failed", errno)); 00115 free(p); 00116 return false; 00117 } 00118 if ((fdout = open(fileout, O_WRONLY)) == -1) { 00119 fprintf(stderr, "%s\n", syserrmsg(buf, sizeof(buf), 00120 "open (output) failed", errno)); 00121 (void)close(fdin); 00122 free(p); 00123 return false; 00124 } 00125 /*[]*/ 00126 { 00127 /*[getaddrinfo]*/ 00128 struct addrinfo *infop; 00129 00130 if ((r = getaddrinfo("localhost", "80", NULL, &infop)) != 0) { 00131 fprintf(stderr, "Got error code %d from getaddrinfo\n", r); 00132 exit(EXIT_FAILURE); 00133 } 00134 /*[read-errnowrong1]*/ 00135 } 00136 amt = read(fd, buf, numbyte); 00137 if (errno != 0) { /* wrong! */ 00138 fprintf(stderr, "Read failed! errno = %d\n", errno); 00139 exit(EXIT_FAILURE); 00140 } 00141 /*[]*/ 00142 /*[read-errnowrong2]*/ 00143 errno = 0; 00144 amt = read(fd, buf, numbyte); 00145 if (errno != 0) { /* wrong! */ 00146 fprintf(stderr, "Read failed! errno = %d\n", errno); 00147 exit(EXIT_FAILURE); 00148 } 00149 /*[]*/ 00150 /*[read-errsymbol]*/ 00151 if ((amt = read(fd, buf, numbyte)) == -1) { 00152 fprintf(stderr, "Read failed!: %s (errno = %d; %s)\n", 00153 strerror(errno), errno, errsymbol(errno)); 00154 exit(EXIT_FAILURE); 00155 } 00156 /*[]*/ 00157 /*[read-perror]*/ 00158 if ((amt = read(fd, buf, numbyte)) == -1) { 00159 perror("Read failed!"); 00160 exit(EXIT_FAILURE); 00161 } 00162 /*[]*/ 00163 #if 0 00164 /*[read-errno]*/ 00165 #include <errno.h> 00166 00167 if ((amt = read(fd, buf, numbyte)) == -1) { 00168 fprintf(stderr, "Read failed! errno = %d\n", errno); 00169 exit(EXIT_FAILURE); 00170 } 00171 /*[]*/ 00172 #endif 00173 /*[read1]*/ 00174 if ((amt = read(fd, buf, numbyte)) == -1) { 00175 fprintf(stderr, "Read failed!\n"); 00176 exit(EXIT_FAILURE); 00177 } 00178 /*[]*/ 00179 } 00180 exit(EXIT_SUCCESS); 00181 00182 }