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