summaryrefslogtreecommitdiff
path: root/looper/looper.c (plain)
blob: d19288f0c455b1a22e970bc67a75c4efa6ddfd9b
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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
 
/*
 * This is a simple template for a program that opens a file (or reads from
 * stdin if no file is specified), then iterates over the lines of a file.
 */

void usage(char *prog) {
  fprintf(stderr, "usage: %s [-v] <file>\n", prog);
  exit(-1);
}
 
int main(int argc, char * argv[]) {
  int opt,verbose=0,lines=0;
  FILE *ifilef=stdin;
  char *ifile=NULL,line[100];
 
  while ( (opt = getopt(argc, argv, "v+")) != -1) {
    switch (opt) {
      case 'v': verbose++; break;
      default: usage(argv[0]); break;
    }
  }
  if (optind < argc) ifile=argv[optind++];
 
  /* loop over the input */
  if (ifile) {
    if ( (ifilef = fopen(ifile,"r")) == NULL) {
      fprintf(stderr,"can't open %s: %s\n", ifile, strerror(errno));
      exit(-1);
    }
  }
 
  while (fgets(line,sizeof(line),ifilef) != NULL) {
    lines++;
    if (verbose) fprintf(stderr, "read line: %s", line);
  }
  printf("%d lines read.\n",lines);
}