File: buthead.c

package info (click to toggle)
buthead 1.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 44 kB
  • ctags: 2
  • sloc: makefile: 35; ansic: 29
file content (46 lines) | stat: -rw-r--r-- 1,111 bytes parent folder | download
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
// buthead: pass through all but first n lines of standard input

// Copyright (C) 2002, Barak A. Pearlmutter <bap@cs.unm.edu>

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(int argc, char **argv)
{
  long lines_to_skip, i;
  int c;
  char *p;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s count.\n", argv[0]);
    exit(2);
  }

  lines_to_skip = strtol(argv[1], &p, 10);

  if (errno || lines_to_skip < 0 || argv[1][0] == '\0' || *p != '\0') {
    fprintf(stderr, "buthead error: invalid line count '%s'.\n", argv[1]);
    exit(2);
  }

  for (i=0; i<lines_to_skip; ) {
    if ((c = getc(stdin)) == EOF) {
      fprintf(stderr, "buthead error: EOF on line %ld while skipping %ld.\n",
	      i+1, lines_to_skip);
      exit(1);
    }

    if (c == '\n') i += 1;
  }

  while ((c = getchar()) != EOF)
    putchar(c);

  return 0;
}