summaryrefslogtreecommitdiff
path: root/support/dktools/dk4mkdh8.c
blob: a8e1c2a0ae32ec4d06e85df879731447718a55fc (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4mkdh8.ctr
*/

/*
Copyright (C) 2015-2017, Dirk Krause

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above opyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**	@file dk4mkdh8.c The dk4mkdh8 module.
*/


#line 156 "dk4mkdh8.ctr"

#include "dk4conf.h"
#include "dk4types.h"

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

#include "dk4mkdh8.h"

#if DK4_HAVE_SYS_STAT_H
#ifndef SYS_STAT_H_INCLUDED
#include <sys/stat.h>
#define	SYS_STAT_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_SYS_TYPES_H
#ifndef SYS_TYPES_H_INCLUDED
#include <sys/types.h>
#define	SYS_TYPES_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_DIRECT_H
#ifndef DIRECT_H_INCLUDED
#include <direct.h>
#define	DIRECT_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_STDLIB_H
#ifndef STDLIB_H_INCLUDED
#include <stdlib.h>
#define	STDLIB_H_INCLUDED 1
#endif
#endif

#if DK4_HAVE_UNISTD_H
#ifndef UNISTD_H_INCLUDED
#include <unistd.h>
#define	UNISTD_H_INCLUDED 1
#endif
#endif

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

#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef DK4MPL_H_INCLUDED
#include "dk4mpl.h"
#endif

#ifndef DK4STR8_H_INCLUDED
#include "dk4str8.h"
#endif

#ifndef DK4PATH8_H_INCLUDED
#include "dk4path8.h"
#endif

#ifndef DK4STAT8_H_INCLUDED
#include "dk4stat8.h"
#endif

#ifndef DK4STAT_H_INCLUDED
#include "dk4stat.h"
#endif





#line 238 "dk4mkdh8.ctr"



#if DK4_ON_WINDOWS
/* +++ Windows */



/**	Check whether c is a character.
	@param	c	Byte to check.
	@return	1 for characters, 0 for other bytes.
*/
static
int
dk4mkdir_hierarchy_c8_is_char(char c)
{
  int	 back = 0;
  if (('a' <= c) && ('z' >= c)) {
    back = 1;
  } else {
    if (('A' <= c) && ('Z' >= c)) {
      back = 1;
    }
  }
  return back;
}



int
dk4mkdir_hierarchy_c8_one(const char *fn, int ro, dk4_er_t *erp)
{
  BY_HANDLE_FILE_INFORMATION	bhfi;
  WIN32_FIND_DATAA		ffdata;
  HANDLE			ha;
  DWORD				dwattr;
  int				found;
  int				i;
  int		 		back = 0;
  

#line 278 "dk4mkdh8.ctr"
  if (NULL == fn) {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    goto finished;
  }
#if 1
  dwattr = GetFileAttributesA(fn);
  if (INVALID_FILE_ATTRIBUTES == dwattr) {
    ha = FindFirstFileA(fn, &ffdata);
    if (INVALID_HANDLE_VALUE != ha) {
      dwattr = ffdata.dwFileAttributes;
      FindClose(ha);
    }
  }
  if (INVALID_FILE_ATTRIBUTES != dwattr) {
    if (0 != (FILE_ATTRIBUTE_DIRECTORY & dwattr)) {
      back = 1;
    } else {
      if (0 != (FILE_ATTRIBUTE_REPARSE_POINT & dwattr)) {
        found = 0;
	for (i = 0; ((3 > i) && (0 == back) && (0 == found)); i++) {
	  ha = CreateFileA(
	    fn,
	    (
	      (0 == i)
	      ? (0)
	      : ((1 == i) ? (FILE_READ_ATTRIBUTES) : (GENERIC_READ))
	    ),
	    (FILE_SHARE_READ | FILE_SHARE_WRITE),
	    NULL,
	    OPEN_EXISTING,
	    (dwattr | FILE_FLAG_BACKUP_SEMANTICS),
	    NULL
	  );
	  if (INVALID_HANDLE_VALUE != ha) {
	    if (GetFileInformationByHandle(ha, &bhfi)) {
	      found = 1;
	      if (0 != (FILE_ATTRIBUTE_DIRECTORY & (bhfi.dwFileAttributes))) {
	        back = 1;
	      }
	    }
	    CloseHandle(ha);
	  }
	}
      }
      if (0 == back) {
        dk4error_set_simple_error_code(erp, DK4_E_NON_DIR);
      }
    }
  } else {
    if (0 != ro) {
      dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
    } else {
#if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
      if (CreateDirectoryA(fn, NULL)) {
        back = 1;
      } else {
        dk4error_set_ldetails(
          erp, DK4_E_CREATE_DIR_FAILED ,
	  (long)((unsigned long)GetLastError())
        );
      }
#else
      if (CreateDirectoryA(fn, NULL)) {
        back = 1;
      } else {
        errno = 0;
        if (0 == _mkdir(fn)) {
	  back = 1;
	} else {
	  dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno);
	}
      }
#endif
    }
  }
#else
  back = 1;
#endif
  

#line 357 "dk4mkdh8.ctr"
  finished:
  return back;
}



char *
dk4mkdir_hierarchy_c8_first_sep(char *fn, int *bptr, int lc, dk4_er_t *erp)
{
  char		*pc	= NULL;
  int		 back	= 0;
  char		 sc;

  if (NULL == fn) {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    goto finished;
  }

  if ('\\' == *fn) {					/* leading bs */
    if ('\\' == fn[1]) {				/* server and share */
      pc = dk4str8_chr(&(fn[2]), '\\');
      if (NULL != pc) {		/* Share name */
        pc = dk4str8_chr(&(pc[1]), '\\');
	if (NULL != pc) {	/* First dir */
	  *pc = '\0';
	  back = dk4mkdir_hierarchy_c8_one(fn, 1, erp);
	  *pc = '\\';
	  if (0 != back) {
	    back = 0;
	    pc = dk4str8_chr(&(pc[1]), '\\');
	    if (NULL == pc) {
	      if (0 != lc) {
	        back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	      } else {
	        back = 1;
	      }
	    }
	  } else {
	    pc = dk4str8_chr(&(pc[1]), '\\');
	    if (NULL == pc) {
	      if (0 != lc) {
	        back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	      }
	    }
	  }
	} else {		/* Share name but no dir */
	  if (0 != lc) {
	    back = dk4mkdir_hierarchy_c8_one(fn, 1, erp);
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	  }
	}
      } else {
        dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
      }
    } else {						/* abs path, no drive */
      pc = dk4str8_chr(&(fn[1]), '\\');
      if (NULL == pc) {
        if (0 != lc) {
	  back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	} else {
          back = 1;
	}
      }
    }
  } else {						/* no leading bs */
    if (0 != dk4mkdir_hierarchy_c8_is_char(*fn)) {	/* leading char */
      if (':' == fn[1]) {				/* drive */
        if ('\\' == fn[2]) {				/* drive, abs path */
	  sc = fn[3];
	  fn[3] = '\0';
	  back = dk4mkdir_hierarchy_c8_one(fn, 1, erp);
	  fn[3] = sc;
	  if (0 != back) {
	    back = 0;
	    pc = dk4str8_chr(&(fn[3]), '\\');
	    if (NULL == pc) {
	      if (0 != lc) {
	        back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	      } else {
	        back = 1;
	      }
	    }
	  }
	} else {					/* drive, rel path */
	  sc = fn[2];
	  fn[2] = '\0';
	  back = dk4mkdir_hierarchy_c8_one(fn, 1, erp);
	  fn[2] = sc;
	  if (0 != back) {
	    back = 0;
	    pc = dk4str8_chr(&(fn[3]), '\\');
	    if (NULL == pc) {
	      if (0 != lc) {
	        back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	      } else {
	        back = 1;
	      }
	    }
	  }
	}
      } else {						/* no drive */
        pc = dk4str8_chr(fn, '\\');
	if (NULL == pc) {
	  if (0 != lc) {
	    back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	  } else {
	    back = 1;
	  }
	}
      }
    } else {						/* no leading char */
      pc = dk4str8_chr(fn, '\\');
      if (NULL == pc) {
        if (0 != lc) {
	  back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
	} else {
          back = 1;
	}
      }
    }
  }

  finished:
  if (NULL != bptr) { *bptr = back; }
  return pc;
}



/* --- Windows */
#else
/* +++ non-Windows */



int
dk4mkdir_hierarchy_c8_one(const char *fn, int ro, dk4_er_t *erp)
{
  dk4_stat_t	stb;
  int		back	= 0;
  

#line 499 "dk4mkdh8.ctr"
  if (NULL == fn) {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    goto finished;
  }
#if 1
  if (0 != dk4stat_c8(&stb, fn, NULL)) {
    back = dk4stat_is_directory(&stb, erp);
    if (0 == back) {
      dk4error_set_simple_error_code(erp, DK4_E_NON_DIR);
    }
  } else {
    if (0 != ro) {
      dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
    } else {
      errno = 0;
      if (0 == mkdir(fn, 0700)) {
        back = 1;
      } else {
        dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno);
      }
    }
  }
#else
  back = 1;
#endif
  

#line 525 "dk4mkdh8.ctr"
  finished:
  return back;
}



char *
dk4mkdir_hierarchy_c8_first_sep(char *fn, int *bptr, int lc, dk4_er_t *erp)
{
  char		*pc	= NULL;
  int		 back	= 0;

  if (NULL == fn) {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    goto finished;
  }

  if ('/' == *fn) {
    pc = dk4str8_chr(&(fn[1]), '/');
  } else {
    pc = dk4str8_chr(fn, '/');
  }
  if (NULL == pc) {
    if (0 != lc) {
      back = dk4mkdir_hierarchy_c8_one(fn, 0, erp);
    } else {
      back = 1;
    }
  }

  finished:
  if (NULL != bptr) { *bptr = back; }
  return pc;
}



/* --- non-Windows */
#endif



/**	Create directory structure using an editable copy of the path.
	@param	fn	File name.
	@param	lc	Flag: Last component in fn is directory too.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if fn is too long,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size allocation,
	- DK4_E_NOT_FOUND<br>
	  if a server/share combination is not present on Windows,
	- DK4_E_NON_DIR<br>
	  if one of the components in fn is not a directory,
	- DK4_E_MKDIR_FAILED<br>
	  with errno in idetails if the function fails to create the directory
	  or a parent directory,
	- DK4_E_CREATE_DIR_FAILED<br>
	  with GetLastError() result in lDetails1 if the CreateDirectory()
	  function failed on a Windows system.
*/
static
int
dk4mkdir_hierarchy_c8_on_copy(char *fn, int lc, dk4_er_t *erp)
{
  char			*pc;
  int			 back 	= 0;
  
  

#line 599 "dk4mkdh8.ctr"

  /* Correct file name */
  dk4path8_correct_sep(fn);

  /* Find first real separator */
  pc = dk4mkdir_hierarchy_c8_first_sep(fn, &back, lc, erp);

  /* Process directories */
  if (NULL != pc) {
    back = 1;
    while (NULL != pc) {
      *pc = '\0';
      if (0 < dk4str8_len(fn)) {
        if (0 == dk4mkdir_hierarchy_c8_one(fn, 0, erp)) {
          back = 0;
        }
      }
#if DK4_HAVE_BACKSLASH_AS_SEP
      *pc = '\\';
      pc = dk4str8_chr(&(pc[1]), '\\');
#else
      *pc = '/';
      pc = dk4str8_chr(&(pc[1]), '/');
#endif
    }
    if (0 != lc) {
      if (0 == dk4mkdir_hierarchy_c8_one(fn, 0, erp)) {
        back = 0;
      }
    }
  }
  

#line 631 "dk4mkdh8.ctr"
  return back;
}



/**	Create directory structure using a local copy of the path.
	@param	fn	File name.
	@param	lc	Flag: Last component in fn is directory too.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if fn is too long,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size allocation,
	- DK4_E_NOT_FOUND<br>
	  if a server/share combination is not present on Windows,
	- DK4_E_NON_DIR<br>
	  if one of the components in fn is not a directory,
	- DK4_E_MKDIR_FAILED<br>
	  with errno in idetails if the function fails to create the directory
	  or a parent directory,
	- DK4_E_CREATE_DIR_FAILED<br>
	  with GetLastError() result in lDetails1 if the CreateDirectory()
	  function failed on a Windows system.
*/
static
int
dk4mkdir_hierarchy_c8_local(const char *fn, int lc, dk4_er_t *erp)
{
  char		 mycp[DK4_MAX_PATH];
  int		 back			= 0;
  

#line 667 "dk4mkdh8.ctr"
  if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) {
    back = dk4mkdir_hierarchy_c8_on_copy(mycp, lc, erp);
  } 

#line 670 "dk4mkdh8.ctr"
  return back;
}



int
dk4mkdir_hierarchy_c8(const char *fn, int lc, dk4_er_t *erp)
{
  char		*mycp;
  size_t	 sz;
  int		 back = 0;
  

#line 682 "dk4mkdh8.ctr"
  if (NULL != fn) {
    sz = dk4str8_len(fn);
    if (DK4_MAX_PATH > sz) {
      back = dk4mkdir_hierarchy_c8_local(fn, lc, erp);
    } else {
      mycp = dk4str8_dup(fn, erp);
      if (NULL != mycp) {
        back = dk4mkdir_hierarchy_c8_on_copy(mycp, lc, erp);
	dk4mem_free(mycp);
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } 

#line 696 "dk4mkdh8.ctr"
  return back;
}



#if DK4_HAVE_UID_T && DK4_HAVE_GID_T && DK4_HAVE_MODE_T && (!DK4_ON_WINDOWS)



/**	Check whether directory exists or attempt to create directory.
	@param	fn	File name.
	@param	ro	Flag: Read only (test directory presence),
			do not attempt to create the directory.
	@param	uid	User ID for new directories.
	@param	gid	Group ID for new directories.
	@param	mode	Mode for new directories.
	@param	erp	Error report, may be NULL.
	@return	1 if directory is available now, 0 otherwise.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_NOT_FOUND<br>
	  if a server/share combination is not present on Windows,
	- DK4_E_NON_DIR<br>
	  if any component in the path is not a directory,
	- DK4_E_MKDIR_FAILED<br>
	  with errno value in iDetails1 if the mkdir() function failed on a
	  non-Windows system,
	- DK4_E_CHOWN_FAILED<br>
	  with errno value in iDetails1 if the chown() function failed on a
	  non-Windows system,
	- DK4_E_CHMOD_FAILED<br>
	  with errno value in iDetails1 if the chmod() function failed on a
	  non-Windows system,
	- DK4_E_CREATE_DIR_FAILED<br>
	  with GetLastError() result in lDetails1 if the CreateDirectory()
	  function failed on a Windows system.
*/
static
int
dk4mkdir_hierarchy_ugm_c8_one(
  const char	*fn,
  int		 ro,
  uid_t		 uid,
  gid_t		 gid,
  mode_t	 mode,
  dk4_er_t	*erp
)
{
  dk4_stat_t	stb;
  int		back	= 0;
  

#line 749 "dk4mkdh8.ctr"
  if (NULL == fn) {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    goto finished;
  }
#if 1
  if (0 != dk4stat_c8(&stb, fn, NULL)) {
    back = dk4stat_is_directory(&stb, erp);
    if (0 == back) {
      dk4error_set_simple_error_code(erp, DK4_E_NON_DIR);
    }
  } else {
    if (0 != ro) {
      dk4error_set_simple_error_code(erp, DK4_E_NOT_FOUND);
    } else {
      errno = 0;
      if (0 == mkdir(fn, mode)) {
        errno = 0;
	if (0 == chown(fn, uid, gid)) {
	  errno = 0;
	  if (0 == chmod(fn, mode)) {
            back = 1;
	  } else {
	    dk4error_set_idetails(erp, DK4_E_CHMOD_FAILED, errno);
	  }
	} else {
	  dk4error_set_idetails(erp, DK4_E_CHOWN_FAILED, errno);
	}
      } else {
        dk4error_set_idetails(erp, DK4_E_MKDIR_FAILED, errno);
      }
    }
  }
#else
  back = 1;
#endif
  

#line 785 "dk4mkdh8.ctr"
  finished:
  return back;
}



/**	Create directory structure using an editable copy of the path.
	@param	fn	File name.
	@param	lc	Flag: Last component in fn is directory too.
	@param	uid	User ID for new directories.
	@param	gid	Group ID for new directories.
	@param	mode	Mode for new directories.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if fn is too long,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size allocation,
	- DK4_E_NOT_FOUND<br>
	  if a server/share combination is not present on Windows,
	- DK4_E_NON_DIR<br>
	  if one of the components in fn is not a directory,
	- DK4_E_MKDIR_FAILED<br>
	  with errno in idetails if the function fails to create the directory
	  or a parent directory,
	- DK4_E_CHOWN_FAILED<br>
	  with errno value in iDetails1 if the chown() function failed on a
	  non-Windows system,
	- DK4_E_CHMOD_FAILED<br>
	  with errno value in iDetails1 if the chmod() function failed on a
	  non-Windows system,
	- DK4_E_CREATE_DIR_FAILED<br>
	  with GetLastError() result in lDetails1 if the CreateDirectory()
	  function failed on a Windows system.
*/
static
int
dk4mkdir_hierarchy_ugm_c8_on_copy(
  char		*fn,
  int		 lc,
  uid_t		 uid,
  gid_t		 gid,
  mode_t	 mode,
  dk4_er_t	*erp
)
{
  char			*pc;
  int			 back 	= 0;
  
  

#line 839 "dk4mkdh8.ctr"

  /* Correct file name */
  dk4path8_correct_sep(fn);

  /* Find first real separator */
  pc = dk4mkdir_hierarchy_c8_first_sep(fn, &back, lc, erp);

  /* Process directories */
  if (NULL != pc) {
    back = 1;
    while (NULL != pc) {
      *pc = '\0';
      if (0 < dk4str8_len(fn)) {
        if (0 == dk4mkdir_hierarchy_ugm_c8_one(fn, 0, uid, gid, mode, erp)) {
          back = 0;
        }
      }
#if DK4_HAVE_BACKSLASH_AS_SEP
      *pc = '\\';
      pc = dk4str8_chr(&(pc[1]), '\\');
#else
      *pc = '/';
      pc = dk4str8_chr(&(pc[1]), '/');
#endif
    }
    if (0 != lc) {
      if (0 == dk4mkdir_hierarchy_ugm_c8_one(fn, 0, uid, gid, mode, erp)) {
        back = 0;
      }
    }
  }
  

#line 871 "dk4mkdh8.ctr"
  return back;
}



/**	Create directory structure using a local copy of the path.
	@param	fn	File name.
	@param	lc	Flag: Last component in fn is directory too.
	@param	uid	User ID for new directories.
	@param	gid	Group ID for new directories.
	@param	mode	Mode for new directories.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL,
	- DK4_E_BUFFER_TOO_SMALL<br>
	  if fn is too long,
	- DK4_E_MATH_OVERFLOW<br>
	  if a mathematical overflow occured in size allocation,
	- DK4_E_NOT_FOUND<br>
	  if a server/share combination is not present on Windows,
	- DK4_E_NON_DIR<br>
	  if one of the components in fn is not a directory,
	- DK4_E_MKDIR_FAILED<br>
	  with errno in idetails if the function fails to create the directory
	  or a parent directory,
	- DK4_E_CHOWN_FAILED<br>
	  with errno value in iDetails1 if the chown() function failed on a
	  non-Windows system,
	- DK4_E_CHMOD_FAILED<br>
	  with errno value in iDetails1 if the chmod() function failed on a
	  non-Windows system,
	- DK4_E_CREATE_DIR_FAILED<br>
	  with GetLastError() result in lDetails1 if the CreateDirectory()
	  function failed on a Windows system.
*/
static
int
dk4mkdir_hierarchy_ugm_c8_local(
  const char	*fn,
  int		 lc,
  uid_t		 uid,
  gid_t		 gid,
  mode_t	 mode,
  dk4_er_t	*erp
)
{
  char		 mycp[DK4_MAX_PATH];
  int		 back			= 0;
  

#line 923 "dk4mkdh8.ctr"
  if (0 != dk4str8_cpy_s(mycp, sizeof(mycp), fn, erp)) {
    back = dk4mkdir_hierarchy_ugm_c8_on_copy(mycp, lc, uid, gid, mode, erp);
  } 

#line 926 "dk4mkdh8.ctr"
  return back;
}



int
dk4mkdir_hierarchy_ugm_c8(
  const char	*fn,
  int		 lc,
  uid_t		 uid,
  gid_t		 gid,
  mode_t	 mode,
  dk4_er_t	*erp
)
{
  char		*mycp;
  size_t	 sz;
  int		 back = 0;
  

#line 945 "dk4mkdh8.ctr"
  if (NULL != fn) {
    sz = dk4str8_len(fn);
    if (DK4_MAX_PATH > sz) {
      back = dk4mkdir_hierarchy_ugm_c8_local(fn, lc, uid, gid, mode, erp);
    } else {
      mycp = dk4str8_dup(fn, erp);
      if (NULL != mycp) {
        back = dk4mkdir_hierarchy_ugm_c8_on_copy(mycp, lc, uid, gid, mode, erp);
	dk4mem_free(mycp);
      }
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } 

#line 959 "dk4mkdh8.ctr"
  return back;
}



#endif