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