about summary refs log tree commit diff
path: root/src/skaembutils/s6-head.c
blob: 427d22068010e437a0b28196413019cfb7ccfce3 (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
/* ISC license. */

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

#define USAGE "s6-head [ -S ] [ -1..9 | -n lines | -c chars ] [ file... ]"
#define dieusage() strerr_dieusage(100, USAGE)

typedef int hea$1_func (int, size_t) ;
typedef hea$1_func *hea$1_func_ref ;

static int dolines (int fd, size_t lines)
{
  char buf[BUFFER_INSIZE] ;
  buffer in = BUFFER_INIT(&buffer_read, fd, buf, BUFFER_INSIZE) ;
  buffer out = BUFFER_INIT(&buffer_write, 1, buf, BUFFER_INSIZE) ;
  struct iovec v[2] ;
  while (lines)
  {
    size_t w = 0 ;
    ssize_t r = buffer_fill(&in) ;
    if (r <= 0) return !r ;
    out.c.n = in.c.n ; out.c.p = in.c.p ;
    buffer_rpeek(&in, v) ;
    for (;;)
    {
      size_t n = siovec_len(v, 2) ;
      size_t i ;
      if (!n) break ;
      i = siovec_bytechr(v, 2, '\n') ;
      if (i < n)
      {
        w += i+1 ;
        siovec_seek(v, 2, i+1) ;
        if (!--lines)
        {
          out.c.n = (out.c.p + w) % out.c.a ;
          break ;
        }
      }
      else siovec_seek(v, 2, i) ;
    }
    if (!buffer_flush(&out)) return 0 ;
    in.c.n = out.c.n ; in.c.p = out.c.p ;
  }
  return 1 ;
}

static int safedolines (int fd, size_t lines)
{
  char tmp[lines] ;
  while (lines)
  {
    size_t r = allread(fd, tmp, lines) ;
    if ((r < lines) && (errno != EPIPE)) return 0 ;
    lines -= byte_count(tmp, r, '\n') ;
    if (buffer_put(buffer_1, tmp, r) < (ssize_t)r) return 0 ;
  }
  if (!buffer_flush(buffer_1)) return 0 ;
  return 1 ;
}

static int safedochars (int fd, size_t chars)
{
  return (fd_catn(fd, 1, chars) >= chars) ;
}

int main (int argc, char const *const *argv)
{
  hea$1_func_ref f ;
  unsigned int lines = 10 ;
  int islines = 1, safe = 0 ;
  PROG = "s6-head" ;
  {
    subgetopt l = SUBGETOPT_ZERO ;
    int done = 0 ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "S123456789n:c:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'S' : safe = 1 ; break ;
        case '1' :
        case '2' :
        case '3' :
        case '4' :
        case '5' :
        case '6' :
        case '7' :
        case '8' :
        case '9' :
        {
          if (done) dieusage() ;
          islines = 1 ;
          lines = opt - '0' ;
          done = 1 ;
          break ;
        }
        case 'n' :
        {
          if (done || !uint0_scan(l.arg, &lines))
            strerr_dieusage(100, USAGE) ;
          islines = 1 ;
          done = 1 ;
          break ;
        }
        case 'c' :
        {
          if (done || !uint0_scan(l.arg, &lines))
            strerr_dieusage(100, USAGE) ;
          islines = 0 ;
          done = 1 ;
          break ;
        }
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  if (argc) safe = 0 ;
  f = islines ? safe ? &safedolines : &dolines : &safedochars ;
  if (!argc)
  {
    if (!(*f)(0, lines))
      strerr_diefu1sys(111, "head stdin") ;
  }
  else
  {
    unsigned int i = 0 ;
    for (; argv[i] ; i++)
    {
      int fd ;
      if (argc >= 2)
      {
        if (i) buffer_putnoflush(buffer_1, "\n", 1) ;
        buffer_putnoflush(buffer_1, "==> ", 4) ;
        if ((buffer_puts(buffer_1, argv[i]) <= 0)
         || (buffer_putflush(buffer_1, " <==\n", 5) < 0))
          strerr_diefu1sys(111, "write to stdout") ;
      }
      if ((argv[i][0] == '-') && !argv[i][1]) fd = 0 ;
      else fd = open_readb(argv[i]) ;
      if (fd == -1)
        strerr_diefu3sys(111, "open ", argv[i], " for reading") ;
      if (!(*f)(fd, lines))
        strerr_diefu2sys(111, "head ", argv[i]) ;
      fd_close(fd) ;
    }
  }
  return 0 ;
}