第四遍遍历源代码,struct union enum

思路、待完善...

  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 sueChain{
 16     struct defSue *next;
 17     char name[64];
 18     char mold;                /*struct union enum*/
 19     char *sue;                /*指向一个结构体字符串*/
 20 }
 21 
 22 struct sueChain *scP,sC;
 23 
 24 int lstat(const char *str, struct stat *sb)
 25 {
 26     int fd, rv;
 27 
 28     if ((fd = open(str, 0)) < 0)
 29         return (-1);
 30     rv = fstat(fd, sb);
 31     (void)close(fd);
 32     return (rv);
 33 }
 34 
 35 //第四遍遍历源代码,struct union enum
 36 int sueCollect(char *file)
 37 {
 38     int fd;
 39     char *pl;
 40     struct stat fst;
 41     unsigned long int i,ret;
 42     unsigned char temp[4096],buf[3200000];
 43     
 44     fd = open(file,O_RDONLY|O_BINARY,00700);
 45     if(fd == -1)return printf("open file - %s error...\n",file);
 46     fstat( fd, &fst );
 47     
 48     lseek( fd, 0, SEEK_SET );//再定位文件指针到文件头
 49     
 50     if(fst.st_size == 0)return printf("file size is zeero...\n");
 51     else if(fst.st_size > 3200000)return printf("sory file size too large...\n");
 52     //直接读内存缓冲区
 53     ret=read(fd,&buf,fst.st_size);
 54     if(ret != fst.st_size)return printf("file read size is something the matter...\n");
 55     //开始循环提取一行数据
 56     for(i=0;i<ret;i++)
 57     {
 58         pl=&buf[i];
 59         //pl 就是一行的数据 struct 判断后面5个字母truct
 60         for(;pl[i]!='s' && pl[i]!='u' && pl[i]!='e' && pl[i];i++);
 61         if(pl[i]=='t' && pl[i+1]=='r' && pl[i+2]=='u' && pl[i+3]=='c' &&
 62         pl[i+4]=='t')
 63         {
 64             i+=5;
 65             //直接跳过Space tab...
 66             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 67             while(scP->next)scP=scP->next;
 68             scP->mold = 1;
 69             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!='{' && pl[i]!=';' && pl[i];i++,n++)
 70                 scP->name[n] = pl[i];
 71             ...判断是否重名
 72             如果没有'{...}'定义边际符号说明是全局定义。
 73             scP->name[n] = 0;
 74             strcpy(temp,scP->name);
 75             for(;pl[i]!='}' && pl[i];i++,n++)temp[n] = pl[i];
 76             if(pl[i]=='}'){temp[n] = pl[i];n++;}
 77             for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 78             if(pl[i]==';')i++;//末尾是';'收尾。
 79             temp[n] = ';';n++;
 80             temp[n] = 0;
 81             scP->sue = strdup(temp);
 82             scP->next = malloc(sizeof(struct sueChain));
 83             memset(scP->next,0,sizeof(struct sueChain));
 84         }
 85         else if(pl[i]=='n' && pl[i+1]=='i' && pl[i+2]=='o' && pl[i+3]=='n')
 86         {
 87             i+=4;
 88             //直接跳过Space tab...
 89             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 90             while(scP->next)scP=scP->next;
 91             scP->mold = 2;
 92             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!='{' && pl[i]!=';' && pl[i];i++,n++)
 93                 scP->name[n] = pl[i];
 94             scP->name[n] = 0;
 95             strcpy(temp,scP->name);
 96             for(;pl[i]!='}' && pl[i];i++,n++)temp[n] = pl[i];
 97             if(pl[i]=='}'){temp[n] = pl[i];n++;}
 98             for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
 99             if(pl[i]==';')i++;//末尾是';'收尾。
100             temp[n] = ';';n++;
101             temp[n] = 0;
102             scP->sue = strdup(temp);
103             scP->next = malloc(sizeof(struct sueChain));
104             memset(scP->next,0,sizeof(struct sueChain));
105         }
106         else if(pl[i]=='n' && pl[i+1]=='u' && pl[i+2]=='m')
107         {
108             i+=3;
109             //直接跳过Space tab...
110             for(;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
111             while(scP->next)scP=scP->next;
112             scP->mold = 3;
113             for(n=0;pl[i]!=0x20 && pl[i]!=0x09 && pl[i]!='{' && pl[i]!=';' && pl[i];i++,n++)
114                 scP->name[n] = pl[i];
115             scP->name[n] = 0;
116             strcpy(temp,scP->name);
117             for(;pl[i]!='}' && pl[i];i++,n++)temp[n] = pl[i];
118             if(pl[i]=='}'){temp[n] = pl[i];n++;}
119             for(i++;pl[i]==0x20 || pl[i]==0x09 || pl[i]==0x0d || pl[i]==0x0a;i++);
120             if(pl[i]==';')i++;//末尾是';'收尾。
121             temp[n] = ';';n++;
122             temp[n] = 0;
123             scP->sue = strdup(temp);
124             scP->next = malloc(sizeof(struct sueChain));
125             memset(scP->next,0,sizeof(struct sueChain));
126         }
127     }
128     close(fd);
129 }
130 
131 
132 void ListFolderContents(const char *root)
133 {
134     DIR *sdir;
135     DWORD attrib;
136     char subdir[512],dupdir[1024];
137     struct dirent *ptr;
138     struct stat stbuf;
139     
140     sdir = opendir(root);
141     
142     while((ptr = readdir(sdir))!=NULL)
143     {
144         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)continue;
145         //判断是目录还是文件或者链接
146         sprintf(subdir,"%s%s",root,ptr->d_name);
147         lstat(subdir,&stbuf);
148         //printf("[root=%s]\nsubdir: %s            stbuf.st_mode=%x            isdir=%d\n",root,subdir,stbuf.st_mode,S_ISDIR(stbuf.st_mode));
149         //Sleep(3000);//调试
150         unsigned len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), NULL, 0);
151         if (len == 0) return printf("error ---001----------\n");
152         else
153         {
154             memset(dupdir,0,1024);
155             len = MultiByteToWideChar(0, 0, subdir, strlen(subdir), dupdir, len);
156             if (len == 0) return printf("error ---002----------\n");
157         }
158         printf("%s\n",subdir);//遍历输出全部的文件
159         //Sleep(1000);
160         attrib = GetFileAttributesW(dupdir);
161         //printf("[attrib=%d][attrib=%ul]\n",attrib,attrib);
162         //遍历子目录
163         //if(S_ISDIR(stbuf.st_mode)) //linux
164         if(attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY) != 0)
165         {
166             strcat(subdir,"\\");
167             ListFolderContents(subdir);
168         }
169     }
170     closedir(sdir);
171 }
172 
173 
174 int main(int argc, char *argv[])
175 {
176     int len;
177     static char ServicePath[512];
178 
179     memset(sC,0,sizeof(struct sueChain));
180     scP = &sC;
181     
182     if( !GetModuleFileName( GetModuleHandle(NULL), ServicePath, 256 ) )  
183     {
184         return printf("Cannot install service (%d)\n", GetLastError());  
185     }
186     for(len=strlen(ServicePath);len>0;len--)
187         if(ServicePath[len]==0x5c){ServicePath[len+1]=0;break;}
188     
189     ListFolderContents(ServicePath);//windows
190     
191 }

 

posted @ 2020-09-17 16:47  一本正经-胡说八道  阅读(189)  评论(0编辑  收藏  举报