Download code

Jump to: navigation, search

Back to Directory_listing_(C,_Windows)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 /tmp/litprog4749588/directorylisting.c:23:21: fatal error: windows.h: No such file or directory
2 compilation terminated.


directorylisting.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Directory_listing_(C,_Windows)?oldid=16958
 14 */
 15 
 16 
 17 /*
 18 **
 19 ** A simple "dir" application. Shows detailed info.
 20 **
 21 */
 22 
 23 #include <windows.h>
 24 #include <stdlib.h>
 25 #include <stdio.h>
 26 #include <string.h>
 27 
 28 //------------------------------------------------------------------------------------------------------------------
 29 // Globals...
 30 //------------------------------------------------------------------------------------------------------------------
 31 WIN32_FIND_DATA *fd;
 32 
 33 //------------------------------------------------------------------------------------------------------------------
 34 // Protos...
 35 //------------------------------------------------------------------------------------------------------------------
 36 int     showdir(const char *path, BOOL recursive);
 37 int     fixpath(const char *inpath, char *outpath);
 38 
 39 //------------------------------------------------------------------------------------------------------------------
 40 // showdir()
 41 //------------------------------------------------------------------------------------------------------------------
 42 int showdir(const char *_path,BOOL recursive)
 43 {
 44   HANDLE fh;
 45   int    attrib_a=0;
 46   int    attrib_c=0;
 47   int    attrib_d=0;
 48   int    attrib_h=0;
 49   int    attrib_n=0;
 50   int    attrib_o=0;
 51   int    attrib_r=0;
 52   int    attrib_s=0;
 53   int    attrib_t=0;
 54   int    filecnt=0;
 55   char	 path[MAX_PATH];
 56   char	 tmppath[MAX_PATH];
 57 
 58   fd = malloc(sizeof(WIN32_FIND_DATA));
 59 
 60   fixpath(_path,path);
 61   strcat(path,"*");
 62 
 63   printf("\nScanning: %s\n",path);
 64 
 65   fh = FindFirstFile((LPCSTR) path,fd);
 66 
 67   if(fh != INVALID_HANDLE_VALUE)
 68   {
 69     do
 70     {
 71       filecnt++;
 72       if(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
 73       {
 74         printf("[%-33.33s] ",fd->cFileName);
 75         if((0 != strcmp(fd->cFileName,".")) && (0 != strcmp(fd->cFileName,"..")))
 76         {
 77           fixpath(_path,tmppath);
 78           strcat(tmppath,fd->cFileName);
 79           fixpath(tmppath,tmppath);
 80           if(recursive)
 81             showdir(tmppath,recursive);
 82         }
 83       }
 84       else
 85       {
 86         printf("%-35.35s "  ,fd->cFileName);
 87       }
 88 
 89       if(fd->dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) 		printf("A") && attrib_a++ ; else printf("-");
 90       if(fd->dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED) 	        printf("C") && attrib_c++ ; else printf("-");
 91       if(fd->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)		printf("D") && attrib_d++ ; else printf("-");
 92       if(fd->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)			printf("H") && attrib_h++ ; else printf("-");
 93       if(fd->dwFileAttributes & FILE_ATTRIBUTE_NORMAL)			printf("N") && attrib_n++ ; else printf("-");
 94       if(fd->dwFileAttributes & FILE_ATTRIBUTE_OFFLINE)			printf("O") && attrib_o++ ; else printf("-");
 95       if(fd->dwFileAttributes & FILE_ATTRIBUTE_READONLY)		printf("R") && attrib_r++ ; else printf("-");
 96       if(fd->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)			printf("S") && attrib_s++ ; else printf("-");
 97       if(fd->dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)		printf("T") && attrib_t++ ; else printf("-");
 98 
 99       //printf("Creation time    : %d\n",fd->ftCreationTime);
100       //printf("Last access time : %d\n",fd->ftLastAccessTime);
101       //printf("Last write time  : %d\n",fd->ftLastWriteTime);
102       printf(" Size : %16lu bytes\n",((fd->nFileSizeHigh * MAXDWORD)+fd->nFileSizeLow));
103 
104     }
105     while(FindNextFile(fh,fd));
106   }
107 
108   printf("\nStatistics : (%d files)\n",filecnt);
109   printf("Archived   : % 8d\n",attrib_a);
110   printf("Compressed : % 8d\n",attrib_c);
111   printf("Directory  : % 8d\n",attrib_d);
112   printf("Hidden     : % 8d\n",attrib_h);
113   printf("Normal     : % 8d\n",attrib_n);
114   printf("Offline    : % 8d\n",attrib_o);
115   printf("Read only  : % 8d\n",attrib_r);
116   printf("System     : % 8d\n",attrib_s);
117   printf("Temporary  : % 8d\n",attrib_t);
118 
119   FindClose(fh);
120 
121   return 1;
122 }
123 
124 //------------------------------------------------------------------------------------------------------------------
125 // fixpath() - Adds \ to the end of a path if not present.
126 //------------------------------------------------------------------------------------------------------------------
127 int fixpath(const char *inpath, char *outpath)
128 {
129   int   n=0;
130 
131   strcpy(outpath,inpath);
132 
133   while(inpath[n]) n++;
134 
135   if(inpath[n-1] != '\\')
136   {
137     strcat(outpath,"\\");
138     return 1;
139   }
140 
141   return 0;
142 }
143 
144 //------------------------------------------------------------------------------------------------------------------
145 // Entry point...
146 //------------------------------------------------------------------------------------------------------------------
147 int main(int argc, char *argv[])
148 {
149   int recursive=0;
150   int ndirs=0;
151 
152   while(*++argv)
153   {
154     if(!strcmp(*argv, "-r") || !strcmp(*argv, "/r"))
155       recursive=1;
156     else
157     {
158       showdir(*argv, recursive);
159       ++ndirs;
160     }
161   }
162   if(!ndirs) showdir(".", recursive);
163 
164   return 0;
165 }
166 
167 


hijacker
hijacker
hijacker
hijacker