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