summaryrefslogtreecommitdiff
path: root/inotail.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2006-10-17 12:28:14 +0200
committerTobias Klauser <tklauser@xenon.tklauser.home>2006-10-17 12:28:14 +0200
commit12003aea40a9bfa07053d0de4e615c4251238d5f (patch)
tree4db8602b74329b5d00174f7d542fd00c398aaabe /inotail.c
parent754e5d49b107539e1ddf6e11dd3f2e5c0be21d2d (diff)
inotail.c: Better error handling in tail_file()
Close fd on any error, rename rc variable to cleanly state what it's for.
Diffstat (limited to 'inotail.c')
-rw-r--r--inotail.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/inotail.c b/inotail.c
index 59e9b47..f113477 100644
--- a/inotail.c
+++ b/inotail.c
@@ -48,6 +48,7 @@
/* Print header with filename before tailing the file? */
static char verbose = 0;
+/* Command line options */
static const struct option long_opts[] = {
{"bytes", required_argument, NULL, 'c'},
{"follow", optional_argument, NULL, 'f'},
@@ -159,7 +160,8 @@ static int tail_pipe(struct file_struct *f)
static int tail_file(struct file_struct *f, int n_lines, char mode)
{
- ssize_t rc = 0;
+ int ret = -1;
+ ssize_t bytes_read = 0;
off_t offset = 0;
char buf[BUFFER_SIZE];
struct stat finfo;
@@ -176,13 +178,12 @@ static int tail_file(struct file_struct *f, int n_lines, char mode)
if (fstat(f->fd, &finfo) < 0) {
fprintf(stderr, "Error: Could not stat file '%s' (%s)\n", f->name, strerror(errno));
- close(f->fd);
- return -1;
+ goto out;
}
if (!IS_TAILABLE(finfo.st_mode)) {
fprintf(stderr, "Error: '%s' of unsupported file type\n", f->name);
- return -1;
+ goto out;
}
/* We cannot seek on these */
@@ -198,20 +199,24 @@ static int tail_file(struct file_struct *f, int n_lines, char mode)
/* We only get negative offsets on errors */
if (offset < 0)
- return -1;
+ goto out;
if (verbose)
write_header(f->name);
lseek(f->fd, offset, SEEK_SET);
- while ((rc = read(f->fd, &buf, BUFFER_SIZE)) > 0)
- write(STDOUT_FILENO, buf, (size_t) rc);
+ while ((bytes_read = read(f->fd, &buf, BUFFER_SIZE)) > 0)
+ write(STDOUT_FILENO, buf, (size_t) bytes_read);
- if (close(f->fd) < 0)
+ ret = 0;
+out:
+ if (close(f->fd) < 0) {
fprintf(stderr, "Error: Could not close file '%s' (%s)\n", f->name, strerror(errno));
+ ret = -1;
+ }
- return 0;
+ return ret;
}
static int handle_inotify_event(struct inotify_event *inev, struct file_struct *fil, int n_ignored)
linux/net-next.git/tree/tools/perf/util/bpf-prologue.c?h=nds-private-remove&id=889711a03e0fba427fb85317900e93c74e3be02a'>bpf-prologue.c11226logplain -rw-r--r--bpf-prologue.h847logplain -rw-r--r--build-id.c19054logplain -rw-r--r--build-id.h1859logplain d---------c++184logplain -rw-r--r--cache.h733logplain -rw-r--r--call-path.c2893logplain -rw-r--r--call-path.h2203logplain -rw-r--r--callchain.c29332logplain -rw-r--r--callchain.h7881logplain -rw-r--r--cgroup.c3195logplain -rw-r--r--cgroup.h359logplain -rw-r--r--cloexec.c1950logplain -rw-r--r--cloexec.h251logplain -rw-r--r--color.c4787logplain -rw-r--r--color.h1647logplain -rw-r--r--comm.c2239logplain -rw-r--r--comm.h561logplain -rw-r--r--config.c16262logplain -rw-r--r--config.h2113logplain -rw-r--r--counts.c1026logplain -rw-r--r--counts.h790logplain -rw-r--r--cpumap.c12627logplain -rw-r--r--cpumap.h1954logplain -rw-r--r--cs-etm.h2061logplain -rw-r--r--ctype.c2018logplain -rw-r--r--data-convert-bt.c36671logplain -rw-r--r--data-convert-bt.h302logplain -rw-r--r--data-convert.h141logplain -rw-r--r--data.c3459logplain -rw-r--r--data.h1369logplain -rw-r--r--db-export.c11434logplain -rw-r--r--db-export.h3816logplain -rw-r--r--debug.c4417logplain -rw-r--r--debug.h2023logplain -rw-r--r--demangle-java.c4219logplain -rw-r--r--demangle-java.h249logplain -rw-r--r--demangle-rust.c6602logplain -rw-r--r--demangle-rust.h170logplain -rw-r--r--drv_configs.c1834logplain -rw-r--r--drv_configs.h844logplain -rw-r--r--dso.c31994logplain -rw-r--r--dso.h10399logplain -rw-r--r--dwarf-aux.c33828logplain -rw-r--r--dwarf-aux.h5101logplain -rw-r--r--dwarf-regs.c1816logplain -rw-r--r--env.c1884logplain -rw-r--r--env.h1268logplain -rw-r--r--event.c36670logplain -rw-r--r--event.h15997logplain -rw-r--r--evlist.c47104logplain -rw-r--r--evlist.h12584logplain -rw-r--r--evsel.c63917logplain -rw-r--r--evsel.h13041logplain -rw-r--r--evsel_fprintf.c5831logplain -rw-r--r--find-vdso-map.c581logplain -rw-r--r--genelf.c11653logplain -rw-r--r--genelf.h1814logplain -rw-r--r--genelf_debug.c14374logplain -rwxr-xr-xgenerate-cmdlist.sh1141logplain -rw-r--r--group.h122logplain -rw-r--r--header.c73410logplain -rw-r--r--header.h4365logplain -rw-r--r--help-unknown-cmd.c3221logplain