遍历源代码,整理define的内容
代码未测试,基本逻辑实现过程已经描述出来了。
1 #include <io.h> 2 #include <time.h> 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <fcntl.h> 7 #include <sys/stat.h> 8 #include <unistd.h> 9 #include <tchar.h> 10 #include <dirent.h> 11 #include <wchar.h> 12 #include <winsock2.h> 13 #include <Windows.h> 14 15 struct chainFile{ 16 struct chainFile *next; 17 char hfilepath[260]; //h头文件路径 18 }; 19 20 struct extFile{ 21 struct extFile *next; 22 char cfilepath[260]; //c文件路径 23 struct chainFile *chain; 24 }; 25 26 struct defVar{ 27 struct defVar *next; 28 char macro[64]; //宏名称 29 char *displace;//置换值 30 }; 31 32 struct chainFile *cF; 33 struct extFile *eP,eF; 34 struct defVar *dP,dV; 35 36 //后期做环境变量处理 37 char incpath[4096]; 38 39 40 int lstat(const char *str, struct stat *sb) 41 { 42 int fd, rv; 43 44 if ((fd = open(str, 0)) < 0) 45 return (-1); 46 rv = fstat(fd, sb); 47 (void)close(fd); 48 return (rv); 49 } 50 51 //注意源代码必须保证是只读。 52 /* 53 第二遍遍历源代码,整理define的内容,这只是代码分析工具非编译器,所以并没有把代码中所有的define替换成对应值。 54 把所有define放一起,不考虑条件判断与重复值。 55 */ 56 int extdefine(char *file) 57 { 58 int fd; 59 char *pl; 60 struct stat fst; 61 unsigned long int i,n,s,ret,marks; 62 unsigned char temp[4096],buf[3200000]; 63 64 fd = open(file,O_RDONLY|O_BINARY,00700); 65 if(fd == -1)return printf("open file - %s error...\n",file); 66 fstat( fd, &fst ); 67 68 lseek( tp, 0, SEEK_SET );//再定位文件指针到文件头 69 70 if(fst.st_size == 0)return printf("file size is zeero...\n"); 71 else if(fst.st_size > 3200000)return printf("sory file size too large...\n"); 72 //直接读内存缓冲区 73 ret=read(fd,&buf,fst.st_size); 74 if(ret != fst.st_size)return printf("file read size is something the matter...\n"); 75 //开始循环提取一行数据 76 for(i=0;i<ret;i++) 77 { 78 pl=&buf[i]; 79 //pl 就是一行的数据 #include <io.h> or #include <" .... 80 for(;pl[i]!='#' && pl[i];i++); 81 for(i++;pl[i]==0x20 || pl[i]== 0x09;i++); 82 //跳过 " "Space //判断后面6个字母include 83 if(pl[i]=='d' && pl[i+1]=='e' && pl[i+2]=='f' && pl[i+3]=='i' && 84 pl[i+4]=='n' && pl[i+5]=='e') 85 { 86 i+=6; 87 //直接跳过Space tab... 88 for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++); 89 while(dP->next)dP=dP->next; 90 dP->next = malloc(sizeof(struct defVar)); 91 memset(dP->next,0,sizeof(struct defVar)); 92 //宏名称是不会出现特殊符号的。宏名称限定在64字符内。 93 for(n=0;n<64 && pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=0x0d && pl[i]!=0x0a && pl[i];i++,n++) 94 { 95 dP->macro[n] = pl[i];//复制宏名称 96 } 97 dP->macro[n] = 0; 98 //跳过特殊符号 99 for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++); 100 //宏类型 [[字符串/数值/条件语句/变量(指向另一个定义)]]... 101 //第一个是双引号就是字符串定义 102 if(pl[i]==0x22) 103 { 104 for(i++,n=0;n<4096 && pl[i];n++,i++) 105 { 106 if(pl[i]==0x5c && pl[i+1]==0x22)//[[\"]] 107 { 108 temp[n] = pl[i]; 109 n++,i++; 110 temp[n] = pl[i]; 111 } 112 else if(pl[i]==0x5c && (pl[i+1]==0x0d || pl[i+1]==0x0a))//[[\\n]] 113 { 114 //temp[n] = pl[i];//去掉转移符[[\]],置换时候不需要这个符号 115 i++;if(pl[i]==0x0d || pl[i]==0x0a)temp[n] = pl[i]; 116 if(pl[i+1]==0x0a){i++;n++;temp[n] = pl[i];} 117 } 118 else if(pl[i]==0x22) 119 { 120 for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++); 121 if(pl[i]==0x22)continue;//字符串拼接 122 else break; 123 } 124 temp[n] = pl[i]; 125 } 126 temp[n] = 0; 127 //marks=1; 128 } 129 else if(pl[i]=='0' && (pl[i+1]=='x' || pl[i+1]=='X')) 130 { 131 n=0; 132 temp[n] = '0'; 133 n++;i++; 134 temp[n] = 'x'; 135 for(n++,i++;pl[i];n++,i++) 136 { 137 if(pl[i]>='0' || pl[i]<='9')temp[n] = pl[i]; 138 else if(pl[i]>='a' || pl[i]<='f')temp[n] = pl[i]; 139 else if((pl[i]>='A' || pl[i]<='F'))temp[n] = pl[i]+32;//转换成小写 140 else {for(;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!=0x0d && pl[i]!=0x0a;i++);break;} 141 } 142 temp[n] = 0; 143 } 144 /*支持定义二进制[[1024 == 0b10000000000]]... 145 else if(pl[i]=='0' && (pl[i+1]=='b' || pl[i+1]=='B')) 146 { 147 148 }*/ 149 else 150 { 151 for(n=0;pl[i];n++,i++) 152 { 153 if(pl[i]==0x5c && (pl[i+1]==0x0d || pl[i+1]==0x0a)) 154 { 155 i++;if(pl[i]==0x0d || pl[i]==0x0a)temp[n] = pl[i]; 156 if(pl[i+1]==0x0a){i++;n++;temp[n] = pl[i];} 157 } 158 else if(pl[i]==0x0d || pl[i]==0x0a)break; 159 temp[n] = pl[i]; 160 } 161 } 162 dP->displace = strdup(temp);//置换值 163 //跳过特殊符号 164 for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++); 165 } 166 //#后面可能是 #define #elif #else #endif #error #if #ifdef #ifndef #line #pragma #undef #include #include_next 167 //... 168 } 169 close(fd); 170 } 171 172 173 void ListFolderContents(const char *root) 174 { 175 DIR *sdir; 176 DWORD attrib; 177 char subdir[512],dupdir[1024]; 178 struct dirent *ptr; 179 struct stat stbuf; 180 181 sdir = opendir(root); 182 183 while((ptr = readdir(sdir))!=NULL) 184 { 185 if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue; 186 //判断是目录还是文件或者链接 187 sprintf(subdir,"%s%s",root,ptr->d_name); 188 lstat(subdir,&stbuf); 189 //printf("[root=%s]\nsubdir: %s stbuf.st_mode=%x isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode)); 190 //Sleep(3000);//调试 191 unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0); 192 if (len == 0) return printf("error ---001----------\n"); 193 else 194 { 195 memset(dupdir,0,1024); 196 len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len); 197 if (len == 0) return printf("error ---002----------\n"); 198 } 199 printf("%s\n",subdir);//遍历输出全部的文件 200 //Sleep(1000); 201 attrib = GetFileAttributesW(dupdir); 202 //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib); 203 //遍历子目录 204 //if(S_ISDIR(stbuf.st_mode)) //linux 205 if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0) 206 { 207 strcat(subdir,"\\"); 208 ListFolderContents(subdir); 209 } 210 } 211 closedir(sdir); 212 } 213 214 215 int main(int argc, char *argv[]) 216 { 217 int len; 218 static char ServicePath[512]; 219 220 memset(dV,0,sizeof(struct defVar)); 221 dP = &dV; 222 223 if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) ) 224 { 225 return printf("Cannot install service (%d)\n", GetLastError()); 226 } 227 for(len=strlen(ServicePath);len>0;len--) 228 if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;} 229 230 ListFolderContents(ServicePath);//windows 231 232 }
Serious. Nonsense