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