File: csize.c

package info (click to toggle)
metrics 1.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 452 kB
  • ctags: 342
  • sloc: ansic: 2,101; lex: 747; makefile: 250; yacc: 233; sh: 209
file content (147 lines) | stat: -rw-r--r-- 3,923 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
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
#include <stdio.h>

#include <csize.h>
#include <patchlevel.h>

/*
 * $Id: csize.c,v 1.14 1995/01/25 08:19:24 lott Exp $
 *
 * csize, a program to measure the size of C source files.
 * Copyright (C) 1994 Christopher Lott <lott@informatik.uni-kl.de>
 * FB Informatik - Bau 57 / Universitaet KL / D--67653 Kaiserslautern / Germany
 *
 * 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.  See the file COPYING for more details.
 *
 * This file holds functions to open files, add total counts,
 * and print summaries.  All the real work is done in the scanner.
 * All global variables are likewise declared in the scanner;
 * they are also declared externally in the the header file.
 *
 */

void
print_results(long nl, 
	      long bl, 
	      long lwc, 
	      long nbncl, 
	      long semi, 
	      long pp, 
	      char *filename)
{
  (void) printf("%8ld %8ld %8ld %8ld %8ld %8ld %s\n",
	 nl, bl, lwc, nbncl, semi, pp, filename);
}


int 
process_file(char *filename)
{
  FILE *fp;
  int rc;

  fp = fopen(filename, "r");
  if (fp == NULL) {
    perror(filename);
    rc = -1;
  }
  else {
    if (Echo) (void) printf("--start of file--\n");
    /* process the input */
    init_scanner(fp, filename);
    while (yylex() != 0)
      ;
    if (Echo) (void) printf("--end of file--\n");
    (void) fclose(fp);
    rc = Lex_errors;
  }

  return rc;
}


int
main(int argc, char ** argv)
{
  /* this would be the place to provide a prototype for 
   * getopt(), but if I do so, g++ is unable to link in
   * the function from the library.  So I just let g++
   * complain about the missing prototype.  Not fatal.
   *
   */
  extern int optind;

  int c, 
      rc,
      errflg = 0, 
      hflg = 0,
      verflg = 0,
      nfiles = 0;
  long total_newlines = 0, 
       total_blank_lines = 0, 
       total_lines_w_comments = 0,
       total_nb_nc_lines = 0,
       total_semicolons = 0,
       total_pp_directives = 0;

  while ((c = getopt(argc, argv, "ehv")) != -1)
    switch (c) {
    case 'e':
      ++Echo;
      break;
    case 'h':
      ++hflg;
      break;
    case 'v':
      ++verflg;
      break;
    default:
      ++errflg;
    }

  /* If -v was given then ignore other args, print version info, and exit */
  if (verflg) {
    fprintf(stderr, "csize version information: %s\n", CSIZE_VERSION);
    return 0;
  }

  /* invalid arguments */
  if (argc == 1 || optind == argc || errflg) {
    (void)fprintf(stderr, "usage: %s [ -ehv ] file.c [ file.c ... ]\n", *argv);
    (void)fprintf(stderr, "       -e == echo the input files\n");
    (void)fprintf(stderr, "       -h == print a header before the data\n");
    (void)fprintf(stderr, "       -v == report version information\n");
    return -1;
  }

  /* arguments are ok, do some work */
  for (; optind < argc; optind++) {
    rc = process_file(argv[optind]);
    if (rc == 0) {
      if (hflg) {
	(void)printf("   total    blank lines w/   nb, nc    semi- preproc. file\n");
	(void)printf("   lines    lines comments    lines   colons  direct.\n");
	(void)printf("--------+--------+--------+--------+--------+--------+----\n");
	hflg = 0;  /* only print the header once */
      }
      print_results(C_newlines, C_blank_lines, C_lines_w_comments, 
		    C_nb_nc_lines, C_semicolons, C_pp_directives, 
		    argv[optind]);
      total_newlines         += C_newlines;
      total_blank_lines      += C_blank_lines;
      total_lines_w_comments += C_lines_w_comments;
      total_nb_nc_lines      += C_nb_nc_lines;
      total_semicolons       += C_semicolons;
      total_pp_directives    += C_pp_directives;
      ++nfiles;
    }
  }
  if (nfiles > 1) {
    print_results(total_newlines, total_blank_lines, 
		  total_lines_w_comments, total_nb_nc_lines,
		  total_semicolons, total_pp_directives, "total");
  }
  
  return rc;
}