about summary refs log tree commit diff
path: root/src/skaembutils/s6-tail.c
blob: 4c71f9c49d34b54b9f31b702d6190199bd8d58c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ISC license. */

#include <sys/types.h>
#include <errno.h>
#include <skalibs/sgetopt.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/bytestr.h>
#include <skalibs/uint.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>
#include <skalibs/siovec.h>

#define USAGE "s6-tail [ -c chars | -n lines | -1..9 ] [ file ]"

typedef int tailfunc_t (int, size_t) ;
typedef tailfunc_t *tailfunc_t_ref ;

static int pluslines (int fd, size_t n)
{
  if (n) n-- ;
  {
    char buf[BUFFER_INSIZE] ;
    buffer b = BUFFER_INIT(&buffer_read, fd, buf, BUFFER_INSIZE) ;
    unsigned int count = 0 ;
    while (count < n)
    {
      register ssize_t r = buffer_fill(&b) ;
      if (r <= 0) return !r ;
      while (!buffer_isempty(&b) && (count < n))
      {
        siovec_t v[2] ;
        size_t i ;
        buffer_rpeek(&b, v) ;
        i = siovec_bytechr(v, 2, '\n') ;
        if (i < buffer_len(&b))
        {
          count++ ; i++ ;
        }
        buffer_rseek(&b, i) ;
      }
    }
    b.op = &buffer_write ;
    b.fd = 1 ;
    if (!buffer_flush(&b)) return 0 ;
  }
  return (fd_cat(fd, 1) >= 0) ;
}

static int pluschars (int fd, size_t n)
{
  if (n-- > 1)
  {
    int nil = open_write("/dev/null") ;
    if (nil < 0) return 0 ;
    if (!fd_catn(fd, nil, n))
    {
      register int e = errno ;
      fd_close(nil) ;
      errno = e ;
      return 0 ;
    }
    fd_close(nil) ;
  }
  return (fd_cat(fd, 1) >= 0) ;
}

static int minuslines (int fd, size_t n)
{
  char buf[BUFFER_INSIZE] ;
  buffer b = BUFFER_INIT(&buffer_read, fd, buf, BUFFER_INSIZE) ;
  size_t head = 0, tail = 0 ;
  stralloc tab[n+1] ;
  for (; head <= n ; head++) tab[head] = stralloc_zero ;
  head = 0 ;
  for (;;)
  {
    register int r ;
    r = skagetln(&b, tab + tail, '\n') ;
    if (!r) break ;
    if (r < 0)
    {
      if (errno == EPIPE) break ;
      else goto err ;
    }
    tail = (tail + 1) % (n+1) ;
    if (tail == head)
    {
      tab[head].len = 0 ;
      head = (head + 1) % (n+1) ;
    }
  }
  buffer_init(&b, &buffer_write, 1, buf, BUFFER_INSIZE) ;
  for (; head != tail ; head = (head + 1) % (n+1))
  {
    if (buffer_put(&b, tab[head].s, tab[head].len) < tab[head].len)
      goto err ;
  }
  for (head = 0 ; head <= n ; head++) stralloc_free(tab + head) ;
  return buffer_flush(&b) ;
 err:
  for (head = 0 ; head <= n ; head++) stralloc_free(tab + head) ;
  return 0 ;
}

static int minuschars (int fd, size_t n)
{
  char buf[BUFFER_INSIZE + n] ;
  buffer b = BUFFER_INIT(&buffer_read, fd, buf, BUFFER_INSIZE + n) ;
  for (;;)
  {
    register ssize_t r = buffer_fill(&b) ;
    if (!r) break ;
    if (r < 0) return 0 ;
    buffer_rseek(&b, buffer_len(&b)) ;
    buffer_unget(&b, n) ;
  }
  b.op = &buffer_write ;
  b.fd = 1 ;
  return buffer_flush(&b) ;
}

int main (int argc, char const *const *argv)
{
  tailfunc_t_ref f = &minuslines ;
  unsigned int n = 10 ;
  int gotit = 0 ;
  PROG = "s6-tail" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "123456789n:c:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case '1' :
        case '2' :
        case '3' :
        case '4' :
        case '5' :
        case '6' :
        case '7' :
        case '8' :
        case '9' :
        {
          if (gotit) strerr_dieusage(100, USAGE) ;
          gotit = 1 ;
          f = &minuslines ;
          n = opt - '0' ;
          break ;
        }
        case 'n':
        {
          if (gotit) strerr_dieusage(100, USAGE) ;
          gotit = 1 ;
          f = &minuslines ;
          if (*l.arg == '-') l.arg++ ;
          else if (*l.arg == '+')
          {
            f = &pluslines ;
            l.arg++ ;
          }
          if (!uint0_scan(l.arg, &n)) strerr_dieusage(100, USAGE) ;
          break ;
        }
        case 'c':
        {
          if (gotit) strerr_dieusage(100, USAGE) ;
          gotit = 1 ;
          f = &minuschars ;
          if (*l.arg == '-') l.arg++ ;
          else if (*l.arg == '+')
          {
            f = &pluschars ;
            l.arg++ ;
          }
          if (!uint0_scan(l.arg, &n)) strerr_dieusage(100, USAGE) ;
          break ;
        }
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (!argc)
  {
    if (!(*f)(0, n))
      strerr_diefu1sys(111, "tail stdin") ;
  }
  else
  {
    int fd = open_readb(argv[0]) ;
    if (fd == -1) strerr_diefu3sys(111, "open ", argv[0], " for reading") ;
    if (!(*f)(fd, n))
      strerr_diefu2sys(111, "tail ", argv[0]) ;
    fd_close(fd) ;
  }
  return 0 ;
}