04/10/12 01:20:51 ADUO8BdO
#!/bin/sh
cat > /tmp/count_c.c <<EOS
#include <stdio.h>
int main(int argc,char *argv[]){
FILE *fp;
char c;
int count=0;
if(argc != 2 && argc != 3){fprintf(stderr,"Usage: %s char [file]\n",argv[0]);exit(1);}
if(argc == 3){
if((fp = fopen(argv[2],"r")) == NULL){perror(argv[2]);exit(1);}
}else{fp = stdin;}
while((c=getc(fp)) != EOF){
if(c == argv[1][0])count++;
}
printf("%d\n",count);
return 0;
}
EOS
gcc -o /tmp/count_c /tmp/count_c.c
/tmp/count_c $1 $2
rm /tmp/count_c /tmp/count_c.c