html_check.zip/html_check.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
FILE *out;
int flag = 0;
int check(char *fn){
FILE *fp,*tmp;
char str[512],*s,*e;
fp = fopen(fn,"r");
if(fp == NULL)return 1;
if(strstr(fn,".htm") != NULL){
while(fgets(str,512,fp) != NULL){
if(NULL != (s = strstr(str,"href")) )s += 4;
else if(NULL != (s = strstr(str,"src")) )s += 3;
else if(NULL != (s = strstr(str,"action")) )s += 5;
else continue;
while(*s == ' ')s++;
if(*s != '=')continue;
s++;
while(*s == ' ')s++;
if(*s == '"')s++;
if(NULL == (e = strchr(s,'"')) ){
if(NULL == (e = strchr(s,' ')) )continue;
}
*e = '\0';
if(*s != '\0' && *s != '#' && strncmp(s,"mailto:",7)){
if(NULL != (tmp = fopen(s,"r")) ){
fclose(tmp);
if(flag == 0)fprintf(out,"\t%s OK\n",s);
} else fprintf(out,"\t%s NG\n",s);
}
}
}
fclose(fp);
return 0;
}
void dir_list(char *str){
HANDLE hSearch;
WIN32_FIND_DATA fd;
int sp;
static char path[256] = "";
if(!SetCurrentDirectory(str))return;
strcat(path,str);
strcat(path,"/");
fprintf(out,"%s\n",path);
hSearch = FindFirstFile("*", &fd );
if( hSearch == INVALID_HANDLE_VALUE )exit(0);
while(1){
if(*fd.cFileName != '.'){
fprintf(out,"%s%s\n",path,fd.cFileName);
if(check(fd.cFileName) == 1)dir_list(fd.cFileName);
}
if(!FindNextFile( hSearch, &fd )){
if( GetLastError() == ERROR_NO_MORE_FILES ){
break;
}
}
}
FindClose( hSearch );
if(strlen(path) > 2){
SetCurrentDirectory("../");
path[strlen(path)-1] = '\0';
*(strrchr(path,'/')+1) = '\0';
}
return;
}
int main(){
char str[256];
printf("exe以下のフォルダ内に不正なリンクをもったhtmlが無いかチェックします\n正常なリンクも出力するなら0を\n不正なリンクのみ出力するなら1を入力してください\n>");
gets(str);
flag = atoi(str);
out = fopen("address_list.txt","w");
dir_list(".");
fclose(out);
out = fopen("address_list.txt","r");
while(fgets(str,256,out) != NULL){
printf("%s",str);
}
fclose(out);
printf("処理終了しました\n出力と同じものをaddress_list.txtに保存してあります。\n多すぎて途切れていたり、コピペしたいならそちらで\n");
printf("enterで終了します\n");
gets(str);
return 0;
}