summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/comp/omitted.c
blob: 10a20ba533542d8b7e718a36905da1de99655321 (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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
#pragma prototyped noticed

/*
 * workarounds to bring the native interface close to posix and x/open
 */

#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:hide utime utimes
#else
#define utime		______utime
#define utimes		______utimes
#endif

#include <ast.h>
#include <error.h>
#include <tm.h>

#include "FEATURE/omitted"

#undef	OMITTED

#if _win32_botch

#define	OMITTED	1

#include <ls.h>
#include <utime.h>

#if __CYGWIN__
#include <ast_windows.h>
#if _win32_botch_execve || _lib_spawn_mode
#define CONVERT		1
#endif
#endif

#if defined(__STDPP__directive) && defined(__STDPP__hide)
__STDPP__directive pragma pp:nohide utime utimes
#else
#undef	utime
#undef	utimes
#endif

#ifndef MAX_PATH
#define MAX_PATH	PATH_MAX
#endif

/*
 * these workarounds assume each system call foo() has a _foo() entry
 * which is true for __CYGWIN__ and __EMX__ (both gnu based)
 *
 * the workarounds handle:
 *
 *	(1) .exe suffix inconsistencies
 *	(2) /bin/sh reference in execve() and spawnve()
 *	(3) bogus getpagesize() return values
 *	(4) a fork() bug that screws up shell fork()+script
 *
 * NOTE: Not all workarounds can be handled by unix syscall intercepts.
 *	 In particular, { ksh nmake } have workarounds for case-ignorant
 *	 filesystems and { libast } has workarounds for win32 locale info.
 */

#undef _pathconf
#undef pathconf
#undef stat

extern int		_access(const char*, int);
extern unsigned int	_alarm(unsigned int);
extern int		_chmod(const char*, mode_t);
extern int		_close(int);
extern pid_t		_execve(const char*, char* const*, char* const*);
extern int		_link(const char*, const char*);
extern int		_open(const char*, int, ...);
extern long		_pathconf(const char*, int);
extern ssize_t		_read(int, void*, size_t);
extern int		_rename(const char*, const char*);
extern pid_t		_spawnve(int, const char*, char* const*, char* const*);
extern int		_stat(const char*, struct stat*);
extern int		_unlink(const char*);
extern int		_utime(const char*, const struct utimbuf*);
extern int		_utimes(const char*, const struct timeval*);
extern ssize_t		_write(int, const void*, size_t);

#if defined(__EXPORT__)
#define extern	__EXPORT__
#endif

static char*
suffix(register const char* path)
{
	register const char*	s = path + strlen(path);
	register int		c;

	while (s > path)
		if ((c = *--s) == '.')
			return (char*)s + 1;
		else if (c == '/' || c == '\\')
			break;
	return 0;
}

static int
execrate(const char* path, char* buf, int size, int physical)
{
	char*	s;
	int	n;
	int	oerrno;

	if (suffix(path))
		return 0;
	oerrno = errno;
	if (physical || strlen(path) >= size || !(s = pathcanon(strcpy(buf, path), PATH_PHYSICAL|PATH_DOTDOT|PATH_EXISTS)))
		snprintf(buf, size, "%s.exe", path);
	else if (!suffix(buf) && ((buf + size) - s) >= 4)
		strcpy(s, ".exe");
	errno = oerrno;
	return 1;
}

/*
 * return 0 if path is magic, -1 otherwise
 * ux!=0 set to 1 if path is unix executable
 * ux!=0 also retains errno for -1 return
 */

static int
magic(const char* path, int* ux)
{
	int		fd;
	int		r;
	int		n;
	int		m;
	int		oerrno;
#if CONVERT
	unsigned char	buf[512];
#else
	unsigned char	buf[2];
#endif

	oerrno = errno;
	if ((fd = _open(path, O_RDONLY, 0)) >= 0)
	{
#if CONVERT
		if (ux)
			n = sizeof(buf);
		else
#endif
			n = 2;
		r = (m = _read(fd, buf, n)) >= 2 && (buf[1] == 0x5a && (buf[0] == 0x4c || buf[0] == 0x4d) || ux && buf[0] == '#' && buf[1] == '!' && (*ux = 1) && !(ux = 0)) ? 0 : -1;
		close(fd);
		if (ux)
		{
			if (r)
				oerrno = ENOEXEC;
			else if (m > 61 && (n = buf[60] | (buf[61]<<8) + 92) < (m - 1))
				*ux = (buf[n] | (buf[n+1]<<8)) == 3;
			else
				*ux = 0;
		}
	}
	else if (!ux)
		r = -1;
	else if (errno == ENOENT)
	{
		oerrno = errno;
		r = -1;
	}
	else
	{
		r = 0;
		*ux = 0;
	}
	errno = oerrno;
	return r;
}

#if _win32_botch_access

extern int
access(const char* path, int op)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	oerrno = errno;
	if ((r = _access(path, op)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		r = _access(buf, op);
	}
	return r;
}

#endif

#if _win32_botch_alarm

extern unsigned int
alarm(unsigned int s)
{
	unsigned int		n;
	unsigned int		r;

	static unsigned int	a;

	n = (unsigned int)time(NiL);
	if (a <= n)
		r = 0;
	else
		r = a - n;
	a = n + s - 1;
	(void)_alarm(s);
	return r;
}

#endif

#if _win32_botch_chmod

extern int
chmod(const char* path, mode_t mode)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	if ((r = _chmod(path, mode)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		return _chmod(buf, mode);
	}
	if (!(r = _chmod(path, mode)) &&
	    (mode & (S_IXUSR|S_IXGRP|S_IXOTH)) &&
	    !suffix(path) &&
	    (strlen(path) + 4) < sizeof(buf))
	{
		oerrno = errno;
		if (!magic(path, NiL))
		{
			snprintf(buf, sizeof(buf), "%s.exe", path);
			_rename(path, buf);
		}
		errno = oerrno;
	}
	return r;
}

#endif

#if _win32_botch_execve || _lib_spawn_mode

#if _lib_spawn_mode

/*
 * can anyone get const prototype args straight?
 */

#define execve		______execve
#define spawnve		______spawnve

#include <process.h>

#undef	execve
#undef	spawnve

#endif

#if CONVERT

/*
 * this intercept converts dos env vars to unix
 * we'd rather intercept main but can't twist cc to do it
 * getuid() gets ksh to do the right thing and
 * that's our main concern
 *
 *	DOSPATHVARS='a b c'	convert { a b c }
 */

extern uid_t		_getuid(void);

static int		convertinit;

/*
 * convertvars[0] names the list of env var names
 * convertvars[i] are not converted
 */

static const char*	convertvars[] = { "DOSPATHVARS", "PATH" };

static int
convert(register const char* d, const char* s)
{
	register const char*	t;
	register const char*	v;
	int			i;

	for (i = 0; i < elementsof(convertvars); i++)
	{
		for (v = convertvars[i], t = s; *t && *t == *v; t++, v++);
		if (*t == '=' && *v == 0)
			return 0;
	}
	for (;;)
	{
		while (*d == ' ' || *d == '\t')
			d++;
		if (!*d)
			break;
		for (t = s; *t && *t == *d; d++, t++);
		if (*t == '=' && (*d == ' ' || *d == '\t' || *d == 0))
			return t - s + 1;
		while (*d && *d != ' ' && *d != '\t')
			d++;
	}
	return 0;
}

uid_t
getuid(void)
{
	register char*		d;
	register char*		s;
	register char*		t;
	register char**		e;
	int			n;
	int			m;

	if (!convertinit++ && (d = getenv(convertvars[0])))
		for (e = environ; s = *e; e++)
			if ((n = convert(d, s)) && (m = cygwin_win32_to_posix_path_list_buf_size(s + n)) > 0)
			{
				if (!(t = malloc(n + m + 1)))
					break;
				*e = t;
				memcpy(t, s, n);
				cygwin_win32_to_posix_path_list(s + n, t + n);
			}
	return _getuid();
}

#endif

#ifndef _P_OVERLAY
#define _P_OVERLAY	(-1)
#endif

#define DEBUG		1

static pid_t
runve(int mode, const char* path, char* const* argv, char* const* envv)
{
	register char*	s;
	register char**	p;
	register char**	v;

	void*		m1;
	void*		m2;
	pid_t		pid;
	int		oerrno;
	int		ux;
	int		n;
#if defined(_P_DETACH) && defined(_P_NOWAIT)
	int		pgrp;
#endif
#if CONVERT
	char*		d;
	char*		t;
	int		m;
#endif
	struct stat	st;
	char		buf[PATH_MAX];
	char		tmp[PATH_MAX];

#if DEBUG
	static int	trace;
#endif

#if defined(_P_DETACH) && defined(_P_NOWAIT)
	if (mode == _P_DETACH)
	{
		/*
		 * 2004-02-29 cygwin _P_DETACH is useless:
		 *	spawn*() returns 0 instead of the spawned pid
		 *	spawned { pgid sid } are the same as the parent
		 */

		mode = _P_NOWAIT;
		pgrp = 1;
	}
	else
		pgrp = 0;
#endif
	if (!envv)
		envv = (char* const*)environ;
	m1 = m2 = 0;
	oerrno = errno;
#if DEBUG
	if (!trace)
		trace = (s = getenv("_AST_exec_trace")) ? *s : 'n';
#endif
	if (execrate(path, buf, sizeof(buf), 0))
	{
		if (!_stat(buf, &st))
			path = (const char*)buf;
		else
			errno = oerrno;
	}
	if (path != (const char*)buf && _stat(path, &st))
		return -1;
	if (!S_ISREG(st.st_mode) || !(st.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH)))
	{
		errno = EACCES;
		return -1;
	}
	if (magic(path, &ux))
	{
#if _CYGWIN_fork_works
		errno = ENOEXEC;
		return -1;
#else
		ux = 1;
		p = (char**)argv;
		while (*p++);
		if (!(v = (char**)malloc((p - (char**)argv + 2) * sizeof(char*))))
		{
			errno = EAGAIN;
			return -1;
		}
		m1 = v;
		p = v;
		*p++ = (char*)path;
		*p++ = (char*)path;
		path = (const char*)pathshell();
		if (*argv)
			argv++;
		while (*p++ = (char*)*argv++);
		argv = (char* const*)v;
#endif
	}

	/*
	 * the win32 dll search order is
	 *	(1) the directory of path
	 *	(2) .
	 *	(3) /c/(WINNT|WINDOWS)/system32 /c/(WINNT|WINDOWS)
	 *	(4) the directories on $PATH
	 * there are no cygwin dlls in (3), so if (1) and (2) fail
	 * to produce the required dlls its up to (4)
	 *
	 * the standard allows PATH to be anything once the path
	 * to an executable is determined; this code ensures that PATH
	 * contains /bin so that at least the cygwin dll, required
	 * by all cygwin executables, will be found
	 */

	if (p = (char**)envv)
	{
		n = 1;
		while (s = *p++)
			if (strneq(s, "PATH=", 5))
			{
				s += 5;
				do
				{
					s = pathcat(tmp, s, ':', NiL, "");
					if (streq(tmp, "/usr/bin/") || streq(tmp, "/bin/"))
					{
						n = 0;
						break;
					}
				} while (s);
				if (n)
				{
					n = 0;
					snprintf(tmp, sizeof(tmp), "%s:/bin", *(p - 1));
					*(p - 1) = tmp;
				}
				break;
			}
		if (n)
		{
			n = p - (char**)envv + 1;
			p = (char**)envv;
			if (v = (char**)malloc(n * sizeof(char*)))
			{
				m2 = v;
				envv = (char* const*)v;
				*v++ = strcpy(tmp, "PATH=/bin");
				while (*v++ = *p++);
			}
		}
#if CONVERT
		if (!ux && (d = getenv(convertvars[0])))
			for (p = (char**)envv; s = *p; p++)
				if ((n = convert(d, s)) && (m = cygwin_posix_to_win32_path_list_buf_size(s + n)) > 0)
				{
					if (!(t = malloc(n + m + 1)))
						break;
					*p = t;
					memcpy(t, s, n);
					cygwin_posix_to_win32_path_list(s + n, t + n);
				}
#endif
	}

#if DEBUG
	if (trace == 'a' || trace == 'e')
	{
		sfprintf(sfstderr, "%s %s [", mode == _P_OVERLAY ? "_execve" : "_spawnve", path);
		for (n = 0; argv[n]; n++)
			sfprintf(sfstderr, " '%s'", argv[n]);
		if (trace == 'e')
		{
			sfprintf(sfstderr, " ] [");
			for (n = 0; envv[n]; n++)
				sfprintf(sfstderr, " '%s'", envv[n]);
		}
		sfprintf(sfstderr, " ]\n");
		sfsync(sfstderr);
	}
#endif
#if _lib_spawn_mode
	if (mode != _P_OVERLAY)
	{
		pid = _spawnve(mode, path, argv, envv);
#if defined(_P_DETACH) && defined(_P_NOWAIT)
		if (pid > 0 && pgrp)
			setpgid(pid, 0);
#endif
	}
	else
#endif
	{
#if defined(_P_DETACH) && defined(_P_NOWAIT)
		if (pgrp)
			setpgid(0, 0);
#endif
		pid = _execve(path, argv, envv);
	}
	if (m1)
		free(m1);
	if (m2)
		free(m2);
	return pid;
}

#if _win32_botch_execve

extern pid_t
execve(const char* path, char* const* argv, char* const* envv)
{
	return runve(_P_OVERLAY, path, argv, envv);
}

#endif

#if _lib_spawn_mode

extern pid_t
spawnve(int mode, const char* path, char* const* argv, char* const* envv)
{
	return runve(mode, path, argv, envv);
}

#endif

#endif

#if _win32_botch_getpagesize

extern size_t
getpagesize(void)
{
	return 64 * 1024;
}

#endif

#if _win32_botch_link

extern int
link(const char* fp, const char* tp)
{
	int	r;
	int	oerrno;
	char	fb[PATH_MAX];
	char	tb[PATH_MAX];

	oerrno = errno;
	if ((r = _link(fp, tp)) && errno == ENOENT && execrate(fp, fb, sizeof(fb), 1))
	{
		if (execrate(tp, tb, sizeof(tb), 1))
			tp = tb;
		errno = oerrno;
		r = _link(fb, tp);
	}
	return r;
}

#endif

#if _win32_botch_open || _win32_botch_copy

#if _win32_botch_copy

/*
 * this should intercept the important cases
 * dup*() and exec*() fd's will not be intercepted
 */

typedef struct Exe_test_s
{
	int		test;
	ino_t		ino;
	char		path[PATH_MAX];
} Exe_test_t;

static Exe_test_t*	exe[16];

extern int
close(int fd)
{
	int		r;
	int		oerrno;
	struct stat	st;
	char		buf[PATH_MAX];

	if (fd >= 0 && fd < elementsof(exe) && exe[fd])
	{
		r = exe[fd]->test;
		exe[fd]->test = 0;
		if (r > 0 && !fstat(fd, &st) && st.st_ino == exe[fd]->ino)
		{
			if (r = _close(fd))
				return r;
			oerrno = errno;
			if (!stat(exe[fd]->path, &st) && st.st_ino == exe[fd]->ino)
			{
				snprintf(buf, sizeof(buf), "%s.exe", exe[fd]->path);
				_rename(exe[fd]->path, buf);
			}
			errno = oerrno;
			return 0;
		}
	}
	return _close(fd);
}

extern ssize_t
write(int fd, const void* buf, size_t n)
{
	if (fd >= 0 && fd < elementsof(exe) && exe[fd] && exe[fd]->test < 0)
		exe[fd]->test = n >= 2 && ((unsigned char*)buf)[1] == 0x5a && (((unsigned char*)buf)[0] == 0x4c || ((unsigned char*)buf)[0] == 0x4d) && !lseek(fd, (off_t)0, SEEK_CUR);
	return _write(fd, buf, n);
}

#endif

extern int
open(const char* path, int flags, ...)
{
	int		fd;
	int		mode;
	int		oerrno;
	char		buf[PATH_MAX];
#if _win32_botch_copy
	struct stat	st;
#endif
	va_list		ap;

	va_start(ap, flags);
	mode = (flags & O_CREAT) ? va_arg(ap, int) : 0;
	oerrno = errno;
	fd = _open(path, flags, mode);
#if _win32_botch_open
	if (fd < 0 && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		fd = _open(buf, flags, mode);
	}
#endif
#if _win32_botch_copy
	if (fd >= 0 && fd < elementsof(exe) && strlen(path) < PATH_MAX &&
	    (flags & (O_CREAT|O_TRUNC)) == (O_CREAT|O_TRUNC) && (mode & 0111))
	{
		if (!suffix(path) && !fstat(fd, &st) && (exe[fd] || (exe[fd] = (Exe_test_t*)malloc(sizeof(Exe_test_t)))))
		{
			exe[fd]->test = -1;
			exe[fd]->ino = st.st_ino;
			strcpy(exe[fd]->path, path);
		}
		errno = oerrno;
	}
#endif
	va_end(ap);
	return fd;
}

#endif

#if _win32_botch_pathconf

extern long
pathconf(const char* path, int op)
{
	if (_access(path, F_OK))
		return -1;
	return _pathconf(path, op);
}

#endif

#if _win32_botch_rename

extern int
rename(const char* fp, const char* tp)
{
	int	r;
	int	oerrno;
	char	fb[PATH_MAX];
	char	tb[PATH_MAX];

	oerrno = errno;
	if ((r = _rename(fp, tp)) && errno == ENOENT && execrate(fp, fb, sizeof(fb), 1))
	{
		if (execrate(tp, tb, sizeof(tb), 1))
			tp = tb;
		errno = oerrno;
		r = _rename(fb, tp);
	}
	return r;
}

#endif

#if _win32_botch_stat

extern int
stat(const char* path, struct stat* st)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	oerrno = errno;
	if ((r = _stat(path, st)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		r = _stat(buf, st);
	}
	return r;
}

#endif

#if _win32_botch_truncate

extern int
truncate(const char* path, off_t offset)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	oerrno = errno;
	if ((r = _truncate(path, offset)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		r = _truncate(buf, offset);
	}
	return r;
}

#endif

#if _win32_botch_unlink

extern int
unlink(const char* path)
{
	int		r;
	int		drive;
	int		mask;
	int		suffix;
	int		stop;
	int		oerrno;
	unsigned long	base;
	char		buf[PATH_MAX];
	char		tmp[MAX_PATH];

#define DELETED_DIR_1	7
#define DELETED_DIR_2	16

	static char	deleted[] = "%c:\\temp\\.deleted\\%08x.%03x";

	static int	count = 0;

#if __CYGWIN__

	DWORD		fattr = FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE;
	DWORD		share = FILE_SHARE_DELETE;
	HANDLE		hp;
	struct stat	st;
	char		nat[MAX_PATH];

	oerrno = errno;
	if (lstat(path, &st) || !S_ISREG(st.st_mode))
		goto try_unlink;
	cygwin_conv_to_full_win32_path(path, nat);
	if (!strncasecmp(nat + 1, ":\\temp\\", 7))
		goto try_unlink;
	drive = nat[0];
	path = (const char*)nat;
	for (;;)
	{
		hp = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE, NULL);
		if (hp != INVALID_HANDLE_VALUE)
		{
			CloseHandle(hp);
			errno = oerrno;
			return 0;
		}
		if (GetLastError() != ERROR_FILE_NOT_FOUND)
			break;
		if (path == (const char*)buf || !execrate(path, buf, sizeof(buf), 1))
		{
			errno = ENOENT;
			return -1;
		}
		path = (const char*)buf;
	}
#else
	if (_access(path, 0))
#if _win32_botch_access
	{
		if (errno != ENOENT || !execrate(path, buf, sizeof(buf), 1) || _access(buf, 0))
			return -1;
		path = (const char*)buf;
	}
#else
		return -1;
#endif
	drive = 'C':
#endif

	/*
	 * rename to a `deleted' path just in case the file is open
	 * otherwise directory readers may choke on phantom entries
	 */

	base = ((getuid() & 0xffff) << 16) | (time(NiL) & 0xffff);
	suffix = (getpid() & 0xfff) + count++;
	snprintf(tmp, sizeof(tmp), deleted, drive, base, suffix);
	if (!_rename(path, tmp))
	{
		path = (const char*)tmp;
		goto try_delete;
	}
	if (errno != ENOTDIR && errno != ENOENT)
		goto try_unlink;
	tmp[DELETED_DIR_2] = 0;
	if (_access(tmp, 0))
	{
		mask = umask(0);
		tmp[DELETED_DIR_1] = 0;
		if (_access(tmp, 0) && _mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO))
		{
			umask(mask);
			goto try_unlink;
		}
		tmp[DELETED_DIR_1] = '\\';
		r = _mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO);
		umask(mask);
		if (r)
			goto try_unlink;
		errno = 0;
	}
	tmp[DELETED_DIR_2] = '\\';
	if (!errno && !_rename(path, tmp))
	{
		path = (const char*)tmp;
		goto try_delete;
	}
#if !__CYGWIN__
	if (errno == ENOENT)
	{
#if !_win32_botch_access
		if (execrate(path, buf, sizeof(buf), 1) && !_rename(buf, tmp))
			path = (const char*)tmp;
#endif
		goto try_unlink;
	}
#endif
	stop = suffix;
	do
	{
		snprintf(tmp, sizeof(tmp), deleted, drive, base, suffix);
		if (!_rename(path, tmp))
		{
			path = (const char*)tmp;
			goto try_delete;
		}
		if (++suffix > 0xfff)
			suffix = 0;
	} while (suffix != stop);
 try_delete:
#if __CYGWIN__
	hp = CreateFile(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_DELETE_ON_CLOSE, NULL);
	if (hp != INVALID_HANDLE_VALUE)
	{
		CloseHandle(hp);
		errno = oerrno;
		return 0;
	}
#endif
 try_unlink:
	errno = oerrno;
	return _unlink(path);
}

#endif

#if _win32_botch_utime

#if __CYGWIN__

/*
 * cygwin refuses to set st_ctime for some operations
 * this rejects that refusal
 */

static void
ctime_now(const char* path)
{
	HANDLE		hp;
	SYSTEMTIME	st;
	FILETIME	ct;
	WIN32_FIND_DATA	ff;
	struct stat	fs;
	int		oerrno;
	char		tmp[MAX_PATH];

	if (_stat(path, &fs) || (fs.st_mode & S_IWUSR) || _chmod(path, (fs.st_mode | S_IWUSR) & S_IPERM))
		fs.st_mode = 0;
	cygwin_conv_to_win32_path(path, tmp);
	hp = CreateFile(tmp, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hp && hp != INVALID_HANDLE_VALUE)
	{
		GetSystemTime(&st);
		SystemTimeToFileTime(&st, &ct);
		SetFileTime(hp, &ct, 0, 0);
		CloseHandle(hp);
	}
	if (fs.st_mode)
		_chmod(path, fs.st_mode & S_IPERM);
	errno = oerrno;
}

#else

#define ctime_now(p)

#endif

extern int
utimes(const char* path, const struct timeval* ut)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	oerrno = errno;
	if ((r = _utimes(path, ut)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		r = _utimes(path = buf, ut);
	}
	if (!r)
		ctime_now(path);
	return r;
}

extern int
utime(const char* path, const struct utimbuf* ut)
{
	int	r;
	int	oerrno;
	char	buf[PATH_MAX];

	oerrno = errno;
	if ((r = _utime(path, ut)) && errno == ENOENT && execrate(path, buf, sizeof(buf), 0))
	{
		errno = oerrno;
		r = _utime(path = buf, ut);
	}
	if (!r)
		ctime_now(path);
	return r;
}

#endif

#endif

/*
 * some systems (sun) miss a few functions required by their
 * own bsd-like macros
 */

#if !_lib_bzero || defined(bzero)

#undef	bzero

void
bzero(void* b, size_t n)
{
	memset(b, 0, n);
}

#endif

#if !_lib_getpagesize || defined(getpagesize)

#ifndef OMITTED
#define OMITTED	1
#endif

#undef	getpagesize

#ifdef	_SC_PAGESIZE
#undef	_AST_PAGESIZE
#define _AST_PAGESIZE	(int)sysconf(_SC_PAGESIZE)
#else
#ifndef _AST_PAGESIZE
#define _AST_PAGESIZE	4096
#endif
#endif

int
getpagesize()
{
	return _AST_PAGESIZE;
}

#endif

#if __CYGWIN__ && defined(__IMPORT__) && defined(__EXPORT__)

#ifndef OMITTED
#define OMITTED	1
#endif

/*
 * a few _imp__FUNCTION symbols are needed to avoid
 * static link multiple definitions
 */

#ifndef strtod
__EXPORT__ double (*_imp__strtod)(const char*, char**) = strtod;
#endif

#endif

#ifndef OMITTED

NoN(omitted)

#endif