#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <sys/types.h>

char * check(char * c) ;
char str[5000] = "" ;


main() {
	DIR *dir;
	struct dirent *entry;

	//if ((dir = opendir("/home/virtual/tinbustock.com/www/html/news/feed/")) == NULL)
	if ((dir = opendir("/home/stock/www/news/feed/")) == NULL)
		perror("opendir() error");
	else {
		while ((entry = readdir(dir)) != NULL){
		if(strcmp(entry->d_name,".")&&strcmp(entry->d_name,"..")){
			printf("  %s\n", entry->d_name);
			char path[100];
			//char path1[] = "/home/virtual/tinbustock.com/www/html/news/feed/" ;
			char path1[] = "/home/stock/www/news/feed/";
			//char path_tmp[] = "/home/virtual/tinbustock.com/www/html/news/check/" ;
			char path_tmp[] = "/home/stock/www/news/check/";

			memset(path,'\0',sizeof(path)) ;
			strcpy(path,path1) ;
    		FILE *fr = fopen(strcat(path,entry->d_name), "r");

			memset(path,'\0',sizeof(path)) ;
			strcpy(path,path_tmp) ;
    		FILE *fw = fopen(strcat(path,entry->d_name), "wt");

    		if ( fr != NULL )
    		{
        		char line[5000];

        		while ( fgets(line,sizeof line,fr) != NULL )
        		{
            		fprintf(fw, "%s",check(line));
        		}
        		fclose(fr);
        		fclose(fw);
    		}
		}
		}
		closedir(dir);
	}
}



char *check(char *c)
{
    int unasc_end = 0;
    int index = 0 ;
    memset(str, '\0', sizeof(str)) ;

    for(int i=0; i<strlen(c); i++){
        if(isascii(c[i])){
            str[index] = c[i] ;
            index ++ ;
            unasc_end = 1 ;
        } else {
            if(unasc_end == 1){
                str[index] = '`';
                index ++ ;
                unasc_end = 0 ;
            }
        }
    }
    return str ;
}


