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