tail.c
author viric@llimona
Sat, 09 Feb 2008 11:56:03 +0100
changeset 196 bca29e2a1a86
parent 189 192ea76be533
child 203 664044b1de73
permissions -rw-r--r--
Fixing a bug related to a message on -t on last job, when it was skipped.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
183
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     1
/*
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     2
    Task Spooler - a task queue system for the unix user
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     4
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     5
    Please find the license in the provided COPYING file.
95d49e8a8cec Updating 'help' and some other related files to 0.5
viric@llimona
parents: 179
diff changeset
     6
*/
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
     7
#include <unistd.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
     8
#include <fcntl.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
     9
#include <sys/types.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    10
#include <sys/stat.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    11
#include <stdio.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    12
#include <errno.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    13
#include <string.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    14
#include <stdlib.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    15
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    16
#include <sys/time.h> /* Dep de main.h */
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    17
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    18
#include "main.h"
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    19
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    20
enum { BSIZE=1024 };
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    21
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    22
static int min(int a, int b)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    23
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    24
    if (a < b)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    25
        return a;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    26
    return b;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    27
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    28
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    29
static int max(int a, int b)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    30
{
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    31
    if (a > b)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    32
        return a;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    33
    return b;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    34
}
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    35
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    36
static void tail_error(const char *str)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    37
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    38
    fprintf(stderr, "%s", str);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    39
    fprintf(stderr, ". errno: %i (%s)\n",
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    40
                    errno, strerror(errno));
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    41
    exit(-1);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    42
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    43
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    44
static void seek_at_last_lines(int fd, int lines)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    45
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    46
    char buf[BSIZE];
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    47
    int lines_found = 0;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    48
    int last_lseek = BSIZE;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    49
    int last_read;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    50
    int move_offset;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    51
    int i;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    52
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    53
    last_lseek = lseek(fd, 0, SEEK_END);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    54
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    55
    do
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    56
    {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    57
        int next_read;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    58
        next_read = min(last_lseek, BSIZE);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    59
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    60
        /* we should end looping if last_lseek == 0
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    61
         * This means we already read all the file. */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    62
        if (next_read <= 0)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    63
            break;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    64
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    65
        /* last_lseek will be -1 at the beginning of the file,
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    66
         * if we wanted to go farer than it. */
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    67
        last_lseek = lseek(fd, -BSIZE, SEEK_CUR);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    68
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    69
        if (last_lseek == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    70
            last_lseek = lseek(fd, 0, SEEK_SET);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    71
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    72
        last_read = read(fd, buf, next_read);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    73
        if (last_read == -1)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    74
        {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    75
            if (errno == EINTR)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    76
                continue;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    77
            tail_error("Error reading");
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    78
        }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    79
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    80
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    81
        for(i = last_read-1; i >= 0; --i)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    82
        {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    83
            if (buf[i] == '\n')
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    84
            {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    85
                ++lines_found;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    86
                if (lines_found > lines)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    87
                    break;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    88
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    89
        }
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    90
        
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    91
        /* Go back the read bytes */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    92
        last_lseek = lseek(fd, -last_read, SEEK_CUR);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    93
    } while(lines_found < lines);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    94
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    95
    /* Calculate the position */
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    96
    move_offset = i - last_read + 1;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    97
    lseek(fd, move_offset, SEEK_CUR);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    98
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    99
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   100
static void set_non_blocking(int fd)
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   101
{
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   102
    long arg;
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   103
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   104
    arg = O_RDONLY | O_NONBLOCK;
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   105
    fcntl(fd, F_SETFL, arg);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   106
}
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   107
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   108
int tail_file(const char *fname)
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   109
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   110
    int fd;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   111
    int res;
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   112
    int waiting_end = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   113
    int end_res = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   114
    int endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   115
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   116
    fd_set readset;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   117
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   118
    fd = open(fname, O_RDONLY);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   119
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   120
    if (fd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   121
        tail_error("Error: Cannot open the outut file");
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   122
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   123
    seek_at_last_lines(fd, 10);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   124
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   125
    /* we don't want the next read calls to block. */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   126
    set_non_blocking(fd);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   127
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   128
    do
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   129
    {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   130
        char buf[BSIZE];
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   131
        int maxfd;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   132
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   133
        FD_ZERO(&readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   134
        maxfd = -1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   135
        if (!endfile_reached)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   136
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   137
            FD_SET(fd, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   138
            maxfd = fd;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   139
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   140
        if (waiting_end)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   141
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   142
            FD_SET(server_socket, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   143
            maxfd = max(fd, server_socket);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   144
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   145
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   146
        /* If we don't have fd's to wait for, let's sleep */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   147
        if (maxfd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   148
        {
179
60e972139b56 Moving from nanosleep to usleep, because I don't know in what standard nanosleep is defined.
lbatlle@npdl268.bpo.hp.com
parents: 174
diff changeset
   149
            usleep(1 /* sec */* 1000000);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   150
        } else
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   151
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   152
            /* Otherwise, do a normal select */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   153
            struct timeval tv = {1 /*sec*/, 0 };
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   154
            res = select(maxfd + 1, &readset, 0, 0, &tv);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   155
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   156
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   157
        if (FD_ISSET(server_socket, &readset))
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   158
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   159
            end_res = c_wait_job();
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   160
            waiting_end = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   161
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   162
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   163
        /* We always read when select awakes */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   164
        res = read(fd, buf, BSIZE);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   165
        if (res == -1)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   166
        {
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   167
            if (errno == EINTR || errno == EAGAIN)
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   168
            {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   169
                res = 1; /* Hack for the while condition */
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   170
                continue;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   171
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   172
            tail_error("Error reading");
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   173
        }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   174
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   175
        if (res == 0)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   176
            endfile_reached = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   177
        else
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   178
            endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   179
188
64058e15c0dd Faster tail... write(buffer) instead of putchar.
lbatlle@npdl268.bpo.hp.com
parents: 187
diff changeset
   180
        write(1, buf, res);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   181
    } while(!endfile_reached || waiting_end);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   182
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   183
    close(fd);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   184
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   185
    return end_res;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   186
}