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