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