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