1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +00:00

tail: Shell programs should return 1 to indicate failure.

This commit is contained in:
Andreas Kling 2019-06-06 10:53:42 +02:00
parent 1f17b40d00
commit abb3643d88

View file

@ -10,7 +10,7 @@
int tail_from_pos(CFile& file, off_t startline, bool want_follow) int tail_from_pos(CFile& file, off_t startline, bool want_follow)
{ {
if (!file.seek(startline + 1)) if (!file.seek(startline + 1))
return -1; return 1;
while (true) { while (true) {
const auto& b = file.read(4096); const auto& b = file.read(4096);
@ -26,9 +26,8 @@ int tail_from_pos(CFile& file, off_t startline, bool want_follow)
} }
} }
if (write(STDOUT_FILENO, b.pointer(), b.size()) < 0) { if (write(STDOUT_FILENO, b.pointer(), b.size()) < 0)
return -1; return 1;
}
} }
return 0; return 0;
@ -41,7 +40,7 @@ off_t find_seek_pos(CFile& file, int wanted_lines)
off_t pos = 0; off_t pos = 0;
if (!file.seek(0, CIODevice::SeekMode::FromEndPosition, &pos)) { if (!file.seek(0, CIODevice::SeekMode::FromEndPosition, &pos)) {
fprintf(stderr, "Failed to find end of file: %s\n", file.error_string()); fprintf(stderr, "Failed to find end of file: %s\n", file.error_string());
return -1; return 1;
} }
off_t end = pos; off_t end = pos;
@ -70,7 +69,7 @@ off_t find_seek_pos(CFile& file, int wanted_lines)
static void exit_because_we_wanted_lines() static void exit_because_we_wanted_lines()
{ {
fprintf(stderr, "Expected a line count after -n"); fprintf(stderr, "Expected a line count after -n");
exit(-1); exit(1);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -86,7 +85,7 @@ int main(int argc, char *argv[])
Vector<String> values = args.get_single_values(); Vector<String> values = args.get_single_values();
if (values.size() != 1) { if (values.size() != 1) {
args_parser.print_usage(); args_parser.print_usage();
return -1; return 1;
} }
int line_count = 0; int line_count = 0;
@ -94,14 +93,14 @@ int main(int argc, char *argv[])
line_count = strtol(args.get("n").characters(), NULL, 10); line_count = strtol(args.get("n").characters(), NULL, 10);
if (errno == EINVAL) { if (errno == EINVAL) {
args_parser.print_usage(); args_parser.print_usage();
return -1; return 1;
} }
} }
CFile f(values[0]); CFile f(values[0]);
if (!f.open(CIODevice::ReadOnly)) { if (!f.open(CIODevice::ReadOnly)) {
fprintf(stderr, "Error opening file %s: %s\n", f.filename().characters(), strerror(errno)); fprintf(stderr, "Error opening file %s: %s\n", f.filename().characters(), strerror(errno));
exit(-1); exit(1);
} }
bool flag_follow = args.is_present("f"); bool flag_follow = args.is_present("f");