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