Skip to content

Conversation

Ishaan-debug02
Copy link

No description provided.

@Ishaan-debug02
Copy link
Author

This implementation of the tail command in xv6 meets all the requirements specified in the assignment.
It correctly handles the default case (tail filename) by opening the file, reading its contents into a 4KB buffer, counting newline characters, and printing the last 10 lines by default.

Custom line counts using the -N option (e.g., tail -5 file.txt) are supported by parsing the -N argument and adjusting the number of lines to display. Invalid or negative values fall back to the default of 10 lines, as expected.

When no filename is given, the program reads from standard input (stdin), either manually entered or redirected with <, and prints the last 10 (or N) lines after reaching EOF.

Piped input is also supported, such as grep hello file.txt | tail -3, since the implementation reads from file descriptor 0, which works seamlessly with xv6 pipes.

The code gracefully handles cases where the input has fewer than N lines by printing all available lines, and it includes basic error handling for invalid files or arguments.

The entire file is read into a fixed-size buffer (char buf[4096]), as file sizes in xv6 are limited to 4KB. The implementation uses only standard xv6 system calls (like open, read, write) and follows patterns similar to existing user programs like cat.

Overall, this version of tail passes all functional tests and works correctly across files, redirected input, and pipelines.

@tanishachandani
Copy link

Hey, the overall code is well written. Just a few points:

  1. xv6-public doesn’t have atoi() built in for user programs, so this will likely throw a compile error. You’ll need to write a small function to convert strings to integers manually.
  2. line 73 – This line ends up showing one line less than what the user asks for, and it only happens when a filename is provided (not stdin), creating inconsistent outputs. Probably better to remove it and let the main logic handle exactly N lines as expected.

@Hashmmath
Copy link

Hey, yeah as Tanisha said the code is well written, along with her comments I would like to add mine. Have you tried executing your tail command with files other than README. If yes, then its amazing and if not then try to identify where you should change lines in your files and describe about the change in your comment. Thank you.

@rohitshididnyu
Copy link

Hey, the overall code is well written. Just a few points:

  1. xv6-public doesn’t have atoi() built in for user programs, so this will likely throw a compile error. You’ll need to write a small function to convert strings to integers manually.
  2. line 73 – This line ends up showing one line less than what the user asks for, and it only happens when a filename is provided (not stdin), creating inconsistent outputs. Probably better to remove it and let the main logic handle exactly N lines as expected.

+1

@ghost

This comment was marked as off-topic.

@Ishaan-debug02
Copy link
Author

Ishaan-debug02 commented Aug 12, 2025

Hey, the overall code is well written. Just a few points:

  1. xv6-public doesn’t have atoi() built in for user programs, so this will likely throw a compile error. You’ll need to write a small function to convert strings to integers manually.
  2. line 73 – This line ends up showing one line less than what the user asks for, and it only happens when a filename is provided (not stdin), creating inconsistent outputs. Probably better to remove it and let the main logic handle exactly N lines as expected.

@tanishachandani Great points, thanks for catching that!
Regarding atoi(): You're right — xv6-public doesn't provide this by default in user space. I’ll add a custom atoi() function to avoid compile errors.
For line 73: I now realize subtracting 1 from numLinesToShow when a filename is provided leads to inconsistent behavior compared to stdin or piped input. I’ll remove that adjustment to ensure the tail command consistently outputs exactly N lines across all input modes.

@Ishaan-debug02
Copy link
Author

Hey, yeah as Tanisha said the code is well written, along with her comments I would like to add mine. Have you tried executing your tail command with files other than README. If yes, then its amazing and if not then try to identify where you should change lines in your files and describe about the change in your comment. Thank you.

Hey, yeah as Tanisha said the code is well written, along with her comments I would like to add mine. Have you tried executing your tail command with files other than README. If yes, then its amazing and if not then try to identify where you should change lines in your files and describe about the change in your comment. Thank you.

@Hashmmath Thanks for the suggestion! I initially tested with README, but based on your comment, I ran additional tests with a few different files of varying line lengths. The output matched expectations — the last N lines were displayed correctly in each case. I’ll add a brief note in the PR comments describing the test files and the results to make this clearer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants