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