#define BUFSIZZ 512 // caseC: do something that we know returns -1 // failure to make sure we understand data sizes. // john refling. caseC.c /* OUTPUT: this case shows correct return value for fail open fake file, which fails, as expected fno = -1, errno = 2 'No such file or directory' try write() 512 to bad file, expect fail RV = 0xffffffffffffffff or -1, errno = 9 'Bad file descriptor' RV IS NOT == 0 RV IS < 0 RV IS == -1 RV IS == -1L RV IS NOT == -1U can detect this failure. */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { char *buf = calloc(1, BUFSIZZ); printf("\nopen fake file, which fails, as expected\n"); int fno = open("/dev/flopZpy", O_RDWR, 0777); printf("fno = %d, errno = %d '%s'\n", fno, errno, strerror(errno)); // write some data to bad file number printf("\ntry write() 512 to bad file, expect fail\n"); errno = 0; ssize_t RV = write(fno, buf, BUFSIZZ); // print out the RV, it is a ssize_t, use %ld printf("RV = 0x%lx or %ld, errno = %d '%s'\n\n", (long unsigned)RV, RV, errno, strerror(errno)); if (512 == RV) return(0); // see what constants RV matches: fprintf(stderr, "RV %s == 0\n", (RV == 0)?"IS":"IS NOT"); fprintf(stderr, "RV %s < 0\n", (RV < 0)?"IS":"IS NOT"); fprintf(stderr, "RV %s == -1\n", (RV == -1)?"IS":"IS NOT"); fprintf(stderr, "RV %s == -1L\n", (RV == -1L)?"IS":"IS NOT"); fprintf(stderr, "RV %s == -1U\n\n", (RV == -1U)?"IS":"IS NOT"); fprintf(stderr, "can detect this failure.\n"); return 0; }