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