程序博客网 > 报名源码asp
来源:互联网 发布:报名源码asp 编辑:程序博客网 时间:2024/06/06 12:50
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <locale.h>#include <ctype.h>#include <err.h>#include <sys/param.h>#include <sys/stat.h>#include <sys/file.h>#include <unistd.h>#include <util.h>int64_ttlinect, twordct, tcharct;intdoline, doword, dochar, humanchar;int rval;extern char *__progname;voidprint_counts(int64_t, int64_t, int64_t, char *);voidcnt(char *);intmain(int argc, char *argv[]){int ch;setlocale(LC_ALL, "");while ((ch = getopt(argc, argv, "lwchm")) != -1)switch((char)ch) {case 'l':doline = 1;break;case 'w':doword = 1;break;case 'c':case 'm':dochar = 1;break;case 'h':humanchar = 1;break;case '?':default:(void)fprintf(stderr, "usage: %s [-c | -m] [-hlw] [file ...]/n", __progname);exit(1);}argv += optind;argc -= optind;/* * wc is unusual in that its flags are on by default, so, * if you don't get any arguments, you have to turn them * all on. */if (!doline && !doword && !dochar)doline = doword = dochar = 1;if (!*argv) {cnt((char *)NULL);} else {int dototal = (argc > 1);do {cnt(*argv);} while(*++argv);if (dototal)print_counts(tlinect, twordct, tcharct, "total");}exit(rval);}voidcnt(char *file){u_char *C;short gotsp;int len;int64_t linect, wordct, charct;struct stat sbuf;int fd;u_char buf[MAXBSIZE];linect = wordct = charct = 0;if (file) {if ((fd = open(file, O_RDONLY, 0)) < 0) {warn("%s", file);rval = 1;return;}} else {fd = STDIN_FILENO;}if (!doword) {/* * Line counting is split out because it's a lot * faster to get lines than to get words, since * the word count requires some logic. */if (doline) {while ((len = read(fd, buf, MAXBSIZE)) > 0) {charct += len;for (C = buf; len--; ++C)if (*C == '/n')++linect;}if (len == -1) {warn("%s", file);rval = 1;}}/* * If all we need is the number of characters and * it's a directory or a regular or linked file, just * stat the puppy. We avoid testing for it not being * a special device in case someone adds a new type * of inode. */else if (dochar) {mode_t ifmt;if (fstat(fd, &sbuf)) {warn("%s", file);rval = 1;} else {ifmt = sbuf.st_mode & S_IFMT;if (ifmt == S_IFREG || ifmt == S_IFLNK || ifmt == S_IFDIR) {charct = sbuf.st_size;} else {while ((len = read(fd, buf, MAXBSIZE)) > 0)charct += len;if (len == -1) {warn("%s", file);rval = 1;}}}}} else {/* Do it the hard way... */gotsp = 1;while ((len = read(fd, buf, MAXBSIZE)) > 0) {/* * This loses in the presence of multi-byte characters. * To do it right would require a function to return a * character while knowing how many bytes it consumed. */charct += len;for (C = buf; len--; ++C) {if (isspace (*C)) {gotsp = 1;if (*C == '/n')++linect;} else {/* * This line implements the POSIX * spec, i.e. a word is a "maximal * string of characters delimited by * whitespace." Notice nothing was * said about a character being * printing or non-printing. */if (gotsp) {gotsp = 0;++wordct;}}}}if (len == -1) {warn("%s", file);rval = 1;}}print_counts(linect, wordct, charct, file);/* * Don't bother checking doline, doword, or dochar -- speeds * up the common case */tlinect += linect;twordct += wordct;tcharct += charct;if (close(fd) != 0) {warn("%s", file);rval = 1;}}void format_and_print(long long v){if (humanchar) {char result[FMT_SCALED_STRSIZE];(void)fmt_scaled(v, result);(void)printf("%7s", result);} else {(void)printf(" %7lld", v);}}voidprint_counts(int64_t lines, int64_t words, int64_t chars, char *name){if (doline)format_and_print((long long)lines);if (doword)format_and_print((long long)words);if (dochar)format_and_print((long long)chars);if (name)(void)printf(" %s/n", name);else(void)printf("/n");}
看看效果如何?