class: title # Files and Directories ## CSC 213 – September 29, 2025 --- # Agenda for Today 1. Department Events 2. Upcoming Work 3. Files and Directories 5. Wrap Up --- # Department Events ## CS Table: Open Source Software Supply-Chain Security .indent[ Tuesday, September 30th at noon in JRC 224C ] ## CS Extra: Welcome Session .indent[ *Information for 2nd year intended CS majors* Thursday, October 2nd at 4:15pm in Noyce 3821 ] ## Flu Shot Clinic .indent[ October 2nd and 3rd from 9am–4pm in the Natatorium Lobby Register with Hy-Vee at
] *To receive 0.25% extra credit, send an email to let me know you attended no more than 48 hours after the event.* --- class: section, blue # Upcoming Work --- # Lab: Malloc **Due:** Wednesday, October 8th by 11:59pm **Late Deadline:** Wednesday, October 15th by 11:59pm ## Questions? --- # Assignment: Listing Directories **Due:** Monday, October 6th by 11:59pm Let's look at the assignment now. --- class: section, blue # Files and Directories --- # Discussion: Filesystem Basics **Questions for think, pair, share:** 1. What is a file? 2. What is a directory? 3. What could we find on a computer's filesystem other than files and directories? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- # Discussion: Filesystem Permissions **Questions for think, pair, share:** 1. A file has permissions `rwxrw-r--`. What can different users do with this file? 2. A directory has permissions `rwxr-xr--`. What can different users do with this directory? *Start by thinking of answers on your own. I will let you know when to start discussion.* --- class: section, green # Puzzles --- # Puzzle Rules ## Format Each puzzle shows a program fragment. The program finishes executing and exits without any errors. ## Setup The first puzzle program runs in an empty directory. Each following puzzle runs in the same directory, which has all the outputs from the previous puzzles. ## Responses Your task is to determine (without running the code) what files will be in the directory after each puzzle program runs, and what those files will contain. --- # Puzzle 1 ```c int fd = open("message.txt", O_RDWR | O_CREAT, 0640); char* message = "Hello"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 2 ```c int fd = open("message.txt", O_RDWR | O_CREAT | O_APPEND, 0640); char* message = " world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 3 ```c int fd = open("message.txt", O_RDWR); char* message = "J"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 4 ```c link("message.txt", "greeting.txt"); int fd = open("greeting.txt", O_RDWR | O_APPEND); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 5 ```c symlink("message.txt", "symlink.txt"); int fd = open("symlink.txt", O_RDWR | O_APPEND); char* message = "What?\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 6 ```c int fd = open("message.txt", O_RDWR | O_TRUNC); unlink("message.txt"); char* message = "Well, this is awkward.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 7 ```c int fd = open("message.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Curiouser and curiouser!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 8 ```c mkdir("somedir", 0755); int fd = open("somedir/message.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Hello world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 9 ```c chdir("somedir"); int fd = open("greeting.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 10 ```c int fd = open("message.txt", O_RDWR, 0640); char* data = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); data[0] = 'F'; data[14] = 'f'; munmap(data, 4096); close(fd); ``` --- # Puzzle 11 ```c int fd = open("fork.txt", O_RDWR | O_CREAT, 0640); pid_t child_id = fork(); if (child_id == 0) { char* message = "fork\n"; write(fd, message, strlen(message)); } else { wait(NULL); char* message = "spoon\n"; write(fd, message, strlen(message)); } close(fd); ``` --- # Puzzle 12 ```c int fd = open("dup.txt", O_RDWR | O_CREAT, 0640); dup2(fd, STDOUT_FILENO); char* message = "Hello from write().\n"; write(fd, message, strlen(message)); printf("Hello from printf().\n"); message = "Interesting...\n"; write(fd, message, strlen(message)); close(fd); ``` --- class: section, blue # Wrap Up --- # Reminders ## Lab The malloc lab is due next week. We will spend all of Wednesday working on the lab, but you should find additional time to work with your group. ## Assignment The listing directories assignment is due next Monday. ## Need Help? My regular office hours schedule is now posted and times are bookable. Don't forget about Tuesday's mentor session from 5–6pm. ## Reading *No reading for the next class.*