wc.c

Go to the documentation of this file.
00001 /* wc - count lines, words and characters       Author: David Messer */
00002 
00003 #include <ctype.h>
00004 #include <stdlib.h>
00005 #include <stdio.h>
00006 
00007 /*
00008  *
00009  *      Usage:  wc [-lwc] [names]
00010  *
00011  *              Flags:
00012  *                      l - count lines.
00013  *                      w - count words.
00014  *                      c - count characters.
00015  *
00016  *              Flags l, w, and c are default.
00017  *              Words are delimited by any non-alphabetic character.
00018  *
00019  *  Released into the PUBLIC-DOMAIN 02/10/86
00020  *
00021  *      If you find this program to be of use to you, a donation of
00022  *      whatever you think it is worth will be cheerfully accepted.
00023  *
00024  *      Written by: David L. Messer
00025  *                              P.O. Box 19130, Mpls, MN,  55119
00026  *      Program (heavily) modified by Andy Tanenbaum
00027  */
00028 
00029 
00030 int lflag;                      /* Count lines */
00031 int wflag;                      /* Count words */
00032 int cflag;                      /* Count characters */
00033 
00034 long lcount;                    /* Count of lines */
00035 long wcount;                    /* Count of words */
00036 long ccount;                    /* Count of characters */
00037 
00038 long ltotal;                    /* Total count of lines */
00039 long wtotal;                    /* Total count of words */
00040 long ctotal;                    /* Total count of characters */
00041 
00042 _PROTOTYPE(int main, (int argc, char **argv));
00043 _PROTOTYPE(void count, (FILE *f));
00044 _PROTOTYPE(void usage, (void));
00045 
00046 int main(argc, argv)
00047 int argc;
00048 char *argv[];
00049 {
00050   int k;
00051   char *cp;
00052   int tflag, files;
00053 
00054   /* Get flags. */
00055   files = argc - 1;
00056   k = 1;
00057   cp = argv[1];
00058   if (argc > 1 && *cp++ == '-') {
00059         files--;
00060         k++;                    /* points to first file */
00061         while (*cp != 0) {
00062                 switch (*cp) {
00063                     case 'l':   lflag++;        break;
00064                     case 'w':   wflag++;        break;
00065                     case 'c':   cflag++;        break;
00066                     default:    usage();
00067                 }
00068                 cp++;
00069         }
00070   }
00071 
00072   /* If no flags are set, treat as wc -lwc. */
00073   if (!lflag && !wflag && !cflag) {
00074         lflag = 1;
00075         wflag = 1;
00076         cflag = 1;
00077   }
00078 
00079   /* Process files. */
00080   tflag = files >= 2;           /* set if # files > 1 */
00081 
00082   /* Check to see if input comes from std input. */
00083   if (k >= argc) {
00084         count(stdin);
00085         if (lflag) printf(" %6ld", lcount);
00086         if (wflag) printf(" %6ld", wcount);
00087         if (cflag) printf(" %6ld", ccount);
00088         printf(" \n");
00089         fflush(stdout);
00090         exit(0);
00091   }
00092 
00093   /* There is an explicit list of files.  Loop on files. */
00094   while (k < argc) {
00095         FILE *f;
00096 
00097         if ((f = fopen(argv[k], "r")) == NULL) {
00098                 fprintf(stderr, "wc: cannot open %s\n", argv[k]);
00099         } else {
00100                 count(f);
00101                 if (lflag) printf(" %6ld", lcount);
00102                 if (wflag) printf(" %6ld", wcount);
00103                 if (cflag) printf(" %6ld", ccount);
00104                 printf(" %s\n", argv[k]);
00105                 fclose(f);
00106         }
00107         k++;
00108   }
00109 
00110   if (tflag) {
00111         if (lflag) printf(" %6ld", ltotal);
00112         if (wflag) printf(" %6ld", wtotal);
00113         if (cflag) printf(" %6ld", ctotal);
00114         printf(" total\n");
00115   }
00116   fflush(stdout);
00117   return(0);
00118 }
00119 
00120 void count(f)
00121 FILE *f;
00122 {
00123   register int c;
00124   register int word = 0;
00125 
00126   lcount = 0;
00127   wcount = 0;
00128   ccount = 0L;
00129 
00130   while ((c = getc(f)) != EOF) {
00131         ccount++;
00132 
00133         if (isspace(c)) {
00134                 if (word) wcount++;
00135                 word = 0;
00136         } else {
00137                 word = 1;
00138         }
00139 
00140         if (c == '\n' || c == '\f') lcount++;
00141   }
00142   ltotal += lcount;
00143   wtotal += wcount;
00144   ctotal += ccount;
00145 }
00146 
00147 void usage()
00148 {
00149   fprintf(stderr, "Usage: wc [-lwc] [name ...]\n");
00150   exit(1);
00151 }

Generated on Fri Apr 14 22:57:13 2006 for minix by  doxygen 1.4.6