File: compat.c

package info (click to toggle)
pax 1:20201030-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 736 kB
  • sloc: ansic: 10,222; sh: 1,567; makefile: 41
file content (196 lines) | stat: -rw-r--r-- 4,230 bytes parent folder | download
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
/*-
 * Copyright © 2019, 2020
 *	mirabilos <m@mirbsd.org>
 * Copyright © 2018
 *	mirabilos <t.glaser@tarent.de>
 * The copyright notices and licences of the files in .linked/ inclu‐
 * ded below shall be considered being part of this copyright notice.
 * Also contains material part of “jupp” (Joe’s Own Editor), © 2018
 *	mirabilos <m@mirbsd.org>
 * Contains code from “mksh” (The MirBSD Korn Shell) © 2015
 *	mirabilos <m@mirbsd.org>
 *	KO Myung-Hun <komh@chollian.net>
 *
 * Provided that these terms and disclaimer and all copyright notices
 * are retained or reproduced in an accompanying document, permission
 * is granted to deal in this work without restriction, including un‐
 * limited rights to use, publicly perform, distribute, sell, modify,
 * merge, give away, or sublicence.
 *
 * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
 * the utmost extent permitted by applicable law, neither express nor
 * implied; without malicious intent or gross negligence. In no event
 * may a licensor, author or contributor be held liable for indirect,
 * direct, other damage, loss, or other issues arising in any way out
 * of dealing in the work, even if advised of the possibility of such
 * damage or existence of a defect, except proven that it results out
 * of said person’s immediate fault when using the work as intended.
 */

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>

#include "compat.h"
#define PAX_JUST_THE_WARNINGS
#include "extern.h"

__RCSID("$MirOS: src/bin/pax/compat.c,v 1.6 2020/09/04 21:09:00 tg Exp $");

int
binopen3(int features, const char *path, int flags, mode_t mode)
{
	int fd;

#ifdef O_BINARY
	flags |= O_BINARY;
#endif
#ifdef O_CLOEXEC
	if (features & BO_CLEXEC) {
		flags |= O_CLOEXEC;
		features &= ~BO_CLEXEC;
	}
#endif
#ifdef O_DIRECTORY
	if (features & BO_MAYBE_DIR)
		flags |= O_DIRECTORY;
#endif

	if ((fd = (features & BO__TWO) ? open(path, flags) :
	    open(path, flags, mode)) != -1) {
#ifdef __OS2__
		setmode(fd, O_BINARY);
#endif
		if ((features & BO_CLEXEC) &&
		    fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
			syswarn(0, errno,
			    "Failed to set the close-on-exec flag");
	}
	return (fd);
}

ssize_t
dwrite(int fd, const void *data, size_t size)
{
	const unsigned char *buf = data;
	ssize_t rv = 0, z;

	while (size) {
		if ((z = write(fd, buf, size)) < 0) {
			if (errno == EINTR)
				continue;
			return (rv ? /* fucked up since we got some */ -2 : -1);
		}
		rv += z;
		buf += z;
		size -= z;
	}
	return (rv);
}

#if !HAVE_DPRINTF
#include <stdarg.h>
#include <string.h>
static char *dprintf_donum(char *, unsigned long long);

/* replacement only as powerful as needed for this */
void
dprintf(int fd, const char *fmt, ...)
{
	/* %s %llu %lu %d */
	const char *ccp;
	char *cp, buf[24];
	unsigned long long uval;
	va_list ap;

	va_start(ap, fmt);
 loop:
	switch (*fmt) {
	case 0:
		goto out;
	case '%':
		break;
	default:
		ccp = fmt;
		do {
			++ccp;
		} while (*ccp && *ccp != '%');
		if (dwrite(fd, fmt, ccp - fmt) < 0)
			goto out;
		fmt = ccp;
		goto loop;
	}
	switch (*++fmt) {
	case 'd': {
		int ival;

		ival = va_arg(ap, int);
		cp = buf;
		if (ival < 0) {
			*cp++ = '-';
			uval = -(long long)ival;
		} else
			uval = ival;
		goto num;
	}
	case 'l':
		switch (*++fmt) {
		case 'l':
			if (*++fmt != 'u') {
 errfmt:
				/* could do something about this? */
				goto loop;
			}
			uval = va_arg(ap, unsigned long long);
			break;
		case 'u':
			uval = va_arg(ap, unsigned long);
			break;
		default:
			goto errfmt;
		}
		cp = buf;
 num:
		*dprintf_donum(cp, uval) = '\0';
		cp = buf;
		if (0)
			/* FALLTHROUGH */
	case 's':
		  cp = va_arg(ap, char *);
		if (dwrite(fd, cp, strlen(cp)) < 0)
			goto out;
		break;
	default:
		goto errfmt;
	}
	++fmt;
	goto loop;

 out:
	va_end(ap);
}

static char *
dprintf_donum(char *cp, unsigned long long val)
{
	unsigned long long subval = val / 10;

	if (subval)
		cp = dprintf_donum(cp, subval);
	*cp++ = '0' + (val % 10);
	return (cp);
}
#endif

#if !HAVE_STRLCPY || !HAVE_STRLCAT
#undef WIDEC
#define OUTSIDE_OF_LIBKERN
#if !HAVE_STRLCPY
#define L_strlcat
#endif
#if !HAVE_STRLCAT
#define L_strlcpy
#endif
#include ".linked/strlfun.inc"
#endif