w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

dirent.c
Go to the documentation of this file.
1 /*
2 Public domain. No warranty. Akira Kakuto
3 */
4 
5 /*
6 DIR * opendir(char *name);
7 struct dirent * readdir(DIR * dirp);
8 int closedir(DIR * dirp);
9 
10  to be used on VC++ 2.0
11  ( by A. Kakuto, 1995 )
12  rewritten on Jan. 18, 1997 ( A.K. )
13 
14  dirent.h should be included
15 
16  Corrected by Clerk Ma, to work in 64bit mode in Visual Studio 2015,(2017).
17 */
18 
19 #include <windows.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <malloc.h>
23 #include "kpathsea/dirent.h"
24 
25 static int find(DIR *dp, char *name)
26 {
27  HANDLE h;
28  WIN32_FIND_DATA *pfd;
29 
30  pfd = (WIN32_FIND_DATA *)(dp->_d_buf);
31  if((h = FindFirstFile(name,pfd)) == INVALID_HANDLE_VALUE)
32  return -1;
33  dp->_d_hdir = h;
34  pfd->dwReserved1 = strlen(pfd->cFileName);
35  dp->_d_magic = pfd->dwFileAttributes;
36  pfd->dwReserved0 = (dp->_d_magic & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
37  dp->_d_direntptr = (struct dirent *)&(pfd->dwReserved0);
38  dp->_d_nfiles = 0;
39  return 0;
40 }
41 
42 static int findn(DIR *dp)
43 {
44  BOOL res;
45  HANDLE h;
46  WIN32_FIND_DATA *pfd;
47 
48  h = dp->_d_hdir;
49  pfd = (WIN32_FIND_DATA *)(dp->_d_buf);
50  if((res = FindNextFile(h,pfd)) == FALSE)
51  return -1;
52  pfd->dwReserved1 = strlen(pfd->cFileName);
53  dp->_d_magic = pfd->dwFileAttributes;
54  pfd->dwReserved0 = (dp->_d_magic & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
55  dp->_d_direntptr = (struct dirent *)&(pfd->dwReserved0);
56 
57  return 0;
58 }
59 
60 DIR *opendir(char *fname)
61 {
62  DIR *dirptr;
63  char nbuf[512];
64  char *name, *lastpos;
65 
66  if(!(dirptr = (DIR *)malloc(sizeof(DIR))))
67  return NULL;
68  strcpy(nbuf,fname);
69  name = nbuf;
70 
71  if(*name != '\0' && *(name+1) == ':')
72  name += 2;
73 
74  if(*name == '\0') {
75  *name='/'; *(name+1)='\0'; goto add_ast;
76  }
77  if(*(name+1) == '\0' && (*name == '/' || *name == '\\')) {
78  *name='/'; goto add_ast;
79  }
80 
81  lastpos = nbuf;
82  while(*lastpos)
83  lastpos++;
84  lastpos--;
85  if(*lastpos == '/' || *lastpos == '\\') {
86  *lastpos = '\0'; lastpos--;
87  }
88 
89  if(*name == '.' && name[1] == '\0') {
90  name[1]='/'; name[2]='\0'; goto add_ast;
91  }
92  if(*name == '.' && name[1] == '.' && name[2] == '\0') {
93  name[2]='/'; name[3]='\0'; goto add_ast;
94  }
95  if(*lastpos == '.' && (*(lastpos-1) == '/' || *(lastpos-1) == '\\')) {
96  *(lastpos+1)='/'; *(lastpos+2)='\0'; goto add_ast;
97  }
98  if(*lastpos == '.' && *(lastpos-1) == '.' &&
99  (*(lastpos-2) == '/' || *(lastpos-2) == '\\')) {
100  *(lastpos+1)='/'; *(lastpos+2)='\0'; goto add_ast;
101  }
102 
103  if(find(dirptr, nbuf) != 0) {
104  free(dirptr);
105  return NULL;
106  }
107  if(strpbrk(name,"*?") == NULL) {
108  if(dirptr->_d_magic & FILE_ATTRIBUTE_DIRECTORY) {
109  FindClose(dirptr->_d_hdir);
110  strcat(name, "/");
111  add_ast:
112  strcat(name,"*");
113  if(find(dirptr,nbuf) != 0) {
114  free(dirptr);
115  return NULL;
116  }
117  }
118  }
119  dirptr->_d_nfiles = 0;
120  return dirptr;
121 }
122 
123 int closedir(DIR *dirp)
124 {
125  HANDLE h;
126  BOOL res;
127 
128  h = dirp->_d_hdir;
129  res = FindClose(h);
130  if( res == FALSE) {
131  free(dirp);
132  return -1;
133  }
134  free(dirp);
135  return 0;
136 }
137 
138 struct dirent * readdir(DIR *dirp)
139 {
140  if(dirp == NULL)
141  return NULL;
142  if(++(dirp->_d_nfiles) == 1) {
143  return (dirp->_d_direntptr);
144  }
145  if(findn(dirp) != 0)
146  return NULL;
147  return (dirp->_d_direntptr);
148 }
#define name
char *__cdecl strpbrk(char const *_Str, char const *_Control)
BOOL
Definition: dd.h:100
@ FALSE
Definition: dd.h:101
#define free(a)
Definition: decNumber.cpp:310
int h
Definition: dviconv.c:9
char * strcpy()
static char nbuf[100]
Definition: hpcdtoppm.c:135
#define NULL
Definition: ftobjs.h:61
static int find(DIR *dp, char *name)
Definition: dirent.c:25
static int findn(DIR *dp)
Definition: dirent.c:42
struct dirent * readdir(DIR *dirp)
Definition: dirent.c:138
int closedir(DIR *dirp)
Definition: dirent.c:123
DIR * opendir(char *fname)
Definition: dirent.c:60
#define malloc
Definition: alloca.c:91
#define res(length)
Definition: picttoppm.c:287
char * fname
Definition: plain2.c:121
RETTYPE mp_ptr mp_size_t mp_srcptr dp
Definition: sec_div.c:70
Definition: dirent.h:44
unsigned _d_nfiles
Definition: dirent.h:48
unsigned _d_magic
Definition: dirent.h:47
HANDLE _d_hdir
Definition: dirent.h:45
struct dirent * _d_direntptr
Definition: dirent.h:46
Definition: dirent.h:34
Definition: sh2.c:920
*job_name strlen((char *) job_name) - 4)