In this assignment, we worked with system-level C programs to better understand how operating system commands like ls and whoami function behind the scenes.
The project had two main parts:
- Part 1: Modify the ls command so that when we run it normally (without any flags), it behaves as if we ran ls -l.
- Part 2: Modify the whoami command so that it prints a friendly message saying You are: instead of just showing the username.
Part 1 — Modified ls.c
The default ls command just lists filenames in the current directory. In my modified version, I made it display a clearer message that helps users understand what’s happening. My version opens the current directory ("."), loops through its contents using readdir(), and prints all non-hidden files (those not starting with a dot).
Changes Made:
- Added a descriptive message: "Here are the files in this folder:"
- Used standard directory functions (opendir, readdir, closedir) to show the list of visible files.
Part 2 — Modified whoami.c
The original whoami command simply prints the username of the current logged-in user. I modified it to add a friendly prefix, so it now says “You are: .”
Changes Made:
- Used the getlogin() function to retrieve the username.
- Added an error check in case getlogin() returns NULL.
- Added a formatted message using printf().
Pros:
- Simple and clean solution.
- No extra user input needed.
- Works exactly as required.
Cons:
- It skips hidden files (those starting with .), which may not always be desired.
- The output is not formatted in columns like the real ls command.