net-snmp 5.7
opendir.c
00001 /*
00002  * opendir() replacement for MSVC.
00003  */
00004 
00005 #define WIN32IO_IS_STDIO
00006 #define PATHLEN 1024
00007 
00008 #include <net-snmp/net-snmp-config.h>
00009 #include <net-snmp/types.h>
00010 #include <net-snmp/library/system.h>
00011 #ifdef HAVE_SYS_STAT_H
00012 #include <sys/stat.h>
00013 #endif
00014 #include <tchar.h>
00015 #include <windows.h>
00016 
00017 
00018 /*
00019  * The idea here is to read all the directory names into a string table
00020  * * (separated by nulls) and when one of the other dir functions is called
00021  * * return the pointer to the current file name.
00022  */
00023 DIR            *
00024 opendir(const char *filename)
00025 {
00026     DIR            *p;
00027     long            len;
00028     long            idx;
00029     char            scannamespc[PATHLEN];
00030     char           *scanname = scannamespc;
00031     struct stat     sbuf;
00032     WIN32_FIND_DATA FindData;
00033     HANDLE          fh;
00034 
00035     /*
00036      * check to see if filename is a directory 
00037      */
00038     if ((stat(filename, &sbuf) < 0) || ((sbuf.st_mode & S_IFDIR) == 0)) {
00039         return NULL;
00040     }
00041 
00042     /*
00043      * get the file system characteristics 
00044      */
00045     /*
00046      * if(GetFullPathName(filename, SNMP_MAXPATH, root, &dummy)) {
00047      * *    if(dummy = strchr(root, '\\'))
00048      * *        *++dummy = '\0';
00049      * *    if(GetVolumeInformation(root, volname, SNMP_MAXPATH, &serial,
00050      * *                            &maxname, &flags, 0, 0)) {
00051      * *        downcase = !(flags & FS_CASE_IS_PRESERVED);
00052      * *    }
00053      * *  }
00054      * *  else {
00055      * *    downcase = TRUE;
00056      * *  }
00057      */
00058 
00059     /*
00060      * Create the search pattern 
00061      */
00062     strcpy(scanname, filename);
00063 
00064     if (strchr("/\\", *(scanname + strlen(scanname) - 1)) == NULL)
00065         strcat(scanname, "/*");
00066     else
00067         strcat(scanname, "*");
00068 
00069     /*
00070      * do the FindFirstFile call 
00071      */
00072     fh = FindFirstFile(scanname, &FindData);
00073     if (fh == INVALID_HANDLE_VALUE) {
00074         return NULL;
00075     }
00076 
00077     /*
00078      * Get us a DIR structure 
00079      */
00080     p = (DIR *) malloc(sizeof(DIR));
00081     /*
00082      * Newz(1303, p, 1, DIR); 
00083      */
00084     if (p == NULL)
00085         return NULL;
00086 
00087     /*
00088      * now allocate the first part of the string table for
00089      * * the filenames that we find.
00090      */
00091     idx = strlen(FindData.cFileName) + 1;
00092     p->start = (char *) malloc(idx);
00093     /*
00094      * New(1304, p->start, idx, char);
00095      */
00096     if (p->start == NULL) {
00097         free(p);
00098         return NULL;
00099     }
00100     strcpy(p->start, FindData.cFileName);
00101     /*
00102      * if(downcase)
00103      * *    strlwr(p->start);
00104      */
00105     p->nfiles = 0;
00106 
00107     /*
00108      * loop finding all the files that match the wildcard
00109      * * (which should be all of them in this directory!).
00110      * * the variable idx should point one past the null terminator
00111      * * of the previous string found.
00112      */
00113     while (FindNextFile(fh, &FindData)) {
00114         len = strlen(FindData.cFileName);
00115         /*
00116          * bump the string table size by enough for the
00117          * * new name and it's null terminator
00118          */
00119         p->start = (char *) realloc((void *) p->start, idx + len + 1);
00120         /*
00121          * Renew(p->start, idx+len+1, char);
00122          */
00123         if (p->start == NULL) {
00124             free(p);
00125             return NULL;
00126         }
00127         strcpy(&p->start[idx], FindData.cFileName);
00128         /*
00129          * if (downcase) 
00130          * *        strlwr(&p->start[idx]);
00131          */
00132         p->nfiles++;
00133         idx += len + 1;
00134     }
00135     FindClose(fh);
00136     p->size = idx;
00137     p->curr = p->start;
00138     return p;
00139 }