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; |
a687059c |
95 | |
a687059c |
96 | forkprocess = 1; /* assume true if no fork */ |
c07a80fd |
97 | |
a0d0e21e |
98 | if (IoIFP(io)) { |
760ac839 |
99 | fd = PerlIO_fileno(IoIFP(io)); |
8990e307 |
100 | if (IoTYPE(io) == '-') |
c2ab57d4 |
101 | result = 0; |
6e21c824 |
102 | else if (fd <= maxsysfd) { |
8990e307 |
103 | saveifp = IoIFP(io); |
104 | saveofp = IoOFP(io); |
105 | savetype = IoTYPE(io); |
6e21c824 |
106 | result = 0; |
107 | } |
8990e307 |
108 | else if (IoTYPE(io) == '|') |
3028581b |
109 | result = PerlProc_pclose(IoIFP(io)); |
8990e307 |
110 | else if (IoIFP(io) != IoOFP(io)) { |
111 | if (IoOFP(io)) { |
760ac839 |
112 | result = PerlIO_close(IoOFP(io)); |
113 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
114 | } |
115 | else |
760ac839 |
116 | result = PerlIO_close(IoIFP(io)); |
a687059c |
117 | } |
a687059c |
118 | else |
760ac839 |
119 | result = PerlIO_close(IoIFP(io)); |
6e21c824 |
120 | if (result == EOF && fd > maxsysfd) |
760ac839 |
121 | PerlIO_printf(PerlIO_stderr(), "Warning: unable to close filehandle %s properly.\n", |
79072805 |
122 | GvENAME(gv)); |
8990e307 |
123 | IoOFP(io) = IoIFP(io) = Nullfp; |
a687059c |
124 | } |
c07a80fd |
125 | |
126 | if (as_raw) { |
127 | result = rawmode & 3; |
128 | IoTYPE(io) = "<>++"[result]; |
129 | writing = (result > 0); |
3028581b |
130 | fd = PerlLIO_open3(name, rawmode, rawperm); |
c07a80fd |
131 | if (fd == -1) |
132 | fp = NULL; |
133 | else { |
360e5741 |
134 | char *fpmode; |
135 | if (result == 0) |
136 | fpmode = "r"; |
137 | #ifdef O_APPEND |
138 | else if (rawmode & O_APPEND) |
139 | fpmode = (result == 1) ? "a" : "a+"; |
140 | #endif |
141 | else |
142 | fpmode = (result == 1) ? "w" : "r+"; |
143 | fp = PerlIO_fdopen(fd, fpmode); |
c07a80fd |
144 | if (!fp) |
3028581b |
145 | PerlLIO_close(fd); |
c07a80fd |
146 | } |
a687059c |
147 | } |
c07a80fd |
148 | else { |
149 | char *myname; |
150 | char mode[3]; /* stdio file mode ("r\0" or "r+\0") */ |
151 | int dodup; |
152 | |
153 | myname = savepvn(name, len); |
154 | SAVEFREEPV(myname); |
155 | name = myname; |
156 | while (len && isSPACE(name[len-1])) |
157 | name[--len] = '\0'; |
158 | |
159 | mode[0] = mode[1] = mode[2] = '\0'; |
160 | IoTYPE(io) = *name; |
161 | if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */ |
162 | mode[1] = *name++; |
163 | --len; |
164 | writing = 1; |
a687059c |
165 | } |
c07a80fd |
166 | |
167 | if (*name == '|') { |
168 | /*SUPPRESS 530*/ |
169 | for (name++; isSPACE(*name); name++) ; |
170 | if (strNE(name,"-")) |
171 | TAINT_ENV(); |
172 | TAINT_PROPER("piped open"); |
173 | if (dowarn && name[strlen(name)-1] == '|') |
174 | warn("Can't do bidirectional pipe"); |
3028581b |
175 | fp = PerlProc_popen(name,"w"); |
c07a80fd |
176 | writing = 1; |
177 | } |
178 | else if (*name == '>') { |
179 | TAINT_PROPER("open"); |
bf38876a |
180 | name++; |
c07a80fd |
181 | if (*name == '>') { |
182 | mode[0] = IoTYPE(io) = 'a'; |
bf38876a |
183 | name++; |
a0d0e21e |
184 | } |
c07a80fd |
185 | else |
186 | mode[0] = 'w'; |
187 | writing = 1; |
188 | |
189 | if (*name == '&') { |
190 | duplicity: |
191 | dodup = 1; |
192 | name++; |
193 | if (*name == '=') { |
194 | dodup = 0; |
a0d0e21e |
195 | name++; |
c07a80fd |
196 | } |
197 | if (!*name && supplied_fp) |
198 | fp = supplied_fp; |
a0d0e21e |
199 | else { |
c07a80fd |
200 | /*SUPPRESS 530*/ |
201 | for (; isSPACE(*name); name++) ; |
202 | if (isDIGIT(*name)) |
203 | fd = atoi(name); |
204 | else { |
205 | IO* thatio; |
206 | gv = gv_fetchpv(name,FALSE,SVt_PVIO); |
207 | thatio = GvIO(gv); |
208 | if (!thatio) { |
6e21c824 |
209 | #ifdef EINVAL |
c07a80fd |
210 | SETERRNO(EINVAL,SS$_IVCHAN); |
6e21c824 |
211 | #endif |
c07a80fd |
212 | goto say_false; |
213 | } |
214 | if (IoIFP(thatio)) { |
760ac839 |
215 | fd = PerlIO_fileno(IoIFP(thatio)); |
c07a80fd |
216 | if (IoTYPE(thatio) == 's') |
217 | IoTYPE(io) = 's'; |
218 | } |
219 | else |
220 | fd = -1; |
a0d0e21e |
221 | } |
fec02dd3 |
222 | if (dodup) |
3028581b |
223 | fd = PerlLIO_dup(fd); |
760ac839 |
224 | if (!(fp = PerlIO_fdopen(fd,mode))) { |
c07a80fd |
225 | if (dodup) |
3028581b |
226 | PerlLIO_close(fd); |
517844ec |
227 | } |
c07a80fd |
228 | } |
bf38876a |
229 | } |
c07a80fd |
230 | else { |
231 | /*SUPPRESS 530*/ |
232 | for (; isSPACE(*name); name++) ; |
233 | if (strEQ(name,"-")) { |
760ac839 |
234 | fp = PerlIO_stdout(); |
c07a80fd |
235 | IoTYPE(io) = '-'; |
236 | } |
237 | else { |
760ac839 |
238 | fp = PerlIO_open(name,mode); |
c07a80fd |
239 | } |
bf38876a |
240 | } |
241 | } |
c07a80fd |
242 | else if (*name == '<') { |
243 | /*SUPPRESS 530*/ |
244 | for (name++; isSPACE(*name); name++) ; |
bf38876a |
245 | mode[0] = 'r'; |
bf38876a |
246 | if (*name == '&') |
247 | goto duplicity; |
a687059c |
248 | if (strEQ(name,"-")) { |
760ac839 |
249 | fp = PerlIO_stdin(); |
8990e307 |
250 | IoTYPE(io) = '-'; |
a687059c |
251 | } |
bf38876a |
252 | else |
760ac839 |
253 | fp = PerlIO_open(name,mode); |
a687059c |
254 | } |
255 | else if (name[len-1] == '|') { |
a687059c |
256 | name[--len] = '\0'; |
99b89507 |
257 | while (len && isSPACE(name[len-1])) |
a687059c |
258 | name[--len] = '\0'; |
99b89507 |
259 | /*SUPPRESS 530*/ |
260 | for (; isSPACE(*name); name++) ; |
79072805 |
261 | if (strNE(name,"-")) |
262 | TAINT_ENV(); |
263 | TAINT_PROPER("piped open"); |
3028581b |
264 | fp = PerlProc_popen(name,"r"); |
8990e307 |
265 | IoTYPE(io) = '|'; |
a687059c |
266 | } |
267 | else { |
8990e307 |
268 | IoTYPE(io) = '<'; |
99b89507 |
269 | /*SUPPRESS 530*/ |
270 | for (; isSPACE(*name); name++) ; |
a687059c |
271 | if (strEQ(name,"-")) { |
760ac839 |
272 | fp = PerlIO_stdin(); |
8990e307 |
273 | IoTYPE(io) = '-'; |
a687059c |
274 | } |
275 | else |
760ac839 |
276 | fp = PerlIO_open(name,"r"); |
a687059c |
277 | } |
278 | } |
bee1dbe2 |
279 | if (!fp) { |
8990e307 |
280 | if (dowarn && IoTYPE(io) == '<' && strchr(name, '\n')) |
bee1dbe2 |
281 | warn(warn_nl, "open"); |
6e21c824 |
282 | goto say_false; |
bee1dbe2 |
283 | } |
8990e307 |
284 | if (IoTYPE(io) && |
285 | IoTYPE(io) != '|' && IoTYPE(io) != '-') { |
96827780 |
286 | dTHR; |
3028581b |
287 | if (PerlLIO_fstat(PerlIO_fileno(fp),&statbuf) < 0) { |
760ac839 |
288 | (void)PerlIO_close(fp); |
6e21c824 |
289 | goto say_false; |
a687059c |
290 | } |
1462b684 |
291 | if (S_ISSOCK(statbuf.st_mode)) |
8990e307 |
292 | IoTYPE(io) = 's'; /* in case a socket was passed in to us */ |
99b89507 |
293 | #ifdef HAS_SOCKET |
294 | else if ( |
c623bd54 |
295 | #ifdef S_IFMT |
99b89507 |
296 | !(statbuf.st_mode & S_IFMT) |
297 | #else |
298 | !statbuf.st_mode |
299 | #endif |
300 | ) { |
96827780 |
301 | char tmpbuf[256]; |
302 | Sock_size_t buflen = sizeof tmpbuf; |
3028581b |
303 | if (PerlSock_getsockname(PerlIO_fileno(fp), (struct sockaddr *)tmpbuf, |
d574b85e |
304 | &buflen) >= 0 |
305 | || errno != ENOTSOCK) |
8990e307 |
306 | IoTYPE(io) = 's'; /* some OS's return 0 on fstat()ed socket */ |
99b89507 |
307 | /* but some return 0 for streams too, sigh */ |
308 | } |
bf38876a |
309 | #endif |
a687059c |
310 | } |
6e21c824 |
311 | if (saveifp) { /* must use old fp? */ |
760ac839 |
312 | fd = PerlIO_fileno(saveifp); |
6e21c824 |
313 | if (saveofp) { |
760ac839 |
314 | PerlIO_flush(saveofp); /* emulate PerlIO_close() */ |
6e21c824 |
315 | if (saveofp != saveifp) { /* was a socket? */ |
760ac839 |
316 | PerlIO_close(saveofp); |
99b89507 |
317 | if (fd > 2) |
318 | Safefree(saveofp); |
6e21c824 |
319 | } |
320 | } |
760ac839 |
321 | if (fd != PerlIO_fileno(fp)) { |
bee1dbe2 |
322 | int pid; |
79072805 |
323 | SV *sv; |
bee1dbe2 |
324 | |
3028581b |
325 | PerlLIO_dup2(PerlIO_fileno(fp), fd); |
760ac839 |
326 | sv = *av_fetch(fdpid,PerlIO_fileno(fp),TRUE); |
a0d0e21e |
327 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 |
328 | pid = SvIVX(sv); |
329 | SvIVX(sv) = 0; |
79072805 |
330 | sv = *av_fetch(fdpid,fd,TRUE); |
a0d0e21e |
331 | (void)SvUPGRADE(sv, SVt_IV); |
463ee0b2 |
332 | SvIVX(sv) = pid; |
760ac839 |
333 | PerlIO_close(fp); |
bee1dbe2 |
334 | |
6e21c824 |
335 | } |
336 | fp = saveifp; |
760ac839 |
337 | PerlIO_clearerr(fp); |
6e21c824 |
338 | } |
a0d0e21e |
339 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
760ac839 |
340 | fd = PerlIO_fileno(fp); |
a0d0e21e |
341 | fcntl(fd,F_SETFD,fd > maxsysfd); |
1462b684 |
342 | #endif |
8990e307 |
343 | IoIFP(io) = fp; |
bf38876a |
344 | if (writing) { |
96827780 |
345 | dTHR; |
8990e307 |
346 | if (IoTYPE(io) == 's' |
347 | || (IoTYPE(io) == '>' && S_ISCHR(statbuf.st_mode)) ) { |
760ac839 |
348 | if (!(IoOFP(io) = PerlIO_fdopen(PerlIO_fileno(fp),"w"))) { |
349 | PerlIO_close(fp); |
8990e307 |
350 | IoIFP(io) = Nullfp; |
6e21c824 |
351 | goto say_false; |
fe14fcc3 |
352 | } |
1462b684 |
353 | } |
354 | else |
8990e307 |
355 | IoOFP(io) = fp; |
bf38876a |
356 | } |
a687059c |
357 | return TRUE; |
6e21c824 |
358 | |
359 | say_false: |
8990e307 |
360 | IoIFP(io) = saveifp; |
361 | IoOFP(io) = saveofp; |
362 | IoTYPE(io) = savetype; |
6e21c824 |
363 | return FALSE; |
a687059c |
364 | } |
365 | |
760ac839 |
366 | PerlIO * |
8ac85365 |
367 | nextargv(register GV *gv) |
a687059c |
368 | { |
79072805 |
369 | register SV *sv; |
99b89507 |
370 | #ifndef FLEXFILENAMES |
c623bd54 |
371 | int filedev; |
372 | int fileino; |
99b89507 |
373 | #endif |
c623bd54 |
374 | int fileuid; |
375 | int filegid; |
fe14fcc3 |
376 | |
79072805 |
377 | if (!argvoutgv) |
85e6fe83 |
378 | argvoutgv = gv_fetchpv("ARGVOUT",TRUE,SVt_PVIO); |
fe14fcc3 |
379 | if (filemode & (S_ISUID|S_ISGID)) { |
760ac839 |
380 | PerlIO_flush(IoIFP(GvIOn(argvoutgv))); /* chmod must follow last write */ |
fe14fcc3 |
381 | #ifdef HAS_FCHMOD |
382 | (void)fchmod(lastfd,filemode); |
383 | #else |
3028581b |
384 | (void)PerlLIO_chmod(oldname,filemode); |
fe14fcc3 |
385 | #endif |
386 | } |
387 | filemode = 0; |
79072805 |
388 | while (av_len(GvAV(gv)) >= 0) { |
11343788 |
389 | dTHR; |
85aff577 |
390 | STRLEN oldlen; |
79072805 |
391 | sv = av_shift(GvAV(gv)); |
8990e307 |
392 | SAVEFREESV(sv); |
79072805 |
393 | sv_setsv(GvSV(gv),sv); |
394 | SvSETMAGIC(GvSV(gv)); |
85aff577 |
395 | oldname = SvPVx(GvSV(gv), oldlen); |
396 | if (do_open(gv,oldname,oldlen,inplace!=0,0,0,Nullfp)) { |
a687059c |
397 | if (inplace) { |
79072805 |
398 | TAINT_PROPER("inplace open"); |
85aff577 |
399 | if (oldlen == 1 && *oldname == '-') { |
4633a7c4 |
400 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a0d0e21e |
401 | return IoIFP(GvIOp(gv)); |
c623bd54 |
402 | } |
99b89507 |
403 | #ifndef FLEXFILENAMES |
c623bd54 |
404 | filedev = statbuf.st_dev; |
405 | fileino = statbuf.st_ino; |
99b89507 |
406 | #endif |
a687059c |
407 | filemode = statbuf.st_mode; |
408 | fileuid = statbuf.st_uid; |
409 | filegid = statbuf.st_gid; |
c623bd54 |
410 | if (!S_ISREG(filemode)) { |
411 | warn("Can't do inplace edit: %s is not a regular file", |
412 | oldname ); |
79072805 |
413 | do_close(gv,FALSE); |
c623bd54 |
414 | continue; |
415 | } |
a687059c |
416 | if (*inplace) { |
ff8e2863 |
417 | #ifdef SUFFIX |
79072805 |
418 | add_suffix(sv,inplace); |
ff8e2863 |
419 | #else |
79072805 |
420 | sv_catpv(sv,inplace); |
ff8e2863 |
421 | #endif |
c623bd54 |
422 | #ifndef FLEXFILENAMES |
3028581b |
423 | if (PerlLIO_stat(SvPVX(sv),&statbuf) >= 0 |
c623bd54 |
424 | && statbuf.st_dev == filedev |
39e571d4 |
425 | && statbuf.st_ino == fileino |
426 | #ifdef DJGPP |
427 | || (_djstat_fail_bits & _STFAIL_TRUENAME)!=0 |
428 | #endif |
429 | ) { |
430 | warn("Can't do inplace edit: %s would not be uniq", |
463ee0b2 |
431 | SvPVX(sv) ); |
79072805 |
432 | do_close(gv,FALSE); |
c623bd54 |
433 | continue; |
434 | } |
435 | #endif |
fe14fcc3 |
436 | #ifdef HAS_RENAME |
bee1dbe2 |
437 | #ifndef DOSISH |
3028581b |
438 | if (PerlLIO_rename(oldname,SvPVX(sv)) < 0) { |
c623bd54 |
439 | warn("Can't rename %s to %s: %s, skipping file", |
2304df62 |
440 | oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
441 | do_close(gv,FALSE); |
c623bd54 |
442 | continue; |
443 | } |
a687059c |
444 | #else |
79072805 |
445 | do_close(gv,FALSE); |
3028581b |
446 | (void)PerlLIO_unlink(SvPVX(sv)); |
447 | (void)PerlLIO_rename(oldname,SvPVX(sv)); |
85aff577 |
448 | do_open(gv,SvPVX(sv),SvCUR(sv),inplace!=0,0,0,Nullfp); |
55497cff |
449 | #endif /* DOSISH */ |
ff8e2863 |
450 | #else |
463ee0b2 |
451 | (void)UNLINK(SvPVX(sv)); |
452 | if (link(oldname,SvPVX(sv)) < 0) { |
c623bd54 |
453 | warn("Can't rename %s to %s: %s, skipping file", |
2304df62 |
454 | oldname, SvPVX(sv), Strerror(errno) ); |
79072805 |
455 | do_close(gv,FALSE); |
c623bd54 |
456 | continue; |
457 | } |
a687059c |
458 | (void)UNLINK(oldname); |
459 | #endif |
460 | } |
461 | else { |
a8c18271 |
462 | #if !defined(DOSISH) && !defined(AMIGAOS) |
edc7bc49 |
463 | # ifndef VMS /* Don't delete; use automatic file versioning */ |
fe14fcc3 |
464 | if (UNLINK(oldname) < 0) { |
85aff577 |
465 | warn("Can't remove %s: %s, skipping file", |
466 | oldname, Strerror(errno) ); |
79072805 |
467 | do_close(gv,FALSE); |
fe14fcc3 |
468 | continue; |
469 | } |
edc7bc49 |
470 | # endif |
ff8e2863 |
471 | #else |
463ee0b2 |
472 | croak("Can't do inplace edit without backup"); |
ff8e2863 |
473 | #endif |
a687059c |
474 | } |
475 | |
85aff577 |
476 | sv_setpvn(sv,">",!inplace); |
477 | sv_catpvn(sv,oldname,oldlen); |
748a9306 |
478 | SETERRNO(0,0); /* in case sprintf set errno */ |
85aff577 |
479 | if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv),inplace!=0, |
480 | O_WRONLY|O_CREAT|OPEN_EXCL,0666,Nullfp)) { |
c623bd54 |
481 | warn("Can't do inplace edit on %s: %s", |
2304df62 |
482 | oldname, Strerror(errno) ); |
79072805 |
483 | do_close(gv,FALSE); |
fe14fcc3 |
484 | continue; |
485 | } |
4633a7c4 |
486 | setdefout(argvoutgv); |
760ac839 |
487 | lastfd = PerlIO_fileno(IoIFP(GvIOp(argvoutgv))); |
3028581b |
488 | (void)PerlLIO_fstat(lastfd,&statbuf); |
fe14fcc3 |
489 | #ifdef HAS_FCHMOD |
490 | (void)fchmod(lastfd,filemode); |
a687059c |
491 | #else |
3e3baf6d |
492 | # if !(defined(WIN32) && defined(__BORLANDC__)) |
493 | /* Borland runtime creates a readonly file! */ |
3028581b |
494 | (void)PerlLIO_chmod(oldname,filemode); |
3e3baf6d |
495 | # endif |
a687059c |
496 | #endif |
fe14fcc3 |
497 | if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) { |
498 | #ifdef HAS_FCHOWN |
499 | (void)fchown(lastfd,fileuid,filegid); |
a687059c |
500 | #else |
fe14fcc3 |
501 | #ifdef HAS_CHOWN |
502 | (void)chown(oldname,fileuid,filegid); |
a687059c |
503 | #endif |
b1248f16 |
504 | #endif |
fe14fcc3 |
505 | } |
a687059c |
506 | } |
a0d0e21e |
507 | return IoIFP(GvIOp(gv)); |
a687059c |
508 | } |
509 | else |
22fae026 |
510 | PerlIO_printf(PerlIO_stderr(), "Can't open %s: %s\n", |
511 | SvPV(sv, na), Strerror(errno)); |
a687059c |
512 | } |
513 | if (inplace) { |
79072805 |
514 | (void)do_close(argvoutgv,FALSE); |
4633a7c4 |
515 | setdefout(gv_fetchpv("STDOUT",TRUE,SVt_PVIO)); |
a687059c |
516 | } |
517 | return Nullfp; |
518 | } |
519 | |
fe14fcc3 |
520 | #ifdef HAS_PIPE |
afd9f252 |
521 | void |
8ac85365 |
522 | do_pipe(SV *sv, GV *rgv, GV *wgv) |
afd9f252 |
523 | { |
79072805 |
524 | register IO *rstio; |
525 | register IO *wstio; |
afd9f252 |
526 | int fd[2]; |
527 | |
79072805 |
528 | if (!rgv) |
afd9f252 |
529 | goto badexit; |
79072805 |
530 | if (!wgv) |
afd9f252 |
531 | goto badexit; |
532 | |
a0d0e21e |
533 | rstio = GvIOn(rgv); |
534 | wstio = GvIOn(wgv); |
afd9f252 |
535 | |
a0d0e21e |
536 | if (IoIFP(rstio)) |
79072805 |
537 | do_close(rgv,FALSE); |
a0d0e21e |
538 | if (IoIFP(wstio)) |
79072805 |
539 | do_close(wgv,FALSE); |
afd9f252 |
540 | |
3028581b |
541 | if (PerlProc_pipe(fd) < 0) |
afd9f252 |
542 | goto badexit; |
760ac839 |
543 | IoIFP(rstio) = PerlIO_fdopen(fd[0], "r"); |
544 | IoOFP(wstio) = PerlIO_fdopen(fd[1], "w"); |
8990e307 |
545 | IoIFP(wstio) = IoOFP(wstio); |
546 | IoTYPE(rstio) = '<'; |
547 | IoTYPE(wstio) = '>'; |
548 | if (!IoIFP(rstio) || !IoOFP(wstio)) { |
760ac839 |
549 | if (IoIFP(rstio)) PerlIO_close(IoIFP(rstio)); |
3028581b |
550 | else PerlLIO_close(fd[0]); |
760ac839 |
551 | if (IoOFP(wstio)) PerlIO_close(IoOFP(wstio)); |
3028581b |
552 | else PerlLIO_close(fd[1]); |
fe14fcc3 |
553 | goto badexit; |
554 | } |
afd9f252 |
555 | |
79072805 |
556 | sv_setsv(sv,&sv_yes); |
afd9f252 |
557 | return; |
558 | |
559 | badexit: |
79072805 |
560 | sv_setsv(sv,&sv_undef); |
afd9f252 |
561 | return; |
562 | } |
b1248f16 |
563 | #endif |
afd9f252 |
564 | |
517844ec |
565 | /* explicit renamed to avoid C++ conflict -- kja */ |
a687059c |
566 | bool |
a0d0e21e |
567 | #ifndef CAN_PROTOTYPE |
517844ec |
568 | do_close(gv,not_implicit) |
79072805 |
569 | GV *gv; |
517844ec |
570 | bool not_implicit; |
8990e307 |
571 | #else |
517844ec |
572 | do_close(GV *gv, bool not_implicit) |
a0d0e21e |
573 | #endif /* CAN_PROTOTYPE */ |
a687059c |
574 | { |
1193dd27 |
575 | bool retval; |
576 | IO *io; |
a687059c |
577 | |
79072805 |
578 | if (!gv) |
579 | gv = argvgv; |
a0d0e21e |
580 | if (!gv || SvTYPE(gv) != SVt_PVGV) { |
748a9306 |
581 | SETERRNO(EBADF,SS$_IVCHAN); |
c2ab57d4 |
582 | return FALSE; |
99b89507 |
583 | } |
79072805 |
584 | io = GvIO(gv); |
585 | if (!io) { /* never opened */ |
517844ec |
586 | if (dowarn && not_implicit) |
79072805 |
587 | warn("Close on unopened file <%s>",GvENAME(gv)); |
a687059c |
588 | return FALSE; |
589 | } |
1193dd27 |
590 | retval = io_close(io); |
517844ec |
591 | if (not_implicit) { |
1193dd27 |
592 | IoLINES(io) = 0; |
593 | IoPAGE(io) = 0; |
594 | IoLINES_LEFT(io) = IoPAGE_LEN(io); |
595 | } |
596 | IoTYPE(io) = ' '; |
597 | return retval; |
598 | } |
599 | |
600 | bool |
8ac85365 |
601 | io_close(IO *io) |
1193dd27 |
602 | { |
603 | bool retval = FALSE; |
604 | int status; |
605 | |
8990e307 |
606 | if (IoIFP(io)) { |
607 | if (IoTYPE(io) == '|') { |
3028581b |
608 | status = PerlProc_pclose(IoIFP(io)); |
f86702cc |
609 | STATUS_NATIVE_SET(status); |
1e422769 |
610 | retval = (STATUS_POSIX == 0); |
a687059c |
611 | } |
8990e307 |
612 | else if (IoTYPE(io) == '-') |
a687059c |
613 | retval = TRUE; |
614 | else { |
8990e307 |
615 | if (IoOFP(io) && IoOFP(io) != IoIFP(io)) { /* a socket */ |
760ac839 |
616 | retval = (PerlIO_close(IoOFP(io)) != EOF); |
617 | PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */ |
c2ab57d4 |
618 | } |
619 | else |
760ac839 |
620 | retval = (PerlIO_close(IoIFP(io)) != EOF); |
a687059c |
621 | } |
8990e307 |
622 | IoOFP(io) = IoIFP(io) = Nullfp; |
79072805 |
623 | } |
1193dd27 |
624 | |
a687059c |
625 | return retval; |
626 | } |
627 | |
628 | bool |
8ac85365 |
629 | do_eof(GV *gv) |
a687059c |
630 | { |
11343788 |
631 | dTHR; |
79072805 |
632 | register IO *io; |
a687059c |
633 | int ch; |
634 | |
79072805 |
635 | io = GvIO(gv); |
a687059c |
636 | |
79072805 |
637 | if (!io) |
a687059c |
638 | return TRUE; |
639 | |
8990e307 |
640 | while (IoIFP(io)) { |
a687059c |
641 | |
760ac839 |
642 | if (PerlIO_has_cntptr(IoIFP(io))) { /* (the code works without this) */ |
643 | if (PerlIO_get_cnt(IoIFP(io)) > 0) /* cheat a little, since */ |
644 | return FALSE; /* this is the most usual case */ |
645 | } |
a687059c |
646 | |
760ac839 |
647 | ch = PerlIO_getc(IoIFP(io)); |
a687059c |
648 | if (ch != EOF) { |
760ac839 |
649 | (void)PerlIO_ungetc(IoIFP(io),ch); |
a687059c |
650 | return FALSE; |
651 | } |
760ac839 |
652 | if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) { |
653 | if (PerlIO_get_cnt(IoIFP(io)) < -1) |
654 | PerlIO_set_cnt(IoIFP(io),-1); |
655 | } |
8990e307 |
656 | if (op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */ |
79072805 |
657 | if (!nextargv(argvgv)) /* get another fp handy */ |
a687059c |
658 | return TRUE; |
659 | } |
660 | else |
661 | return TRUE; /* normal fp, definitely end of file */ |
662 | } |
663 | return TRUE; |
664 | } |
665 | |
666 | long |
8ac85365 |
667 | do_tell(GV *gv) |
a687059c |
668 | { |
79072805 |
669 | register IO *io; |
96e4d5b1 |
670 | register PerlIO *fp; |
a687059c |
671 | |
96e4d5b1 |
672 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
673 | #ifdef ULTRIX_STDIO_BOTCH |
96e4d5b1 |
674 | if (PerlIO_eof(fp)) |
675 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
676 | #endif |
8903cb82 |
677 | return PerlIO_tell(fp); |
96e4d5b1 |
678 | } |
a687059c |
679 | if (dowarn) |
8903cb82 |
680 | warn("tell() on unopened file"); |
748a9306 |
681 | SETERRNO(EBADF,RMS$_IFI); |
a687059c |
682 | return -1L; |
683 | } |
684 | |
685 | bool |
8ac85365 |
686 | do_seek(GV *gv, long int pos, int whence) |
a687059c |
687 | { |
79072805 |
688 | register IO *io; |
137443ea |
689 | register PerlIO *fp; |
a687059c |
690 | |
137443ea |
691 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) { |
bee1dbe2 |
692 | #ifdef ULTRIX_STDIO_BOTCH |
137443ea |
693 | if (PerlIO_eof(fp)) |
694 | (void)PerlIO_seek(fp, 0L, 2); /* ultrix 1.2 workaround */ |
bee1dbe2 |
695 | #endif |
8903cb82 |
696 | return PerlIO_seek(fp, pos, whence) >= 0; |
137443ea |
697 | } |
a687059c |
698 | if (dowarn) |
8903cb82 |
699 | warn("seek() on unopened file"); |
748a9306 |
700 | SETERRNO(EBADF,RMS$_IFI); |
a687059c |
701 | return FALSE; |
702 | } |
703 | |
8903cb82 |
704 | long |
8ac85365 |
705 | do_sysseek(GV *gv, long int pos, int whence) |
8903cb82 |
706 | { |
707 | register IO *io; |
708 | register PerlIO *fp; |
709 | |
710 | if (gv && (io = GvIO(gv)) && (fp = IoIFP(io))) |
3028581b |
711 | return PerlLIO_lseek(PerlIO_fileno(fp), pos, whence); |
8903cb82 |
712 | if (dowarn) |
713 | warn("sysseek() on unopened file"); |
714 | SETERRNO(EBADF,RMS$_IFI); |
715 | return -1L; |
716 | } |
717 | |
a0d0e21e |
718 | #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(F_FREESP) |
c2ab57d4 |
719 | /* code courtesy of William Kucharski */ |
fe14fcc3 |
720 | #define HAS_CHSIZE |
6eb13c3b |
721 | |
517844ec |
722 | I32 my_chsize(fd, length) |
79072805 |
723 | I32 fd; /* file descriptor */ |
85e6fe83 |
724 | Off_t length; /* length to set file to */ |
6eb13c3b |
725 | { |
6eb13c3b |
726 | struct flock fl; |
727 | struct stat filebuf; |
728 | |
3028581b |
729 | if (PerlLIO_fstat(fd, &filebuf) < 0) |
6eb13c3b |
730 | return -1; |
731 | |
732 | if (filebuf.st_size < length) { |
733 | |
734 | /* extend file length */ |
735 | |
3028581b |
736 | if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0) |
6eb13c3b |
737 | return -1; |
738 | |
739 | /* write a "0" byte */ |
740 | |
3028581b |
741 | if ((PerlLIO_write(fd, "", 1)) != 1) |
6eb13c3b |
742 | return -1; |
743 | } |
744 | else { |
745 | /* truncate length */ |
746 | |
747 | fl.l_whence = 0; |
748 | fl.l_len = 0; |
749 | fl.l_start = length; |
a0d0e21e |
750 | fl.l_type = F_WRLCK; /* write lock on file space */ |
6eb13c3b |
751 | |
752 | /* |
a0d0e21e |
753 | * This relies on the UNDOCUMENTED F_FREESP argument to |
6eb13c3b |
754 | * fcntl(2), which truncates the file so that it ends at the |
755 | * position indicated by fl.l_start. |
756 | * |
757 | * Will minor miracles never cease? |
758 | */ |
759 | |
a0d0e21e |
760 | if (fcntl(fd, F_FREESP, &fl) < 0) |
6eb13c3b |
761 | return -1; |
762 | |
763 | } |
764 | |
765 | return 0; |
766 | } |
a0d0e21e |
767 | #endif /* F_FREESP */ |
ff8e2863 |
768 | |
a687059c |
769 | bool |
6acef3b7 |
770 | do_print(register SV *sv, PerlIO *fp) |
a687059c |
771 | { |
772 | register char *tmps; |
463ee0b2 |
773 | STRLEN len; |
a687059c |
774 | |
79072805 |
775 | /* assuming fp is checked earlier */ |
776 | if (!sv) |
777 | return TRUE; |
778 | if (ofmt) { |
8990e307 |
779 | if (SvGMAGICAL(sv)) |
79072805 |
780 | mg_get(sv); |
463ee0b2 |
781 | if (SvIOK(sv) && SvIVX(sv) != 0) { |
760ac839 |
782 | PerlIO_printf(fp, ofmt, (double)SvIVX(sv)); |
783 | return !PerlIO_error(fp); |
79072805 |
784 | } |
463ee0b2 |
785 | if ( (SvNOK(sv) && SvNVX(sv) != 0.0) |
79072805 |
786 | || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) { |
760ac839 |
787 | PerlIO_printf(fp, ofmt, SvNVX(sv)); |
788 | return !PerlIO_error(fp); |
79072805 |
789 | } |
a687059c |
790 | } |
79072805 |
791 | switch (SvTYPE(sv)) { |
792 | case SVt_NULL: |
8990e307 |
793 | if (dowarn) |
794 | warn(warn_uninit); |
ff8e2863 |
795 | return TRUE; |
79072805 |
796 | case SVt_IV: |
a0d0e21e |
797 | if (SvIOK(sv)) { |
798 | if (SvGMAGICAL(sv)) |
799 | mg_get(sv); |
760ac839 |
800 | PerlIO_printf(fp, "%ld", (long)SvIVX(sv)); |
801 | return !PerlIO_error(fp); |
a0d0e21e |
802 | } |
803 | /* FALL THROUGH */ |
79072805 |
804 | default: |
463ee0b2 |
805 | tmps = SvPV(sv, len); |
79072805 |
806 | break; |
ff8e2863 |
807 | } |
760ac839 |
808 | if (len && (PerlIO_write(fp,tmps,len) == 0 || PerlIO_error(fp))) |
a687059c |
809 | return FALSE; |
760ac839 |
810 | return !PerlIO_error(fp); |
a687059c |
811 | } |
812 | |
79072805 |
813 | I32 |
8ac85365 |
814 | my_stat(ARGSproto) |
a687059c |
815 | { |
4e35701f |
816 | djSP; |
79072805 |
817 | IO *io; |
748a9306 |
818 | GV* tmpgv; |
79072805 |
819 | |
a0d0e21e |
820 | if (op->op_flags & OPf_REF) { |
79072805 |
821 | EXTEND(sp,1); |
748a9306 |
822 | tmpgv = cGVOP->op_gv; |
823 | do_fstat: |
824 | io = GvIO(tmpgv); |
8990e307 |
825 | if (io && IoIFP(io)) { |
748a9306 |
826 | statgv = tmpgv; |
79072805 |
827 | sv_setpv(statname,""); |
828 | laststype = OP_STAT; |
3028581b |
829 | return (laststatval = PerlLIO_fstat(PerlIO_fileno(IoIFP(io)), &statcache)); |
a687059c |
830 | } |
831 | else { |
748a9306 |
832 | if (tmpgv == defgv) |
57ebbfd0 |
833 | return laststatval; |
a687059c |
834 | if (dowarn) |
835 | warn("Stat on unopened file <%s>", |
748a9306 |
836 | GvENAME(tmpgv)); |
79072805 |
837 | statgv = Nullgv; |
838 | sv_setpv(statname,""); |
57ebbfd0 |
839 | return (laststatval = -1); |
a687059c |
840 | } |
841 | } |
842 | else { |
748a9306 |
843 | SV* sv = POPs; |
79072805 |
844 | PUTBACK; |
748a9306 |
845 | if (SvTYPE(sv) == SVt_PVGV) { |
846 | tmpgv = (GV*)sv; |
847 | goto do_fstat; |
848 | } |
849 | else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVGV) { |
850 | tmpgv = (GV*)SvRV(sv); |
851 | goto do_fstat; |
852 | } |
853 | |
79072805 |
854 | statgv = Nullgv; |
463ee0b2 |
855 | sv_setpv(statname,SvPV(sv, na)); |
79072805 |
856 | laststype = OP_STAT; |
3028581b |
857 | laststatval = PerlLIO_stat(SvPV(sv, na),&statcache); |
463ee0b2 |
858 | if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\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 | |