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