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