summaryrefslogtreecommitdiff
path: root/support/dktools/dk4dira.ctr
blob: 00a9909fd388f67e17112d2a198e797b62163023 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
SPDX-License-Identifier:	BSD-3-Clause



%%	header

/**	@file	dk4dira.h	Directory operations with application support.
*/

#ifndef	DK4CONF_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#ifndef	DK4DIR_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4dir.h"
#else
#include <dktools-4/dk4dir.h>
#endif
#endif

#ifndef	DK4APP_H_INCLUDED
#if DK4_BUILDING_DKTOOLS4
#include "dk4app.h"
#else
#include <dktools-4/dk4app.h>
#endif
#endif


#ifdef __cplusplus
extern "C" {
#endif

/**	Open directory.
	@param	path	Directory path name.
	@param	om	Opening mode.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new directory structure on success, NULL on error.
	On success use dk4dir_close() to destroy the directory
	structure.
*/
dk4_dir_t *
dk4dir_open_app(const dkChar *path, int om, dk4_app_t *app);

#ifdef __cplusplus
}
#endif


%%	module


#include "dk4conf.h"
#include "dk4const.h"

#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_ERRNO_H
#ifndef ERRNO_H_INCLUDED
#include <errno.h>
#define ERRNO_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif

#include "dk4mem.h"
#include "dk4dira.h"
#include "dk4error.h"
#if DK4_ON_WINDOWS
#include "dk4maodd.h"
#include "dk4maodh.h"
#endif


$!trace-include



dk4_dir_t *
dk4dir_open_app(const dkChar *path, int om, dk4_app_t *app)
{
#if DK4_ON_WINDOWS
  dkChar	 eb1[8 * sizeof(dk4_um_t) + 16];
  dkChar	 eb2[8 * sizeof(dk4_um_t) + 16];
#endif
  dk4_er_t	 er;
  dk4_dir_t	*back	= NULL;
#if DK4_ON_WINDOWS
  DWORD		 le;
  int		 found;
  int		 res;
#endif
#if	DK4_USE_ASSERT
  assert(NULL != path);
#endif
  if (NULL != path) {
    dk4error_init(&er);
    back = dk4dir_open(path, om, &er);
    if (NULL == back) {
      switch (er.ec) {
        case DK4_E_NOT_SUPPORTED : {
	  dk4app_log_base3(app, DK4_LL_ERROR, 144, 145, path);
	} break;
	case DK4_E_MEMORY_ALLOCATION_FAILED : {
	  dk4app_log_base3(app, DK4_LL_ERROR, 144, 103, path);
	} break;
	case DK4_E_MATH_OVERFLOW : {
	  dk4app_log_base3(app, DK4_LL_ERROR, 144, 146, path);
	} break;
	case DK4_E_BUFFER_TOO_SMALL : {
	  dk4app_log_base3(app, DK4_LL_ERROR, 144, 42, path);
	} break;
#if !DK4_ON_WINDOWS
	case DK4_E_OPENDIR_FAILED : {
	  switch (er.dt.iDetails1) {
#ifdef EACCES
	    case EACCES : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 138, path);
	    } break;
#endif
#ifdef EBADF
	    case EBADF : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 122, path);
	    } break;
#endif
#ifdef EMFILE
	    case EMFILE : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 114, path);
	    } break;
#endif
#ifdef ENFILE
	    case ENFILE : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 115, path);
	    } break;
#endif
#ifdef ENOENT
	    case ENOENT : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 51, path);
	    } break;
#endif
#ifdef ENOMEM
	    case ENOMEM : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 142, path);
	    } break;
#endif
#ifdef ENOTDIR
	    case ENOTDIR : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 150, path);
	    } break;
#endif
#ifdef ELOOP
	    case ELOOP : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 61, path);
	    } break;
#endif
#ifdef ENAMETOOLONG
	    case ENAMETOOLONG : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 65, path);
	    } break;
#endif
	    default : {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 10, path);
	    } break;
	  }
	} break;
#endif
#if DK4_ON_WINDOWS
	case DK4_E_FINDFIRSTFILE_FAILED : {
	  le = er.dt.lDetails1;
	  found = 0;
	  if (ERROR_FILE_NOT_FOUND == le) {
	    dk4app_log_base3(app, DK4_LL_ERROR, 144, 147, path);
	    found = 1;
	  }
          if (ERROR_ACCESS_DENIED == le) {
	    dk4app_log_base3(app, DK4_LL_ERROR, 144, 138, path);
	    found = 1;
	  }
	  if (ERROR_PATH_NOT_FOUND == le) {
	    dk4app_log_base3(app, DK4_LL_ERROR, 144, 51, path);
	    found = 1;
	  }
	  if (ERROR_BAD_NET_NAME == le) {
	    dk4app_log_base3(app, DK4_LL_ERROR, 144, 74, path);
	    found = 1;
	  }
	  if (ERROR_BAD_NETPATH == le) {
	    dk4app_log_base3(app, DK4_LL_ERROR, 144, 75, path);
	    found = 1;
	  }
	  if (0 == found) {
	    res = dk4ma_write_decimal_unsigned(
	      eb1,DK4_SIZEOF(eb1,dkChar),(dk4_um_t)le,0,NULL
	    );
	    if (0 != res) {
	      res = dk4ma_write_hex_unsigned(
	        eb2, DK4_SIZEOF(eb2,dkChar), (dk4_um_t)le, 0, NULL
	      );
	    }
	    if (0 != res) {
	      dk4app_log_base7(app,DK4_LL_ERROR,144,69,70,71,path,eb1,eb2);
	    } else {
	      dk4app_log_base3(app, DK4_LL_ERROR, 144, 77, path);
	    }
	  }
	} break;
#endif
	default : {
	  dk4app_log_base3(app, DK4_LL_ERROR, 144, 10, path);
	} break;
      }
    }
  }
  return back;
}