C 循环将牵引文件加入到变量中

C 代码分析工具最基础的一步吧

  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 int lstat(const char *str, struct stat *sb)
 16 {
 17     int fd, rv;
 18 
 19     if ((fd = open(str, 0)) < 0)
 20         return (-1);
 21     rv = fstat(fd, sb);
 22     (void)close(fd);
 23     return (rv);
 24 }
 25 
 26 //先理一下思路大概实现过程。待续...
 27 filesarray(const char *file)
 28 {
 29     int fd;
 30     char *pl;
 31     struct stat fst;
 32     unsigned int i,ret;
 33     unsigned char buf[3200000];
 34     
 35     fd = open(file,O_RDONLY|O_BINARY,00700);
 36     if(fd == -1)return printf("open file - %s error...\n",file);
 37     fstat( fd, &fst );
 38     
 39     lseek( tp, 0, SEEK_SET );//再定位文件指针到文件头
 40     
 41     
 42     /*
 43     //一行行读
 44     for(i=0;i<fst.st_size;)
 45     {
 46         
 47     }*/
 48     if(fst.st_size == 0)return printf("file size is zeero...\n");
 49     else if(fst.st_size > 3200000)return printf("sory file size too large...\n");
 50     //直接读内存缓冲区
 51     ret=read(fd,&buf,fst.st_size);
 52     if(ret != fst.st_size)return printf("file read size is something the matter...\n");
 53     //开始循环提取一行数据
 54     for(i=0;i<ret;i++)
 55     {
 56         pl=&buf[0];
 57         if(buf[i]==0x0d || buf[i]==0x0a)
 58         {
 59             buf[i]==0x0;
 60             //跳过换行符
 61             for(i++;buf[i]==0x0d || buf[i]==0x0a;i++);
 62             //pl 就是一行的数据 #include  <io.h> or #include <" ....
 63             (envroot,linecode)
 64             {
 65                 int i,v,z,n;
 66                 char retstr[260];
 67                 
 68                 retstr[0] = 0;
 69                 z = strlen(linecode);
 70                 
 71                 for(i=0;i<z;i++)
 72                 {
 73                     //没有#找到就退出了没有后来了哦
 74                     if(linecode[i]=='#')
 75                     {
 76                         for(v=i+1;linecode[v]==0x20 || linecode[v]== 0x09;v++);
 77                         //include
 78                         i = v;//跳过 " "Space //判断后面6个字母对不对哦
 79                         if(linecode[i]=='i' &&
 80                         linecode[i+1]=='n' &&
 81                         linecode[i+2]=='c' &&
 82                         linecode[i+3]=='l' &&
 83                         linecode[i+4]=='u' &&
 84                         linecode[i+5]=='d' &&
 85                         linecode[i+6]=='e' &&
 86                         )
 87                         {
 88                             //直接跳过< or " or Space...
 89                             for(v=i+7;linecode[v]==0x3c || linecode[v]== 0x22 || linecode[v]==0x20 || linecode[v]== 0x09;v++);
 90                             n = 0;
 91                             while(linecode[v] && (linecode[v]!=0x3c || linecode[v]!=0x22))
 92                             {
 93                                 retstr[n] = linecode[v];
 94                                 n++;
 95                                 v++;
 96                             }
 97                             retstr[n] = 0;//截断一下
 98                             return retstr;
 99                         }
100                         //...go...
101                     }
102                 }
103                 return retstr;
104             }
105             ///envroot + retstr
106             
107             //获取到的路径与envlists列表中的值进行对比,如果没有存在再加入进去
108             //envlists
109             //...
110             
111             
112             i--;//like
113             continue;
114         }
115         //;//like 如果只有一行没有回车
116         if((i+1)==ret)
117         {
118             //
119             break;
120         }
121     }
122     
123     
124     close(fd);
125 }
126 //打开文件把整个个文件内容读入内存缓冲区,第一次做我们不考虑大文件了直接分配一个3M空间,谁一个文件3M我也很佩服了
127 //代码书写一般都是一行一个文件
128 //#include  <io.h>
129 //#include ""
130 //读行数据,然后再读文件变量
131 //如果遇到#符号判断后面是否为include,跳过空格,直到遇到" or < 。记录标。开始提取文件名。遇到" or >结束。这里不做检查#include  <io.h" 也是允许的
132 //-----[[[[addfile 如果是以< 开头的]]]]这里只考虑先所有的头文件都在一个根目录下,如果补全的话我们自己手动去找齐
133 
134 
135 //检查文件是否存在,存在的话进入递归
136 //有的文件会多次被引用,要排查一下
137 
138 
139 
140 
141 
142 
143 
144 //释放内存空间,循环递归下一个文件
145 
146 
147 
148 void ListFolderContents(const char *root)
149 {
150     DIR *sdir;
151     DWORD attrib;
152     char subdir[512],dupdir[1024];
153     struct dirent *ptr;
154     struct stat stbuf;
155     
156     sdir = opendir(root);
157     
158     while((ptr = readdir(sdir))!=NULL)
159     {
160         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue;
161         //判断是目录还是文件或者链接
162         sprintf(subdir,"%s%s",root,ptr->d_name);
163         lstat(subdir,&stbuf);
164         //printf("[root=%s]\nsubdir: %s            stbuf.st_mode=%x            isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode));
165         //Sleep(3000);//调试
166         unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0);
167         if (len == 0) return printf("error ---001----------\n");
168         else
169         {
170             memset(dupdir,0,1024);
171             len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len);
172             if (len == 0) return printf("error ---002----------\n");
173         }
174         printf("%s\n",subdir);//遍历输出全部的文件
175         //Sleep(1000);
176         attrib = GetFileAttributesW(dupdir);
177         //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib);
178         //遍历子目录
179         //if(S_ISDIR(stbuf.st_mode)) //linux
180         if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
181         {
182             strcat(subdir,"\\");
183             ListFolderContents(subdir);
184         }
185     }
186     closedir(sdir);
187 }
188 
189 
190 int main(int argc, char *argv[])
191 {
192     int len;
193     static char ServicePath[512];
194 
195     if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) )  
196     {
197         return printf("Cannot install service (%d)\n", GetLastError());  
198     }
199     for(len=strlen(ServicePath);len>0;len--)
200         if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;}
201     
202     ListFolderContents(ServicePath);//windows
203     
204 }

猜你喜欢

转载自www.cnblogs.com/yfii/p/13372730.html
今日推荐