diff options
Diffstat (limited to '')
-rw-r--r-- | README | 27 | ||||
-rw-r--r-- | loc.1 | 31 | ||||
-rw-r--r-- | loc.c | 21 |
3 files changed, 44 insertions, 35 deletions
@@ -2,25 +2,28 @@ NAME loc - count number of lines of code SYNOPSIS - loc [file] ... + loc [-s] [file] ... DESCRIPTION loc counts the number of lines of code for each specified file. loc will treat each file as a C source code or C header - file. If file is -, the standard input is read. - - If no file is specified, the standard input is read, and - the number of lines of code is output on the format - - "%zu\n", <#lines of code> - - If exactly one file is specified, its number of lines of - code is output on the format - - "%zu\n", <#lines of code> + file. If file is -, or if a file is not specified, the + standard input is read. If more than one file is specified, each successfully line counted file will have its of lines of code output on the format "%s:%*s%zu\n", <file>, <some positive integer>, "", <#lines of code> + + The total for all processed files, is printed on the last + line with the format + + "%zu\n", <#total lines of code> + + If exactly one or no files are specified, this will be the + only line output. + +OPTIONS + -s + Only output the last line. (The total over all files.) @@ -13,30 +13,27 @@ will treat each file as a C source code or C header file. If .I file is .BR - , -the standard input is read. +or if a file is not specified, the standard input is read. .PP -If no +If more than one .I file -is specified, the standard input is read, and the number -of lines of code is output on the format +is specified, each successfully line counted file will have +its of lines of code output on the format .nf - \fB"%zu\n"\fP, <\fI#lines of code\fP> + \fB"%s:%*s%zu\n"\fP, <\fIfile\fP>, <\fIsome positive integer\fP>, \fB""\fP, <\fI#lines of code\fP> .fi .PP -If exactly one -.I file -is specified, its number of lines of code is output on the format +The total for all processed files, is printed on the last +line with the format .nf - \fB"%zu\n"\fP, <\fI#lines of code\fP> + \fB"%zu\n"\fP, <\fI#total lines of code\fP> .fi .PP -If more than one -.I file -is specified, each successfully line counted file will have -its of lines of code output on the format -.nf - - \fB"%s:%*s%zu\n"\fP, <\fIfile\fP>, <\fIsome positive integer\fP>, \fB""\fP, <\fI#lines of code\fP> -.fi +If exactly one or no files are specified, this will be the +only line output. +.SH OPTIONS +.TP +.B \-s +Only output the last line. (The total over all files.) @@ -21,7 +21,7 @@ struct result { static void usage(void) { - fprintf(stderr, "usage: %s [file] ...\n", argv0); + fprintf(stderr, "usage: %s [-s] [file] ...\n", argv0); exit(1); } @@ -140,11 +140,15 @@ zuwidth(size_t num) int main(int argc, char *argv[]) { - ssize_t n; + ssize_t n, total = 0; struct result *res; int i, fd, maxleft = 0, maxright = 0, maxwidth, left, right, ret = 0; + int sum_only = 0; ARGBEGIN { + case 's': + sum_only = 1; + break; default: usage(); } ARGEND; @@ -181,12 +185,17 @@ main(int argc, char *argv[]) } } maxwidth = maxleft + maxright; - for (i = 0; i < argc; i++) - if (res[i].n < 0) + for (i = 0; i < argc; i++) { + if (res[i].n < 0) { ret = 1; - else - printf("%s:%*s %zi\n", argv[i], maxwidth - res[i].width, "", res[i].n); + } else { + if (!sum_only) + printf("%s:%*s %zi\n", argv[i], maxwidth - res[i].width, "", res[i].n); + total += res[i].n; + } + } free(res); + printf("%zi\n", total); } if (fflush(stdin) || ferror(stdin) || fclose(stdin)) |