summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inotail.c76
-rw-r--r--inotail.h3
2 files changed, 61 insertions, 18 deletions
diff --git a/inotail.c b/inotail.c
index 5c96a50..069762b 100644
--- a/inotail.c
+++ b/inotail.c
@@ -39,7 +39,7 @@
#include "inotail.h"
#define PROGRAM_NAME "inotail"
-#define BUFFER_SIZE 4096
+#define DEFAULT_BUFFER_SIZE 4096
/* inotify event buffer length for one file */
#define INOTIFY_BUFLEN (4 * sizeof(struct inotify_event))
@@ -63,6 +63,18 @@ static const struct option long_opts[] = {
{ NULL, 0, NULL, 0 }
};
+static char *emalloc(size_t size)
+{
+ char *ret = malloc(size);
+
+ if (!ret) {
+ fprintf(stderr, "Error: Failed to allocate %d bytes of memory (%s)\n", size, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ return ret;
+}
+
static void usage(const int status)
{
fprintf(stdout, "Usage: %s [OPTION]... [FILE]...\n\n"
@@ -83,6 +95,7 @@ static inline void setup_file(struct file_struct *f)
{
f->fd = f->i_watch = -1;
f->st_size = 0;
+ f->st_blksize = DEFAULT_BUFFER_SIZE;
f->ignore = 0;
}
@@ -115,16 +128,16 @@ static void write_header(char *filename)
static off_t lines_to_offset_from_end(struct file_struct *f, unsigned long n_lines)
{
- char buf[BUFFER_SIZE];
off_t offset = f->st_size;
+ char *buf = emalloc(f->st_blksize);
n_lines++; /* We also count the last \n */
while (offset > 0 && n_lines > 0) {
int i;
- ssize_t rc, block_size = BUFFER_SIZE; /* Size of the current block we're reading */
+ ssize_t rc, block_size = f->st_blksize; /* Size of the current block we're reading */
- if (offset < BUFFER_SIZE)
+ if (offset < block_size)
block_size = offset;
/* Start of current block */
@@ -138,23 +151,27 @@ static off_t lines_to_offset_from_end(struct file_struct *f, unsigned long n_lin
rc = read(f->fd, buf, block_size);
if (unlikely(rc < 0)) {
fprintf(stderr, "Error: Could not read from file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
}
for (i = block_size - 1; i > 0; i--) {
if (buf[i] == '\n') {
- if (--n_lines == 0)
+ if (--n_lines == 0) {
+ free(buf);
return offset += i + 1; /* We don't want the first \n */
+ }
}
}
}
+ free(buf);
return offset;
}
static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_lines)
{
- char buf[BUFFER_SIZE];
+ char *buf;
off_t offset = 0;
/* tail everything for 'inotail -n +0' */
@@ -162,10 +179,11 @@ static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_l
return 0;
n_lines--;
+ buf = emalloc(f->st_blksize);
while (offset <= f->st_size && n_lines > 0) {
int i;
- ssize_t rc, block_size = BUFFER_SIZE;
+ ssize_t rc, block_size = f->st_blksize;
if (lseek(f->fd, offset, SEEK_SET) == (off_t) -1) {
fprintf(stderr, "Error: Could not seek in file '%s' (%s)\n", f->name, strerror(errno));
@@ -175,20 +193,24 @@ static off_t lines_to_offset_from_begin(struct file_struct *f, unsigned long n_l
rc = read(f->fd, buf, block_size);
if (unlikely(rc < 0)) {
fprintf(stderr, "Error: Could not read from file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
} else if (rc < block_size)
block_size = rc;
for (i = 0; i < block_size; i++) {
if (buf[i] == '\n') {
- if (--n_lines == 0)
+ if (--n_lines == 0) {
+ free(buf);
return offset + i + 1;
+ }
}
}
offset += block_size;
}
+ free(buf);
return offset;
}
@@ -217,20 +239,22 @@ static off_t bytes_to_offset(struct file_struct *f, unsigned long n_bytes)
static ssize_t tail_pipe(struct file_struct *f)
{
ssize_t rc;
- char buf[BUFFER_SIZE];
+ char *buf = emalloc(f->st_blksize);
if (verbose)
write_header(f->name);
- /* FIXME: We will just tail everything here for now */
- while ((rc = read(f->fd, buf, BUFFER_SIZE)) > 0) {
+ /* We will just tail everything here */
+ while ((rc = read(f->fd, buf, f->st_blksize)) > 0) {
if (write(STDOUT_FILENO, buf, (size_t) rc) <= 0) {
/* e.g. when writing to a pipe which gets closed */
fprintf(stderr, "Error: Could not write to stdout (%s)\n", strerror(errno));
- return -1;
+ rc = -1;
+ break;
}
}
+ free(buf);
return rc;
}
@@ -238,7 +262,7 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
{
ssize_t bytes_read = 0;
off_t offset = 0;
- char buf[BUFFER_SIZE];
+ char *buf;
struct stat finfo;
if (strcmp(f->name, "-") == 0)
@@ -269,6 +293,9 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
return tail_pipe(f);
f->st_size = finfo.st_size;
+ f->st_blksize = finfo.st_blksize; /* TODO: Can this value be 0 or negative? */
+
+ buf = emalloc(f->st_blksize);
if (mode == M_LINES)
offset = lines_to_offset(f, n_units);
@@ -278,6 +305,7 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
/* We only get negative offsets on errors */
if (unlikely(offset < 0)) {
ignore_file(f);
+ free(buf);
return -1;
}
@@ -289,28 +317,32 @@ static int tail_file(struct file_struct *f, unsigned long n_units, char mode, ch
return -1;
}
- while ((bytes_read = read(f->fd, buf, BUFFER_SIZE)) > 0)
+ while ((bytes_read = read(f->fd, buf, f->st_blksize)) > 0)
write(STDOUT_FILENO, buf, (size_t) bytes_read);
if (!forever) {
if (close(f->fd) < 0) {
fprintf(stderr, "Error: Could not close file '%s' (%s)\n", f->name, strerror(errno));
+ free(buf);
return -1;
}
} /* Let the fd open otherwise, we'll need it */
+ free(buf);
return 0;
}
static int handle_inotify_event(struct inotify_event *inev, struct file_struct *f)
{
+ char *fbuf;
int ret = 0;
if (inev->mask & IN_MODIFY) {
ssize_t rc;
- char fbuf[BUFFER_SIZE];
struct stat finfo;
+ fbuf = emalloc(f->st_blksize);
+
if (verbose)
write_header(f->name);
@@ -321,7 +353,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct *
goto ignore;
}
- while ((rc = read(f->fd, fbuf, BUFFER_SIZE)) != 0)
+ while ((rc = read(f->fd, fbuf, f->st_blksize)) != 0)
write(STDOUT_FILENO, fbuf, (size_t) rc);
if (fstat(f->fd, &finfo) < 0) {
@@ -332,6 +364,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct *
f->st_size = finfo.st_size;
+ free(fbuf);
return ret;
} else if (inev->mask & IN_DELETE_SELF) {
fprintf(stderr, "File '%s' deleted.\n", f->name);
@@ -344,6 +377,7 @@ static int handle_inotify_event(struct inotify_event *inev, struct file_struct *
ignore:
ignore_file(f);
+ free(fbuf);
return ret;
}
@@ -476,8 +510,14 @@ int main(int argc, char **argv)
specified and standard input is a pipe. */
if (forever) {
struct stat finfo;
- if (fstat(STDIN_FILENO, &finfo) == 0
- && IS_PIPELIKE(finfo.st_mode))
+ int rc = fstat(STDIN_FILENO, &finfo);
+
+ if (rc == -1) {
+ fprintf(stderr, "Error: Could not stat stdin (%s)\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ if (rc == 0 && IS_PIPELIKE(finfo.st_mode))
forever = 0;
}
}
diff --git a/inotail.h b/inotail.h
index 836c1de..b215b43 100644
--- a/inotail.h
+++ b/inotail.h
@@ -7,6 +7,8 @@
#ifndef _INOTAIL_H
#define _INOTAIL_H
+#include <sys/types.h>
+
/* Number of items to tail. */
#define DEFAULT_N_LINES 10
@@ -18,6 +20,7 @@ struct file_struct {
char *name; /* Name of file (or '-' for stdin) */
int fd; /* File descriptor (or -1 if file is not open */
off_t st_size; /* File size */
+ blksize_t st_blksize; /* Blocksize for filesystem I/O */
unsigned ignore; /* Whether to ignore the file in further processing */
int i_watch; /* Inotify watch associated with file_struct */
};