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