Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* doio.c |
a687059c | 2 | * |
3818b22b | 3 | * Copyright (c) 1991-2000, Larry Wall |
a687059c | 4 | * |
6e21c824 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
a687059c | 7 | * |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "Far below them they saw the white waters pour into a foaming bowl, and | |
12 | * then swirl darkly about a deep oval basin in the rocks, until they found | |
13 | * their way out again through a narrow gate, and flowed away, fuming and | |
14 | * chattering, into calmer and more level reaches." | |
a687059c LW |
15 | */ |
16 | ||
17 | #include "EXTERN.h" | |
864dbfa3 | 18 | #define PERL_IN_DOIO_C |
a687059c LW |
19 | #include "perl.h" |
20 | ||
fe14fcc3 | 21 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
aec308ec | 22 | #ifndef HAS_SEM |
c2ab57d4 | 23 | #include <sys/ipc.h> |
aec308ec | 24 | #endif |
fe14fcc3 | 25 | #ifdef HAS_MSG |
c2ab57d4 | 26 | #include <sys/msg.h> |
e5d73d77 | 27 | #endif |
fe14fcc3 | 28 | #ifdef HAS_SHM |
c2ab57d4 | 29 | #include <sys/shm.h> |
a0d0e21e | 30 | # ifndef HAS_SHMAT_PROTOTYPE |
20ce7b12 | 31 | extern Shmat_t shmat (int, char *, int); |
a0d0e21e | 32 | # endif |
c2ab57d4 | 33 | #endif |
e5d73d77 | 34 | #endif |
c2ab57d4 | 35 | |
663a0e37 | 36 | #ifdef I_UTIME |
3730b96e | 37 | # if defined(_MSC_VER) || defined(__MINGW32__) |
3fe9a6f1 | 38 | # include <sys/utime.h> |
39 | # else | |
40 | # include <utime.h> | |
41 | # endif | |
663a0e37 | 42 | #endif |
85aff577 | 43 | |
85aff577 CS |
44 | #ifdef O_EXCL |
45 | # define OPEN_EXCL O_EXCL | |
46 | #else | |
47 | # define OPEN_EXCL 0 | |
48 | #endif | |
a687059c | 49 | |
76121258 | 50 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
51 | #include <signal.h> | |
52 | #endif | |
53 | ||
54 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ | |
55 | #ifdef I_UNISTD | |
56 | # include <unistd.h> | |
57 | #endif | |
58 | ||
232e078e AD |
59 | #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */ |
60 | # include <sys/socket.h> | |
29209bc5 | 61 | # if defined(USE_SOCKS) && defined(I_SOCKS) |
86959918 JH |
62 | # include <socks.h> |
63 | # endif | |
64 | # ifdef I_NETBSD | |
65 | # include <netdb.h> | |
66 | # endif | |
232e078e AD |
67 | # ifndef ENOTSOCK |
68 | # ifdef I_NET_ERRNO | |
69 | # include <net/errno.h> | |
70 | # endif | |
71 | # endif | |
72 | #endif | |
73 | ||
a687059c | 74 | bool |
6170680b IZ |
75 | Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
76 | int rawmode, int rawperm, PerlIO *supplied_fp) | |
77 | { | |
78 | return do_open9(gv, name, len, as_raw, rawmode, rawperm, | |
79 | supplied_fp, Nullsv, 0); | |
80 | } | |
81 | ||
82 | bool | |
83 | Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw, | |
84 | int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs, | |
85 | I32 num_svs) | |
a687059c | 86 | { |
a0d0e21e | 87 | register IO *io = GvIOn(gv); |
760ac839 LW |
88 | PerlIO *saveifp = Nullfp; |
89 | PerlIO *saveofp = Nullfp; | |
6e21c824 | 90 | char savetype = ' '; |
c07a80fd | 91 | int writing = 0; |
760ac839 | 92 | PerlIO *fp; |
c07a80fd | 93 | int fd; |
94 | int result; | |
3500f679 | 95 | bool was_fdopen = FALSE; |
a687059c | 96 | |
3280af22 | 97 | PL_forkprocess = 1; /* assume true if no fork */ |
c07a80fd | 98 | |
a0d0e21e | 99 | if (IoIFP(io)) { |
760ac839 | 100 | fd = PerlIO_fileno(IoIFP(io)); |
8990e307 | 101 | if (IoTYPE(io) == '-') |
c2ab57d4 | 102 | result = 0; |
3280af22 | 103 | else if (fd <= PL_maxsysfd) { |
8990e307 LW |
104 | saveifp = IoIFP(io); |
105 | saveofp = IoOFP(io); | |
106 | savetype = IoTYPE(io); | |
6e21c824 LW |
107 | result = 0; |
108 | } | |
8990e307 | 109 | else if (IoTYPE(io) == '|') |
3028581b | 110 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 LW |
111 | else if (IoIFP(io) != IoOFP(io)) { |
112 | if (IoOFP(io)) { | |
760ac839 | 113 | result = PerlIO_close(IoOFP(io)); |
6170680b | 114 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 LW |
115 | } |
116 | else | |
760ac839 | 117 | result = PerlIO_close(IoIFP(io)); |
a687059c | 118 | } |
a687059c | 119 | else |
760ac839 | 120 | result = PerlIO_close(IoIFP(io)); |
3280af22 | 121 | if (result == EOF && fd > PL_maxsysfd) |
bf49b057 | 122 | PerlIO_printf(Perl_error_log, |
6170680b IZ |
123 | "Warning: unable to close filehandle %s properly.\n", |
124 | GvENAME(gv)); | |
8990e307 | 125 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c | 126 | } |
c07a80fd | 127 | |
128 | if (as_raw) { | |
09458382 | 129 | #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE) |
5ff3f7a4 GS |
130 | rawmode |= O_LARGEFILE; |
131 | #endif | |
132 | ||
9d116dd7 JH |
133 | #ifndef O_ACCMODE |
134 | #define O_ACCMODE 3 /* Assume traditional implementation */ | |
135 | #endif | |
5ff3f7a4 | 136 | |
9d116dd7 JH |
137 | switch (result = rawmode & O_ACCMODE) { |
138 | case O_RDONLY: | |
139 | IoTYPE(io) = '<'; | |
140 | break; | |
141 | case O_WRONLY: | |
142 | IoTYPE(io) = '>'; | |
143 | break; | |
144 | case O_RDWR: | |
145 | default: | |
146 | IoTYPE(io) = '+'; | |
147 | break; | |
148 | } | |
149 | ||
c07a80fd | 150 | writing = (result > 0); |
3028581b | 151 | fd = PerlLIO_open3(name, rawmode, rawperm); |
9d116dd7 | 152 | |
c07a80fd | 153 | if (fd == -1) |
154 | fp = NULL; | |
155 | else { | |
a52cb5f7 | 156 | char *fpmode; |
9d116dd7 | 157 | if (result == O_RDONLY) |
360e5741 CS |
158 | fpmode = "r"; |
159 | #ifdef O_APPEND | |
160 | else if (rawmode & O_APPEND) | |
9d116dd7 | 161 | fpmode = (result == O_WRONLY) ? "a" : "a+"; |
360e5741 CS |
162 | #endif |
163 | else | |
9d116dd7 | 164 | fpmode = (result == O_WRONLY) ? "w" : "r+"; |
360e5741 | 165 | fp = PerlIO_fdopen(fd, fpmode); |
c07a80fd | 166 | if (!fp) |
3028581b | 167 | PerlLIO_close(fd); |
c07a80fd | 168 | } |
a687059c | 169 | } |
c07a80fd | 170 | else { |
faecd977 GS |
171 | char *type; |
172 | char *oname = name; | |
6170680b | 173 | STRLEN tlen; |
faecd977 | 174 | STRLEN olen = len; |
c07a80fd | 175 | char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ |
176 | int dodup; | |
177 | ||
faecd977 GS |
178 | type = savepvn(name, len); |
179 | tlen = len; | |
180 | SAVEFREEPV(type); | |
6170680b | 181 | if (num_svs) { |
faecd977 GS |
182 | STRLEN l; |
183 | name = SvPV(svs, l) ; | |
184 | len = (I32)l; | |
185 | name = savepvn(name, len); | |
186 | SAVEFREEPV(name); | |
6170680b | 187 | } |
faecd977 | 188 | else { |
6170680b IZ |
189 | while (tlen && isSPACE(type[tlen-1])) |
190 | type[--tlen] = '\0'; | |
faecd977 GS |
191 | name = type; |
192 | len = tlen; | |
193 | } | |
c07a80fd | 194 | mode[0] = mode[1] = mode[2] = '\0'; |
6170680b IZ |
195 | IoTYPE(io) = *type; |
196 | if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */ | |
197 | mode[1] = *type++; | |
198 | --tlen; | |
c07a80fd | 199 | writing = 1; |
a687059c | 200 | } |
c07a80fd | 201 | |
6170680b IZ |
202 | if (*type == '|') { |
203 | if (num_svs && (tlen != 2 || type[1] != '-')) { | |
204 | unknown_desr: | |
894356b3 | 205 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname); |
6170680b | 206 | } |
c07a80fd | 207 | /*SUPPRESS 530*/ |
faecd977 GS |
208 | for (type++, tlen--; isSPACE(*type); type++, tlen--) ; |
209 | if (!num_svs) { | |
6170680b | 210 | name = type; |
faecd977 GS |
211 | len = tlen; |
212 | } | |
06eaf0bc GS |
213 | if (*name == '\0') { /* command is missing 19990114 */ |
214 | dTHR; | |
215 | if (ckWARN(WARN_PIPE)) | |
cea2e8a9 | 216 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc GS |
217 | errno = EPIPE; |
218 | goto say_false; | |
219 | } | |
6170680b | 220 | if (strNE(name,"-") || num_svs) |
c07a80fd | 221 | TAINT_ENV(); |
222 | TAINT_PROPER("piped open"); | |
faecd977 | 223 | if (name[len-1] == '|') { |
d008e5eb | 224 | dTHR; |
faecd977 | 225 | name[--len] = '\0' ; |
599cee73 | 226 | if (ckWARN(WARN_PIPE)) |
9a7dcd9c | 227 | Perl_warner(aTHX_ WARN_PIPE, "Can't open bidirectional pipe"); |
7b8d334a | 228 | } |
3028581b | 229 | fp = PerlProc_popen(name,"w"); |
c07a80fd | 230 | writing = 1; |
231 | } | |
6170680b | 232 | else if (*type == '>') { |
c07a80fd | 233 | TAINT_PROPER("open"); |
6170680b IZ |
234 | type++; |
235 | if (*type == '>') { | |
c07a80fd | 236 | mode[0] = IoTYPE(io) = 'a'; |
6170680b IZ |
237 | type++; |
238 | tlen--; | |
a0d0e21e | 239 | } |
c07a80fd | 240 | else |
241 | mode[0] = 'w'; | |
242 | writing = 1; | |
243 | ||
6170680b IZ |
244 | if (num_svs && tlen != 1) |
245 | goto unknown_desr; | |
246 | if (*type == '&') { | |
247 | name = type; | |
c07a80fd | 248 | duplicity: |
249 | dodup = 1; | |
250 | name++; | |
251 | if (*name == '=') { | |
252 | dodup = 0; | |
a0d0e21e | 253 | name++; |
c07a80fd | 254 | } |
255 | if (!*name && supplied_fp) | |
256 | fp = supplied_fp; | |
a0d0e21e | 257 | else { |
c07a80fd | 258 | /*SUPPRESS 530*/ |
259 | for (; isSPACE(*name); name++) ; | |
260 | if (isDIGIT(*name)) | |
261 | fd = atoi(name); | |
262 | else { | |
263 | IO* thatio; | |
264 | gv = gv_fetchpv(name,FALSE,SVt_PVIO); | |
265 | thatio = GvIO(gv); | |
266 | if (!thatio) { | |
6e21c824 | 267 | #ifdef EINVAL |
c07a80fd | 268 | SETERRNO(EINVAL,SS$_IVCHAN); |
6e21c824 | 269 | #endif |
c07a80fd | 270 | goto say_false; |
271 | } | |
272 | if (IoIFP(thatio)) { | |
54195c32 | 273 | PerlIO *fp = IoIFP(thatio); |
7211d486 JH |
274 | /* Flush stdio buffer before dup. --mjd |
275 | * Unfortunately SEEK_CURing 0 seems to | |
276 | * be optimized away on most platforms; | |
277 | * only Solaris and Linux seem to flush | |
278 | * on that. --jhi */ | |
54195c32 | 279 | PerlIO_seek(fp, 0, SEEK_CUR); |
7211d486 JH |
280 | /* On the other hand, do all platforms |
281 | * take gracefully to flushing a read-only | |
282 | * filehandle? Perhaps we should do | |
283 | * fsetpos(src)+fgetpos(dst)? --nik */ | |
284 | PerlIO_flush(fp); | |
54195c32 | 285 | fd = PerlIO_fileno(fp); |
c07a80fd | 286 | if (IoTYPE(thatio) == 's') |
287 | IoTYPE(io) = 's'; | |
288 | } | |
289 | else | |
290 | fd = -1; | |
a0d0e21e | 291 | } |
fec02dd3 | 292 | if (dodup) |
3028581b | 293 | fd = PerlLIO_dup(fd); |
3500f679 RS |
294 | else |
295 | was_fdopen = TRUE; | |
760ac839 | 296 | if (!(fp = PerlIO_fdopen(fd,mode))) { |
c07a80fd | 297 | if (dodup) |
3028581b | 298 | PerlLIO_close(fd); |
faecd977 | 299 | } |
c07a80fd | 300 | } |
bf38876a | 301 | } |
c07a80fd | 302 | else { |
303 | /*SUPPRESS 530*/ | |
6170680b IZ |
304 | for (; isSPACE(*type); type++) ; |
305 | if (strEQ(type,"-")) { | |
760ac839 | 306 | fp = PerlIO_stdout(); |
c07a80fd | 307 | IoTYPE(io) = '-'; |
308 | } | |
309 | else { | |
6170680b | 310 | fp = PerlIO_open((num_svs ? name : type), mode); |
c07a80fd | 311 | } |
bf38876a LW |
312 | } |
313 | } | |
6170680b IZ |
314 | else if (*type == '<') { |
315 | if (num_svs && tlen != 1) | |
316 | goto unknown_desr; | |
c07a80fd | 317 | /*SUPPRESS 530*/ |
6170680b | 318 | for (type++; isSPACE(*type); type++) ; |
bf38876a | 319 | mode[0] = 'r'; |
6170680b IZ |
320 | if (*type == '&') { |
321 | name = type; | |
bf38876a | 322 | goto duplicity; |
6170680b IZ |
323 | } |
324 | if (strEQ(type,"-")) { | |
760ac839 | 325 | fp = PerlIO_stdin(); |
8990e307 | 326 | IoTYPE(io) = '-'; |
a687059c | 327 | } |
bf38876a | 328 | else |
6170680b | 329 | fp = PerlIO_open((num_svs ? name : type), mode); |
a687059c | 330 | } |
6170680b IZ |
331 | else if (tlen > 1 && type[tlen-1] == '|') { |
332 | if (num_svs) { | |
333 | if (tlen != 2 || type[0] != '-') | |
334 | goto unknown_desr; | |
335 | } | |
336 | else { | |
337 | type[--tlen] = '\0'; | |
338 | while (tlen && isSPACE(type[tlen-1])) | |
339 | type[--tlen] = '\0'; | |
340 | /*SUPPRESS 530*/ | |
341 | for (; isSPACE(*type); type++) ; | |
342 | name = type; | |
343 | } | |
06eaf0bc GS |
344 | if (*name == '\0') { /* command is missing 19990114 */ |
345 | dTHR; | |
346 | if (ckWARN(WARN_PIPE)) | |
cea2e8a9 | 347 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc GS |
348 | errno = EPIPE; |
349 | goto say_false; | |
350 | } | |
6170680b | 351 | if (strNE(name,"-") || num_svs) |
79072805 LW |
352 | TAINT_ENV(); |
353 | TAINT_PROPER("piped open"); | |
3028581b | 354 | fp = PerlProc_popen(name,"r"); |
8990e307 | 355 | IoTYPE(io) = '|'; |
a687059c LW |
356 | } |
357 | else { | |
6170680b IZ |
358 | if (num_svs) |
359 | goto unknown_desr; | |
360 | name = type; | |
8990e307 | 361 | IoTYPE(io) = '<'; |
99b89507 LW |
362 | /*SUPPRESS 530*/ |
363 | for (; isSPACE(*name); name++) ; | |
a687059c | 364 | if (strEQ(name,"-")) { |
760ac839 | 365 | fp = PerlIO_stdin(); |
8990e307 | 366 | IoTYPE(io) = '-'; |
a687059c LW |
367 | } |
368 | else | |
760ac839 | 369 | fp = PerlIO_open(name,"r"); |
a687059c LW |
370 | } |
371 | } | |
bee1dbe2 | 372 | if (!fp) { |
d008e5eb | 373 | dTHR; |
599cee73 | 374 | if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n')) |
cea2e8a9 | 375 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open"); |
6e21c824 | 376 | goto say_false; |
bee1dbe2 | 377 | } |
8990e307 LW |
378 | if (IoTYPE(io) && |
379 | IoTYPE(io) != '|' && IoTYPE(io) != '-') { | |
96827780 | 380 | dTHR; |
3280af22 | 381 | if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) { |
760ac839 | 382 | (void)PerlIO_close(fp); |
6e21c824 | 383 | goto say_false; |
a687059c | 384 | } |
3280af22 | 385 | if (S_ISSOCK(PL_statbuf.st_mode)) |
8990e307 | 386 | IoTYPE(io) = 's'; /* in case a socket was passed in to us */ |
99b89507 LW |
387 | #ifdef HAS_SOCKET |
388 | else if ( | |
c623bd54 | 389 | #ifdef S_IFMT |
3280af22 | 390 | !(PL_statbuf.st_mode & S_IFMT) |
99b89507 | 391 | #else |
b28d0864 | 392 | !PL_statbuf.st_mode |
99b89507 LW |
393 | #endif |
394 | ) { | |
96827780 MB |
395 | char tmpbuf[256]; |
396 | Sock_size_t buflen = sizeof tmpbuf; | |
3028581b | 397 | if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf, |
d574b85e CS |
398 | &buflen) >= 0 |
399 | || errno != ENOTSOCK) | |
8990e307 | 400 | IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */ |
99b89507 LW |
401 | /* but some return 0 for streams too, sigh */ |
402 | } | |
bf38876a | 403 | #endif |
a687059c | 404 | } |
6e21c824 | 405 | if (saveifp) { /* must use old fp? */ |
760ac839 | 406 | fd = PerlIO_fileno(saveifp); |
6e21c824 | 407 | if (saveofp) { |
760ac839 | 408 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 | 409 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 | 410 | PerlIO_close(saveofp); |
99b89507 LW |
411 | if (fd > 2) |
412 | Safefree(saveofp); | |
6e21c824 LW |
413 | } |
414 | } | |
760ac839 | 415 | if (fd != PerlIO_fileno(fp)) { |
d8a83dd3 | 416 | Pid_t pid; |
79072805 | 417 | SV *sv; |
bee1dbe2 | 418 | |
3028581b | 419 | PerlLIO_dup2(PerlIO_fileno(fp), fd); |
3280af22 | 420 | sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE); |
a0d0e21e | 421 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 LW |
422 | pid = SvIVX(sv); |
423 | SvIVX(sv) = 0; | |
3280af22 | 424 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
a0d0e21e | 425 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 | 426 | SvIVX(sv) = pid; |
3500f679 RS |
427 | if (!was_fdopen) |
428 | PerlIO_close(fp); | |
bee1dbe2 | 429 | |
6e21c824 LW |
430 | } |
431 | fp = saveifp; | |
760ac839 | 432 | PerlIO_clearerr(fp); |
6e21c824 | 433 | } |
a0d0e21e | 434 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
a8710ca1 GS |
435 | { |
436 | int save_errno = errno; | |
437 | fd = PerlIO_fileno(fp); | |
438 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ | |
439 | errno = save_errno; | |
440 | } | |
1462b684 | 441 | #endif |
8990e307 | 442 | IoIFP(io) = fp; |
684bef36 | 443 | IoFLAGS(io) &= ~IOf_NOLINE; |
bf38876a | 444 | if (writing) { |
96827780 | 445 | dTHR; |
8990e307 | 446 | if (IoTYPE(io) == 's' |
3280af22 | 447 | || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) { |
760ac839 LW |
448 | if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) { |
449 | PerlIO_close(fp); | |
8990e307 | 450 | IoIFP(io) = Nullfp; |
6e21c824 | 451 | goto say_false; |
fe14fcc3 | 452 | } |
1462b684 LW |
453 | } |
454 | else | |
8990e307 | 455 | IoOFP(io) = fp; |
bf38876a | 456 | } |
a687059c | 457 | return TRUE; |
6e21c824 LW |
458 | |
459 | say_false: | |
8990e307 LW |
460 | IoIFP(io) = saveifp; |
461 | IoOFP(io) = saveofp; | |
462 | IoTYPE(io) = savetype; | |
6e21c824 | 463 | return FALSE; |
a687059c LW |
464 | } |
465 | ||
760ac839 | 466 | PerlIO * |
864dbfa3 | 467 | Perl_nextargv(pTHX_ register GV *gv) |
a687059c | 468 | { |
79072805 | 469 | register SV *sv; |
99b89507 | 470 | #ifndef FLEXFILENAMES |
c623bd54 LW |
471 | int filedev; |
472 | int fileino; | |
99b89507 | 473 | #endif |
761237fe JB |
474 | Uid_t fileuid; |
475 | Gid_t filegid; | |
18708f5a | 476 | IO *io = GvIOp(gv); |
fe14fcc3 | 477 | |
3280af22 NIS |
478 | if (!PL_argvoutgv) |
479 | PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); | |
18708f5a GS |
480 | if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) { |
481 | IoFLAGS(io) &= ~IOf_START; | |
7a1c5554 GS |
482 | if (PL_inplace) { |
483 | if (!PL_argvout_stack) | |
484 | PL_argvout_stack = newAV(); | |
18708f5a | 485 | av_push(PL_argvout_stack, SvREFCNT_inc(PL_defoutgv)); |
7a1c5554 | 486 | } |
18708f5a | 487 | } |
3280af22 NIS |
488 | if (PL_filemode & (S_ISUID|S_ISGID)) { |
489 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ | |
fe14fcc3 | 490 | #ifdef HAS_FCHMOD |
3280af22 | 491 | (void)fchmod(PL_lastfd,PL_filemode); |
fe14fcc3 | 492 | #else |
b28d0864 | 493 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 LW |
494 | #endif |
495 | } | |
3280af22 | 496 | PL_filemode = 0; |
79072805 | 497 | while (av_len(GvAV(gv)) >= 0) { |
11343788 | 498 | dTHR; |
85aff577 | 499 | STRLEN oldlen; |
79072805 | 500 | sv = av_shift(GvAV(gv)); |
8990e307 | 501 | SAVEFREESV(sv); |
79072805 LW |
502 | sv_setsv(GvSV(gv),sv); |
503 | SvSETMAGIC(GvSV(gv)); | |
3280af22 | 504 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
9d116dd7 | 505 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) { |
3280af22 | 506 | if (PL_inplace) { |
79072805 | 507 | TAINT_PROPER("inplace open"); |
3280af22 | 508 | if (oldlen == 1 && *PL_oldname == '-') { |
4633a7c4 | 509 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e | 510 | return IoIFP(GvIOp(gv)); |
c623bd54 | 511 | } |
99b89507 | 512 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
513 | filedev = PL_statbuf.st_dev; |
514 | fileino = PL_statbuf.st_ino; | |
99b89507 | 515 | #endif |
3280af22 NIS |
516 | PL_filemode = PL_statbuf.st_mode; |
517 | fileuid = PL_statbuf.st_uid; | |
518 | filegid = PL_statbuf.st_gid; | |
519 | if (!S_ISREG(PL_filemode)) { | |
0453d815 PM |
520 | if (ckWARN_d(WARN_INPLACE)) |
521 | Perl_warner(aTHX_ WARN_INPLACE, | |
522 | "Can't do inplace edit: %s is not a regular file", | |
523 | PL_oldname ); | |
79072805 | 524 | do_close(gv,FALSE); |
c623bd54 LW |
525 | continue; |
526 | } | |
3280af22 NIS |
527 | if (*PL_inplace) { |
528 | char *star = strchr(PL_inplace, '*'); | |
2d259d92 | 529 | if (star) { |
3280af22 | 530 | char *begin = PL_inplace; |
2d259d92 CK |
531 | sv_setpvn(sv, "", 0); |
532 | do { | |
533 | sv_catpvn(sv, begin, star - begin); | |
3280af22 | 534 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 CK |
535 | begin = ++star; |
536 | } while ((star = strchr(begin, '*'))); | |
3d66d7bb GS |
537 | if (*begin) |
538 | sv_catpv(sv,begin); | |
2d259d92 CK |
539 | } |
540 | else { | |
3280af22 | 541 | sv_catpv(sv,PL_inplace); |
2d259d92 | 542 | } |
c623bd54 | 543 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
544 | if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0 |
545 | && PL_statbuf.st_dev == filedev | |
546 | && PL_statbuf.st_ino == fileino | |
39e571d4 LM |
547 | #ifdef DJGPP |
548 | || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0 | |
549 | #endif | |
f248d071 GS |
550 | ) |
551 | { | |
552 | if (ckWARN_d(WARN_INPLACE)) | |
553 | Perl_warner(aTHX_ WARN_INPLACE, | |
554 | "Can't do inplace edit: %s would not be unique", | |
555 | SvPVX(sv)); | |
79072805 | 556 | do_close(gv,FALSE); |
c623bd54 LW |
557 | continue; |
558 | } | |
559 | #endif | |
fe14fcc3 | 560 | #ifdef HAS_RENAME |
d308986b | 561 | #if !defined(DOSISH) && !defined(__CYGWIN__) |
3280af22 | 562 | if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 PM |
563 | if (ckWARN_d(WARN_INPLACE)) |
564 | Perl_warner(aTHX_ WARN_INPLACE, | |
565 | "Can't rename %s to %s: %s, skipping file", | |
566 | PL_oldname, SvPVX(sv), Strerror(errno) ); | |
79072805 | 567 | do_close(gv,FALSE); |
c623bd54 LW |
568 | continue; |
569 | } | |
a687059c | 570 | #else |
79072805 | 571 | do_close(gv,FALSE); |
3028581b | 572 | (void)PerlLIO_unlink(SvPVX(sv)); |
b28d0864 | 573 | (void)PerlLIO_rename(PL_oldname,SvPVX(sv)); |
9d116dd7 | 574 | do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp); |
55497cff | 575 | #endif /* DOSISH */ |
ff8e2863 | 576 | #else |
463ee0b2 | 577 | (void)UNLINK(SvPVX(sv)); |
b28d0864 | 578 | if (link(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 PM |
579 | if (ckWARN_d(WARN_INPLACE)) |
580 | Perl_warner(aTHX_ WARN_INPLACE, | |
581 | "Can't rename %s to %s: %s, skipping file", | |
582 | PL_oldname, SvPVX(sv), Strerror(errno) ); | |
79072805 | 583 | do_close(gv,FALSE); |
c623bd54 LW |
584 | continue; |
585 | } | |
b28d0864 | 586 | (void)UNLINK(PL_oldname); |
a687059c LW |
587 | #endif |
588 | } | |
589 | else { | |
a8c18271 | 590 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 | 591 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 | 592 | if (UNLINK(PL_oldname) < 0) { |
0453d815 PM |
593 | if (ckWARN_d(WARN_INPLACE)) |
594 | Perl_warner(aTHX_ WARN_INPLACE, | |
595 | "Can't remove %s: %s, skipping file", | |
596 | PL_oldname, Strerror(errno) ); | |
79072805 | 597 | do_close(gv,FALSE); |
fe14fcc3 LW |
598 | continue; |
599 | } | |
edc7bc49 | 600 | # endif |
ff8e2863 | 601 | #else |
cea2e8a9 | 602 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 | 603 | #endif |
a687059c LW |
604 | } |
605 | ||
3280af22 NIS |
606 | sv_setpvn(sv,">",!PL_inplace); |
607 | sv_catpvn(sv,PL_oldname,oldlen); | |
748a9306 | 608 | SETERRNO(0,0); /* in case sprintf set errno */ |
4119ab01 HM |
609 | #ifdef VMS |
610 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, | |
18708f5a | 611 | O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) |
4119ab01 | 612 | #else |
3280af22 | 613 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
18708f5a | 614 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) |
4119ab01 | 615 | #endif |
18708f5a | 616 | { |
0453d815 PM |
617 | if (ckWARN_d(WARN_INPLACE)) |
618 | Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s", | |
619 | PL_oldname, Strerror(errno) ); | |
79072805 | 620 | do_close(gv,FALSE); |
fe14fcc3 LW |
621 | continue; |
622 | } | |
3280af22 NIS |
623 | setdefout(PL_argvoutgv); |
624 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); | |
625 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); | |
fe14fcc3 | 626 | #ifdef HAS_FCHMOD |
3280af22 | 627 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c | 628 | #else |
3e3baf6d TB |
629 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
630 | /* Borland runtime creates a readonly file! */ | |
b28d0864 | 631 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
3e3baf6d | 632 | # endif |
a687059c | 633 | #endif |
3280af22 | 634 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 | 635 | #ifdef HAS_FCHOWN |
3280af22 | 636 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c | 637 | #else |
fe14fcc3 | 638 | #ifdef HAS_CHOWN |
b28d0864 | 639 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c | 640 | #endif |
b1248f16 | 641 | #endif |
fe14fcc3 | 642 | } |
a687059c | 643 | } |
a0d0e21e | 644 | return IoIFP(GvIOp(gv)); |
a687059c | 645 | } |
4d61ec05 GS |
646 | else { |
647 | dTHR; | |
648 | if (ckWARN_d(WARN_INPLACE)) { | |
6af84f9f GS |
649 | int eno = errno; |
650 | if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0 | |
651 | && !S_ISREG(PL_statbuf.st_mode)) | |
652 | { | |
4d61ec05 GS |
653 | Perl_warner(aTHX_ WARN_INPLACE, |
654 | "Can't do inplace edit: %s is not a regular file", | |
9a7dcd9c | 655 | PL_oldname); |
6af84f9f | 656 | } |
4d61ec05 | 657 | else |
9a7dcd9c | 658 | Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s", |
6af84f9f | 659 | PL_oldname, Strerror(eno)); |
4d61ec05 GS |
660 | } |
661 | } | |
a687059c | 662 | } |
18708f5a GS |
663 | if (io && (IoFLAGS(io) & IOf_ARGV)) |
664 | IoFLAGS(io) |= IOf_START; | |
3280af22 NIS |
665 | if (PL_inplace) { |
666 | (void)do_close(PL_argvoutgv,FALSE); | |
7a1c5554 GS |
667 | if (io && (IoFLAGS(io) & IOf_ARGV) |
668 | && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0) | |
669 | { | |
18708f5a GS |
670 | GV *oldout = (GV*)av_pop(PL_argvout_stack); |
671 | setdefout(oldout); | |
672 | SvREFCNT_dec(oldout); | |
673 | return Nullfp; | |
674 | } | |
4633a7c4 | 675 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c LW |
676 | } |
677 | return Nullfp; | |
678 | } | |
679 | ||
fe14fcc3 | 680 | #ifdef HAS_PIPE |
afd9f252 | 681 | void |
864dbfa3 | 682 | Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv) |
afd9f252 | 683 | { |
79072805 LW |
684 | register IO *rstio; |
685 | register IO *wstio; | |
afd9f252 LW |
686 | int fd[2]; |
687 | ||
79072805 | 688 | if (!rgv) |
afd9f252 | 689 | goto badexit; |
79072805 | 690 | if (!wgv) |
afd9f252 LW |
691 | goto badexit; |
692 | ||
a0d0e21e LW |
693 | rstio = GvIOn(rgv); |
694 | wstio = GvIOn(wgv); | |
afd9f252 | 695 | |
a0d0e21e | 696 | if (IoIFP(rstio)) |
79072805 | 697 | do_close(rgv,FALSE); |
a0d0e21e | 698 | if (IoIFP(wstio)) |
79072805 | 699 | do_close(wgv,FALSE); |
afd9f252 | 700 | |
3028581b | 701 | if (PerlProc_pipe(fd) < 0) |
afd9f252 | 702 | goto badexit; |
760ac839 LW |
703 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
704 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); | |
8990e307 LW |
705 | IoIFP(wstio) = IoOFP(wstio); |
706 | IoTYPE(rstio) = '<'; | |
707 | IoTYPE(wstio) = '>'; | |
708 | if (!IoIFP(rstio) || !IoOFP(wstio)) { | |
760ac839 | 709 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b | 710 | else PerlLIO_close(fd[0]); |
760ac839 | 711 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b | 712 | else PerlLIO_close(fd[1]); |
fe14fcc3 LW |
713 | goto badexit; |
714 | } | |
afd9f252 | 715 | |
3280af22 | 716 | sv_setsv(sv,&PL_sv_yes); |
afd9f252 LW |
717 | return; |
718 | ||
719 | badexit: | |
3280af22 | 720 | sv_setsv(sv,&PL_sv_undef); |
afd9f252 LW |
721 | return; |
722 | } | |
b1248f16 | 723 | #endif |
afd9f252 | 724 | |
517844ec | 725 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c | 726 | bool |
864dbfa3 | 727 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c | 728 | { |
1193dd27 IZ |
729 | bool retval; |
730 | IO *io; | |
a687059c | 731 | |
79072805 | 732 | if (!gv) |
3280af22 | 733 | gv = PL_argvgv; |
a0d0e21e | 734 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
1d2dff63 GS |
735 | if (not_implicit) |
736 | SETERRNO(EBADF,SS$_IVCHAN); | |
c2ab57d4 | 737 | return FALSE; |
99b89507 | 738 | } |
79072805 LW |
739 | io = GvIO(gv); |
740 | if (!io) { /* never opened */ | |
1d2dff63 | 741 | if (not_implicit) { |
d008e5eb | 742 | dTHR; |
599cee73 | 743 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 | 744 | Perl_warner(aTHX_ WARN_UNOPENED, |
599cee73 | 745 | "Close on unopened file <%s>",GvENAME(gv)); |
1d2dff63 GS |
746 | SETERRNO(EBADF,SS$_IVCHAN); |
747 | } | |
a687059c LW |
748 | return FALSE; |
749 | } | |
f2b5be74 | 750 | retval = io_close(io, not_implicit); |
517844ec | 751 | if (not_implicit) { |
1193dd27 IZ |
752 | IoLINES(io) = 0; |
753 | IoPAGE(io) = 0; | |
754 | IoLINES_LEFT(io) = IoPAGE_LEN(io); | |
755 | } | |
756 | IoTYPE(io) = ' '; | |
757 | return retval; | |
758 | } | |
759 | ||
760 | bool | |
f2b5be74 | 761 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 IZ |
762 | { |
763 | bool retval = FALSE; | |
764 | int status; | |
765 | ||
8990e307 LW |
766 | if (IoIFP(io)) { |
767 | if (IoTYPE(io) == '|') { | |
3028581b | 768 | status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 GS |
769 | if (not_implicit) { |
770 | STATUS_NATIVE_SET(status); | |
771 | retval = (STATUS_POSIX == 0); | |
772 | } | |
773 | else { | |
774 | retval = (status != -1); | |
775 | } | |
a687059c | 776 | } |
8990e307 | 777 | else if (IoTYPE(io) == '-') |
a687059c LW |
778 | retval = TRUE; |
779 | else { | |
8990e307 | 780 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 LW |
781 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
782 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ | |
c2ab57d4 LW |
783 | } |
784 | else | |
760ac839 | 785 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c | 786 | } |
8990e307 | 787 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 | 788 | } |
f2b5be74 | 789 | else if (not_implicit) { |
20408e3c GS |
790 | SETERRNO(EBADF,SS$_IVCHAN); |
791 | } | |
1193dd27 | 792 | |
a687059c LW |
793 | return retval; |
794 | } | |
795 | ||
796 | bool | |
864dbfa3 | 797 | Perl_do_eof(pTHX_ GV *gv) |
a687059c | 798 | { |
11343788 | 799 | dTHR; |
79072805 | 800 | register IO *io; |
a687059c LW |
801 | int ch; |
802 | ||
79072805 | 803 | io = GvIO(gv); |
a687059c | 804 | |
79072805 | 805 | if (!io) |
a687059c | 806 | return TRUE; |
af8c498a GS |
807 | else if (ckWARN(WARN_IO) |
808 | && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout() | |
809 | || IoIFP(io) == PerlIO_stderr())) | |
810 | { | |
811 | SV* sv = sv_newmortal(); | |
812 | gv_efullname3(sv, gv, Nullch); | |
813 | Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output", | |
814 | SvPV_nolen(sv)); | |
815 | } | |
a687059c | 816 | |
8990e307 | 817 | while (IoIFP(io)) { |
a687059c | 818 | |
760ac839 | 819 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 | 820 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 LW |
821 | return FALSE; /* this is the most usual case */ |
822 | } | |
a687059c | 823 | |
760ac839 | 824 | ch = PerlIO_getc(IoIFP(io)); |
a687059c | 825 | if (ch != EOF) { |
760ac839 | 826 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c LW |
827 | return FALSE; |
828 | } | |
760ac839 | 829 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 JH |
830 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
831 | PerlIO_set_cnt(IoIFP(io),-1); | |
760ac839 | 832 | } |
533c011a | 833 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
3280af22 | 834 | if (!nextargv(PL_argvgv)) /* get another fp handy */ |
a687059c LW |
835 | return TRUE; |
836 | } | |
837 | else | |
838 | return TRUE; /* normal fp, definitely end of file */ | |
839 | } | |
840 | return TRUE; | |
841 | } | |
842 | ||
5ff3f7a4 | 843 | Off_t |
864dbfa3 | 844 | Perl_do_tell(pTHX_ GV *gv) |
a687059c | 845 | { |
79072805 | 846 | register IO *io; |
96e4d5b1 | 847 | register PerlIO *fp; |
a687059c | 848 | |
96e4d5b1 | 849 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 850 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 | 851 | if (PerlIO_eof(fp)) |
852 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 853 | #endif |
8903cb82 | 854 | return PerlIO_tell(fp); |
96e4d5b1 | 855 | } |
d008e5eb GS |
856 | { |
857 | dTHR; | |
858 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 859 | Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file"); |
d008e5eb | 860 | } |
748a9306 | 861 | SETERRNO(EBADF,RMS$_IFI); |
5ff3f7a4 | 862 | return (Off_t)-1; |
a687059c LW |
863 | } |
864 | ||
865 | bool | |
864dbfa3 | 866 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c | 867 | { |
79072805 | 868 | register IO *io; |
137443ea | 869 | register PerlIO *fp; |
a687059c | 870 | |
137443ea | 871 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 872 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea | 873 | if (PerlIO_eof(fp)) |
874 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 875 | #endif |
8903cb82 | 876 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea | 877 | } |
d008e5eb GS |
878 | { |
879 | dTHR; | |
880 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 881 | Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file"); |
d008e5eb | 882 | } |
748a9306 | 883 | SETERRNO(EBADF,RMS$_IFI); |
a687059c LW |
884 | return FALSE; |
885 | } | |
886 | ||
97cc44eb | 887 | Off_t |
864dbfa3 | 888 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 | 889 | { |
890 | register IO *io; | |
891 | register PerlIO *fp; | |
892 | ||
893 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) | |
3028581b | 894 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
d008e5eb GS |
895 | { |
896 | dTHR; | |
897 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 898 | Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file"); |
d008e5eb | 899 | } |
8903cb82 | 900 | SETERRNO(EBADF,RMS$_IFI); |
d9b3e12d | 901 | return (Off_t)-1; |
8903cb82 | 902 | } |
903 | ||
6ff81951 | 904 | int |
864dbfa3 | 905 | Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag) |
6ff81951 GS |
906 | { |
907 | if (flag != TRUE) | |
cea2e8a9 | 908 | Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */ |
6ff81951 | 909 | #ifdef DOSISH |
61ae2fbf | 910 | #if defined(atarist) || defined(__MINT__) |
6ff81951 GS |
911 | if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN)) |
912 | return 1; | |
913 | else | |
914 | return 0; | |
915 | #else | |
916 | if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) { | |
917 | #if defined(WIN32) && defined(__BORLANDC__) | |
918 | /* The translation mode of the stream is maintained independent | |
919 | * of the translation mode of the fd in the Borland RTL (heavy | |
920 | * digging through their runtime sources reveal). User has to | |
921 | * set the mode explicitly for the stream (though they don't | |
922 | * document this anywhere). GSAR 97-5-24 | |
923 | */ | |
924 | PerlIO_seek(fp,0L,0); | |
873ef191 | 925 | ((FILE*)fp)->flags |= _F_BIN; |
6ff81951 GS |
926 | #endif |
927 | return 1; | |
928 | } | |
929 | else | |
930 | return 0; | |
931 | #endif | |
932 | #else | |
933 | #if defined(USEMYBINMODE) | |
1cab015a | 934 | if (my_binmode(fp,iotype) != FALSE) |
6ff81951 GS |
935 | return 1; |
936 | else | |
937 | return 0; | |
938 | #else | |
939 | return 1; | |
940 | #endif | |
941 | #endif | |
942 | } | |
943 | ||
a0d0e21e | 944 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 | 945 | /* code courtesy of William Kucharski */ |
fe14fcc3 | 946 | #define HAS_CHSIZE |
6eb13c3b | 947 | |
517844ec | 948 | I32 my_chsize(fd, length) |
79072805 | 949 | I32 fd; /* file descriptor */ |
85e6fe83 | 950 | Off_t length; /* length to set file to */ |
6eb13c3b | 951 | { |
6eb13c3b LW |
952 | struct flock fl; |
953 | struct stat filebuf; | |
954 | ||
3028581b | 955 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b LW |
956 | return -1; |
957 | ||
958 | if (filebuf.st_size < length) { | |
959 | ||
960 | /* extend file length */ | |
961 | ||
3028581b | 962 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b LW |
963 | return -1; |
964 | ||
965 | /* write a "0" byte */ | |
966 | ||
3028581b | 967 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b LW |
968 | return -1; |
969 | } | |
970 | else { | |
971 | /* truncate length */ | |
972 | ||
973 | fl.l_whence = 0; | |
974 | fl.l_len = 0; | |
975 | fl.l_start = length; | |
a0d0e21e | 976 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b LW |
977 | |
978 | /* | |
a0d0e21e | 979 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b LW |
980 | * fcntl(2), which truncates the file so that it ends at the |
981 | * position indicated by fl.l_start. | |
982 | * | |
983 | * Will minor miracles never cease? | |
984 | */ | |
985 | ||
a0d0e21e | 986 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b LW |
987 | return -1; |
988 | ||
989 | } | |
990 | ||
991 | return 0; | |
992 | } | |
a0d0e21e | 993 | #endif /* F_FREESP */ |
ff8e2863 | 994 | |
a687059c | 995 | bool |
864dbfa3 | 996 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c LW |
997 | { |
998 | register char *tmps; | |
463ee0b2 | 999 | STRLEN len; |
a687059c | 1000 | |
79072805 LW |
1001 | /* assuming fp is checked earlier */ |
1002 | if (!sv) | |
1003 | return TRUE; | |
3280af22 | 1004 | if (PL_ofmt) { |
8990e307 | 1005 | if (SvGMAGICAL(sv)) |
79072805 | 1006 | mg_get(sv); |
463ee0b2 | 1007 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
65202027 | 1008 | PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv)); |
760ac839 | 1009 | return !PerlIO_error(fp); |
79072805 | 1010 | } |
463ee0b2 | 1011 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 | 1012 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
3280af22 | 1013 | PerlIO_printf(fp, PL_ofmt, SvNVX(sv)); |
760ac839 | 1014 | return !PerlIO_error(fp); |
79072805 | 1015 | } |
a687059c | 1016 | } |
79072805 LW |
1017 | switch (SvTYPE(sv)) { |
1018 | case SVt_NULL: | |
d008e5eb GS |
1019 | { |
1020 | dTHR; | |
1021 | if (ckWARN(WARN_UNINITIALIZED)) | |
b89fed5f | 1022 | report_uninit(); |
d008e5eb | 1023 | } |
ff8e2863 | 1024 | return TRUE; |
79072805 | 1025 | case SVt_IV: |
a0d0e21e LW |
1026 | if (SvIOK(sv)) { |
1027 | if (SvGMAGICAL(sv)) | |
1028 | mg_get(sv); | |
cf2093f6 | 1029 | if (SvIsUV(sv)) |
57def98f | 1030 | PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv)); |
cf2093f6 | 1031 | else |
57def98f | 1032 | PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv)); |
760ac839 | 1033 | return !PerlIO_error(fp); |
a0d0e21e LW |
1034 | } |
1035 | /* FALL THROUGH */ | |
79072805 | 1036 | default: |
463ee0b2 | 1037 | tmps = SvPV(sv, len); |
79072805 | 1038 | break; |
ff8e2863 | 1039 | } |
94e4c244 JH |
1040 | /* To detect whether the process is about to overstep its |
1041 | * filesize limit we would need getrlimit(). We could then | |
1042 | * also transparently raise the limit with setrlimit() -- | |
1043 | * but only until the system hard limit/the filesystem limit, | |
c5dd3cdd JH |
1044 | * at which we would get EPERM. Note that when using buffered |
1045 | * io the write failure can be delayed until the flush/close. --jhi */ | |
760ac839 | 1046 | if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) |
a687059c | 1047 | return FALSE; |
760ac839 | 1048 | return !PerlIO_error(fp); |
a687059c LW |
1049 | } |
1050 | ||
79072805 | 1051 | I32 |
cea2e8a9 | 1052 | Perl_my_stat(pTHX) |
a687059c | 1053 | { |
4e35701f | 1054 | djSP; |
79072805 | 1055 | IO *io; |
748a9306 | 1056 | GV* tmpgv; |
79072805 | 1057 | |
533c011a | 1058 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1059 | EXTEND(SP,1); |
638eceb6 | 1060 | tmpgv = cGVOP_gv; |
748a9306 LW |
1061 | do_fstat: |
1062 | io = GvIO(tmpgv); | |
8990e307 | 1063 | if (io && IoIFP(io)) { |
3280af22 NIS |
1064 | PL_statgv = tmpgv; |
1065 | sv_setpv(PL_statname,""); | |
1066 | PL_laststype = OP_STAT; | |
1067 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); | |
a687059c LW |
1068 | } |
1069 | else { | |
3280af22 NIS |
1070 | if (tmpgv == PL_defgv) |
1071 | return PL_laststatval; | |
599cee73 | 1072 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 | 1073 | Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>", |
748a9306 | 1074 | GvENAME(tmpgv)); |
3280af22 NIS |
1075 | PL_statgv = Nullgv; |
1076 | sv_setpv(PL_statname,""); | |
1077 | return (PL_laststatval = -1); | |
a687059c LW |
1078 | } |
1079 | } | |
1080 | else { | |
748a9306 | 1081 | SV* sv = POPs; |
4b74e3fb | 1082 | char *s; |
2d8e6c8d | 1083 | STRLEN n_a; |
79072805 | 1084 | PUTBACK; |
748a9306 LW |
1085 | if (SvTYPE(sv) == SVt_PVGV) { |
1086 | tmpgv = (GV*)sv; | |
1087 | goto do_fstat; | |
1088 | } | |
1089 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { | |
1090 | tmpgv = (GV*)SvRV(sv); | |
1091 | goto do_fstat; | |
1092 | } | |
1093 | ||
2d8e6c8d | 1094 | s = SvPV(sv, n_a); |
3280af22 NIS |
1095 | PL_statgv = Nullgv; |
1096 | sv_setpv(PL_statname, s); | |
1097 | PL_laststype = OP_STAT; | |
1098 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); | |
599cee73 | 1099 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
cea2e8a9 | 1100 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat"); |
3280af22 | 1101 | return PL_laststatval; |
a687059c LW |
1102 | } |
1103 | } | |
1104 | ||
79072805 | 1105 | I32 |
cea2e8a9 | 1106 | Perl_my_lstat(pTHX) |
c623bd54 | 1107 | { |
4e35701f | 1108 | djSP; |
79072805 | 1109 | SV *sv; |
2d8e6c8d | 1110 | STRLEN n_a; |
533c011a | 1111 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1112 | EXTEND(SP,1); |
638eceb6 | 1113 | if (cGVOP_gv == PL_defgv) { |
3280af22 | 1114 | if (PL_laststype != OP_LSTAT) |
cea2e8a9 | 1115 | Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat"); |
3280af22 | 1116 | return PL_laststatval; |
fe14fcc3 | 1117 | } |
cea2e8a9 | 1118 | Perl_croak(aTHX_ "You can't use -l on a filehandle"); |
fe14fcc3 | 1119 | } |
c623bd54 | 1120 | |
3280af22 NIS |
1121 | PL_laststype = OP_LSTAT; |
1122 | PL_statgv = Nullgv; | |
79072805 LW |
1123 | sv = POPs; |
1124 | PUTBACK; | |
2d8e6c8d | 1125 | sv_setpv(PL_statname,SvPV(sv, n_a)); |
2d8e6c8d | 1126 | PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache); |
2d8e6c8d | 1127 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) |
cea2e8a9 | 1128 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat"); |
3280af22 | 1129 | return PL_laststatval; |
c623bd54 LW |
1130 | } |
1131 | ||
a687059c | 1132 | bool |
864dbfa3 | 1133 | Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp) |
a687059c | 1134 | { |
d5a9bfb0 IZ |
1135 | return do_aexec5(really, mark, sp, 0, 0); |
1136 | } | |
1137 | ||
1138 | bool | |
2aa1486d GS |
1139 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
1140 | int fd, int do_report) | |
d5a9bfb0 | 1141 | { |
cd39f2b6 JH |
1142 | #ifdef MACOS_TRADITIONAL |
1143 | Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system"); | |
1144 | #else | |
a687059c | 1145 | register char **a; |
a687059c | 1146 | char *tmps; |
2d8e6c8d | 1147 | STRLEN n_a; |
a687059c | 1148 | |
79072805 | 1149 | if (sp > mark) { |
11343788 | 1150 | dTHR; |
3280af22 NIS |
1151 | New(401,PL_Argv, sp - mark + 1, char*); |
1152 | a = PL_Argv; | |
79072805 LW |
1153 | while (++mark <= sp) { |
1154 | if (*mark) | |
2d8e6c8d | 1155 | *a++ = SvPVx(*mark, n_a); |
a687059c LW |
1156 | else |
1157 | *a++ = ""; | |
1158 | } | |
1159 | *a = Nullch; | |
3280af22 | 1160 | if (*PL_Argv[0] != '/') /* will execvp use PATH? */ |
79072805 | 1161 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
2d8e6c8d | 1162 | if (really && *(tmps = SvPV(really, n_a))) |
3280af22 | 1163 | PerlProc_execvp(tmps,PL_Argv); |
a687059c | 1164 | else |
3280af22 | 1165 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
599cee73 | 1166 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 | 1167 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
599cee73 | 1168 | PL_Argv[0], Strerror(errno)); |
d5a9bfb0 IZ |
1169 | if (do_report) { |
1170 | int e = errno; | |
1171 | ||
1172 | PerlLIO_write(fd, (void*)&e, sizeof(int)); | |
1173 | PerlLIO_close(fd); | |
1174 | } | |
a687059c | 1175 | } |
bee1dbe2 | 1176 | do_execfree(); |
cd39f2b6 | 1177 | #endif |
a687059c LW |
1178 | return FALSE; |
1179 | } | |
1180 | ||
fe14fcc3 | 1181 | void |
864dbfa3 | 1182 | Perl_do_execfree(pTHX) |
ff8e2863 | 1183 | { |
3280af22 NIS |
1184 | if (PL_Argv) { |
1185 | Safefree(PL_Argv); | |
1186 | PL_Argv = Null(char **); | |
ff8e2863 | 1187 | } |
3280af22 NIS |
1188 | if (PL_Cmd) { |
1189 | Safefree(PL_Cmd); | |
1190 | PL_Cmd = Nullch; | |
ff8e2863 LW |
1191 | } |
1192 | } | |
1193 | ||
cd39f2b6 | 1194 | #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) && !defined(MACOS_TRADITIONAL) |
760ac839 | 1195 | |
a687059c | 1196 | bool |
864dbfa3 | 1197 | Perl_do_exec(pTHX_ char *cmd) |
a687059c | 1198 | { |
e446cec8 IZ |
1199 | return do_exec3(cmd,0,0); |
1200 | } | |
1201 | ||
1202 | bool | |
864dbfa3 | 1203 | Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report) |
e446cec8 | 1204 | { |
a687059c LW |
1205 | register char **a; |
1206 | register char *s; | |
a687059c LW |
1207 | char flags[10]; |
1208 | ||
748a9306 LW |
1209 | while (*cmd && isSPACE(*cmd)) |
1210 | cmd++; | |
1211 | ||
a687059c LW |
1212 | /* save an extra exec if possible */ |
1213 | ||
bf38876a | 1214 | #ifdef CSH |
3280af22 | 1215 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) { |
a687059c | 1216 | strcpy(flags,"-c"); |
3280af22 | 1217 | s = cmd+PL_cshlen+3; |
a687059c LW |
1218 | if (*s == 'f') { |
1219 | s++; | |
1220 | strcat(flags,"f"); | |
1221 | } | |
1222 | if (*s == ' ') | |
1223 | s++; | |
1224 | if (*s++ == '\'') { | |
1225 | char *ncmd = s; | |
1226 | ||
1227 | while (*s) | |
1228 | s++; | |
1229 | if (s[-1] == '\n') | |
1230 | *--s = '\0'; | |
1231 | if (s[-1] == '\'') { | |
1232 | *--s = '\0'; | |
3280af22 | 1233 | PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0); |
a687059c LW |
1234 | *s = '\''; |
1235 | return FALSE; | |
1236 | } | |
1237 | } | |
1238 | } | |
bf38876a | 1239 | #endif /* CSH */ |
a687059c LW |
1240 | |
1241 | /* see if there are shell metacharacters in it */ | |
1242 | ||
748a9306 LW |
1243 | if (*cmd == '.' && isSPACE(cmd[1])) |
1244 | goto doshell; | |
1245 | ||
1246 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) | |
1247 | goto doshell; | |
1248 | ||
c170e444 | 1249 | for (s = cmd; *s && isALNUM(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 LW |
1250 | if (*s == '=') |
1251 | goto doshell; | |
748a9306 | 1252 | |
a687059c | 1253 | for (s = cmd; *s; s++) { |
93a17b20 | 1254 | if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c LW |
1255 | if (*s == '\n' && !s[1]) { |
1256 | *s = '\0'; | |
1257 | break; | |
1258 | } | |
603a98b0 IZ |
1259 | /* handle the 2>&1 construct at the end */ |
1260 | if (*s == '>' && s[1] == '&' && s[2] == '1' | |
1261 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) | |
1262 | && (!s[3] || isSPACE(s[3]))) | |
1263 | { | |
1264 | char *t = s + 3; | |
1265 | ||
1266 | while (*t && isSPACE(*t)) | |
1267 | ++t; | |
1268 | if (!*t && (dup2(1,2) != -1)) { | |
1269 | s[-2] = '\0'; | |
1270 | break; | |
1271 | } | |
1272 | } | |
a687059c | 1273 | doshell: |
3280af22 | 1274 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); |
a687059c LW |
1275 | return FALSE; |
1276 | } | |
1277 | } | |
748a9306 | 1278 | |
3280af22 NIS |
1279 | New(402,PL_Argv, (s - cmd) / 2 + 2, char*); |
1280 | PL_Cmd = savepvn(cmd, s-cmd); | |
1281 | a = PL_Argv; | |
1282 | for (s = PL_Cmd; *s;) { | |
99b89507 | 1283 | while (*s && isSPACE(*s)) s++; |
a687059c LW |
1284 | if (*s) |
1285 | *(a++) = s; | |
99b89507 | 1286 | while (*s && !isSPACE(*s)) s++; |
a687059c LW |
1287 | if (*s) |
1288 | *s++ = '\0'; | |
1289 | } | |
1290 | *a = Nullch; | |
3280af22 NIS |
1291 | if (PL_Argv[0]) { |
1292 | PerlProc_execvp(PL_Argv[0],PL_Argv); | |
b1248f16 | 1293 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 | 1294 | do_execfree(); |
a687059c | 1295 | goto doshell; |
b1248f16 | 1296 | } |
d008e5eb GS |
1297 | { |
1298 | dTHR; | |
e446cec8 IZ |
1299 | int e = errno; |
1300 | ||
d008e5eb | 1301 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 | 1302 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
d008e5eb | 1303 | PL_Argv[0], Strerror(errno)); |
e446cec8 IZ |
1304 | if (do_report) { |
1305 | PerlLIO_write(fd, (void*)&e, sizeof(int)); | |
1306 | PerlLIO_close(fd); | |
1307 | } | |
d008e5eb | 1308 | } |
a687059c | 1309 | } |
ff8e2863 | 1310 | do_execfree(); |
a687059c LW |
1311 | return FALSE; |
1312 | } | |
1313 | ||
6890e559 | 1314 | #endif /* OS2 || WIN32 */ |
760ac839 | 1315 | |
79072805 | 1316 | I32 |
864dbfa3 | 1317 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c | 1318 | { |
11343788 | 1319 | dTHR; |
79072805 LW |
1320 | register I32 val; |
1321 | register I32 val2; | |
1322 | register I32 tot = 0; | |
20408e3c | 1323 | char *what; |
a687059c | 1324 | char *s; |
79072805 | 1325 | SV **oldmark = mark; |
2d8e6c8d | 1326 | STRLEN n_a; |
a687059c | 1327 | |
20408e3c | 1328 | #define APPLY_TAINT_PROPER() \ |
3280af22 | 1329 | STMT_START { \ |
17406bd6 | 1330 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 | 1331 | } STMT_END |
20408e3c GS |
1332 | |
1333 | /* This is a first heuristic; it doesn't catch tainting magic. */ | |
3280af22 | 1334 | if (PL_tainting) { |
463ee0b2 | 1335 | while (++mark <= sp) { |
bbce6d69 | 1336 | if (SvTAINTED(*mark)) { |
1337 | TAINT; | |
1338 | break; | |
1339 | } | |
463ee0b2 LW |
1340 | } |
1341 | mark = oldmark; | |
1342 | } | |
a687059c | 1343 | switch (type) { |
79072805 | 1344 | case OP_CHMOD: |
20408e3c GS |
1345 | what = "chmod"; |
1346 | APPLY_TAINT_PROPER(); | |
79072805 | 1347 | if (++mark <= sp) { |
463ee0b2 | 1348 | val = SvIVx(*mark); |
20408e3c GS |
1349 | APPLY_TAINT_PROPER(); |
1350 | tot = sp - mark; | |
79072805 | 1351 | while (++mark <= sp) { |
2d8e6c8d | 1352 | char *name = SvPVx(*mark, n_a); |
20408e3c GS |
1353 | APPLY_TAINT_PROPER(); |
1354 | if (PerlLIO_chmod(name, val)) | |
a687059c LW |
1355 | tot--; |
1356 | } | |
1357 | } | |
1358 | break; | |
fe14fcc3 | 1359 | #ifdef HAS_CHOWN |
79072805 | 1360 | case OP_CHOWN: |
20408e3c GS |
1361 | what = "chown"; |
1362 | APPLY_TAINT_PROPER(); | |
79072805 | 1363 | if (sp - mark > 2) { |
463ee0b2 LW |
1364 | val = SvIVx(*++mark); |
1365 | val2 = SvIVx(*++mark); | |
20408e3c | 1366 | APPLY_TAINT_PROPER(); |
a0d0e21e | 1367 | tot = sp - mark; |
79072805 | 1368 | while (++mark <= sp) { |
2d8e6c8d | 1369 | char *name = SvPVx(*mark, n_a); |
20408e3c | 1370 | APPLY_TAINT_PROPER(); |
36660982 | 1371 | if (PerlLIO_chown(name, val, val2)) |
a687059c LW |
1372 | tot--; |
1373 | } | |
1374 | } | |
1375 | break; | |
b1248f16 | 1376 | #endif |
dd64f1c3 AD |
1377 | /* |
1378 | XXX Should we make lchown() directly available from perl? | |
1379 | For now, we'll let Configure test for HAS_LCHOWN, but do | |
1380 | nothing in the core. | |
1381 | --AD 5/1998 | |
1382 | */ | |
fe14fcc3 | 1383 | #ifdef HAS_KILL |
79072805 | 1384 | case OP_KILL: |
20408e3c GS |
1385 | what = "kill"; |
1386 | APPLY_TAINT_PROPER(); | |
55497cff | 1387 | if (mark == sp) |
1388 | break; | |
2d8e6c8d | 1389 | s = SvPVx(*++mark, n_a); |
79072805 LW |
1390 | if (isUPPER(*s)) { |
1391 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') | |
1392 | s += 3; | |
1393 | if (!(val = whichsig(s))) | |
cea2e8a9 | 1394 | Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s); |
79072805 LW |
1395 | } |
1396 | else | |
463ee0b2 | 1397 | val = SvIVx(*mark); |
20408e3c GS |
1398 | APPLY_TAINT_PROPER(); |
1399 | tot = sp - mark; | |
3595fcef | 1400 | #ifdef VMS |
1401 | /* kill() doesn't do process groups (job trees?) under VMS */ | |
1402 | if (val < 0) val = -val; | |
1403 | if (val == SIGKILL) { | |
1404 | # include <starlet.h> | |
1405 | /* Use native sys$delprc() to insure that target process is | |
1406 | * deleted; supervisor-mode images don't pay attention to | |
1407 | * CRTL's emulation of Unix-style signals and kill() | |
1408 | */ | |
1409 | while (++mark <= sp) { | |
1410 | I32 proc = SvIVx(*mark); | |
1411 | register unsigned long int __vmssts; | |
20408e3c | 1412 | APPLY_TAINT_PROPER(); |
3595fcef | 1413 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1414 | tot--; | |
1415 | switch (__vmssts) { | |
1416 | case SS$_NONEXPR: | |
1417 | case SS$_NOSUCHNODE: | |
1418 | SETERRNO(ESRCH,__vmssts); | |
1419 | break; | |
1420 | case SS$_NOPRIV: | |
1421 | SETERRNO(EPERM,__vmssts); | |
1422 | break; | |
1423 | default: | |
1424 | SETERRNO(EVMSERR,__vmssts); | |
1425 | } | |
1426 | } | |
1427 | } | |
1428 | break; | |
1429 | } | |
1430 | #endif | |
79072805 LW |
1431 | if (val < 0) { |
1432 | val = -val; | |
1433 | while (++mark <= sp) { | |
463ee0b2 | 1434 | I32 proc = SvIVx(*mark); |
20408e3c | 1435 | APPLY_TAINT_PROPER(); |
fe14fcc3 | 1436 | #ifdef HAS_KILLPG |
3028581b | 1437 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c | 1438 | #else |
3028581b | 1439 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c | 1440 | #endif |
79072805 | 1441 | tot--; |
a687059c | 1442 | } |
79072805 LW |
1443 | } |
1444 | else { | |
1445 | while (++mark <= sp) { | |
20408e3c GS |
1446 | I32 proc = SvIVx(*mark); |
1447 | APPLY_TAINT_PROPER(); | |
1448 | if (PerlProc_kill(proc, val)) | |
79072805 | 1449 | tot--; |
a687059c LW |
1450 | } |
1451 | } | |
1452 | break; | |
b1248f16 | 1453 | #endif |
79072805 | 1454 | case OP_UNLINK: |
20408e3c GS |
1455 | what = "unlink"; |
1456 | APPLY_TAINT_PROPER(); | |
79072805 LW |
1457 | tot = sp - mark; |
1458 | while (++mark <= sp) { | |
2d8e6c8d | 1459 | s = SvPVx(*mark, n_a); |
20408e3c | 1460 | APPLY_TAINT_PROPER(); |
3280af22 | 1461 | if (PL_euid || PL_unsafe) { |
a687059c LW |
1462 | if (UNLINK(s)) |
1463 | tot--; | |
1464 | } | |
1465 | else { /* don't let root wipe out directories without -U */ | |
3280af22 | 1466 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c LW |
1467 | tot--; |
1468 | else { | |
1469 | if (UNLINK(s)) | |
1470 | tot--; | |
1471 | } | |
1472 | } | |
1473 | } | |
1474 | break; | |
a0d0e21e | 1475 | #ifdef HAS_UTIME |
79072805 | 1476 | case OP_UTIME: |
20408e3c GS |
1477 | what = "utime"; |
1478 | APPLY_TAINT_PROPER(); | |
79072805 | 1479 | if (sp - mark > 2) { |
748a9306 | 1480 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 LW |
1481 | struct utimbuf utbuf; |
1482 | #else | |
a687059c | 1483 | struct { |
dd2821f6 GS |
1484 | Time_t actime; |
1485 | Time_t modtime; | |
a687059c | 1486 | } utbuf; |
663a0e37 | 1487 | #endif |
a687059c | 1488 | |
afd9f252 | 1489 | Zero(&utbuf, sizeof utbuf, char); |
517844ec | 1490 | #ifdef BIG_TIME |
dd2821f6 GS |
1491 | utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */ |
1492 | utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */ | |
517844ec | 1493 | #else |
dd2821f6 GS |
1494 | utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */ |
1495 | utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */ | |
517844ec | 1496 | #endif |
20408e3c | 1497 | APPLY_TAINT_PROPER(); |
79072805 LW |
1498 | tot = sp - mark; |
1499 | while (++mark <= sp) { | |
2d8e6c8d | 1500 | char *name = SvPVx(*mark, n_a); |
20408e3c GS |
1501 | APPLY_TAINT_PROPER(); |
1502 | if (PerlLIO_utime(name, &utbuf)) | |
a687059c LW |
1503 | tot--; |
1504 | } | |
a687059c LW |
1505 | } |
1506 | else | |
79072805 | 1507 | tot = 0; |
a687059c | 1508 | break; |
a0d0e21e | 1509 | #endif |
a687059c LW |
1510 | } |
1511 | return tot; | |
20408e3c | 1512 | |
20408e3c | 1513 | #undef APPLY_TAINT_PROPER |
a687059c LW |
1514 | } |
1515 | ||
1516 | /* Do the permissions allow some operation? Assumes statcache already set. */ | |
a0d0e21e | 1517 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae JH |
1518 | bool |
1519 | Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp) | |
1520 | /* Note: we use `effective' both for uids and gids. | |
1521 | * Here we are betting on Uid_t being equal or wider than Gid_t. */ | |
a687059c | 1522 | { |
bee1dbe2 | 1523 | #ifdef DOSISH |
fe14fcc3 LW |
1524 | /* [Comments and code from Len Reed] |
1525 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write | |
1526 | * to write-protected files. The execute permission bit is set | |
1527 | * by the Miscrosoft C library stat() function for the following: | |
1528 | * .exe files | |
1529 | * .com files | |
1530 | * .bat files | |
1531 | * directories | |
1532 | * All files and directories are readable. | |
1533 | * Directories and special files, e.g. "CON", cannot be | |
1534 | * write-protected. | |
1535 | * [Comment by Tom Dinger -- a directory can have the write-protect | |
1536 | * bit set in the file system, but DOS permits changes to | |
1537 | * the directory anyway. In addition, all bets are off | |
1538 | * here for networked software, such as Novell and | |
1539 | * Sun's PC-NFS.] | |
1540 | */ | |
1541 | ||
bee1dbe2 LW |
1542 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1543 | * too so it will actually look into the files for magic numbers | |
1544 | */ | |
7f4774ae | 1545 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 | 1546 | |
55497cff | 1547 | #else /* ! DOSISH */ |
3280af22 | 1548 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
7f4774ae | 1549 | if (mode == S_IXUSR) { |
c623bd54 | 1550 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c LW |
1551 | return TRUE; |
1552 | } | |
1553 | else | |
1554 | return TRUE; /* root reads and writes anything */ | |
1555 | return FALSE; | |
1556 | } | |
3280af22 | 1557 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
7f4774ae | 1558 | if (statbufp->st_mode & mode) |
a687059c LW |
1559 | return TRUE; /* ok as "user" */ |
1560 | } | |
d8eceb89 | 1561 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae | 1562 | if (statbufp->st_mode & mode >> 3) |
a687059c LW |
1563 | return TRUE; /* ok as "group" */ |
1564 | } | |
7f4774ae | 1565 | else if (statbufp->st_mode & mode >> 6) |
a687059c LW |
1566 | return TRUE; /* ok as "other" */ |
1567 | return FALSE; | |
55497cff | 1568 | #endif /* ! DOSISH */ |
a687059c | 1569 | } |
a0d0e21e | 1570 | #endif /* ! VMS */ |
a687059c | 1571 | |
d8eceb89 JH |
1572 | bool |
1573 | Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective) | |
a687059c | 1574 | { |
cd39f2b6 JH |
1575 | #ifdef MACOS_TRADITIONAL |
1576 | /* This is simply not correct for AppleShare, but fix it yerself. */ | |
1577 | return TRUE; | |
1578 | #else | |
3280af22 | 1579 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c | 1580 | return TRUE; |
fe14fcc3 | 1581 | #ifdef HAS_GETGROUPS |
a687059c LW |
1582 | #ifndef NGROUPS |
1583 | #define NGROUPS 32 | |
1584 | #endif | |
1585 | { | |
a0d0e21e | 1586 | Groups_t gary[NGROUPS]; |
79072805 | 1587 | I32 anum; |
a687059c LW |
1588 | |
1589 | anum = getgroups(NGROUPS,gary); | |
1590 | while (--anum >= 0) | |
1591 | if (gary[anum] == testgid) | |
1592 | return TRUE; | |
1593 | } | |
1594 | #endif | |
1595 | return FALSE; | |
cd39f2b6 | 1596 | #endif |
a687059c | 1597 | } |
c2ab57d4 | 1598 | |
fe14fcc3 | 1599 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 1600 | |
79072805 | 1601 | I32 |
864dbfa3 | 1602 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1603 | { |
11343788 | 1604 | dTHR; |
c2ab57d4 | 1605 | key_t key; |
79072805 | 1606 | I32 n, flags; |
c2ab57d4 | 1607 | |
463ee0b2 LW |
1608 | key = (key_t)SvNVx(*++mark); |
1609 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); | |
1610 | flags = SvIVx(*++mark); | |
748a9306 | 1611 | SETERRNO(0,0); |
c2ab57d4 LW |
1612 | switch (optype) |
1613 | { | |
fe14fcc3 | 1614 | #ifdef HAS_MSG |
79072805 | 1615 | case OP_MSGGET: |
c2ab57d4 | 1616 | return msgget(key, flags); |
e5d73d77 | 1617 | #endif |
fe14fcc3 | 1618 | #ifdef HAS_SEM |
79072805 | 1619 | case OP_SEMGET: |
c2ab57d4 | 1620 | return semget(key, n, flags); |
e5d73d77 | 1621 | #endif |
fe14fcc3 | 1622 | #ifdef HAS_SHM |
79072805 | 1623 | case OP_SHMGET: |
c2ab57d4 | 1624 | return shmget(key, n, flags); |
e5d73d77 | 1625 | #endif |
fe14fcc3 | 1626 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1627 | default: |
cea2e8a9 | 1628 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 1629 | #endif |
c2ab57d4 LW |
1630 | } |
1631 | return -1; /* should never happen */ | |
1632 | } | |
1633 | ||
79072805 | 1634 | I32 |
864dbfa3 | 1635 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1636 | { |
11343788 | 1637 | dTHR; |
79072805 | 1638 | SV *astr; |
c2ab57d4 | 1639 | char *a; |
a0d0e21e LW |
1640 | I32 id, n, cmd, infosize, getinfo; |
1641 | I32 ret = -1; | |
c2ab57d4 | 1642 | |
463ee0b2 LW |
1643 | id = SvIVx(*++mark); |
1644 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; | |
1645 | cmd = SvIVx(*++mark); | |
79072805 | 1646 | astr = *++mark; |
c2ab57d4 LW |
1647 | infosize = 0; |
1648 | getinfo = (cmd == IPC_STAT); | |
1649 | ||
1650 | switch (optype) | |
1651 | { | |
fe14fcc3 | 1652 | #ifdef HAS_MSG |
79072805 | 1653 | case OP_MSGCTL: |
c2ab57d4 LW |
1654 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1655 | infosize = sizeof(struct msqid_ds); | |
1656 | break; | |
e5d73d77 | 1657 | #endif |
fe14fcc3 | 1658 | #ifdef HAS_SHM |
79072805 | 1659 | case OP_SHMCTL: |
c2ab57d4 LW |
1660 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1661 | infosize = sizeof(struct shmid_ds); | |
1662 | break; | |
e5d73d77 | 1663 | #endif |
fe14fcc3 | 1664 | #ifdef HAS_SEM |
79072805 | 1665 | case OP_SEMCTL: |
39398f3f | 1666 | #ifdef Semctl |
c2ab57d4 LW |
1667 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1668 | infosize = sizeof(struct semid_ds); | |
1669 | else if (cmd == GETALL || cmd == SETALL) | |
1670 | { | |
8e591e46 | 1671 | struct semid_ds semds; |
bd89102f AD |
1672 | union semun semun; |
1673 | ||
84902520 | 1674 | semun.buf = &semds; |
c2ab57d4 | 1675 | getinfo = (cmd == GETALL); |
9b89d93d GB |
1676 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
1677 | return -1; | |
6e21c824 LW |
1678 | infosize = semds.sem_nsems * sizeof(short); |
1679 | /* "short" is technically wrong but much more portable | |
1680 | than guessing about u_?short(_t)? */ | |
c2ab57d4 | 1681 | } |
39398f3f | 1682 | #else |
cea2e8a9 | 1683 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 1684 | #endif |
c2ab57d4 | 1685 | break; |
e5d73d77 | 1686 | #endif |
fe14fcc3 | 1687 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1688 | default: |
cea2e8a9 | 1689 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 1690 | #endif |
c2ab57d4 LW |
1691 | } |
1692 | ||
1693 | if (infosize) | |
1694 | { | |
a0d0e21e | 1695 | STRLEN len; |
c2ab57d4 LW |
1696 | if (getinfo) |
1697 | { | |
a0d0e21e LW |
1698 | SvPV_force(astr, len); |
1699 | a = SvGROW(astr, infosize+1); | |
c2ab57d4 LW |
1700 | } |
1701 | else | |
1702 | { | |
463ee0b2 LW |
1703 | a = SvPV(astr, len); |
1704 | if (len != infosize) | |
cea2e8a9 | 1705 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 JH |
1706 | PL_op_desc[optype], |
1707 | (unsigned long)len, | |
1708 | (long)infosize); | |
c2ab57d4 LW |
1709 | } |
1710 | } | |
1711 | else | |
1712 | { | |
c030ccd9 | 1713 | IV i = SvIV(astr); |
56431972 | 1714 | a = INT2PTR(char *,i); /* ouch */ |
c2ab57d4 | 1715 | } |
748a9306 | 1716 | SETERRNO(0,0); |
c2ab57d4 LW |
1717 | switch (optype) |
1718 | { | |
fe14fcc3 | 1719 | #ifdef HAS_MSG |
79072805 | 1720 | case OP_MSGCTL: |
bee1dbe2 | 1721 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 | 1722 | break; |
e5d73d77 | 1723 | #endif |
fe14fcc3 | 1724 | #ifdef HAS_SEM |
bd89102f | 1725 | case OP_SEMCTL: { |
39398f3f | 1726 | #ifdef Semctl |
bd89102f AD |
1727 | union semun unsemds; |
1728 | ||
1729 | unsemds.buf = (struct semid_ds *)a; | |
1730 | ret = Semctl(id, n, cmd, unsemds); | |
39398f3f | 1731 | #else |
cea2e8a9 | 1732 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 1733 | #endif |
bd89102f | 1734 | } |
c2ab57d4 | 1735 | break; |
e5d73d77 | 1736 | #endif |
fe14fcc3 | 1737 | #ifdef HAS_SHM |
79072805 | 1738 | case OP_SHMCTL: |
bee1dbe2 | 1739 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 | 1740 | break; |
e5d73d77 | 1741 | #endif |
c2ab57d4 LW |
1742 | } |
1743 | if (getinfo && ret >= 0) { | |
79072805 LW |
1744 | SvCUR_set(astr, infosize); |
1745 | *SvEND(astr) = '\0'; | |
a0d0e21e | 1746 | SvSETMAGIC(astr); |
c2ab57d4 LW |
1747 | } |
1748 | return ret; | |
1749 | } | |
1750 | ||
79072805 | 1751 | I32 |
864dbfa3 | 1752 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1753 | { |
fe14fcc3 | 1754 | #ifdef HAS_MSG |
11343788 | 1755 | dTHR; |
79072805 | 1756 | SV *mstr; |
c2ab57d4 | 1757 | char *mbuf; |
79072805 | 1758 | I32 id, msize, flags; |
463ee0b2 | 1759 | STRLEN len; |
c2ab57d4 | 1760 | |
463ee0b2 | 1761 | id = SvIVx(*++mark); |
79072805 | 1762 | mstr = *++mark; |
463ee0b2 LW |
1763 | flags = SvIVx(*++mark); |
1764 | mbuf = SvPV(mstr, len); | |
1765 | if ((msize = len - sizeof(long)) < 0) | |
cea2e8a9 | 1766 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 | 1767 | SETERRNO(0,0); |
bee1dbe2 | 1768 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 | 1769 | #else |
cea2e8a9 | 1770 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 | 1771 | #endif |
c2ab57d4 LW |
1772 | } |
1773 | ||
79072805 | 1774 | I32 |
864dbfa3 | 1775 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1776 | { |
fe14fcc3 | 1777 | #ifdef HAS_MSG |
11343788 | 1778 | dTHR; |
79072805 | 1779 | SV *mstr; |
c2ab57d4 LW |
1780 | char *mbuf; |
1781 | long mtype; | |
79072805 | 1782 | I32 id, msize, flags, ret; |
463ee0b2 | 1783 | STRLEN len; |
79072805 | 1784 | |
463ee0b2 | 1785 | id = SvIVx(*++mark); |
79072805 | 1786 | mstr = *++mark; |
463ee0b2 LW |
1787 | msize = SvIVx(*++mark); |
1788 | mtype = (long)SvIVx(*++mark); | |
1789 | flags = SvIVx(*++mark); | |
a0d0e21e LW |
1790 | SvPV_force(mstr, len); |
1791 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); | |
1792 | ||
748a9306 | 1793 | SETERRNO(0,0); |
bee1dbe2 | 1794 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 | 1795 | if (ret >= 0) { |
79072805 LW |
1796 | SvCUR_set(mstr, sizeof(long)+ret); |
1797 | *SvEND(mstr) = '\0'; | |
c2ab57d4 LW |
1798 | } |
1799 | return ret; | |
e5d73d77 | 1800 | #else |
cea2e8a9 | 1801 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 | 1802 | #endif |
c2ab57d4 LW |
1803 | } |
1804 | ||
79072805 | 1805 | I32 |
864dbfa3 | 1806 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1807 | { |
fe14fcc3 | 1808 | #ifdef HAS_SEM |
11343788 | 1809 | dTHR; |
79072805 | 1810 | SV *opstr; |
c2ab57d4 | 1811 | char *opbuf; |
463ee0b2 LW |
1812 | I32 id; |
1813 | STRLEN opsize; | |
c2ab57d4 | 1814 | |
463ee0b2 | 1815 | id = SvIVx(*++mark); |
79072805 | 1816 | opstr = *++mark; |
463ee0b2 | 1817 | opbuf = SvPV(opstr, opsize); |
c2ab57d4 LW |
1818 | if (opsize < sizeof(struct sembuf) |
1819 | || (opsize % sizeof(struct sembuf)) != 0) { | |
748a9306 | 1820 | SETERRNO(EINVAL,LIB$_INVARG); |
c2ab57d4 LW |
1821 | return -1; |
1822 | } | |
748a9306 | 1823 | SETERRNO(0,0); |
6e21c824 | 1824 | return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf)); |
e5d73d77 | 1825 | #else |
cea2e8a9 | 1826 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 | 1827 | #endif |
c2ab57d4 LW |
1828 | } |
1829 | ||
79072805 | 1830 | I32 |
864dbfa3 | 1831 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1832 | { |
fe14fcc3 | 1833 | #ifdef HAS_SHM |
11343788 | 1834 | dTHR; |
79072805 | 1835 | SV *mstr; |
c2ab57d4 | 1836 | char *mbuf, *shm; |
79072805 | 1837 | I32 id, mpos, msize; |
463ee0b2 | 1838 | STRLEN len; |
c2ab57d4 | 1839 | struct shmid_ds shmds; |
c2ab57d4 | 1840 | |
463ee0b2 | 1841 | id = SvIVx(*++mark); |
79072805 | 1842 | mstr = *++mark; |
463ee0b2 LW |
1843 | mpos = SvIVx(*++mark); |
1844 | msize = SvIVx(*++mark); | |
748a9306 | 1845 | SETERRNO(0,0); |
c2ab57d4 LW |
1846 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
1847 | return -1; | |
1848 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { | |
748a9306 | 1849 | SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 LW |
1850 | return -1; |
1851 | } | |
8ac85365 | 1852 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 LW |
1853 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
1854 | return -1; | |
79072805 | 1855 | if (optype == OP_SHMREAD) { |
9f538c04 GS |
1856 | /* suppress warning when reading into undef var (tchrist 3/Mar/00) */ |
1857 | if (! SvOK(mstr)) | |
1858 | sv_setpvn(mstr, "", 0); | |
a0d0e21e LW |
1859 | SvPV_force(mstr, len); |
1860 | mbuf = SvGROW(mstr, msize+1); | |
1861 | ||
bee1dbe2 | 1862 | Copy(shm + mpos, mbuf, msize, char); |
79072805 LW |
1863 | SvCUR_set(mstr, msize); |
1864 | *SvEND(mstr) = '\0'; | |
a0d0e21e | 1865 | SvSETMAGIC(mstr); |
c2ab57d4 LW |
1866 | } |
1867 | else { | |
79072805 | 1868 | I32 n; |
c2ab57d4 | 1869 | |
a0d0e21e | 1870 | mbuf = SvPV(mstr, len); |
463ee0b2 | 1871 | if ((n = len) > msize) |
c2ab57d4 | 1872 | n = msize; |
bee1dbe2 | 1873 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 | 1874 | if (n < msize) |
bee1dbe2 | 1875 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 LW |
1876 | } |
1877 | return shmdt(shm); | |
e5d73d77 | 1878 | #else |
cea2e8a9 | 1879 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 | 1880 | #endif |
c2ab57d4 LW |
1881 | } |
1882 | ||
fe14fcc3 | 1883 | #endif /* SYSV IPC */ |
4e35701f | 1884 |