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