#include <stdio.h>
#include <stdlib.h>

/* CRコードをカウントする。 */

int main(int argc, char *args[]) {
	
	int ch;
	int count = 0;
	FILE *file;
	
	if (argc == 2) {
		file = fopen(args[1], "r");
		if (file == NULL) {
			perror(NULL);
			exit(EXIT_FAILURE);
		}
	} else if (argc > 2) {
		printf("arguments are 0 or 1.\n");
		return 0;
	} else {
		file = stdin;
	}
	
	while ((ch = fgetc(file)) != EOF) {
		if (ch == 0x0D) { /* 残念でした！ バイナリファイルとして開かないと検出できません！ */
			++count;
		}
	}
	
	printf("Count CR Code (0x0D): %d\n", count);
	
	if (file != stdin) {
		fclose(file);
	}
	
	return 0;
}
