53 lines
1.1 KiB
C
53 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <errno.h>
|
|
#include <ctype.h>
|
|
|
|
int linenum;
|
|
|
|
int getword(FILE *, char *, int);
|
|
|
|
int main(int argc, char *argv[]) {
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
FILE *fp = fopen(argv[i], "r");
|
|
if (fp == NULL) {
|
|
fprintf(
|
|
stderr,
|
|
"%s: can't open '%s' (%s)\n",
|
|
argv[0], argv[i], strerror(errno)
|
|
);
|
|
} else {
|
|
doubleword(argv[i], fp);
|
|
fclose(fp);
|
|
}
|
|
}
|
|
if (argc == 1) doubleword(NULL, stdin);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int getword(FILE *fp, char *buf, int size) {
|
|
int c;
|
|
c = getc(fp);
|
|
|
|
// Scan forward to nonspace or EOF.
|
|
for ( ; c != EOF && isspace(c); c = getc(fp))
|
|
if (c == '\n')
|
|
linenum++;
|
|
|
|
// Copy word to buff.
|
|
{
|
|
int i = 0;
|
|
for ( ; c != EOF && !isspace(c); c = getc(fp))
|
|
if (i < size - 1)
|
|
buf[i++] = tolower(c);
|
|
if (i < size)
|
|
buf[i] = '\0';
|
|
}
|
|
|
|
if (c != EOF)
|
|
ungetc(c, fp);
|
|
return buf[0] != '\0';
|
|
}
|