Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Userland: Improve head program #155

Merged
merged 1 commit into from
Jun 1, 2019

Conversation

deoxxa
Copy link
Contributor

@deoxxa deoxxa commented Jun 1, 2019

  • allow specifying files as arguments, e.g. head a b c
  • support multiple files
  • print a filename header when multiple files are being printed
  • allow suppression or forcing of filename header via flags

This change drops support for the legacy -123 syntax in favour of the
more widely-supported -n 123 form.

fixes #105

Copy link
Collaborator

@awesomekling awesomekling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! The main issue to fix is the handling of what strtol() returns. The rest are just some coding style conformance and efficiency things.

Userland/head.cpp Outdated Show resolved Hide resolved
Userland/head.cpp Outdated Show resolved Hide resolved
Userland/head.cpp Outdated Show resolved Hide resolved
Userland/head.cpp Outdated Show resolved Hide resolved
Userland/head.cpp Outdated Show resolved Hide resolved
if (!str)
break;
fputs(str, stdout);
printf("%s", str);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the old version of this code. fputs() avoids having to parse the format string since it's just "%s" anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I changed it to the printf call instead was to avoid the newline from puts doubling up with the newline from the source data. I've changed this to instead strip the trailing newline from the source string, and made this into a puts call. I didn't make it fputs because I can't think of any reason we'd want to output to anything other than stdout.

if ((pos = strchr(str, '\n')) != nullptr)
*pos = '\0';

puts(str);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fputs(str, stdout) doesn't add a newline while puts(str) does.
A silly ancient C library artifact (of many) :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Well, now that original code makes sense. Thanks for bearing with me!

* allow specifying files as arguments, e.g. `head a b c`
* support multiple files
* print a filename header when multiple files are being printed
* allow suppression or forcing of filename header via flags

This change drops support for the legacy `-123` syntax in favour of the
more widely-supported `-n 123` form.

fixes SerenityOS#105
@awesomekling awesomekling merged commit 6cabd34 into SerenityOS:master Jun 1, 2019
@awesomekling
Copy link
Collaborator

Excellent! Thanks for working on this :)

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.

head: Support "head <file>", not just stdin
2 participants