Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* doio.c |
a687059c | 2 | * |
4eb8286e | 3 | * Copyright (c) 1991-1999, Larry Wall |
a687059c | 4 | * |
6e21c824 LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
a687059c | 7 | * |
a0d0e21e LW |
8 | */ |
9 | ||
10 | /* | |
11 | * "Far below them they saw the white waters pour into a foaming bowl, and | |
12 | * then swirl darkly about a deep oval basin in the rocks, until they found | |
13 | * their way out again through a narrow gate, and flowed away, fuming and | |
14 | * chattering, into calmer and more level reaches." | |
a687059c LW |
15 | */ |
16 | ||
17 | #include "EXTERN.h" | |
864dbfa3 | 18 | #define PERL_IN_DOIO_C |
a687059c LW |
19 | #include "perl.h" |
20 | ||
fe14fcc3 | 21 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
aec308ec | 22 | #ifndef HAS_SEM |
c2ab57d4 | 23 | #include <sys/ipc.h> |
aec308ec | 24 | #endif |
fe14fcc3 | 25 | #ifdef HAS_MSG |
c2ab57d4 | 26 | #include <sys/msg.h> |
e5d73d77 | 27 | #endif |
fe14fcc3 | 28 | #ifdef HAS_SHM |
c2ab57d4 | 29 | #include <sys/shm.h> |
a0d0e21e | 30 | # ifndef HAS_SHMAT_PROTOTYPE |
20ce7b12 | 31 | extern Shmat_t shmat (int, char *, int); |
a0d0e21e | 32 | # endif |
c2ab57d4 | 33 | #endif |
e5d73d77 | 34 | #endif |
c2ab57d4 | 35 | |
663a0e37 | 36 | #ifdef I_UTIME |
3730b96e | 37 | # if defined(_MSC_VER) || defined(__MINGW32__) |
3fe9a6f1 PP |
38 | # include <sys/utime.h> |
39 | # else | |
40 | # include <utime.h> | |
41 | # endif | |
663a0e37 | 42 | #endif |
85aff577 | 43 | |
ff8e2863 LW |
44 | #ifdef I_FCNTL |
45 | #include <fcntl.h> | |
46 | #endif | |
fe14fcc3 LW |
47 | #ifdef I_SYS_FILE |
48 | #include <sys/file.h> | |
49 | #endif | |
85aff577 CS |
50 | #ifdef O_EXCL |
51 | # define OPEN_EXCL O_EXCL | |
52 | #else | |
53 | # define OPEN_EXCL 0 | |
54 | #endif | |
a687059c | 55 | |
76121258 PP |
56 | #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX) |
57 | #include <signal.h> | |
58 | #endif | |
59 | ||
60 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ | |
61 | #ifdef I_UNISTD | |
62 | # include <unistd.h> | |
63 | #endif | |
64 | ||
232e078e AD |
65 | #if defined(HAS_SOCKET) && !defined(VMS) /* VMS handles sockets via vmsish.h */ |
66 | # include <sys/socket.h> | |
29209bc5 | 67 | # if defined(USE_SOCKS) && defined(I_SOCKS) |
86959918 JH |
68 | # include <socks.h> |
69 | # endif | |
70 | # ifdef I_NETBSD | |
71 | # include <netdb.h> | |
72 | # endif | |
232e078e AD |
73 | # ifndef ENOTSOCK |
74 | # ifdef I_NET_ERRNO | |
75 | # include <net/errno.h> | |
76 | # endif | |
77 | # endif | |
78 | #endif | |
79 | ||
d574b85e CS |
80 | /* Put this after #includes because <unistd.h> defines _XOPEN_*. */ |
81 | #ifndef Sock_size_t | |
137443ea | 82 | # if _XOPEN_VERSION >= 5 || defined(_XOPEN_SOURCE_EXTENDED) || defined(__GLIBC__) |
d574b85e CS |
83 | # define Sock_size_t Size_t |
84 | # else | |
85 | # define Sock_size_t int | |
86 | # endif | |
87 | #endif | |
88 | ||
a687059c | 89 | bool |
6170680b IZ |
90 | Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw, |
91 | int rawmode, int rawperm, PerlIO *supplied_fp) | |
92 | { | |
93 | return do_open9(gv, name, len, as_raw, rawmode, rawperm, | |
94 | supplied_fp, Nullsv, 0); | |
95 | } | |
96 | ||
97 | bool | |
98 | Perl_do_open9(pTHX_ GV *gv, register char *name, I32 len, int as_raw, | |
99 | int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs, | |
100 | I32 num_svs) | |
a687059c | 101 | { |
a0d0e21e | 102 | register IO *io = GvIOn(gv); |
760ac839 LW |
103 | PerlIO *saveifp = Nullfp; |
104 | PerlIO *saveofp = Nullfp; | |
6e21c824 | 105 | char savetype = ' '; |
c07a80fd | 106 | int writing = 0; |
760ac839 | 107 | PerlIO *fp; |
c07a80fd PP |
108 | int fd; |
109 | int result; | |
3500f679 | 110 | bool was_fdopen = FALSE; |
a687059c | 111 | |
3280af22 | 112 | PL_forkprocess = 1; /* assume true if no fork */ |
c07a80fd | 113 | |
a0d0e21e | 114 | if (IoIFP(io)) { |
760ac839 | 115 | fd = PerlIO_fileno(IoIFP(io)); |
8990e307 | 116 | if (IoTYPE(io) == '-') |
c2ab57d4 | 117 | result = 0; |
3280af22 | 118 | else if (fd <= PL_maxsysfd) { |
8990e307 LW |
119 | saveifp = IoIFP(io); |
120 | saveofp = IoOFP(io); | |
121 | savetype = IoTYPE(io); | |
6e21c824 LW |
122 | result = 0; |
123 | } | |
8990e307 | 124 | else if (IoTYPE(io) == '|') |
3028581b | 125 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 LW |
126 | else if (IoIFP(io) != IoOFP(io)) { |
127 | if (IoOFP(io)) { | |
760ac839 | 128 | result = PerlIO_close(IoOFP(io)); |
6170680b | 129 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 LW |
130 | } |
131 | else | |
760ac839 | 132 | result = PerlIO_close(IoIFP(io)); |
a687059c | 133 | } |
a687059c | 134 | else |
760ac839 | 135 | result = PerlIO_close(IoIFP(io)); |
3280af22 | 136 | if (result == EOF && fd > PL_maxsysfd) |
6170680b IZ |
137 | PerlIO_printf(PerlIO_stderr(), |
138 | "Warning: unable to close filehandle %s properly.\n", | |
139 | GvENAME(gv)); | |
8990e307 | 140 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c | 141 | } |
c07a80fd PP |
142 | |
143 | if (as_raw) { | |
5ff3f7a4 GS |
144 | #if defined(O_LARGEFILE) |
145 | rawmode |= O_LARGEFILE; | |
146 | #endif | |
147 | ||
9d116dd7 JH |
148 | #ifndef O_ACCMODE |
149 | #define O_ACCMODE 3 /* Assume traditional implementation */ | |
150 | #endif | |
5ff3f7a4 | 151 | |
9d116dd7 JH |
152 | switch (result = rawmode & O_ACCMODE) { |
153 | case O_RDONLY: | |
154 | IoTYPE(io) = '<'; | |
155 | break; | |
156 | case O_WRONLY: | |
157 | IoTYPE(io) = '>'; | |
158 | break; | |
159 | case O_RDWR: | |
160 | default: | |
161 | IoTYPE(io) = '+'; | |
162 | break; | |
163 | } | |
164 | ||
c07a80fd | 165 | writing = (result > 0); |
3028581b | 166 | fd = PerlLIO_open3(name, rawmode, rawperm); |
9d116dd7 | 167 | |
c07a80fd PP |
168 | if (fd == -1) |
169 | fp = NULL; | |
170 | else { | |
360e5741 | 171 | char *fpmode; |
9d116dd7 | 172 | if (result == O_RDONLY) |
360e5741 CS |
173 | fpmode = "r"; |
174 | #ifdef O_APPEND | |
175 | else if (rawmode & O_APPEND) | |
9d116dd7 | 176 | fpmode = (result == O_WRONLY) ? "a" : "a+"; |
360e5741 CS |
177 | #endif |
178 | else | |
9d116dd7 | 179 | fpmode = (result == O_WRONLY) ? "w" : "r+"; |
360e5741 | 180 | fp = PerlIO_fdopen(fd, fpmode); |
c07a80fd | 181 | if (!fp) |
3028581b | 182 | PerlLIO_close(fd); |
c07a80fd | 183 | } |
a687059c | 184 | } |
c07a80fd PP |
185 | else { |
186 | char *myname; | |
6170680b IZ |
187 | char *type = name; |
188 | char *otype = name; | |
189 | STRLEN tlen; | |
190 | STRLEN otlen = len; | |
c07a80fd PP |
191 | char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ |
192 | int dodup; | |
193 | ||
6170680b IZ |
194 | if (num_svs) { |
195 | type = name; | |
196 | name = SvPV(svs, tlen) ; | |
197 | len = (I32)tlen; | |
198 | } | |
199 | ||
200 | tlen = otlen; | |
c07a80fd PP |
201 | myname = savepvn(name, len); |
202 | SAVEFREEPV(myname); | |
203 | name = myname; | |
6170680b IZ |
204 | if (!num_svs) |
205 | while (tlen && isSPACE(type[tlen-1])) | |
206 | type[--tlen] = '\0'; | |
c07a80fd PP |
207 | |
208 | mode[0] = mode[1] = mode[2] = '\0'; | |
6170680b IZ |
209 | IoTYPE(io) = *type; |
210 | if (*type == '+' && tlen > 1 && type[tlen-1] != '|') { /* scary */ | |
211 | mode[1] = *type++; | |
212 | --tlen; | |
c07a80fd | 213 | writing = 1; |
a687059c | 214 | } |
c07a80fd | 215 | |
6170680b IZ |
216 | if (*type == '|') { |
217 | if (num_svs && (tlen != 2 || type[1] != '-')) { | |
218 | unknown_desr: | |
219 | Perl_croak(aTHX_ "Unknown open() mode '%.*s'", otlen, otype); | |
220 | } | |
c07a80fd | 221 | /*SUPPRESS 530*/ |
6170680b IZ |
222 | for (type++; isSPACE(*type); type++) ; |
223 | if (!num_svs) | |
224 | name = type; | |
06eaf0bc GS |
225 | if (*name == '\0') { /* command is missing 19990114 */ |
226 | dTHR; | |
227 | if (ckWARN(WARN_PIPE)) | |
cea2e8a9 | 228 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc GS |
229 | errno = EPIPE; |
230 | goto say_false; | |
231 | } | |
6170680b | 232 | if (strNE(name,"-") || num_svs) |
c07a80fd PP |
233 | TAINT_ENV(); |
234 | TAINT_PROPER("piped open"); | |
7b8d334a | 235 | if (name[strlen(name)-1] == '|') { |
d008e5eb | 236 | dTHR; |
7b8d334a | 237 | name[strlen(name)-1] = '\0' ; |
599cee73 | 238 | if (ckWARN(WARN_PIPE)) |
cea2e8a9 | 239 | Perl_warner(aTHX_ WARN_PIPE, "Can't do bidirectional pipe"); |
7b8d334a | 240 | } |
3028581b | 241 | fp = PerlProc_popen(name,"w"); |
c07a80fd PP |
242 | writing = 1; |
243 | } | |
6170680b | 244 | else if (*type == '>') { |
c07a80fd | 245 | TAINT_PROPER("open"); |
6170680b IZ |
246 | type++; |
247 | if (*type == '>') { | |
c07a80fd | 248 | mode[0] = IoTYPE(io) = 'a'; |
6170680b IZ |
249 | type++; |
250 | tlen--; | |
a0d0e21e | 251 | } |
c07a80fd PP |
252 | else |
253 | mode[0] = 'w'; | |
254 | writing = 1; | |
255 | ||
6170680b IZ |
256 | if (num_svs && tlen != 1) |
257 | goto unknown_desr; | |
258 | if (*type == '&') { | |
259 | name = type; | |
c07a80fd PP |
260 | duplicity: |
261 | dodup = 1; | |
262 | name++; | |
263 | if (*name == '=') { | |
264 | dodup = 0; | |
a0d0e21e | 265 | name++; |
c07a80fd PP |
266 | } |
267 | if (!*name && supplied_fp) | |
268 | fp = supplied_fp; | |
a0d0e21e | 269 | else { |
c07a80fd PP |
270 | /*SUPPRESS 530*/ |
271 | for (; isSPACE(*name); name++) ; | |
272 | if (isDIGIT(*name)) | |
273 | fd = atoi(name); | |
274 | else { | |
275 | IO* thatio; | |
276 | gv = gv_fetchpv(name,FALSE,SVt_PVIO); | |
277 | thatio = GvIO(gv); | |
278 | if (!thatio) { | |
6e21c824 | 279 | #ifdef EINVAL |
c07a80fd | 280 | SETERRNO(EINVAL,SS$_IVCHAN); |
6e21c824 | 281 | #endif |
c07a80fd PP |
282 | goto say_false; |
283 | } | |
284 | if (IoIFP(thatio)) { | |
760ac839 | 285 | fd = PerlIO_fileno(IoIFP(thatio)); |
c07a80fd PP |
286 | if (IoTYPE(thatio) == 's') |
287 | IoTYPE(io) = 's'; | |
288 | } | |
289 | else | |
290 | fd = -1; | |
a0d0e21e | 291 | } |
fec02dd3 | 292 | if (dodup) |
3028581b | 293 | fd = PerlLIO_dup(fd); |
3500f679 RS |
294 | else |
295 | was_fdopen = TRUE; | |
760ac839 | 296 | if (!(fp = PerlIO_fdopen(fd,mode))) { |
c07a80fd | 297 | if (dodup) |
3028581b | 298 | PerlLIO_close(fd); |
517844ec | 299 | } |
c07a80fd | 300 | } |
bf38876a | 301 | } |
c07a80fd PP |
302 | else { |
303 | /*SUPPRESS 530*/ | |
6170680b IZ |
304 | for (; isSPACE(*type); type++) ; |
305 | if (strEQ(type,"-")) { | |
760ac839 | 306 | fp = PerlIO_stdout(); |
c07a80fd PP |
307 | IoTYPE(io) = '-'; |
308 | } | |
309 | else { | |
6170680b | 310 | fp = PerlIO_open((num_svs ? name : type), mode); |
c07a80fd | 311 | } |
bf38876a LW |
312 | } |
313 | } | |
6170680b IZ |
314 | else if (*type == '<') { |
315 | if (num_svs && tlen != 1) | |
316 | goto unknown_desr; | |
c07a80fd | 317 | /*SUPPRESS 530*/ |
6170680b | 318 | for (type++; isSPACE(*type); type++) ; |
bf38876a | 319 | mode[0] = 'r'; |
6170680b IZ |
320 | if (*type == '&') { |
321 | name = type; | |
bf38876a | 322 | goto duplicity; |
6170680b IZ |
323 | } |
324 | if (strEQ(type,"-")) { | |
760ac839 | 325 | fp = PerlIO_stdin(); |
8990e307 | 326 | IoTYPE(io) = '-'; |
a687059c | 327 | } |
bf38876a | 328 | else |
6170680b | 329 | fp = PerlIO_open((num_svs ? name : type), mode); |
a687059c | 330 | } |
6170680b IZ |
331 | else if (tlen > 1 && type[tlen-1] == '|') { |
332 | if (num_svs) { | |
333 | if (tlen != 2 || type[0] != '-') | |
334 | goto unknown_desr; | |
335 | } | |
336 | else { | |
337 | type[--tlen] = '\0'; | |
338 | while (tlen && isSPACE(type[tlen-1])) | |
339 | type[--tlen] = '\0'; | |
340 | /*SUPPRESS 530*/ | |
341 | for (; isSPACE(*type); type++) ; | |
342 | name = type; | |
343 | } | |
06eaf0bc GS |
344 | if (*name == '\0') { /* command is missing 19990114 */ |
345 | dTHR; | |
346 | if (ckWARN(WARN_PIPE)) | |
cea2e8a9 | 347 | Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open"); |
06eaf0bc GS |
348 | errno = EPIPE; |
349 | goto say_false; | |
350 | } | |
6170680b | 351 | if (strNE(name,"-") || num_svs) |
79072805 LW |
352 | TAINT_ENV(); |
353 | TAINT_PROPER("piped open"); | |
3028581b | 354 | fp = PerlProc_popen(name,"r"); |
8990e307 | 355 | IoTYPE(io) = '|'; |
a687059c LW |
356 | } |
357 | else { | |
6170680b IZ |
358 | if (num_svs) |
359 | goto unknown_desr; | |
360 | name = type; | |
8990e307 | 361 | IoTYPE(io) = '<'; |
99b89507 LW |
362 | /*SUPPRESS 530*/ |
363 | for (; isSPACE(*name); name++) ; | |
a687059c | 364 | if (strEQ(name,"-")) { |
760ac839 | 365 | fp = PerlIO_stdin(); |
8990e307 | 366 | IoTYPE(io) = '-'; |
a687059c LW |
367 | } |
368 | else | |
760ac839 | 369 | fp = PerlIO_open(name,"r"); |
a687059c LW |
370 | } |
371 | } | |
bee1dbe2 | 372 | if (!fp) { |
d008e5eb | 373 | dTHR; |
599cee73 | 374 | if (ckWARN(WARN_NEWLINE) && IoTYPE(io) == '<' && strchr(name, '\n')) |
cea2e8a9 | 375 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open"); |
6e21c824 | 376 | goto say_false; |
bee1dbe2 | 377 | } |
8990e307 LW |
378 | if (IoTYPE(io) && |
379 | IoTYPE(io) != '|' && IoTYPE(io) != '-') { | |
96827780 | 380 | dTHR; |
3280af22 | 381 | if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) { |
760ac839 | 382 | (void)PerlIO_close(fp); |
6e21c824 | 383 | goto say_false; |
a687059c | 384 | } |
3280af22 | 385 | if (S_ISSOCK(PL_statbuf.st_mode)) |
8990e307 | 386 | IoTYPE(io) = 's'; /* in case a socket was passed in to us */ |
99b89507 LW |
387 | #ifdef HAS_SOCKET |
388 | else if ( | |
c623bd54 | 389 | #ifdef S_IFMT |
3280af22 | 390 | !(PL_statbuf.st_mode & S_IFMT) |
99b89507 | 391 | #else |
b28d0864 | 392 | !PL_statbuf.st_mode |
99b89507 LW |
393 | #endif |
394 | ) { | |
96827780 MB |
395 | char tmpbuf[256]; |
396 | Sock_size_t buflen = sizeof tmpbuf; | |
3028581b | 397 | if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf, |
d574b85e CS |
398 | &buflen) >= 0 |
399 | || errno != ENOTSOCK) | |
8990e307 | 400 | IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */ |
99b89507 LW |
401 | /* but some return 0 for streams too, sigh */ |
402 | } | |
bf38876a | 403 | #endif |
a687059c | 404 | } |
6e21c824 | 405 | if (saveifp) { /* must use old fp? */ |
760ac839 | 406 | fd = PerlIO_fileno(saveifp); |
6e21c824 | 407 | if (saveofp) { |
760ac839 | 408 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 | 409 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 | 410 | PerlIO_close(saveofp); |
99b89507 LW |
411 | if (fd > 2) |
412 | Safefree(saveofp); | |
6e21c824 LW |
413 | } |
414 | } | |
760ac839 | 415 | if (fd != PerlIO_fileno(fp)) { |
bee1dbe2 | 416 | int pid; |
79072805 | 417 | SV *sv; |
bee1dbe2 | 418 | |
3028581b | 419 | PerlLIO_dup2(PerlIO_fileno(fp), fd); |
3280af22 | 420 | sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE); |
a0d0e21e | 421 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 LW |
422 | pid = SvIVX(sv); |
423 | SvIVX(sv) = 0; | |
3280af22 | 424 | sv = *av_fetch(PL_fdpid,fd,TRUE); |
a0d0e21e | 425 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 | 426 | SvIVX(sv) = pid; |
3500f679 RS |
427 | if (!was_fdopen) |
428 | PerlIO_close(fp); | |
bee1dbe2 | 429 | |
6e21c824 LW |
430 | } |
431 | fp = saveifp; | |
760ac839 | 432 | PerlIO_clearerr(fp); |
6e21c824 | 433 | } |
a0d0e21e | 434 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
a8710ca1 GS |
435 | { |
436 | int save_errno = errno; | |
437 | fd = PerlIO_fileno(fp); | |
438 | fcntl(fd,F_SETFD,fd > PL_maxsysfd); /* can change errno */ | |
439 | errno = save_errno; | |
440 | } | |
1462b684 | 441 | #endif |
8990e307 | 442 | IoIFP(io) = fp; |
bf38876a | 443 | if (writing) { |
96827780 | 444 | dTHR; |
8990e307 | 445 | if (IoTYPE(io) == 's' |
3280af22 | 446 | || (IoTYPE(io) == '>' && S_ISCHR(PL_statbuf.st_mode)) ) { |
760ac839 LW |
447 | if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) { |
448 | PerlIO_close(fp); | |
8990e307 | 449 | IoIFP(io) = Nullfp; |
6e21c824 | 450 | goto say_false; |
fe14fcc3 | 451 | } |
1462b684 LW |
452 | } |
453 | else | |
8990e307 | 454 | IoOFP(io) = fp; |
bf38876a | 455 | } |
a687059c | 456 | return TRUE; |
6e21c824 LW |
457 | |
458 | say_false: | |
8990e307 LW |
459 | IoIFP(io) = saveifp; |
460 | IoOFP(io) = saveofp; | |
461 | IoTYPE(io) = savetype; | |
6e21c824 | 462 | return FALSE; |
a687059c LW |
463 | } |
464 | ||
760ac839 | 465 | PerlIO * |
864dbfa3 | 466 | Perl_nextargv(pTHX_ register GV *gv) |
a687059c | 467 | { |
79072805 | 468 | register SV *sv; |
99b89507 | 469 | #ifndef FLEXFILENAMES |
c623bd54 LW |
470 | int filedev; |
471 | int fileino; | |
99b89507 | 472 | #endif |
761237fe JB |
473 | Uid_t fileuid; |
474 | Gid_t filegid; | |
fe14fcc3 | 475 | |
3280af22 NIS |
476 | if (!PL_argvoutgv) |
477 | PL_argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); | |
478 | if (PL_filemode & (S_ISUID|S_ISGID)) { | |
479 | PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv))); /* chmod must follow last write */ | |
fe14fcc3 | 480 | #ifdef HAS_FCHMOD |
3280af22 | 481 | (void)fchmod(PL_lastfd,PL_filemode); |
fe14fcc3 | 482 | #else |
b28d0864 | 483 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
fe14fcc3 LW |
484 | #endif |
485 | } | |
3280af22 | 486 | PL_filemode = 0; |
79072805 | 487 | while (av_len(GvAV(gv)) >= 0) { |
11343788 | 488 | dTHR; |
85aff577 | 489 | STRLEN oldlen; |
79072805 | 490 | sv = av_shift(GvAV(gv)); |
8990e307 | 491 | SAVEFREESV(sv); |
79072805 LW |
492 | sv_setsv(GvSV(gv),sv); |
493 | SvSETMAGIC(GvSV(gv)); | |
3280af22 | 494 | PL_oldname = SvPVx(GvSV(gv), oldlen); |
9d116dd7 | 495 | if (do_open(gv,PL_oldname,oldlen,PL_inplace!=0,O_RDONLY,0,Nullfp)) { |
3280af22 | 496 | if (PL_inplace) { |
79072805 | 497 | TAINT_PROPER("inplace open"); |
3280af22 | 498 | if (oldlen == 1 && *PL_oldname == '-') { |
4633a7c4 | 499 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e | 500 | return IoIFP(GvIOp(gv)); |
c623bd54 | 501 | } |
99b89507 | 502 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
503 | filedev = PL_statbuf.st_dev; |
504 | fileino = PL_statbuf.st_ino; | |
99b89507 | 505 | #endif |
3280af22 NIS |
506 | PL_filemode = PL_statbuf.st_mode; |
507 | fileuid = PL_statbuf.st_uid; | |
508 | filegid = PL_statbuf.st_gid; | |
509 | if (!S_ISREG(PL_filemode)) { | |
0453d815 PM |
510 | if (ckWARN_d(WARN_INPLACE)) |
511 | Perl_warner(aTHX_ WARN_INPLACE, | |
512 | "Can't do inplace edit: %s is not a regular file", | |
513 | PL_oldname ); | |
79072805 | 514 | do_close(gv,FALSE); |
c623bd54 LW |
515 | continue; |
516 | } | |
3280af22 NIS |
517 | if (*PL_inplace) { |
518 | char *star = strchr(PL_inplace, '*'); | |
2d259d92 | 519 | if (star) { |
3280af22 | 520 | char *begin = PL_inplace; |
2d259d92 CK |
521 | sv_setpvn(sv, "", 0); |
522 | do { | |
523 | sv_catpvn(sv, begin, star - begin); | |
3280af22 | 524 | sv_catpvn(sv, PL_oldname, oldlen); |
2d259d92 CK |
525 | begin = ++star; |
526 | } while ((star = strchr(begin, '*'))); | |
3d66d7bb GS |
527 | if (*begin) |
528 | sv_catpv(sv,begin); | |
2d259d92 CK |
529 | } |
530 | else { | |
3280af22 | 531 | sv_catpv(sv,PL_inplace); |
2d259d92 | 532 | } |
c623bd54 | 533 | #ifndef FLEXFILENAMES |
b28d0864 NIS |
534 | if (PerlLIO_stat(SvPVX(sv),&PL_statbuf) >= 0 |
535 | && PL_statbuf.st_dev == filedev | |
536 | && PL_statbuf.st_ino == fileino | |
39e571d4 ML |
537 | #ifdef DJGPP |
538 | || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0 | |
539 | #endif | |
f248d071 GS |
540 | ) |
541 | { | |
542 | if (ckWARN_d(WARN_INPLACE)) | |
543 | Perl_warner(aTHX_ WARN_INPLACE, | |
544 | "Can't do inplace edit: %s would not be unique", | |
545 | SvPVX(sv)); | |
79072805 | 546 | do_close(gv,FALSE); |
c623bd54 LW |
547 | continue; |
548 | } | |
549 | #endif | |
fe14fcc3 | 550 | #ifdef HAS_RENAME |
2c2d71f5 | 551 | #if !defined(DOSISH) && !defined(CYGWIN) |
3280af22 | 552 | if (PerlLIO_rename(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 PM |
553 | if (ckWARN_d(WARN_INPLACE)) |
554 | Perl_warner(aTHX_ WARN_INPLACE, | |
555 | "Can't rename %s to %s: %s, skipping file", | |
556 | PL_oldname, SvPVX(sv), Strerror(errno) ); | |
79072805 | 557 | do_close(gv,FALSE); |
c623bd54 LW |
558 | continue; |
559 | } | |
a687059c | 560 | #else |
79072805 | 561 | do_close(gv,FALSE); |
3028581b | 562 | (void)PerlLIO_unlink(SvPVX(sv)); |
b28d0864 | 563 | (void)PerlLIO_rename(PL_oldname,SvPVX(sv)); |
9d116dd7 | 564 | do_open(gv,SvPVX(sv),SvCUR(sv),PL_inplace!=0,O_RDONLY,0,Nullfp); |
55497cff | 565 | #endif /* DOSISH */ |
ff8e2863 | 566 | #else |
463ee0b2 | 567 | (void)UNLINK(SvPVX(sv)); |
b28d0864 | 568 | if (link(PL_oldname,SvPVX(sv)) < 0) { |
0453d815 PM |
569 | if (ckWARN_d(WARN_INPLACE)) |
570 | Perl_warner(aTHX_ WARN_INPLACE, | |
571 | "Can't rename %s to %s: %s, skipping file", | |
572 | PL_oldname, SvPVX(sv), Strerror(errno) ); | |
79072805 | 573 | do_close(gv,FALSE); |
c623bd54 LW |
574 | continue; |
575 | } | |
b28d0864 | 576 | (void)UNLINK(PL_oldname); |
a687059c LW |
577 | #endif |
578 | } | |
579 | else { | |
a8c18271 | 580 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 | 581 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
3280af22 | 582 | if (UNLINK(PL_oldname) < 0) { |
0453d815 PM |
583 | if (ckWARN_d(WARN_INPLACE)) |
584 | Perl_warner(aTHX_ WARN_INPLACE, | |
585 | "Can't remove %s: %s, skipping file", | |
586 | PL_oldname, Strerror(errno) ); | |
79072805 | 587 | do_close(gv,FALSE); |
fe14fcc3 LW |
588 | continue; |
589 | } | |
edc7bc49 | 590 | # endif |
ff8e2863 | 591 | #else |
cea2e8a9 | 592 | Perl_croak(aTHX_ "Can't do inplace edit without backup"); |
ff8e2863 | 593 | #endif |
a687059c LW |
594 | } |
595 | ||
3280af22 NIS |
596 | sv_setpvn(sv,">",!PL_inplace); |
597 | sv_catpvn(sv,PL_oldname,oldlen); | |
748a9306 | 598 | SETERRNO(0,0); /* in case sprintf set errno */ |
4119ab01 HM |
599 | #ifdef VMS |
600 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, | |
601 | O_WRONLY|O_CREAT|O_TRUNC,0,Nullfp)) { | |
602 | #else | |
3280af22 | 603 | if (!do_open(PL_argvoutgv,SvPVX(sv),SvCUR(sv),PL_inplace!=0, |
85aff577 | 604 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) { |
4119ab01 | 605 | #endif |
0453d815 PM |
606 | if (ckWARN_d(WARN_INPLACE)) |
607 | Perl_warner(aTHX_ WARN_INPLACE, "Can't do inplace edit on %s: %s", | |
608 | PL_oldname, Strerror(errno) ); | |
79072805 | 609 | do_close(gv,FALSE); |
fe14fcc3 LW |
610 | continue; |
611 | } | |
3280af22 NIS |
612 | setdefout(PL_argvoutgv); |
613 | PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv))); | |
614 | (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf); | |
fe14fcc3 | 615 | #ifdef HAS_FCHMOD |
3280af22 | 616 | (void)fchmod(PL_lastfd,PL_filemode); |
a687059c | 617 | #else |
3e3baf6d TB |
618 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
619 | /* Borland runtime creates a readonly file! */ | |
b28d0864 | 620 | (void)PerlLIO_chmod(PL_oldname,PL_filemode); |
3e3baf6d | 621 | # endif |
a687059c | 622 | #endif |
3280af22 | 623 | if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) { |
fe14fcc3 | 624 | #ifdef HAS_FCHOWN |
3280af22 | 625 | (void)fchown(PL_lastfd,fileuid,filegid); |
a687059c | 626 | #else |
fe14fcc3 | 627 | #ifdef HAS_CHOWN |
b28d0864 | 628 | (void)PerlLIO_chown(PL_oldname,fileuid,filegid); |
a687059c | 629 | #endif |
b1248f16 | 630 | #endif |
fe14fcc3 | 631 | } |
a687059c | 632 | } |
a0d0e21e | 633 | return IoIFP(GvIOp(gv)); |
a687059c | 634 | } |
4d61ec05 GS |
635 | else { |
636 | dTHR; | |
637 | if (ckWARN_d(WARN_INPLACE)) { | |
638 | if (!S_ISREG(PL_statbuf.st_mode)) | |
639 | Perl_warner(aTHX_ WARN_INPLACE, | |
640 | "Can't do inplace edit: %s is not a regular file", | |
641 | PL_oldname ); | |
642 | else | |
643 | Perl_warner(aTHX_ WARN_INPLACE, "Can't open %s: %s\n", | |
644 | PL_oldname, Strerror(errno)); | |
645 | } | |
646 | } | |
a687059c | 647 | } |
3280af22 NIS |
648 | if (PL_inplace) { |
649 | (void)do_close(PL_argvoutgv,FALSE); | |
4633a7c4 | 650 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c LW |
651 | } |
652 | return Nullfp; | |
653 | } | |
654 | ||
fe14fcc3 | 655 | #ifdef HAS_PIPE |
afd9f252 | 656 | void |
864dbfa3 | 657 | Perl_do_pipe(pTHX_ SV *sv, GV *rgv, GV *wgv) |
afd9f252 | 658 | { |
79072805 LW |
659 | register IO *rstio; |
660 | register IO *wstio; | |
afd9f252 LW |
661 | int fd[2]; |
662 | ||
79072805 | 663 | if (!rgv) |
afd9f252 | 664 | goto badexit; |
79072805 | 665 | if (!wgv) |
afd9f252 LW |
666 | goto badexit; |
667 | ||
a0d0e21e LW |
668 | rstio = GvIOn(rgv); |
669 | wstio = GvIOn(wgv); | |
afd9f252 | 670 | |
a0d0e21e | 671 | if (IoIFP(rstio)) |
79072805 | 672 | do_close(rgv,FALSE); |
a0d0e21e | 673 | if (IoIFP(wstio)) |
79072805 | 674 | do_close(wgv,FALSE); |
afd9f252 | 675 | |
3028581b | 676 | if (PerlProc_pipe(fd) < 0) |
afd9f252 | 677 | goto badexit; |
760ac839 LW |
678 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
679 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); | |
8990e307 LW |
680 | IoIFP(wstio) = IoOFP(wstio); |
681 | IoTYPE(rstio) = '<'; | |
682 | IoTYPE(wstio) = '>'; | |
683 | if (!IoIFP(rstio) || !IoOFP(wstio)) { | |
760ac839 | 684 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b | 685 | else PerlLIO_close(fd[0]); |
760ac839 | 686 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b | 687 | else PerlLIO_close(fd[1]); |
fe14fcc3 LW |
688 | goto badexit; |
689 | } | |
afd9f252 | 690 | |
3280af22 | 691 | sv_setsv(sv,&PL_sv_yes); |
afd9f252 LW |
692 | return; |
693 | ||
694 | badexit: | |
3280af22 | 695 | sv_setsv(sv,&PL_sv_undef); |
afd9f252 LW |
696 | return; |
697 | } | |
b1248f16 | 698 | #endif |
afd9f252 | 699 | |
517844ec | 700 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c | 701 | bool |
864dbfa3 | 702 | Perl_do_close(pTHX_ GV *gv, bool not_implicit) |
a687059c | 703 | { |
1193dd27 IZ |
704 | bool retval; |
705 | IO *io; | |
a687059c | 706 | |
79072805 | 707 | if (!gv) |
3280af22 | 708 | gv = PL_argvgv; |
a0d0e21e | 709 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
1d2dff63 GS |
710 | if (not_implicit) |
711 | SETERRNO(EBADF,SS$_IVCHAN); | |
c2ab57d4 | 712 | return FALSE; |
99b89507 | 713 | } |
79072805 LW |
714 | io = GvIO(gv); |
715 | if (!io) { /* never opened */ | |
1d2dff63 | 716 | if (not_implicit) { |
d008e5eb | 717 | dTHR; |
599cee73 | 718 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 | 719 | Perl_warner(aTHX_ WARN_UNOPENED, |
599cee73 | 720 | "Close on unopened file <%s>",GvENAME(gv)); |
1d2dff63 GS |
721 | SETERRNO(EBADF,SS$_IVCHAN); |
722 | } | |
a687059c LW |
723 | return FALSE; |
724 | } | |
f2b5be74 | 725 | retval = io_close(io, not_implicit); |
517844ec | 726 | if (not_implicit) { |
1193dd27 IZ |
727 | IoLINES(io) = 0; |
728 | IoPAGE(io) = 0; | |
729 | IoLINES_LEFT(io) = IoPAGE_LEN(io); | |
730 | } | |
731 | IoTYPE(io) = ' '; | |
732 | return retval; | |
733 | } | |
734 | ||
735 | bool | |
f2b5be74 | 736 | Perl_io_close(pTHX_ IO *io, bool not_implicit) |
1193dd27 IZ |
737 | { |
738 | bool retval = FALSE; | |
739 | int status; | |
740 | ||
8990e307 LW |
741 | if (IoIFP(io)) { |
742 | if (IoTYPE(io) == '|') { | |
3028581b | 743 | status = PerlProc_pclose(IoIFP(io)); |
f2b5be74 GS |
744 | if (not_implicit) { |
745 | STATUS_NATIVE_SET(status); | |
746 | retval = (STATUS_POSIX == 0); | |
747 | } | |
748 | else { | |
749 | retval = (status != -1); | |
750 | } | |
a687059c | 751 | } |
8990e307 | 752 | else if (IoTYPE(io) == '-') |
a687059c LW |
753 | retval = TRUE; |
754 | else { | |
8990e307 | 755 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 LW |
756 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
757 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ | |
c2ab57d4 LW |
758 | } |
759 | else | |
760ac839 | 760 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c | 761 | } |
8990e307 | 762 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 | 763 | } |
f2b5be74 | 764 | else if (not_implicit) { |
20408e3c GS |
765 | SETERRNO(EBADF,SS$_IVCHAN); |
766 | } | |
1193dd27 | 767 | |
a687059c LW |
768 | return retval; |
769 | } | |
770 | ||
771 | bool | |
864dbfa3 | 772 | Perl_do_eof(pTHX_ GV *gv) |
a687059c | 773 | { |
11343788 | 774 | dTHR; |
79072805 | 775 | register IO *io; |
a687059c LW |
776 | int ch; |
777 | ||
79072805 | 778 | io = GvIO(gv); |
a687059c | 779 | |
79072805 | 780 | if (!io) |
a687059c | 781 | return TRUE; |
af8c498a GS |
782 | else if (ckWARN(WARN_IO) |
783 | && (IoTYPE(io) == '>' || IoIFP(io) == PerlIO_stdout() | |
784 | || IoIFP(io) == PerlIO_stderr())) | |
785 | { | |
786 | SV* sv = sv_newmortal(); | |
787 | gv_efullname3(sv, gv, Nullch); | |
788 | Perl_warner(aTHX_ WARN_IO, "Filehandle %s opened only for output", | |
789 | SvPV_nolen(sv)); | |
790 | } | |
a687059c | 791 | |
8990e307 | 792 | while (IoIFP(io)) { |
a687059c | 793 | |
760ac839 | 794 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
a20bf0c3 | 795 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
760ac839 LW |
796 | return FALSE; /* this is the most usual case */ |
797 | } | |
a687059c | 798 | |
760ac839 | 799 | ch = PerlIO_getc(IoIFP(io)); |
a687059c | 800 | if (ch != EOF) { |
760ac839 | 801 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c LW |
802 | return FALSE; |
803 | } | |
760ac839 | 804 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
a20bf0c3 JH |
805 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
806 | PerlIO_set_cnt(IoIFP(io),-1); | |
760ac839 | 807 | } |
533c011a | 808 | if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
3280af22 | 809 | if (!nextargv(PL_argvgv)) /* get another fp handy */ |
a687059c LW |
810 | return TRUE; |
811 | } | |
812 | else | |
813 | return TRUE; /* normal fp, definitely end of file */ | |
814 | } | |
815 | return TRUE; | |
816 | } | |
817 | ||
5ff3f7a4 | 818 | Off_t |
864dbfa3 | 819 | Perl_do_tell(pTHX_ GV *gv) |
a687059c | 820 | { |
79072805 | 821 | register IO *io; |
96e4d5b1 | 822 | register PerlIO *fp; |
a687059c | 823 | |
96e4d5b1 | 824 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 825 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 PP |
826 | if (PerlIO_eof(fp)) |
827 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 828 | #endif |
8903cb82 | 829 | return PerlIO_tell(fp); |
96e4d5b1 | 830 | } |
d008e5eb GS |
831 | { |
832 | dTHR; | |
833 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 834 | Perl_warner(aTHX_ WARN_UNOPENED, "tell() on unopened file"); |
d008e5eb | 835 | } |
748a9306 | 836 | SETERRNO(EBADF,RMS$_IFI); |
5ff3f7a4 | 837 | return (Off_t)-1; |
a687059c LW |
838 | } |
839 | ||
840 | bool | |
864dbfa3 | 841 | Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence) |
a687059c | 842 | { |
79072805 | 843 | register IO *io; |
137443ea | 844 | register PerlIO *fp; |
a687059c | 845 | |
137443ea | 846 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 | 847 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea PP |
848 | if (PerlIO_eof(fp)) |
849 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ | |
bee1dbe2 | 850 | #endif |
8903cb82 | 851 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea | 852 | } |
d008e5eb GS |
853 | { |
854 | dTHR; | |
855 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 856 | Perl_warner(aTHX_ WARN_UNOPENED, "seek() on unopened file"); |
d008e5eb | 857 | } |
748a9306 | 858 | SETERRNO(EBADF,RMS$_IFI); |
a687059c LW |
859 | return FALSE; |
860 | } | |
861 | ||
97cc44eb | 862 | Off_t |
864dbfa3 | 863 | Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence) |
8903cb82 PP |
864 | { |
865 | register IO *io; | |
866 | register PerlIO *fp; | |
867 | ||
868 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) | |
3028581b | 869 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
d008e5eb GS |
870 | { |
871 | dTHR; | |
872 | if (ckWARN(WARN_UNOPENED)) | |
cea2e8a9 | 873 | Perl_warner(aTHX_ WARN_UNOPENED, "sysseek() on unopened file"); |
d008e5eb | 874 | } |
8903cb82 PP |
875 | SETERRNO(EBADF,RMS$_IFI); |
876 | return -1L; | |
877 | } | |
878 | ||
6ff81951 | 879 | int |
864dbfa3 | 880 | Perl_do_binmode(pTHX_ PerlIO *fp, int iotype, int flag) |
6ff81951 GS |
881 | { |
882 | if (flag != TRUE) | |
cea2e8a9 | 883 | Perl_croak(aTHX_ "panic: unsetting binmode"); /* Not implemented yet */ |
6ff81951 | 884 | #ifdef DOSISH |
61ae2fbf | 885 | #if defined(atarist) || defined(__MINT__) |
6ff81951 GS |
886 | if (!PerlIO_flush(fp) && (fp->_flag |= _IOBIN)) |
887 | return 1; | |
888 | else | |
889 | return 0; | |
890 | #else | |
891 | if (PerlLIO_setmode(PerlIO_fileno(fp), OP_BINARY) != -1) { | |
892 | #if defined(WIN32) && defined(__BORLANDC__) | |
893 | /* The translation mode of the stream is maintained independent | |
894 | * of the translation mode of the fd in the Borland RTL (heavy | |
895 | * digging through their runtime sources reveal). User has to | |
896 | * set the mode explicitly for the stream (though they don't | |
897 | * document this anywhere). GSAR 97-5-24 | |
898 | */ | |
899 | PerlIO_seek(fp,0L,0); | |
873ef191 | 900 | ((FILE*)fp)->flags |= _F_BIN; |
6ff81951 GS |
901 | #endif |
902 | return 1; | |
903 | } | |
904 | else | |
905 | return 0; | |
906 | #endif | |
907 | #else | |
908 | #if defined(USEMYBINMODE) | |
1cab015a | 909 | if (my_binmode(fp,iotype) != FALSE) |
6ff81951 GS |
910 | return 1; |
911 | else | |
912 | return 0; | |
913 | #else | |
914 | return 1; | |
915 | #endif | |
916 | #endif | |
917 | } | |
918 | ||
a0d0e21e | 919 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 | 920 | /* code courtesy of William Kucharski */ |
fe14fcc3 | 921 | #define HAS_CHSIZE |
6eb13c3b | 922 | |
517844ec | 923 | I32 my_chsize(fd, length) |
79072805 | 924 | I32 fd; /* file descriptor */ |
85e6fe83 | 925 | Off_t length; /* length to set file to */ |
6eb13c3b | 926 | { |
6eb13c3b LW |
927 | struct flock fl; |
928 | struct stat filebuf; | |
929 | ||
3028581b | 930 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b LW |
931 | return -1; |
932 | ||
933 | if (filebuf.st_size < length) { | |
934 | ||
935 | /* extend file length */ | |
936 | ||
3028581b | 937 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b LW |
938 | return -1; |
939 | ||
940 | /* write a "0" byte */ | |
941 | ||
3028581b | 942 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b LW |
943 | return -1; |
944 | } | |
945 | else { | |
946 | /* truncate length */ | |
947 | ||
948 | fl.l_whence = 0; | |
949 | fl.l_len = 0; | |
950 | fl.l_start = length; | |
a0d0e21e | 951 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b LW |
952 | |
953 | /* | |
a0d0e21e | 954 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b LW |
955 | * fcntl(2), which truncates the file so that it ends at the |
956 | * position indicated by fl.l_start. | |
957 | * | |
958 | * Will minor miracles never cease? | |
959 | */ | |
960 | ||
a0d0e21e | 961 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b LW |
962 | return -1; |
963 | ||
964 | } | |
965 | ||
966 | return 0; | |
967 | } | |
a0d0e21e | 968 | #endif /* F_FREESP */ |
ff8e2863 | 969 | |
a687059c | 970 | bool |
864dbfa3 | 971 | Perl_do_print(pTHX_ register SV *sv, PerlIO *fp) |
a687059c LW |
972 | { |
973 | register char *tmps; | |
463ee0b2 | 974 | STRLEN len; |
a687059c | 975 | |
79072805 LW |
976 | /* assuming fp is checked earlier */ |
977 | if (!sv) | |
978 | return TRUE; | |
3280af22 | 979 | if (PL_ofmt) { |
8990e307 | 980 | if (SvGMAGICAL(sv)) |
79072805 | 981 | mg_get(sv); |
463ee0b2 | 982 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
65202027 | 983 | PerlIO_printf(fp, PL_ofmt, (NV)SvIVX(sv)); |
760ac839 | 984 | return !PerlIO_error(fp); |
79072805 | 985 | } |
463ee0b2 | 986 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 | 987 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
3280af22 | 988 | PerlIO_printf(fp, PL_ofmt, SvNVX(sv)); |
760ac839 | 989 | return !PerlIO_error(fp); |
79072805 | 990 | } |
a687059c | 991 | } |
79072805 LW |
992 | switch (SvTYPE(sv)) { |
993 | case SVt_NULL: | |
d008e5eb GS |
994 | { |
995 | dTHR; | |
996 | if (ckWARN(WARN_UNINITIALIZED)) | |
cea2e8a9 | 997 | Perl_warner(aTHX_ WARN_UNINITIALIZED, PL_warn_uninit); |
d008e5eb | 998 | } |
ff8e2863 | 999 | return TRUE; |
79072805 | 1000 | case SVt_IV: |
a0d0e21e LW |
1001 | if (SvIOK(sv)) { |
1002 | if (SvGMAGICAL(sv)) | |
1003 | mg_get(sv); | |
cf2093f6 JH |
1004 | #ifdef IV_IS_QUAD |
1005 | if (SvIsUV(sv)) | |
1006 | PerlIO_printf(fp, "%" PERL_PRIu64, (UV)SvUVX(sv)); | |
1007 | else | |
1008 | PerlIO_printf(fp, "%" PERL_PRId64, (IV)SvIVX(sv)); | |
1009 | #else | |
1010 | if (SvIsUV(sv)) | |
25da4f38 IZ |
1011 | PerlIO_printf(fp, "%lu", (unsigned long)SvUVX(sv)); |
1012 | else | |
1013 | PerlIO_printf(fp, "%ld", (long)SvIVX(sv)); | |
cf2093f6 | 1014 | #endif |
760ac839 | 1015 | return !PerlIO_error(fp); |
a0d0e21e LW |
1016 | } |
1017 | /* FALL THROUGH */ | |
79072805 | 1018 | default: |
463ee0b2 | 1019 | tmps = SvPV(sv, len); |
79072805 | 1020 | break; |
ff8e2863 | 1021 | } |
760ac839 | 1022 | if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) |
a687059c | 1023 | return FALSE; |
760ac839 | 1024 | return !PerlIO_error(fp); |
a687059c LW |
1025 | } |
1026 | ||
79072805 | 1027 | I32 |
cea2e8a9 | 1028 | Perl_my_stat(pTHX) |
a687059c | 1029 | { |
4e35701f | 1030 | djSP; |
79072805 | 1031 | IO *io; |
748a9306 | 1032 | GV* tmpgv; |
79072805 | 1033 | |
533c011a | 1034 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1035 | EXTEND(SP,1); |
748a9306 LW |
1036 | tmpgv = cGVOP->op_gv; |
1037 | do_fstat: | |
1038 | io = GvIO(tmpgv); | |
8990e307 | 1039 | if (io && IoIFP(io)) { |
3280af22 NIS |
1040 | PL_statgv = tmpgv; |
1041 | sv_setpv(PL_statname,""); | |
1042 | PL_laststype = OP_STAT; | |
1043 | return (PL_laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &PL_statcache)); | |
a687059c LW |
1044 | } |
1045 | else { | |
3280af22 NIS |
1046 | if (tmpgv == PL_defgv) |
1047 | return PL_laststatval; | |
599cee73 | 1048 | if (ckWARN(WARN_UNOPENED)) |
cea2e8a9 | 1049 | Perl_warner(aTHX_ WARN_UNOPENED, "Stat on unopened file <%s>", |
748a9306 | 1050 | GvENAME(tmpgv)); |
3280af22 NIS |
1051 | PL_statgv = Nullgv; |
1052 | sv_setpv(PL_statname,""); | |
1053 | return (PL_laststatval = -1); | |
a687059c LW |
1054 | } |
1055 | } | |
1056 | else { | |
748a9306 | 1057 | SV* sv = POPs; |
4b74e3fb | 1058 | char *s; |
2d8e6c8d | 1059 | STRLEN n_a; |
79072805 | 1060 | PUTBACK; |
748a9306 LW |
1061 | if (SvTYPE(sv) == SVt_PVGV) { |
1062 | tmpgv = (GV*)sv; | |
1063 | goto do_fstat; | |
1064 | } | |
1065 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { | |
1066 | tmpgv = (GV*)SvRV(sv); | |
1067 | goto do_fstat; | |
1068 | } | |
1069 | ||
2d8e6c8d | 1070 | s = SvPV(sv, n_a); |
3280af22 NIS |
1071 | PL_statgv = Nullgv; |
1072 | sv_setpv(PL_statname, s); | |
1073 | PL_laststype = OP_STAT; | |
1074 | PL_laststatval = PerlLIO_stat(s, &PL_statcache); | |
599cee73 | 1075 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(s, '\n')) |
cea2e8a9 | 1076 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "stat"); |
3280af22 | 1077 | return PL_laststatval; |
a687059c LW |
1078 | } |
1079 | } | |
1080 | ||
79072805 | 1081 | I32 |
cea2e8a9 | 1082 | Perl_my_lstat(pTHX) |
c623bd54 | 1083 | { |
4e35701f | 1084 | djSP; |
79072805 | 1085 | SV *sv; |
2d8e6c8d | 1086 | STRLEN n_a; |
533c011a | 1087 | if (PL_op->op_flags & OPf_REF) { |
924508f0 | 1088 | EXTEND(SP,1); |
3280af22 NIS |
1089 | if (cGVOP->op_gv == PL_defgv) { |
1090 | if (PL_laststype != OP_LSTAT) | |
cea2e8a9 | 1091 | Perl_croak(aTHX_ "The stat preceding -l _ wasn't an lstat"); |
3280af22 | 1092 | return PL_laststatval; |
fe14fcc3 | 1093 | } |
cea2e8a9 | 1094 | Perl_croak(aTHX_ "You can't use -l on a filehandle"); |
fe14fcc3 | 1095 | } |
c623bd54 | 1096 | |
3280af22 NIS |
1097 | PL_laststype = OP_LSTAT; |
1098 | PL_statgv = Nullgv; | |
79072805 LW |
1099 | sv = POPs; |
1100 | PUTBACK; | |
2d8e6c8d | 1101 | sv_setpv(PL_statname,SvPV(sv, n_a)); |
2d8e6c8d | 1102 | PL_laststatval = PerlLIO_lstat(SvPV(sv, n_a),&PL_statcache); |
2d8e6c8d | 1103 | if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && strchr(SvPV(sv, n_a), '\n')) |
cea2e8a9 | 1104 | Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "lstat"); |
3280af22 | 1105 | return PL_laststatval; |
c623bd54 LW |
1106 | } |
1107 | ||
a687059c | 1108 | bool |
864dbfa3 | 1109 | Perl_do_aexec(pTHX_ SV *really, register SV **mark, register SV **sp) |
a687059c | 1110 | { |
d5a9bfb0 IZ |
1111 | return do_aexec5(really, mark, sp, 0, 0); |
1112 | } | |
1113 | ||
1114 | bool | |
2aa1486d GS |
1115 | Perl_do_aexec5(pTHX_ SV *really, register SV **mark, register SV **sp, |
1116 | int fd, int do_report) | |
d5a9bfb0 | 1117 | { |
a687059c | 1118 | register char **a; |
a687059c | 1119 | char *tmps; |
2d8e6c8d | 1120 | STRLEN n_a; |
a687059c | 1121 | |
79072805 | 1122 | if (sp > mark) { |
11343788 | 1123 | dTHR; |
3280af22 NIS |
1124 | New(401,PL_Argv, sp - mark + 1, char*); |
1125 | a = PL_Argv; | |
79072805 LW |
1126 | while (++mark <= sp) { |
1127 | if (*mark) | |
2d8e6c8d | 1128 | *a++ = SvPVx(*mark, n_a); |
a687059c LW |
1129 | else |
1130 | *a++ = ""; | |
1131 | } | |
1132 | *a = Nullch; | |
3280af22 | 1133 | if (*PL_Argv[0] != '/') /* will execvp use PATH? */ |
79072805 | 1134 | TAINT_ENV(); /* testing IFS here is overkill, probably */ |
2d8e6c8d | 1135 | if (really && *(tmps = SvPV(really, n_a))) |
3280af22 | 1136 | PerlProc_execvp(tmps,PL_Argv); |
a687059c | 1137 | else |
3280af22 | 1138 | PerlProc_execvp(PL_Argv[0],PL_Argv); |
599cee73 | 1139 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 | 1140 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
599cee73 | 1141 | PL_Argv[0], Strerror(errno)); |
d5a9bfb0 IZ |
1142 | if (do_report) { |
1143 | int e = errno; | |
1144 | ||
1145 | PerlLIO_write(fd, (void*)&e, sizeof(int)); | |
1146 | PerlLIO_close(fd); | |
1147 | } | |
a687059c | 1148 | } |
bee1dbe2 | 1149 | do_execfree(); |
a687059c LW |
1150 | return FALSE; |
1151 | } | |
1152 | ||
fe14fcc3 | 1153 | void |
864dbfa3 | 1154 | Perl_do_execfree(pTHX) |
ff8e2863 | 1155 | { |
3280af22 NIS |
1156 | if (PL_Argv) { |
1157 | Safefree(PL_Argv); | |
1158 | PL_Argv = Null(char **); | |
ff8e2863 | 1159 | } |
3280af22 NIS |
1160 | if (PL_Cmd) { |
1161 | Safefree(PL_Cmd); | |
1162 | PL_Cmd = Nullch; | |
ff8e2863 LW |
1163 | } |
1164 | } | |
1165 | ||
4d2c4e07 | 1166 | #if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(EPOC) |
760ac839 | 1167 | |
a687059c | 1168 | bool |
864dbfa3 | 1169 | Perl_do_exec(pTHX_ char *cmd) |
a687059c | 1170 | { |
e446cec8 IZ |
1171 | return do_exec3(cmd,0,0); |
1172 | } | |
1173 | ||
1174 | bool | |
864dbfa3 | 1175 | Perl_do_exec3(pTHX_ char *cmd, int fd, int do_report) |
e446cec8 | 1176 | { |
a687059c LW |
1177 | register char **a; |
1178 | register char *s; | |
a687059c LW |
1179 | char flags[10]; |
1180 | ||
748a9306 LW |
1181 | while (*cmd && isSPACE(*cmd)) |
1182 | cmd++; | |
1183 | ||
a687059c LW |
1184 | /* save an extra exec if possible */ |
1185 | ||
bf38876a | 1186 | #ifdef CSH |
3280af22 | 1187 | if (strnEQ(cmd,PL_cshname,PL_cshlen) && strnEQ(cmd+PL_cshlen," -c",3)) { |
a687059c | 1188 | strcpy(flags,"-c"); |
3280af22 | 1189 | s = cmd+PL_cshlen+3; |
a687059c LW |
1190 | if (*s == 'f') { |
1191 | s++; | |
1192 | strcat(flags,"f"); | |
1193 | } | |
1194 | if (*s == ' ') | |
1195 | s++; | |
1196 | if (*s++ == '\'') { | |
1197 | char *ncmd = s; | |
1198 | ||
1199 | while (*s) | |
1200 | s++; | |
1201 | if (s[-1] == '\n') | |
1202 | *--s = '\0'; | |
1203 | if (s[-1] == '\'') { | |
1204 | *--s = '\0'; | |
3280af22 | 1205 | PerlProc_execl(PL_cshname,"csh", flags,ncmd,(char*)0); |
a687059c LW |
1206 | *s = '\''; |
1207 | return FALSE; | |
1208 | } | |
1209 | } | |
1210 | } | |
bf38876a | 1211 | #endif /* CSH */ |
a687059c LW |
1212 | |
1213 | /* see if there are shell metacharacters in it */ | |
1214 | ||
748a9306 LW |
1215 | if (*cmd == '.' && isSPACE(cmd[1])) |
1216 | goto doshell; | |
1217 | ||
1218 | if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4])) | |
1219 | goto doshell; | |
1220 | ||
99b89507 | 1221 | for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */ |
63f2c1e1 LW |
1222 | if (*s == '=') |
1223 | goto doshell; | |
748a9306 | 1224 | |
a687059c | 1225 | for (s = cmd; *s; s++) { |
93a17b20 | 1226 | if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) { |
a687059c LW |
1227 | if (*s == '\n' && !s[1]) { |
1228 | *s = '\0'; | |
1229 | break; | |
1230 | } | |
603a98b0 IZ |
1231 | /* handle the 2>&1 construct at the end */ |
1232 | if (*s == '>' && s[1] == '&' && s[2] == '1' | |
1233 | && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) | |
1234 | && (!s[3] || isSPACE(s[3]))) | |
1235 | { | |
1236 | char *t = s + 3; | |
1237 | ||
1238 | while (*t && isSPACE(*t)) | |
1239 | ++t; | |
1240 | if (!*t && (dup2(1,2) != -1)) { | |
1241 | s[-2] = '\0'; | |
1242 | break; | |
1243 | } | |
1244 | } | |
a687059c | 1245 | doshell: |
3280af22 | 1246 | PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); |
a687059c LW |
1247 | return FALSE; |
1248 | } | |
1249 | } | |
748a9306 | 1250 | |
3280af22 NIS |
1251 | New(402,PL_Argv, (s - cmd) / 2 + 2, char*); |
1252 | PL_Cmd = savepvn(cmd, s-cmd); | |
1253 | a = PL_Argv; | |
1254 | for (s = PL_Cmd; *s;) { | |
99b89507 | 1255 | while (*s && isSPACE(*s)) s++; |
a687059c LW |
1256 | if (*s) |
1257 | *(a++) = s; | |
99b89507 | 1258 | while (*s && !isSPACE(*s)) s++; |
a687059c LW |
1259 | if (*s) |
1260 | *s++ = '\0'; | |
1261 | } | |
1262 | *a = Nullch; | |
3280af22 NIS |
1263 | if (PL_Argv[0]) { |
1264 | PerlProc_execvp(PL_Argv[0],PL_Argv); | |
b1248f16 | 1265 | if (errno == ENOEXEC) { /* for system V NIH syndrome */ |
ff8e2863 | 1266 | do_execfree(); |
a687059c | 1267 | goto doshell; |
b1248f16 | 1268 | } |
d008e5eb GS |
1269 | { |
1270 | dTHR; | |
e446cec8 IZ |
1271 | int e = errno; |
1272 | ||
d008e5eb | 1273 | if (ckWARN(WARN_EXEC)) |
cea2e8a9 | 1274 | Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s", |
d008e5eb | 1275 | PL_Argv[0], Strerror(errno)); |
e446cec8 IZ |
1276 | if (do_report) { |
1277 | PerlLIO_write(fd, (void*)&e, sizeof(int)); | |
1278 | PerlLIO_close(fd); | |
1279 | } | |
d008e5eb | 1280 | } |
a687059c | 1281 | } |
ff8e2863 | 1282 | do_execfree(); |
a687059c LW |
1283 | return FALSE; |
1284 | } | |
1285 | ||
6890e559 | 1286 | #endif /* OS2 || WIN32 */ |
760ac839 | 1287 | |
79072805 | 1288 | I32 |
864dbfa3 | 1289 | Perl_apply(pTHX_ I32 type, register SV **mark, register SV **sp) |
a687059c | 1290 | { |
11343788 | 1291 | dTHR; |
79072805 LW |
1292 | register I32 val; |
1293 | register I32 val2; | |
1294 | register I32 tot = 0; | |
20408e3c | 1295 | char *what; |
a687059c | 1296 | char *s; |
79072805 | 1297 | SV **oldmark = mark; |
2d8e6c8d | 1298 | STRLEN n_a; |
a687059c | 1299 | |
20408e3c | 1300 | #define APPLY_TAINT_PROPER() \ |
3280af22 | 1301 | STMT_START { \ |
17406bd6 | 1302 | if (PL_tainted) { TAINT_PROPER(what); } \ |
873ef191 | 1303 | } STMT_END |
20408e3c GS |
1304 | |
1305 | /* This is a first heuristic; it doesn't catch tainting magic. */ | |
3280af22 | 1306 | if (PL_tainting) { |
463ee0b2 | 1307 | while (++mark <= sp) { |
bbce6d69 PP |
1308 | if (SvTAINTED(*mark)) { |
1309 | TAINT; | |
1310 | break; | |
1311 | } | |
463ee0b2 LW |
1312 | } |
1313 | mark = oldmark; | |
1314 | } | |
a687059c | 1315 | switch (type) { |
79072805 | 1316 | case OP_CHMOD: |
20408e3c GS |
1317 | what = "chmod"; |
1318 | APPLY_TAINT_PROPER(); | |
79072805 | 1319 | if (++mark <= sp) { |
463ee0b2 | 1320 | val = SvIVx(*mark); |
20408e3c GS |
1321 | APPLY_TAINT_PROPER(); |
1322 | tot = sp - mark; | |
79072805 | 1323 | while (++mark <= sp) { |
2d8e6c8d | 1324 | char *name = SvPVx(*mark, n_a); |
20408e3c GS |
1325 | APPLY_TAINT_PROPER(); |
1326 | if (PerlLIO_chmod(name, val)) | |
a687059c LW |
1327 | tot--; |
1328 | } | |
1329 | } | |
1330 | break; | |
fe14fcc3 | 1331 | #ifdef HAS_CHOWN |
79072805 | 1332 | case OP_CHOWN: |
20408e3c GS |
1333 | what = "chown"; |
1334 | APPLY_TAINT_PROPER(); | |
79072805 | 1335 | if (sp - mark > 2) { |
463ee0b2 LW |
1336 | val = SvIVx(*++mark); |
1337 | val2 = SvIVx(*++mark); | |
20408e3c | 1338 | APPLY_TAINT_PROPER(); |
a0d0e21e | 1339 | tot = sp - mark; |
79072805 | 1340 | while (++mark <= sp) { |
2d8e6c8d | 1341 | char *name = SvPVx(*mark, n_a); |
20408e3c | 1342 | APPLY_TAINT_PROPER(); |
36660982 | 1343 | if (PerlLIO_chown(name, val, val2)) |
a687059c LW |
1344 | tot--; |
1345 | } | |
1346 | } | |
1347 | break; | |
b1248f16 | 1348 | #endif |
dd64f1c3 AD |
1349 | /* |
1350 | XXX Should we make lchown() directly available from perl? | |
1351 | For now, we'll let Configure test for HAS_LCHOWN, but do | |
1352 | nothing in the core. | |
1353 | --AD 5/1998 | |
1354 | */ | |
fe14fcc3 | 1355 | #ifdef HAS_KILL |
79072805 | 1356 | case OP_KILL: |
20408e3c GS |
1357 | what = "kill"; |
1358 | APPLY_TAINT_PROPER(); | |
55497cff PP |
1359 | if (mark == sp) |
1360 | break; | |
2d8e6c8d | 1361 | s = SvPVx(*++mark, n_a); |
79072805 LW |
1362 | if (isUPPER(*s)) { |
1363 | if (*s == 'S' && s[1] == 'I' && s[2] == 'G') | |
1364 | s += 3; | |
1365 | if (!(val = whichsig(s))) | |
cea2e8a9 | 1366 | Perl_croak(aTHX_ "Unrecognized signal name \"%s\"",s); |
79072805 LW |
1367 | } |
1368 | else | |
463ee0b2 | 1369 | val = SvIVx(*mark); |
20408e3c GS |
1370 | APPLY_TAINT_PROPER(); |
1371 | tot = sp - mark; | |
3595fcef PP |
1372 | #ifdef VMS |
1373 | /* kill() doesn't do process groups (job trees?) under VMS */ | |
1374 | if (val < 0) val = -val; | |
1375 | if (val == SIGKILL) { | |
1376 | # include <starlet.h> | |
1377 | /* Use native sys$delprc() to insure that target process is | |
1378 | * deleted; supervisor-mode images don't pay attention to | |
1379 | * CRTL's emulation of Unix-style signals and kill() | |
1380 | */ | |
1381 | while (++mark <= sp) { | |
1382 | I32 proc = SvIVx(*mark); | |
1383 | register unsigned long int __vmssts; | |
20408e3c | 1384 | APPLY_TAINT_PROPER(); |
3595fcef PP |
1385 | if (!((__vmssts = sys$delprc(&proc,0)) & 1)) { |
1386 | tot--; | |
1387 | switch (__vmssts) { | |
1388 | case SS$_NONEXPR: | |
1389 | case SS$_NOSUCHNODE: | |
1390 | SETERRNO(ESRCH,__vmssts); | |
1391 | break; | |
1392 | case SS$_NOPRIV: | |
1393 | SETERRNO(EPERM,__vmssts); | |
1394 | break; | |
1395 | default: | |
1396 | SETERRNO(EVMSERR,__vmssts); | |
1397 | } | |
1398 | } | |
1399 | } | |
1400 | break; | |
1401 | } | |
1402 | #endif | |
79072805 LW |
1403 | if (val < 0) { |
1404 | val = -val; | |
1405 | while (++mark <= sp) { | |
463ee0b2 | 1406 | I32 proc = SvIVx(*mark); |
20408e3c | 1407 | APPLY_TAINT_PROPER(); |
fe14fcc3 | 1408 | #ifdef HAS_KILLPG |
3028581b | 1409 | if (PerlProc_killpg(proc,val)) /* BSD */ |
a687059c | 1410 | #else |
3028581b | 1411 | if (PerlProc_kill(-proc,val)) /* SYSV */ |
a687059c | 1412 | #endif |
79072805 | 1413 | tot--; |
a687059c | 1414 | } |
79072805 LW |
1415 | } |
1416 | else { | |
1417 | while (++mark <= sp) { | |
20408e3c GS |
1418 | I32 proc = SvIVx(*mark); |
1419 | APPLY_TAINT_PROPER(); | |
1420 | if (PerlProc_kill(proc, val)) | |
79072805 | 1421 | tot--; |
a687059c LW |
1422 | } |
1423 | } | |
1424 | break; | |
b1248f16 | 1425 | #endif |
79072805 | 1426 | case OP_UNLINK: |
20408e3c GS |
1427 | what = "unlink"; |
1428 | APPLY_TAINT_PROPER(); | |
79072805 LW |
1429 | tot = sp - mark; |
1430 | while (++mark <= sp) { | |
2d8e6c8d | 1431 | s = SvPVx(*mark, n_a); |
20408e3c | 1432 | APPLY_TAINT_PROPER(); |
3280af22 | 1433 | if (PL_euid || PL_unsafe) { |
a687059c LW |
1434 | if (UNLINK(s)) |
1435 | tot--; | |
1436 | } | |
1437 | else { /* don't let root wipe out directories without -U */ | |
3280af22 | 1438 | if (PerlLIO_lstat(s,&PL_statbuf) < 0 || S_ISDIR(PL_statbuf.st_mode)) |
a687059c LW |
1439 | tot--; |
1440 | else { | |
1441 | if (UNLINK(s)) | |
1442 | tot--; | |
1443 | } | |
1444 | } | |
1445 | } | |
1446 | break; | |
a0d0e21e | 1447 | #ifdef HAS_UTIME |
79072805 | 1448 | case OP_UTIME: |
20408e3c GS |
1449 | what = "utime"; |
1450 | APPLY_TAINT_PROPER(); | |
79072805 | 1451 | if (sp - mark > 2) { |
748a9306 | 1452 | #if defined(I_UTIME) || defined(VMS) |
663a0e37 LW |
1453 | struct utimbuf utbuf; |
1454 | #else | |
a687059c | 1455 | struct { |
dd2821f6 GS |
1456 | Time_t actime; |
1457 | Time_t modtime; | |
a687059c | 1458 | } utbuf; |
663a0e37 | 1459 | #endif |
a687059c | 1460 | |
afd9f252 | 1461 | Zero(&utbuf, sizeof utbuf, char); |
517844ec | 1462 | #ifdef BIG_TIME |
dd2821f6 GS |
1463 | utbuf.actime = (Time_t)SvNVx(*++mark); /* time accessed */ |
1464 | utbuf.modtime = (Time_t)SvNVx(*++mark); /* time modified */ | |
517844ec | 1465 | #else |
dd2821f6 GS |
1466 | utbuf.actime = (Time_t)SvIVx(*++mark); /* time accessed */ |
1467 | utbuf.modtime = (Time_t)SvIVx(*++mark); /* time modified */ | |
517844ec | 1468 | #endif |
20408e3c | 1469 | APPLY_TAINT_PROPER(); |
79072805 LW |
1470 | tot = sp - mark; |
1471 | while (++mark <= sp) { | |
2d8e6c8d | 1472 | char *name = SvPVx(*mark, n_a); |
20408e3c GS |
1473 | APPLY_TAINT_PROPER(); |
1474 | if (PerlLIO_utime(name, &utbuf)) | |
a687059c LW |
1475 | tot--; |
1476 | } | |
a687059c LW |
1477 | } |
1478 | else | |
79072805 | 1479 | tot = 0; |
a687059c | 1480 | break; |
a0d0e21e | 1481 | #endif |
a687059c LW |
1482 | } |
1483 | return tot; | |
20408e3c | 1484 | |
20408e3c | 1485 | #undef APPLY_TAINT_PROPER |
a687059c LW |
1486 | } |
1487 | ||
1488 | /* Do the permissions allow some operation? Assumes statcache already set. */ | |
a0d0e21e | 1489 | #ifndef VMS /* VMS' cando is in vms.c */ |
7f4774ae JH |
1490 | bool |
1491 | Perl_cando(pTHX_ Mode_t mode, Uid_t effective, register Stat_t *statbufp) | |
1492 | /* Note: we use `effective' both for uids and gids. | |
1493 | * Here we are betting on Uid_t being equal or wider than Gid_t. */ | |
a687059c | 1494 | { |
bee1dbe2 | 1495 | #ifdef DOSISH |
fe14fcc3 LW |
1496 | /* [Comments and code from Len Reed] |
1497 | * MS-DOS "user" is similar to UNIX's "superuser," but can't write | |
1498 | * to write-protected files. The execute permission bit is set | |
1499 | * by the Miscrosoft C library stat() function for the following: | |
1500 | * .exe files | |
1501 | * .com files | |
1502 | * .bat files | |
1503 | * directories | |
1504 | * All files and directories are readable. | |
1505 | * Directories and special files, e.g. "CON", cannot be | |
1506 | * write-protected. | |
1507 | * [Comment by Tom Dinger -- a directory can have the write-protect | |
1508 | * bit set in the file system, but DOS permits changes to | |
1509 | * the directory anyway. In addition, all bets are off | |
1510 | * here for networked software, such as Novell and | |
1511 | * Sun's PC-NFS.] | |
1512 | */ | |
1513 | ||
bee1dbe2 LW |
1514 | /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat |
1515 | * too so it will actually look into the files for magic numbers | |
1516 | */ | |
7f4774ae | 1517 | return (mode & statbufp->st_mode) ? TRUE : FALSE; |
fe14fcc3 | 1518 | |
55497cff | 1519 | #else /* ! DOSISH */ |
3280af22 | 1520 | if ((effective ? PL_euid : PL_uid) == 0) { /* root is special */ |
7f4774ae | 1521 | if (mode == S_IXUSR) { |
c623bd54 | 1522 | if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode)) |
a687059c LW |
1523 | return TRUE; |
1524 | } | |
1525 | else | |
1526 | return TRUE; /* root reads and writes anything */ | |
1527 | return FALSE; | |
1528 | } | |
3280af22 | 1529 | if (statbufp->st_uid == (effective ? PL_euid : PL_uid) ) { |
7f4774ae | 1530 | if (statbufp->st_mode & mode) |
a687059c LW |
1531 | return TRUE; /* ok as "user" */ |
1532 | } | |
d8eceb89 | 1533 | else if (ingroup(statbufp->st_gid,effective)) { |
7f4774ae | 1534 | if (statbufp->st_mode & mode >> 3) |
a687059c LW |
1535 | return TRUE; /* ok as "group" */ |
1536 | } | |
7f4774ae | 1537 | else if (statbufp->st_mode & mode >> 6) |
a687059c LW |
1538 | return TRUE; /* ok as "other" */ |
1539 | return FALSE; | |
55497cff | 1540 | #endif /* ! DOSISH */ |
a687059c | 1541 | } |
a0d0e21e | 1542 | #endif /* ! VMS */ |
a687059c | 1543 | |
d8eceb89 JH |
1544 | bool |
1545 | Perl_ingroup(pTHX_ Gid_t testgid, Uid_t effective) | |
a687059c | 1546 | { |
3280af22 | 1547 | if (testgid == (effective ? PL_egid : PL_gid)) |
a687059c | 1548 | return TRUE; |
fe14fcc3 | 1549 | #ifdef HAS_GETGROUPS |
a687059c LW |
1550 | #ifndef NGROUPS |
1551 | #define NGROUPS 32 | |
1552 | #endif | |
1553 | { | |
a0d0e21e | 1554 | Groups_t gary[NGROUPS]; |
79072805 | 1555 | I32 anum; |
a687059c LW |
1556 | |
1557 | anum = getgroups(NGROUPS,gary); | |
1558 | while (--anum >= 0) | |
1559 | if (gary[anum] == testgid) | |
1560 | return TRUE; | |
1561 | } | |
1562 | #endif | |
1563 | return FALSE; | |
1564 | } | |
c2ab57d4 | 1565 | |
fe14fcc3 | 1566 | #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM) |
c2ab57d4 | 1567 | |
79072805 | 1568 | I32 |
864dbfa3 | 1569 | Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1570 | { |
11343788 | 1571 | dTHR; |
c2ab57d4 | 1572 | key_t key; |
79072805 | 1573 | I32 n, flags; |
c2ab57d4 | 1574 | |
463ee0b2 LW |
1575 | key = (key_t)SvNVx(*++mark); |
1576 | n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark); | |
1577 | flags = SvIVx(*++mark); | |
748a9306 | 1578 | SETERRNO(0,0); |
c2ab57d4 LW |
1579 | switch (optype) |
1580 | { | |
fe14fcc3 | 1581 | #ifdef HAS_MSG |
79072805 | 1582 | case OP_MSGGET: |
c2ab57d4 | 1583 | return msgget(key, flags); |
e5d73d77 | 1584 | #endif |
fe14fcc3 | 1585 | #ifdef HAS_SEM |
79072805 | 1586 | case OP_SEMGET: |
c2ab57d4 | 1587 | return semget(key, n, flags); |
e5d73d77 | 1588 | #endif |
fe14fcc3 | 1589 | #ifdef HAS_SHM |
79072805 | 1590 | case OP_SHMGET: |
c2ab57d4 | 1591 | return shmget(key, n, flags); |
e5d73d77 | 1592 | #endif |
fe14fcc3 | 1593 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1594 | default: |
cea2e8a9 | 1595 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 1596 | #endif |
c2ab57d4 LW |
1597 | } |
1598 | return -1; /* should never happen */ | |
1599 | } | |
1600 | ||
79072805 | 1601 | I32 |
864dbfa3 | 1602 | Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1603 | { |
11343788 | 1604 | dTHR; |
79072805 | 1605 | SV *astr; |
c2ab57d4 | 1606 | char *a; |
a0d0e21e LW |
1607 | I32 id, n, cmd, infosize, getinfo; |
1608 | I32 ret = -1; | |
c2ab57d4 | 1609 | |
463ee0b2 LW |
1610 | id = SvIVx(*++mark); |
1611 | n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0; | |
1612 | cmd = SvIVx(*++mark); | |
79072805 | 1613 | astr = *++mark; |
c2ab57d4 LW |
1614 | infosize = 0; |
1615 | getinfo = (cmd == IPC_STAT); | |
1616 | ||
1617 | switch (optype) | |
1618 | { | |
fe14fcc3 | 1619 | #ifdef HAS_MSG |
79072805 | 1620 | case OP_MSGCTL: |
c2ab57d4 LW |
1621 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1622 | infosize = sizeof(struct msqid_ds); | |
1623 | break; | |
e5d73d77 | 1624 | #endif |
fe14fcc3 | 1625 | #ifdef HAS_SHM |
79072805 | 1626 | case OP_SHMCTL: |
c2ab57d4 LW |
1627 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1628 | infosize = sizeof(struct shmid_ds); | |
1629 | break; | |
e5d73d77 | 1630 | #endif |
fe14fcc3 | 1631 | #ifdef HAS_SEM |
79072805 | 1632 | case OP_SEMCTL: |
39398f3f | 1633 | #ifdef Semctl |
c2ab57d4 LW |
1634 | if (cmd == IPC_STAT || cmd == IPC_SET) |
1635 | infosize = sizeof(struct semid_ds); | |
1636 | else if (cmd == GETALL || cmd == SETALL) | |
1637 | { | |
8e591e46 | 1638 | struct semid_ds semds; |
bd89102f AD |
1639 | union semun semun; |
1640 | ||
84902520 | 1641 | semun.buf = &semds; |
c2ab57d4 | 1642 | getinfo = (cmd == GETALL); |
9b89d93d GB |
1643 | if (Semctl(id, 0, IPC_STAT, semun) == -1) |
1644 | return -1; | |
6e21c824 LW |
1645 | infosize = semds.sem_nsems * sizeof(short); |
1646 | /* "short" is technically wrong but much more portable | |
1647 | than guessing about u_?short(_t)? */ | |
c2ab57d4 | 1648 | } |
39398f3f | 1649 | #else |
cea2e8a9 | 1650 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 1651 | #endif |
c2ab57d4 | 1652 | break; |
e5d73d77 | 1653 | #endif |
fe14fcc3 | 1654 | #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM) |
e5d73d77 | 1655 | default: |
cea2e8a9 | 1656 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
e5d73d77 | 1657 | #endif |
c2ab57d4 LW |
1658 | } |
1659 | ||
1660 | if (infosize) | |
1661 | { | |
a0d0e21e | 1662 | STRLEN len; |
c2ab57d4 LW |
1663 | if (getinfo) |
1664 | { | |
a0d0e21e LW |
1665 | SvPV_force(astr, len); |
1666 | a = SvGROW(astr, infosize+1); | |
c2ab57d4 LW |
1667 | } |
1668 | else | |
1669 | { | |
463ee0b2 LW |
1670 | a = SvPV(astr, len); |
1671 | if (len != infosize) | |
cea2e8a9 | 1672 | Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld", |
4ec43091 JH |
1673 | PL_op_desc[optype], |
1674 | (unsigned long)len, | |
1675 | (long)infosize); | |
c2ab57d4 LW |
1676 | } |
1677 | } | |
1678 | else | |
1679 | { | |
c030ccd9 | 1680 | IV i = SvIV(astr); |
c2ab57d4 LW |
1681 | a = (char *)i; /* ouch */ |
1682 | } | |
748a9306 | 1683 | SETERRNO(0,0); |
c2ab57d4 LW |
1684 | switch (optype) |
1685 | { | |
fe14fcc3 | 1686 | #ifdef HAS_MSG |
79072805 | 1687 | case OP_MSGCTL: |
bee1dbe2 | 1688 | ret = msgctl(id, cmd, (struct msqid_ds *)a); |
c2ab57d4 | 1689 | break; |
e5d73d77 | 1690 | #endif |
fe14fcc3 | 1691 | #ifdef HAS_SEM |
bd89102f | 1692 | case OP_SEMCTL: { |
39398f3f | 1693 | #ifdef Semctl |
bd89102f AD |
1694 | union semun unsemds; |
1695 | ||
1696 | unsemds.buf = (struct semid_ds *)a; | |
1697 | ret = Semctl(id, n, cmd, unsemds); | |
39398f3f | 1698 | #else |
cea2e8a9 | 1699 | Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]); |
39398f3f | 1700 | #endif |
bd89102f | 1701 | } |
c2ab57d4 | 1702 | break; |
e5d73d77 | 1703 | #endif |
fe14fcc3 | 1704 | #ifdef HAS_SHM |
79072805 | 1705 | case OP_SHMCTL: |
bee1dbe2 | 1706 | ret = shmctl(id, cmd, (struct shmid_ds *)a); |
c2ab57d4 | 1707 | break; |
e5d73d77 | 1708 | #endif |
c2ab57d4 LW |
1709 | } |
1710 | if (getinfo && ret >= 0) { | |
79072805 LW |
1711 | SvCUR_set(astr, infosize); |
1712 | *SvEND(astr) = '\0'; | |
a0d0e21e | 1713 | SvSETMAGIC(astr); |
c2ab57d4 LW |
1714 | } |
1715 | return ret; | |
1716 | } | |
1717 | ||
79072805 | 1718 | I32 |
864dbfa3 | 1719 | Perl_do_msgsnd(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1720 | { |
fe14fcc3 | 1721 | #ifdef HAS_MSG |
11343788 | 1722 | dTHR; |
79072805 | 1723 | SV *mstr; |
c2ab57d4 | 1724 | char *mbuf; |
79072805 | 1725 | I32 id, msize, flags; |
463ee0b2 | 1726 | STRLEN len; |
c2ab57d4 | 1727 | |
463ee0b2 | 1728 | id = SvIVx(*++mark); |
79072805 | 1729 | mstr = *++mark; |
463ee0b2 LW |
1730 | flags = SvIVx(*++mark); |
1731 | mbuf = SvPV(mstr, len); | |
1732 | if ((msize = len - sizeof(long)) < 0) | |
cea2e8a9 | 1733 | Perl_croak(aTHX_ "Arg too short for msgsnd"); |
748a9306 | 1734 | SETERRNO(0,0); |
bee1dbe2 | 1735 | return msgsnd(id, (struct msgbuf *)mbuf, msize, flags); |
e5d73d77 | 1736 | #else |
cea2e8a9 | 1737 | Perl_croak(aTHX_ "msgsnd not implemented"); |
e5d73d77 | 1738 | #endif |
c2ab57d4 LW |
1739 | } |
1740 | ||
79072805 | 1741 | I32 |
864dbfa3 | 1742 | Perl_do_msgrcv(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1743 | { |
fe14fcc3 | 1744 | #ifdef HAS_MSG |
11343788 | 1745 | dTHR; |
79072805 | 1746 | SV *mstr; |
c2ab57d4 LW |
1747 | char *mbuf; |
1748 | long mtype; | |
79072805 | 1749 | I32 id, msize, flags, ret; |
463ee0b2 | 1750 | STRLEN len; |
79072805 | 1751 | |
463ee0b2 | 1752 | id = SvIVx(*++mark); |
79072805 | 1753 | mstr = *++mark; |
463ee0b2 LW |
1754 | msize = SvIVx(*++mark); |
1755 | mtype = (long)SvIVx(*++mark); | |
1756 | flags = SvIVx(*++mark); | |
a0d0e21e LW |
1757 | SvPV_force(mstr, len); |
1758 | mbuf = SvGROW(mstr, sizeof(long)+msize+1); | |
1759 | ||
748a9306 | 1760 | SETERRNO(0,0); |
bee1dbe2 | 1761 | ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags); |
c2ab57d4 | 1762 | if (ret >= 0) { |
79072805 LW |
1763 | SvCUR_set(mstr, sizeof(long)+ret); |
1764 | *SvEND(mstr) = '\0'; | |
c2ab57d4 LW |
1765 | } |
1766 | return ret; | |
e5d73d77 | 1767 | #else |
cea2e8a9 | 1768 | Perl_croak(aTHX_ "msgrcv not implemented"); |
e5d73d77 | 1769 | #endif |
c2ab57d4 LW |
1770 | } |
1771 | ||
79072805 | 1772 | I32 |
864dbfa3 | 1773 | Perl_do_semop(pTHX_ SV **mark, SV **sp) |
c2ab57d4 | 1774 | { |
fe14fcc3 | 1775 | #ifdef HAS_SEM |
11343788 | 1776 | dTHR; |
79072805 | 1777 | SV *opstr; |
c2ab57d4 | 1778 | char *opbuf; |
463ee0b2 LW |
1779 | I32 id; |
1780 | STRLEN opsize; | |
c2ab57d4 | 1781 | |
463ee0b2 | 1782 | id = SvIVx(*++mark); |
79072805 | 1783 | opstr = *++mark; |
463ee0b2 | 1784 | opbuf = SvPV(opstr, opsize); |
c2ab57d4 LW |
1785 | if (opsize < sizeof(struct sembuf) |
1786 | || (opsize % sizeof(struct sembuf)) != 0) { | |
748a9306 | 1787 | SETERRNO(EINVAL,LIB$_INVARG); |
c2ab57d4 LW |
1788 | return -1; |
1789 | } | |
748a9306 | 1790 | SETERRNO(0,0); |
6e21c824 | 1791 | return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf)); |
e5d73d77 | 1792 | #else |
cea2e8a9 | 1793 | Perl_croak(aTHX_ "semop not implemented"); |
e5d73d77 | 1794 | #endif |
c2ab57d4 LW |
1795 | } |
1796 | ||
79072805 | 1797 | I32 |
864dbfa3 | 1798 | Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp) |
c2ab57d4 | 1799 | { |
fe14fcc3 | 1800 | #ifdef HAS_SHM |
11343788 | 1801 | dTHR; |
79072805 | 1802 | SV *mstr; |
c2ab57d4 | 1803 | char *mbuf, *shm; |
79072805 | 1804 | I32 id, mpos, msize; |
463ee0b2 | 1805 | STRLEN len; |
c2ab57d4 | 1806 | struct shmid_ds shmds; |
c2ab57d4 | 1807 | |
463ee0b2 | 1808 | id = SvIVx(*++mark); |
79072805 | 1809 | mstr = *++mark; |
463ee0b2 LW |
1810 | mpos = SvIVx(*++mark); |
1811 | msize = SvIVx(*++mark); | |
748a9306 | 1812 | SETERRNO(0,0); |
c2ab57d4 LW |
1813 | if (shmctl(id, IPC_STAT, &shmds) == -1) |
1814 | return -1; | |
1815 | if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) { | |
748a9306 | 1816 | SETERRNO(EFAULT,SS$_ACCVIO); /* can't do as caller requested */ |
c2ab57d4 LW |
1817 | return -1; |
1818 | } | |
8ac85365 | 1819 | shm = (char *)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0); |
c2ab57d4 LW |
1820 | if (shm == (char *)-1) /* I hate System V IPC, I really do */ |
1821 | return -1; | |
79072805 | 1822 | if (optype == OP_SHMREAD) { |
a0d0e21e LW |
1823 | SvPV_force(mstr, len); |
1824 | mbuf = SvGROW(mstr, msize+1); | |
1825 | ||
bee1dbe2 | 1826 | Copy(shm + mpos, mbuf, msize, char); |
79072805 LW |
1827 | SvCUR_set(mstr, msize); |
1828 | *SvEND(mstr) = '\0'; | |
a0d0e21e | 1829 | SvSETMAGIC(mstr); |
c2ab57d4 LW |
1830 | } |
1831 | else { | |
79072805 | 1832 | I32 n; |
c2ab57d4 | 1833 | |
a0d0e21e | 1834 | mbuf = SvPV(mstr, len); |
463ee0b2 | 1835 | if ((n = len) > msize) |
c2ab57d4 | 1836 | n = msize; |
bee1dbe2 | 1837 | Copy(mbuf, shm + mpos, n, char); |
c2ab57d4 | 1838 | if (n < msize) |
bee1dbe2 | 1839 | memzero(shm + mpos + n, msize - n); |
c2ab57d4 LW |
1840 | } |
1841 | return shmdt(shm); | |
e5d73d77 | 1842 | #else |
cea2e8a9 | 1843 | Perl_croak(aTHX_ "shm I/O not implemented"); |
e5d73d77 | 1844 | #endif |
c2ab57d4 LW |
1845 | } |
1846 | ||
fe14fcc3 | 1847 | #endif /* SYSV IPC */ |
4e35701f | 1848 |