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