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