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