tail.c
author viric@mandarina
Sun, 15 Feb 2009 19:12:08 +0100
changeset 261 dc8f1609bad8
parent 258 825b9ed3f836
child 267 11631dd11ff8
permissions -rw-r--r--
Added tag v0.6.2 for changeset 18d5bf8fa969
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
231
558b281b88f5 Updating the Copyright years from 2007 to 2007-2008.
llbatlle@taga
parents: 228
diff changeset
     3
    Copyright (C) 2007-2008  LluĂ­s Batlle i Rossell
183
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;
211
51938376376e Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents: 203
diff changeset
    49
    int last_read = 0; /* Only to catch not doing any loop */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    50
    int move_offset;
211
51938376376e Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents: 203
diff changeset
    51
    int i = -1; /* Only to catch not doing any loop */
173
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. */
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    67
        last_lseek = lseek(fd, -next_read, 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)
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    87
                    /* We will use 'i' to calculate where to
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    88
                     * put the file cursor, before return. */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    89
                    break;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    90
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    91
        }
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    92
        
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    93
        /* Go back the read bytes */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    94
        last_lseek = lseek(fd, -last_read, SEEK_CUR);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    95
    } while(lines_found < lines);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    96
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    97
    /* Calculate the position */
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    98
    move_offset = i+1;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    99
    lseek(fd, move_offset, SEEK_CUR);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   100
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   101
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   102
static void set_non_blocking(int fd)
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
    long arg;
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   105
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   106
    arg = O_RDONLY | O_NONBLOCK;
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   107
    fcntl(fd, F_SETFL, arg);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   108
}
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   109
203
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   110
/* if last_lines == -1, go on from the start of the file */
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   111
int tail_file(const char *fname, int last_lines)
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   112
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   113
    int fd;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   114
    int res;
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   115
    int waiting_end = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   116
    int end_res = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   117
    int endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   118
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   119
    fd_set readset;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   120
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   121
    fd = open(fname, O_RDONLY);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   122
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   123
    if (fd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   124
        tail_error("Error: Cannot open the outut file");
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   125
203
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   126
    if (last_lines >= 0)
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   127
        seek_at_last_lines(fd, last_lines);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   128
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   129
    /* we don't want the next read calls to block. */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   130
    set_non_blocking(fd);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   131
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   132
    do
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   133
    {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   134
        char buf[BSIZE];
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   135
        int maxfd;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   136
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   137
        FD_ZERO(&readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   138
        maxfd = -1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   139
        if (!endfile_reached)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   140
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   141
            FD_SET(fd, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   142
            maxfd = fd;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   143
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   144
        if (waiting_end)
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
            FD_SET(server_socket, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   147
            maxfd = max(fd, server_socket);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   148
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   149
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   150
        /* 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
   151
        if (maxfd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   152
        {
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
   153
            usleep(1 /* sec */* 1000000);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   154
        } else
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
            /* Otherwise, do a normal select */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   157
            struct timeval tv = {1 /*sec*/, 0 };
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   158
            res = select(maxfd + 1, &readset, 0, 0, &tv);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   159
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   160
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   161
        if (FD_ISSET(server_socket, &readset))
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   162
        {
258
825b9ed3f836 Fixing a bug on tail/cat, waiting for the job end.
viric@mandarina
parents: 231
diff changeset
   163
            end_res = c_wait_job_recv();
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   164
            waiting_end = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   165
        }
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   166
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   167
        /* We always read when select awakes */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   168
        res = read(fd, buf, BSIZE);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   169
        if (res == -1)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   170
        {
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   171
            if (errno == EINTR || errno == EAGAIN)
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   172
            {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   173
                res = 1; /* Hack for the while condition */
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   174
                continue;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   175
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   176
            tail_error("Error reading");
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   177
        }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   178
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   179
        if (res == 0)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   180
            endfile_reached = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   181
        else
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   182
            endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   183
188
64058e15c0dd Faster tail... write(buffer) instead of putchar.
lbatlle@npdl268.bpo.hp.com
parents: 187
diff changeset
   184
        write(1, buf, res);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   185
    } while(!endfile_reached || waiting_end);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   186
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   187
    close(fd);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   188
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   189
    return end_res;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   190
}