tail.c
author viric@mandarina
Fri, 02 Oct 2009 19:20:41 +0200
changeset 274 62048132f95f
parent 267 11631dd11ff8
child 277 1ee3c4ef9402
permissions -rw-r--r--
Making changes so it looks like the code in ts-0.6.4, which I can't find
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
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 258
diff changeset
     3
    Copyright (C) 2007-2009  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>
274
62048132f95f Making changes so it looks like the code in ts-0.6.4, which I can't find
viric@mandarina
parents: 267
diff changeset
    14
#include <sys/select.h>
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    15
#include <stdlib.h>
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    16
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    17
#include <sys/time.h> /* Dep de main.h */
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    18
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    19
#include "main.h"
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    20
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    21
enum { BSIZE=1024 };
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    22
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    23
static int min(int a, int b)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    24
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    25
    if (a < b)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    26
        return a;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    27
    return b;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    28
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    29
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    30
static int max(int a, int b)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    31
{
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    32
    if (a > b)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    33
        return a;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    34
    return b;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    35
}
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    36
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    37
static void tail_error(const char *str)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    38
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    39
    fprintf(stderr, "%s", str);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    40
    fprintf(stderr, ". errno: %i (%s)\n",
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    41
                    errno, strerror(errno));
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    42
    exit(-1);
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
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    45
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
    46
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    47
    char buf[BSIZE];
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    48
    int lines_found = 0;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    49
    int last_lseek = BSIZE;
211
51938376376e Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents: 203
diff changeset
    50
    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
    51
    int move_offset;
211
51938376376e Fixing a bug related to tail of a still 0-bytes file.
viric@mandarina
parents: 203
diff changeset
    52
    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
    53
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    54
    last_lseek = lseek(fd, 0, SEEK_END);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    55
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    56
    do
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    57
    {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    58
        int next_read;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    59
        next_read = min(last_lseek, BSIZE);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    60
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    61
        /* we should end looping if last_lseek == 0
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    62
         * This means we already read all the file. */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    63
        if (next_read <= 0)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    64
            break;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    65
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    66
        /* 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
    67
         * if we wanted to go farer than it. */
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    68
        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
    69
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    70
        if (last_lseek == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    71
            last_lseek = lseek(fd, 0, SEEK_SET);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
    72
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    73
        last_read = read(fd, buf, next_read);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    74
        if (last_read == -1)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    75
        {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    76
            if (errno == EINTR)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    77
                continue;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    78
            tail_error("Error reading");
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
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    82
        for(i = last_read-1; i >= 0; --i)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    83
        {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    84
            if (buf[i] == '\n')
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    85
            {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    86
                ++lines_found;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    87
                if (lines_found > lines)
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    88
                    /* We will use 'i' to calculate where to
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    89
                     * put the file cursor, before return. */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    90
                    break;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    91
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    92
        }
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    93
        
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    94
        /* Go back the read bytes */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
    95
        last_lseek = lseek(fd, -last_read, SEEK_CUR);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    96
    } while(lines_found < lines);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    97
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
    98
    /* Calculate the position */
228
342da3c04e9c Bugfix: Now "-t" returns the last 10 lines
viric@mandarina
parents: 211
diff changeset
    99
    move_offset = i+1;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   100
    lseek(fd, move_offset, SEEK_CUR);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   101
}
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   102
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   103
static void set_non_blocking(int fd)
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   104
{
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   105
    long 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
    arg = O_RDONLY | O_NONBLOCK;
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   108
    fcntl(fd, F_SETFL, arg);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   109
}
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   110
203
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   111
/* 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
   112
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
   113
{
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   114
    int fd;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   115
    int res;
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   116
    int waiting_end = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   117
    int end_res = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   118
    int endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   119
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   120
    fd_set readset;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   121
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   122
    fd = open(fname, O_RDONLY);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   123
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   124
    if (fd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   125
        tail_error("Error: Cannot open the outut file");
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   126
203
664044b1de73 Making '-c' to behave as 'tail -f', but showing *all* the file.
lbatlle@npdl268.bpo.hp.com
parents: 189
diff changeset
   127
    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
   128
        seek_at_last_lines(fd, last_lines);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   129
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   130
    /* we don't want the next read calls to block. */
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   131
    set_non_blocking(fd);
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   132
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   133
    do
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   134
    {
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   135
        char buf[BSIZE];
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   136
        int maxfd;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   137
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   138
        FD_ZERO(&readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   139
        maxfd = -1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   140
        if (!endfile_reached)
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(fd, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   143
            maxfd = fd;
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
        if (waiting_end)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   146
        {
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   147
            FD_SET(server_socket, &readset);
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   148
            maxfd = max(fd, server_socket);
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
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   151
        /* 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
   152
        if (maxfd == -1)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   153
        {
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
   154
            usleep(1 /* sec */* 1000000);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   155
        } else
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
            /* Otherwise, do a normal select */
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   158
            struct timeval tv = {1 /*sec*/, 0 };
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   159
            res = select(maxfd + 1, &readset, 0, 0, &tv);
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
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   162
        if (FD_ISSET(server_socket, &readset))
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   163
        {
258
825b9ed3f836 Fixing a bug on tail/cat, waiting for the job end.
viric@mandarina
parents: 231
diff changeset
   164
            end_res = c_wait_job_recv();
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   165
            waiting_end = 0;
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
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   168
        /* We always read when select awakes */
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   169
        res = read(fd, buf, BSIZE);
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   170
        if (res == -1)
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   171
        {
187
85d52acbab26 Fixed two bugs in tail.c.
viric@llimona
parents: 183
diff changeset
   172
            if (errno == EINTR || errno == EAGAIN)
173
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
                res = 1; /* Hack for the while condition */
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   175
                continue;
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   176
            }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   177
            tail_error("Error reading");
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   178
        }
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   179
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   180
        if (res == 0)
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   181
            endfile_reached = 1;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   182
        else
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   183
            endfile_reached = 0;
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   184
188
64058e15c0dd Faster tail... write(buffer) instead of putchar.
lbatlle@npdl268.bpo.hp.com
parents: 187
diff changeset
   185
        write(1, buf, res);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   186
    } while(!endfile_reached || waiting_end);
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   187
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   188
    close(fd);
174
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   189
c112f67965fb Our implementation of -t (equivalent tail -f), which now
viric@llimona
parents: 173
diff changeset
   190
    return end_res;
173
b572fdd206f4 Adding some code base for our implementation of 'tail'.
viric@llimona
parents:
diff changeset
   191
}