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