Debian Codegraph @

compat.c

raw symbols SCIP
1 /*-
2 * Copyright © 2019, 2020
3 * mirabilos <[email protected]>
4 * Copyright © 2018
5 * mirabilos <[email protected]>
6 * The copyright notices and licences of the files in .linked/ inclu‐
7 * ded below shall be considered being part of this copyright notice.
8 * Also contains material part of “jupp” (Joe’s Own Editor), © 2018
9 * mirabilos <[email protected]>
10 * Contains code from “mksh” (The MirBSD Korn Shell) © 2015
11 * mirabilos <[email protected]>
12 * KO Myung-Hun <[email protected]>
13 *
14 * Provided that these terms and disclaimer and all copyright notices
15 * are retained or reproduced in an accompanying document, permission
16 * is granted to deal in this work without restriction, including un‐
17 * limited rights to use, publicly perform, distribute, sell, modify,
18 * merge, give away, or sublicence.
19 *
20 * This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
21 * the utmost extent permitted by applicable law, neither express nor
22 * implied; without malicious intent or gross negligence. In no event
23 * may a licensor, author or contributor be held liable for indirect,
24 * direct, other damage, loss, or other issues arising in any way out
25 * of dealing in the work, even if advised of the possibility of such
26 * damage or existence of a defect, except proven that it results out
27 * of said person’s immediate fault when using the work as intended.
28 */
29
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <unistd.h>
34
35 #include "compat.h"
36 #define PAX_JUST_THE_WARNINGS
37 #include "extern.h"
38
39 __RCSID("$MirOS: src/bin/pax/compat.c,v 1.6 2020/09/04 21:09:00 tg Exp $");
40
41 int
42 binopen3(int features, const char *path, int flags, mode_t mode)
43 {
44 int fd;
45
46 #ifdef O_BINARY
47 flags |= O_BINARY;
48 #endif
49 #ifdef O_CLOEXEC
50 if (features & BO_CLEXEC) {
51 flags |= O_CLOEXEC;
52 features &= ~BO_CLEXEC;
53 }
54 #endif
55 #ifdef O_DIRECTORY
56 if (features & BO_MAYBE_DIR)
57 flags |= O_DIRECTORY;
58 #endif
59
60 if ((fd = (features & BO__TWO) ? open(path, flags) :
61 open(path, flags, mode)) != -1) {
62 #ifdef __OS2__
63 setmode(fd, O_BINARY);
64 #endif
65 if ((features & BO_CLEXEC) &&
66 fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
67 syswarn(0, errno,
68 "Failed to set the close-on-exec flag");
69 }
70 return (fd);
71 }
72
73 ssize_t
74 dwrite(int fd, const void *data, size_t size)
75 {
76 const unsigned char *buf = data;
77 ssize_t rv = 0, z;
78
79 while (size) {
80 if ((z = write(fd, buf, size)) < 0) {
81 if (errno == EINTR)
82 continue;
83 return (rv ? /* fucked up since we got some */ -2 : -1);
84 }
85 rv += z;
86 buf += z;
87 size -= z;
88 }
89 return (rv);
90 }
91
92 #if !HAVE_DPRINTF
93 #include <stdarg.h>
94 #include <string.h>
95 static char *dprintf_donum(char *, unsigned long long);
96
97 /* replacement only as powerful as needed for this */
98 void
99 dprintf(int fd, const char *fmt, ...)
100 {
101 /* %s %llu %lu %d */
102 const char *ccp;
103 char *cp, buf[24];
104 unsigned long long uval;
105 va_list ap;
106
107 va_start(ap, fmt);
108 loop:
109 switch (*fmt) {
110 case 0:
111 goto out;
112 case '%':
113 break;
114 default:
115 ccp = fmt;
116 do {
117 ++ccp;
118 } while (*ccp && *ccp != '%');
119 if (dwrite(fd, fmt, ccp - fmt) < 0)
120 goto out;
121 fmt = ccp;
122 goto loop;
123 }
124 switch (*++fmt) {
125 case 'd': {
126 int ival;
127
128 ival = va_arg(ap, int);
129 cp = buf;
130 if (ival < 0) {
131 *cp++ = '-';
132 uval = -(long long)ival;
133 } else
134 uval = ival;
135 goto num;
136 }
137 case 'l':
138 switch (*++fmt) {
139 case 'l':
140 if (*++fmt != 'u') {
141 errfmt:
142 /* could do something about this? */
143 goto loop;
144 }
145 uval = va_arg(ap, unsigned long long);
146 break;
147 case 'u':
148 uval = va_arg(ap, unsigned long);
149 break;
150 default:
151 goto errfmt;
152 }
153 cp = buf;
154 num:
155 *dprintf_donum(cp, uval) = '\0';
156 cp = buf;
157 if (0)
158 /* FALLTHROUGH */
159 case 's':
160 cp = va_arg(ap, char *);
161 if (dwrite(fd, cp, strlen(cp)) < 0)
162 goto out;
163 break;
164 default:
165 goto errfmt;
166 }
167 ++fmt;
168 goto loop;
169
170 out:
171 va_end(ap);
172 }
173
174 static char *
175 dprintf_donum(char *cp, unsigned long long val)
176 {
177 unsigned long long subval = val / 10;
178
179 if (subval)
180 cp = dprintf_donum(cp, subval);
181 *cp++ = '0' + (val % 10);
182 return (cp);
183 }
184 #endif
185
186 #if !HAVE_STRLCPY || !HAVE_STRLCAT
187 #undef WIDEC
188 #define OUTSIDE_OF_LIBKERN
189 #if !HAVE_STRLCPY
190 #define L_strlcat
191 #endif
192 #if !HAVE_STRLCAT
193 #define L_strlcpy
194 #endif
195 #include ".linked/strlfun.inc"
196 #endif