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