// // // // // // // // // // // // // // // // // // // // // // // // // // CS270 Class Example // // Author: Robert Heckendorn // University of Idaho // #include #include // for open and open flags #include // for errno #include // for strerror #include // for unlink int main() { int fd; const char *filename="nonexist.txt"; printf("\nOPEN NONEXISTENT FILE\n"); fd = open(filename, O_RDONLY); if (fd == -1) { printf("open nonexistent file: errno = %d\n", errno); printf("problem with file %s: %s\n", filename, strerror(errno)); if (errno==ENOENT) { printf("opening nonexistent file %s\n", filename); } perror("ERROR(main)"); } printf("\nEXECUTE A SUCCESSFUL SYSTEM CALL\n"); // set default: errno = 0; fd = open(filename, O_RDONLY | O_CREAT, 0644); printf("open with create option: errno = %d\n", errno); perror("main"); printf("\nREMOVE CREATED FILE\n"); if (unlink(filename)==-1) { perror("ERROR"); } printf("\nREMOVE NONEXISTENT FILE\n"); if (unlink("xyzzy")==-1) { perror("ERROR"); } printf("\nOPEN DIRECTORY FOR WRITING\n"); fd = open("/", O_WRONLY); if (fd == -1) { printf("open a directory for writing: errno = %d\n", errno); perror("main"); } printf("\nSET ERRNO\n"); errno = 10; perror("main"); return 666; // will mod 256 }