| 1 |
|
| 2 |
0 |
| 3 |
|
| 4 |
8 |
| 5 |
|
| 6 |
‐ |
| 7 |
|
| 8 |
© 2018 |
| 9 |
|
| 10 |
2015 |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
‐ |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
, to |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
d. |
| 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 ? -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 |
|
| 98 |
void |
| 99 |
dprintf(int fd, const char *fmt, ...) |
| 100 |
{ |
| 101 |
|
| 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 |
|
| 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 |
|
| 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 |