This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl5a5:pat/inherit.pat
[perl5.git] / doio.c
CommitLineData
79072805 1/* $RCSfile: doio.c,v $$Revision: 4.1 $$Date: 92/08/07 17:19:42 $
a687059c 2 *
6e21c824 3 * Copyright (c) 1991, 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
LW
7 *
8 * $Log: doio.c,v $
79072805
LW
9 * Revision 4.1 92/08/07 17:19:42 lwall
10 * Stage 6 Snapshot
11 *
faf8582f
LW
12 * Revision 4.0.1.6 92/06/11 21:08:16 lwall
13 * patch34: some systems don't declare h_errno extern in header files
14 *
bee1dbe2
LW
15 * Revision 4.0.1.5 92/06/08 13:00:21 lwall
16 * patch20: some machines don't define ENOTSOCK in errno.h
17 * patch20: new warnings for failed use of stat operators on filenames with \n
18 * patch20: wait failed when STDOUT or STDERR reopened to a pipe
19 * patch20: end of file latch not reset on reopen of STDIN
20 * patch20: seek(HANDLE, 0, 1) went to eof because of ancient Ultrix workaround
21 * patch20: fixed memory leak on system() for vfork() machines
22 * patch20: get*by* routines now return something useful in a scalar context
23 * patch20: h_errno now accessible via $?
24 *
99b89507
LW
25 * Revision 4.0.1.4 91/11/05 16:51:43 lwall
26 * patch11: prepared for ctype implementations that don't define isascii()
27 * patch11: perl mistook some streams for sockets because they return mode 0 too
28 * patch11: reopening STDIN, STDOUT and STDERR failed on some machines
29 * patch11: certain perl errors should set EBADF so that $! looks better
30 * patch11: truncate on a closed filehandle could dump
31 * patch11: stats of _ forgot whether prior stat was actually lstat
32 * patch11: -T returned true on NFS directory
33 *
1462b684
LW
34 * Revision 4.0.1.3 91/06/10 01:21:19 lwall
35 * patch10: read didn't work from character special files open for writing
36 * patch10: close-on-exec wrongly set on system file descriptors
37 *
6e21c824
LW
38 * Revision 4.0.1.2 91/06/07 10:53:39 lwall
39 * patch4: new copyright notice
40 * patch4: system fd's are now treated specially
41 * patch4: added $^F variable to specify maximum system fd, default 2
42 * patch4: character special files now opened with bidirectional stdio buffers
43 * patch4: taintchecks could improperly modify parent in vfork()
44 * patch4: many, many itty-bitty portability fixes
45 *
1c3d792e
LW
46 * Revision 4.0.1.1 91/04/11 17:41:06 lwall
47 * patch1: hopefully straightened out some of the Xenix mess
48 *
fe14fcc3
LW
49 * Revision 4.0 91/03/20 01:07:06 lwall
50 * 4.0 baseline.
a687059c
LW
51 *
52 */
53
54#include "EXTERN.h"
55#include "perl.h"
56
fe14fcc3 57#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 58#include <sys/ipc.h>
fe14fcc3 59#ifdef HAS_MSG
c2ab57d4 60#include <sys/msg.h>
e5d73d77 61#endif
fe14fcc3 62#ifdef HAS_SEM
c2ab57d4 63#include <sys/sem.h>
e5d73d77 64#endif
fe14fcc3 65#ifdef HAS_SHM
c2ab57d4
LW
66#include <sys/shm.h>
67#endif
e5d73d77 68#endif
c2ab57d4 69
663a0e37
LW
70#ifdef I_UTIME
71#include <utime.h>
72#endif
ff8e2863
LW
73#ifdef I_FCNTL
74#include <fcntl.h>
75#endif
fe14fcc3
LW
76#ifdef I_SYS_FILE
77#include <sys/file.h>
78#endif
a687059c
LW
79
80bool
79072805
LW
81do_open(gv,name,len)
82GV *gv;
a687059c 83register char *name;
79072805 84I32 len;
a687059c
LW
85{
86 FILE *fp;
79072805 87 register IO *io = GvIO(gv);
a687059c
LW
88 char *myname = savestr(name);
89 int result;
90 int fd;
91 int writing = 0;
92 char mode[3]; /* stdio file mode ("r\0" or "r+\0") */
6e21c824
LW
93 FILE *saveifp = Nullfp;
94 FILE *saveofp = Nullfp;
95 char savetype = ' ';
a687059c 96
bee1dbe2 97 mode[0] = mode[1] = mode[2] = '\0';
a687059c
LW
98 name = myname;
99 forkprocess = 1; /* assume true if no fork */
99b89507 100 while (len && isSPACE(name[len-1]))
a687059c 101 name[--len] = '\0';
79072805
LW
102 if (!io)
103 io = GvIO(gv) = newIO();
104 else if (io->ifp) {
105 fd = fileno(io->ifp);
106 if (io->type == '-')
c2ab57d4 107 result = 0;
6e21c824 108 else if (fd <= maxsysfd) {
79072805
LW
109 saveifp = io->ifp;
110 saveofp = io->ofp;
111 savetype = io->type;
6e21c824
LW
112 result = 0;
113 }
79072805
LW
114 else if (io->type == '|')
115 result = my_pclose(io->ifp);
116 else if (io->ifp != io->ofp) {
117 if (io->ofp) {
118 result = fclose(io->ofp);
119 fclose(io->ifp); /* clear stdio, fd already closed */
c2ab57d4
LW
120 }
121 else
79072805 122 result = fclose(io->ifp);
a687059c 123 }
a687059c 124 else
79072805 125 result = fclose(io->ifp);
6e21c824 126 if (result == EOF && fd > maxsysfd)
a687059c 127 fprintf(stderr,"Warning: unable to close filehandle %s properly.\n",
79072805
LW
128 GvENAME(gv));
129 io->ofp = io->ifp = Nullfp;
a687059c
LW
130 }
131 if (*name == '+' && len > 1 && name[len-1] != '|') { /* scary */
132 mode[1] = *name++;
133 mode[2] = '\0';
134 --len;
135 writing = 1;
136 }
137 else {
138 mode[1] = '\0';
139 }
79072805 140 io->type = *name;
a687059c 141 if (*name == '|') {
99b89507
LW
142 /*SUPPRESS 530*/
143 for (name++; isSPACE(*name); name++) ;
79072805
LW
144 if (strNE(name,"-"))
145 TAINT_ENV();
146 TAINT_PROPER("piped open");
147 fp = my_popen(name,"w");
a687059c
LW
148 writing = 1;
149 }
a687059c 150 else if (*name == '>') {
79072805 151 TAINT_PROPER("open");
bf38876a
LW
152 name++;
153 if (*name == '>') {
79072805 154 mode[0] = io->type = 'a';
bf38876a 155 name++;
a687059c 156 }
bf38876a 157 else
a687059c 158 mode[0] = 'w';
a687059c 159 writing = 1;
bf38876a
LW
160 if (*name == '&') {
161 duplicity:
162 name++;
99b89507 163 while (isSPACE(*name))
bf38876a 164 name++;
99b89507 165 if (isDIGIT(*name))
bf38876a
LW
166 fd = atoi(name);
167 else {
79072805
LW
168 gv = gv_fetchpv(name,FALSE);
169 if (!gv || !GvIO(gv)) {
6e21c824
LW
170#ifdef EINVAL
171 errno = EINVAL;
172#endif
173 goto say_false;
174 }
79072805
LW
175 if (GvIO(gv) && GvIO(gv)->ifp) {
176 fd = fileno(GvIO(gv)->ifp);
177 if (GvIO(gv)->type == 's')
178 io->type = 's';
bf38876a
LW
179 }
180 else
181 fd = -1;
182 }
fe14fcc3
LW
183 if (!(fp = fdopen(fd = dup(fd),mode))) {
184 close(fd);
185 }
bf38876a
LW
186 }
187 else {
99b89507 188 while (isSPACE(*name))
bf38876a
LW
189 name++;
190 if (strEQ(name,"-")) {
191 fp = stdout;
79072805 192 io->type = '-';
bf38876a
LW
193 }
194 else {
195 fp = fopen(name,mode);
196 }
197 }
a687059c
LW
198 }
199 else {
200 if (*name == '<') {
bf38876a
LW
201 mode[0] = 'r';
202 name++;
99b89507 203 while (isSPACE(*name))
bf38876a
LW
204 name++;
205 if (*name == '&')
206 goto duplicity;
a687059c
LW
207 if (strEQ(name,"-")) {
208 fp = stdin;
79072805 209 io->type = '-';
a687059c 210 }
bf38876a 211 else
a687059c 212 fp = fopen(name,mode);
a687059c
LW
213 }
214 else if (name[len-1] == '|') {
a687059c 215 name[--len] = '\0';
99b89507 216 while (len && isSPACE(name[len-1]))
a687059c 217 name[--len] = '\0';
99b89507
LW
218 /*SUPPRESS 530*/
219 for (; isSPACE(*name); name++) ;
79072805
LW
220 if (strNE(name,"-"))
221 TAINT_ENV();
222 TAINT_PROPER("piped open");
223 fp = my_popen(name,"r");
224 io->type = '|';
a687059c
LW
225 }
226 else {
79072805 227 io->type = '<';
99b89507
LW
228 /*SUPPRESS 530*/
229 for (; isSPACE(*name); name++) ;
a687059c
LW
230 if (strEQ(name,"-")) {
231 fp = stdin;
79072805 232 io->type = '-';
a687059c
LW
233 }
234 else
235 fp = fopen(name,"r");
236 }
237 }
bee1dbe2 238 if (!fp) {
93a17b20 239 if (dowarn && io->type == '<' && strchr(name, '\n'))
bee1dbe2
LW
240 warn(warn_nl, "open");
241 Safefree(myname);
6e21c824 242 goto say_false;
bee1dbe2
LW
243 }
244 Safefree(myname);
79072805
LW
245 if (io->type &&
246 io->type != '|' && io->type != '-') {
a687059c
LW
247 if (fstat(fileno(fp),&statbuf) < 0) {
248 (void)fclose(fp);
6e21c824 249 goto say_false;
a687059c 250 }
1462b684 251 if (S_ISSOCK(statbuf.st_mode))
79072805 252 io->type = 's'; /* in case a socket was passed in to us */
99b89507
LW
253#ifdef HAS_SOCKET
254 else if (
c623bd54 255#ifdef S_IFMT
99b89507
LW
256 !(statbuf.st_mode & S_IFMT)
257#else
258 !statbuf.st_mode
259#endif
260 ) {
79072805 261 I32 buflen = sizeof tokenbuf;
bee1dbe2
LW
262 if (getsockname(fileno(fp), tokenbuf, &buflen) >= 0
263 || errno != ENOTSOCK)
79072805 264 io->type = 's'; /* some OS's return 0 on fstat()ed socket */
99b89507
LW
265 /* but some return 0 for streams too, sigh */
266 }
bf38876a 267#endif
a687059c 268 }
6e21c824
LW
269 if (saveifp) { /* must use old fp? */
270 fd = fileno(saveifp);
271 if (saveofp) {
272 fflush(saveofp); /* emulate fclose() */
273 if (saveofp != saveifp) { /* was a socket? */
274 fclose(saveofp);
99b89507
LW
275 if (fd > 2)
276 Safefree(saveofp);
6e21c824
LW
277 }
278 }
279 if (fd != fileno(fp)) {
bee1dbe2 280 int pid;
79072805 281 SV *sv;
bee1dbe2 282
6e21c824 283 dup2(fileno(fp), fd);
79072805
LW
284 sv = *av_fetch(fdpid,fileno(fp),TRUE);
285 SvUPGRADE(sv, SVt_IV);
463ee0b2
LW
286 pid = SvIVX(sv);
287 SvIVX(sv) = 0;
79072805
LW
288 sv = *av_fetch(fdpid,fd,TRUE);
289 SvUPGRADE(sv, SVt_IV);
463ee0b2 290 SvIVX(sv) = pid;
6e21c824 291 fclose(fp);
bee1dbe2 292
6e21c824
LW
293 }
294 fp = saveifp;
bee1dbe2 295 clearerr(fp);
6e21c824 296 }
79072805 297#if defined(HAS_FCNTL) && defined(FFt_SETFD)
1462b684 298 fd = fileno(fp);
79072805 299 fcntl(fd,FFt_SETFD,fd > maxsysfd);
1462b684 300#endif
79072805 301 io->ifp = fp;
bf38876a 302 if (writing) {
79072805
LW
303 if (io->type == 's'
304 || (io->type == '>' && S_ISCHR(statbuf.st_mode)) ) {
305 if (!(io->ofp = fdopen(fileno(fp),"w"))) {
fe14fcc3 306 fclose(fp);
79072805 307 io->ifp = Nullfp;
6e21c824 308 goto say_false;
fe14fcc3 309 }
1462b684
LW
310 }
311 else
79072805 312 io->ofp = fp;
bf38876a 313 }
a687059c 314 return TRUE;
6e21c824
LW
315
316say_false:
79072805
LW
317 io->ifp = saveifp;
318 io->ofp = saveofp;
319 io->type = savetype;
6e21c824 320 return FALSE;
a687059c
LW
321}
322
323FILE *
79072805
LW
324nextargv(gv)
325register GV *gv;
a687059c 326{
79072805 327 register SV *sv;
99b89507 328#ifndef FLEXFILENAMES
c623bd54
LW
329 int filedev;
330 int fileino;
99b89507 331#endif
c623bd54
LW
332 int fileuid;
333 int filegid;
fe14fcc3 334
79072805
LW
335 if (!argvoutgv)
336 argvoutgv = gv_fetchpv("ARGVOUT",TRUE);
fe14fcc3 337 if (filemode & (S_ISUID|S_ISGID)) {
79072805 338 fflush(GvIO(argvoutgv)->ifp); /* chmod must follow last write */
fe14fcc3
LW
339#ifdef HAS_FCHMOD
340 (void)fchmod(lastfd,filemode);
341#else
342 (void)chmod(oldname,filemode);
343#endif
344 }
345 filemode = 0;
79072805 346 while (av_len(GvAV(gv)) >= 0) {
463ee0b2 347 STRLEN len;
79072805
LW
348 sv = av_shift(GvAV(gv));
349 sv_setsv(GvSV(gv),sv);
350 SvSETMAGIC(GvSV(gv));
463ee0b2
LW
351 oldname = SvPVx(GvSV(gv), len);
352 if (do_open(gv,oldname,len)) {
a687059c 353 if (inplace) {
79072805 354 TAINT_PROPER("inplace open");
c623bd54 355 if (strEQ(oldname,"-")) {
79072805
LW
356 sv_free(sv);
357 defoutgv = gv_fetchpv("STDOUT",TRUE);
358 return GvIO(gv)->ifp;
c623bd54 359 }
99b89507 360#ifndef FLEXFILENAMES
c623bd54
LW
361 filedev = statbuf.st_dev;
362 fileino = statbuf.st_ino;
99b89507 363#endif
a687059c
LW
364 filemode = statbuf.st_mode;
365 fileuid = statbuf.st_uid;
366 filegid = statbuf.st_gid;
c623bd54
LW
367 if (!S_ISREG(filemode)) {
368 warn("Can't do inplace edit: %s is not a regular file",
369 oldname );
79072805
LW
370 do_close(gv,FALSE);
371 sv_free(sv);
c623bd54
LW
372 continue;
373 }
a687059c 374 if (*inplace) {
ff8e2863 375#ifdef SUFFIX
79072805 376 add_suffix(sv,inplace);
ff8e2863 377#else
79072805 378 sv_catpv(sv,inplace);
ff8e2863 379#endif
c623bd54 380#ifndef FLEXFILENAMES
463ee0b2 381 if (stat(SvPVX(sv),&statbuf) >= 0
c623bd54
LW
382 && statbuf.st_dev == filedev
383 && statbuf.st_ino == fileino ) {
384 warn("Can't do inplace edit: %s > 14 characters",
463ee0b2 385 SvPVX(sv) );
79072805
LW
386 do_close(gv,FALSE);
387 sv_free(sv);
c623bd54
LW
388 continue;
389 }
390#endif
fe14fcc3 391#ifdef HAS_RENAME
bee1dbe2 392#ifndef DOSISH
463ee0b2 393 if (rename(oldname,SvPVX(sv)) < 0) {
c623bd54 394 warn("Can't rename %s to %s: %s, skipping file",
463ee0b2 395 oldname, SvPVX(sv), strerror(errno) );
79072805
LW
396 do_close(gv,FALSE);
397 sv_free(sv);
c623bd54
LW
398 continue;
399 }
a687059c 400#else
79072805 401 do_close(gv,FALSE);
463ee0b2
LW
402 (void)unlink(SvPVX(sv));
403 (void)rename(oldname,SvPVX(sv));
404 do_open(gv,SvPVX(sv),SvCUR(GvSV(gv)));
ff8e2863
LW
405#endif /* MSDOS */
406#else
463ee0b2
LW
407 (void)UNLINK(SvPVX(sv));
408 if (link(oldname,SvPVX(sv)) < 0) {
c623bd54 409 warn("Can't rename %s to %s: %s, skipping file",
463ee0b2 410 oldname, SvPVX(sv), strerror(errno) );
79072805
LW
411 do_close(gv,FALSE);
412 sv_free(sv);
c623bd54
LW
413 continue;
414 }
a687059c
LW
415 (void)UNLINK(oldname);
416#endif
417 }
418 else {
bee1dbe2 419#ifndef DOSISH
fe14fcc3
LW
420 if (UNLINK(oldname) < 0) {
421 warn("Can't rename %s to %s: %s, skipping file",
463ee0b2 422 oldname, SvPVX(sv), strerror(errno) );
79072805
LW
423 do_close(gv,FALSE);
424 sv_free(sv);
fe14fcc3
LW
425 continue;
426 }
ff8e2863 427#else
463ee0b2 428 croak("Can't do inplace edit without backup");
ff8e2863 429#endif
a687059c
LW
430 }
431
79072805
LW
432 sv_setpvn(sv,">",1);
433 sv_catpv(sv,oldname);
a687059c 434 errno = 0; /* in case sprintf set errno */
463ee0b2 435 if (!do_open(argvoutgv,SvPVX(sv),SvCUR(sv))) {
c623bd54
LW
436 warn("Can't do inplace edit on %s: %s",
437 oldname, strerror(errno) );
79072805
LW
438 do_close(gv,FALSE);
439 sv_free(sv);
fe14fcc3
LW
440 continue;
441 }
79072805
LW
442 defoutgv = argvoutgv;
443 lastfd = fileno(GvIO(argvoutgv)->ifp);
fe14fcc3
LW
444 (void)fstat(lastfd,&statbuf);
445#ifdef HAS_FCHMOD
446 (void)fchmod(lastfd,filemode);
a687059c
LW
447#else
448 (void)chmod(oldname,filemode);
449#endif
fe14fcc3
LW
450 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
451#ifdef HAS_FCHOWN
452 (void)fchown(lastfd,fileuid,filegid);
a687059c 453#else
fe14fcc3
LW
454#ifdef HAS_CHOWN
455 (void)chown(oldname,fileuid,filegid);
a687059c 456#endif
b1248f16 457#endif
fe14fcc3 458 }
a687059c 459 }
79072805
LW
460 sv_free(sv);
461 return GvIO(gv)->ifp;
a687059c
LW
462 }
463 else
463ee0b2 464 fprintf(stderr,"Can't open %s: %s\n",SvPV(sv, na), strerror(errno));
79072805 465 sv_free(sv);
a687059c
LW
466 }
467 if (inplace) {
79072805
LW
468 (void)do_close(argvoutgv,FALSE);
469 defoutgv = gv_fetchpv("STDOUT",TRUE);
a687059c
LW
470 }
471 return Nullfp;
472}
473
fe14fcc3 474#ifdef HAS_PIPE
afd9f252 475void
79072805
LW
476do_pipe(sv, rgv, wgv)
477SV *sv;
478GV *rgv;
479GV *wgv;
afd9f252 480{
79072805
LW
481 register IO *rstio;
482 register IO *wstio;
afd9f252
LW
483 int fd[2];
484
79072805 485 if (!rgv)
afd9f252 486 goto badexit;
79072805 487 if (!wgv)
afd9f252
LW
488 goto badexit;
489
79072805
LW
490 rstio = GvIO(rgv);
491 wstio = GvIO(wgv);
afd9f252
LW
492
493 if (!rstio)
79072805 494 rstio = GvIO(rgv) = newIO();
afd9f252 495 else if (rstio->ifp)
79072805 496 do_close(rgv,FALSE);
afd9f252 497 if (!wstio)
79072805 498 wstio = GvIO(wgv) = newIO();
afd9f252 499 else if (wstio->ifp)
79072805 500 do_close(wgv,FALSE);
afd9f252
LW
501
502 if (pipe(fd) < 0)
503 goto badexit;
504 rstio->ifp = fdopen(fd[0], "r");
505 wstio->ofp = fdopen(fd[1], "w");
506 wstio->ifp = wstio->ofp;
507 rstio->type = '<';
508 wstio->type = '>';
fe14fcc3
LW
509 if (!rstio->ifp || !wstio->ofp) {
510 if (rstio->ifp) fclose(rstio->ifp);
511 else close(fd[0]);
512 if (wstio->ofp) fclose(wstio->ofp);
513 else close(fd[1]);
514 goto badexit;
515 }
afd9f252 516
79072805 517 sv_setsv(sv,&sv_yes);
afd9f252
LW
518 return;
519
520badexit:
79072805 521 sv_setsv(sv,&sv_undef);
afd9f252
LW
522 return;
523}
b1248f16 524#endif
afd9f252 525
a687059c 526bool
79072805
LW
527do_close(gv,explicit)
528GV *gv;
a687059c
LW
529bool explicit;
530{
531 bool retval = FALSE;
79072805 532 register IO *io;
a687059c
LW
533 int status;
534
79072805
LW
535 if (!gv)
536 gv = argvgv;
537 if (!gv) {
99b89507 538 errno = EBADF;
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 }
79072805
LW
547 if (io->ifp) {
548 if (io->type == '|') {
549 status = my_pclose(io->ifp);
c623bd54 550 retval = (status == 0);
b1248f16 551 statusvalue = (unsigned short)status & 0xffff;
a687059c 552 }
79072805 553 else if (io->type == '-')
a687059c
LW
554 retval = TRUE;
555 else {
79072805
LW
556 if (io->ofp && io->ofp != io->ifp) { /* a socket */
557 retval = (fclose(io->ofp) != EOF);
558 fclose(io->ifp); /* clear stdio, fd already closed */
c2ab57d4
LW
559 }
560 else
79072805 561 retval = (fclose(io->ifp) != EOF);
a687059c 562 }
79072805
LW
563 io->ofp = io->ifp = Nullfp;
564 }
565 if (explicit) {
566 io->lines = 0;
567 io->page = 0;
568 io->lines_left = io->page_len;
a687059c 569 }
79072805 570 io->type = ' ';
a687059c
LW
571 return retval;
572}
573
574bool
79072805
LW
575do_eof(gv)
576GV *gv;
a687059c 577{
79072805 578 register IO *io;
a687059c
LW
579 int ch;
580
79072805 581 io = GvIO(gv);
a687059c 582
79072805 583 if (!io)
a687059c
LW
584 return TRUE;
585
79072805 586 while (io->ifp) {
a687059c
LW
587
588#ifdef STDSTDIO /* (the code works without this) */
79072805 589 if (io->ifp->_cnt > 0) /* cheat a little, since */
a687059c
LW
590 return FALSE; /* this is the most usual case */
591#endif
592
79072805 593 ch = getc(io->ifp);
a687059c 594 if (ch != EOF) {
79072805 595 (void)ungetc(ch, io->ifp);
a687059c
LW
596 return FALSE;
597 }
fe14fcc3 598#ifdef STDSTDIO
79072805
LW
599 if (io->ifp->_cnt < -1)
600 io->ifp->_cnt = -1;
fe14fcc3 601#endif
79072805
LW
602 if (gv == argvgv) { /* not necessarily a real EOF yet? */
603 if (!nextargv(argvgv)) /* get another fp handy */
a687059c
LW
604 return TRUE;
605 }
606 else
607 return TRUE; /* normal fp, definitely end of file */
608 }
609 return TRUE;
610}
611
612long
79072805
LW
613do_tell(gv)
614GV *gv;
a687059c 615{
79072805 616 register IO *io;
a687059c 617
79072805 618 if (!gv)
a687059c
LW
619 goto phooey;
620
79072805
LW
621 io = GvIO(gv);
622 if (!io || !io->ifp)
a687059c
LW
623 goto phooey;
624
bee1dbe2 625#ifdef ULTRIX_STDIO_BOTCH
79072805
LW
626 if (feof(io->ifp))
627 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 628#endif
a687059c 629
79072805 630 return ftell(io->ifp);
a687059c
LW
631
632phooey:
633 if (dowarn)
634 warn("tell() on unopened file");
99b89507 635 errno = EBADF;
a687059c
LW
636 return -1L;
637}
638
639bool
79072805
LW
640do_seek(gv, pos, whence)
641GV *gv;
a687059c
LW
642long pos;
643int whence;
644{
79072805 645 register IO *io;
a687059c 646
79072805 647 if (!gv)
a687059c
LW
648 goto nuts;
649
79072805
LW
650 io = GvIO(gv);
651 if (!io || !io->ifp)
a687059c
LW
652 goto nuts;
653
bee1dbe2 654#ifdef ULTRIX_STDIO_BOTCH
79072805
LW
655 if (feof(io->ifp))
656 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 657#endif
a687059c 658
79072805 659 return fseek(io->ifp, pos, whence) >= 0;
a687059c
LW
660
661nuts:
662 if (dowarn)
663 warn("seek() on unopened file");
99b89507 664 errno = EBADF;
a687059c
LW
665 return FALSE;
666}
667
79072805
LW
668I32
669do_ctl(optype,gv,func,argstr)
670I32 optype;
671GV *gv;
672I32 func;
673SV *argstr;
a687059c 674{
79072805 675 register IO *io;
a687059c 676 register char *s;
79072805 677 I32 retval;
a687059c 678
79072805 679 if (!gv || !argstr || !(io = GvIO(gv)) || !io->ifp) {
99b89507 680 errno = EBADF; /* well, sort of... */
a687059c 681 return -1;
99b89507 682 }
a687059c 683
79072805
LW
684 if (SvPOK(argstr) || !SvNIOK(argstr)) {
685 if (!SvPOK(argstr))
463ee0b2 686 s = SvPV(argstr, na);
a687059c
LW
687
688#ifdef IOCPARM_MASK
689#ifndef IOCPARM_LEN
690#define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
691#endif
692#endif
693#ifdef IOCPARM_LEN
694 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
695#else
696 retval = 256; /* otherwise guess at what's safe */
697#endif
79072805
LW
698 if (SvCUR(argstr) < retval) {
699 Sv_Grow(argstr,retval+1);
700 SvCUR_set(argstr, retval);
a687059c
LW
701 }
702
463ee0b2 703 s = SvPVX(argstr);
79072805 704 s[SvCUR(argstr)] = 17; /* a little sanity check here */
a687059c
LW
705 }
706 else {
463ee0b2 707 retval = SvIV(argstr);
bee1dbe2 708#ifdef DOSISH
c2ab57d4
LW
709 s = (char*)(long)retval; /* ouch */
710#else
a687059c 711 s = (char*)retval; /* ouch */
c2ab57d4 712#endif
a687059c
LW
713 }
714
715#ifndef lint
79072805
LW
716 if (optype == OP_IOCTL)
717 retval = ioctl(fileno(io->ifp), func, s);
a687059c 718 else
bee1dbe2 719#ifdef DOSISH
463ee0b2 720 croak("fcntl is not implemented");
57ebbfd0 721#else
fe14fcc3 722#ifdef HAS_FCNTL
79072805 723 retval = fcntl(fileno(io->ifp), func, s);
a687059c 724#else
463ee0b2 725 croak("fcntl is not implemented");
a687059c 726#endif
57ebbfd0 727#endif
a687059c
LW
728#else /* lint */
729 retval = 0;
730#endif /* lint */
731
79072805
LW
732 if (SvPOK(argstr)) {
733 if (s[SvCUR(argstr)] != 17)
463ee0b2 734 croak("Return value overflowed string");
79072805 735 s[SvCUR(argstr)] = 0; /* put our null back */
a687059c
LW
736 }
737 return retval;
738}
739
79072805 740#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(FFt_FREESP)
c2ab57d4 741 /* code courtesy of William Kucharski */
fe14fcc3 742#define HAS_CHSIZE
6eb13c3b 743
79072805
LW
744I32 chsize(fd, length)
745I32 fd; /* file descriptor */
6eb13c3b
LW
746off_t length; /* length to set file to */
747{
748 extern long lseek();
749 struct flock fl;
750 struct stat filebuf;
751
752 if (fstat(fd, &filebuf) < 0)
753 return -1;
754
755 if (filebuf.st_size < length) {
756
757 /* extend file length */
758
759 if ((lseek(fd, (length - 1), 0)) < 0)
760 return -1;
761
762 /* write a "0" byte */
763
764 if ((write(fd, "", 1)) != 1)
765 return -1;
766 }
767 else {
768 /* truncate length */
769
770 fl.l_whence = 0;
771 fl.l_len = 0;
772 fl.l_start = length;
79072805 773 fl.l_type = FFt_WRLCK; /* write lock on file space */
6eb13c3b
LW
774
775 /*
79072805 776 * This relies on the UNDOCUMENTED FFt_FREESP argument to
6eb13c3b
LW
777 * fcntl(2), which truncates the file so that it ends at the
778 * position indicated by fl.l_start.
779 *
780 * Will minor miracles never cease?
781 */
782
79072805 783 if (fcntl(fd, FFt_FREESP, &fl) < 0)
6eb13c3b
LW
784 return -1;
785
786 }
787
788 return 0;
789}
79072805 790#endif /* FFt_FREESP */
ff8e2863 791
79072805
LW
792I32
793looks_like_number(sv)
794SV *sv;
a687059c
LW
795{
796 register char *s;
797 register char *send;
798
463ee0b2
LW
799 if (!SvPOK(sv)) {
800 STRLEN len;
801 if (!SvPOKp(sv))
802 return TRUE;
803 s = SvPV(sv, len);
804 send = s + len;
805 }
806 else {
807 s = SvPVX(sv);
808 send = s + SvCUR(sv);
809 }
99b89507 810 while (isSPACE(*s))
a687059c
LW
811 s++;
812 if (s >= send)
813 return FALSE;
814 if (*s == '+' || *s == '-')
815 s++;
99b89507 816 while (isDIGIT(*s))
a687059c
LW
817 s++;
818 if (s == send)
819 return TRUE;
820 if (*s == '.')
821 s++;
463ee0b2 822 else if (s == SvPVX(sv))
a687059c 823 return FALSE;
99b89507 824 while (isDIGIT(*s))
a687059c
LW
825 s++;
826 if (s == send)
827 return TRUE;
828 if (*s == 'e' || *s == 'E') {
829 s++;
830 if (*s == '+' || *s == '-')
831 s++;
99b89507 832 while (isDIGIT(*s))
a687059c
LW
833 s++;
834 }
99b89507 835 while (isSPACE(*s))
a687059c
LW
836 s++;
837 if (s >= send)
838 return TRUE;
839 return FALSE;
840}
841
842bool
79072805
LW
843do_print(sv,fp)
844register SV *sv;
a687059c
LW
845FILE *fp;
846{
847 register char *tmps;
79072805 848 SV* tmpstr;
463ee0b2 849 STRLEN len;
a687059c 850
79072805
LW
851 /* assuming fp is checked earlier */
852 if (!sv)
853 return TRUE;
854 if (ofmt) {
855 if (SvMAGICAL(sv))
856 mg_get(sv);
463ee0b2
LW
857 if (SvIOK(sv) && SvIVX(sv) != 0) {
858 fprintf(fp, ofmt, (double)SvIVX(sv));
79072805
LW
859 return !ferror(fp);
860 }
463ee0b2 861 if ( (SvNOK(sv) && SvNVX(sv) != 0.0)
79072805 862 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
463ee0b2 863 fprintf(fp, ofmt, SvNVX(sv));
79072805
LW
864 return !ferror(fp);
865 }
a687059c 866 }
79072805
LW
867 switch (SvTYPE(sv)) {
868 case SVt_NULL:
ff8e2863 869 return TRUE;
79072805 870 case SVt_REF:
463ee0b2 871 fprintf(fp, "%s", sv_2pv(sv, &na));
ff8e2863 872 return !ferror(fp);
79072805
LW
873 case SVt_IV:
874 if (SvMAGICAL(sv))
875 mg_get(sv);
463ee0b2 876 fprintf(fp, "%d", SvIVX(sv));
79072805
LW
877 return !ferror(fp);
878 default:
463ee0b2 879 tmps = SvPV(sv, len);
79072805 880 break;
ff8e2863 881 }
463ee0b2 882 if (len && (fwrite(tmps,1,len,fp) == 0 || ferror(fp)))
a687059c 883 return FALSE;
79072805 884 return TRUE;
a687059c
LW
885}
886
79072805
LW
887I32
888my_stat(ARGS)
889dARGS
a687059c 890{
79072805
LW
891 dSP;
892 IO *io;
893
894 if (op->op_flags & OPf_SPECIAL) {
895 EXTEND(sp,1);
896 io = GvIO(cGVOP->op_gv);
897 if (io && io->ifp) {
898 statgv = cGVOP->op_gv;
899 sv_setpv(statname,"");
900 laststype = OP_STAT;
901 return (laststatval = fstat(fileno(io->ifp), &statcache));
a687059c
LW
902 }
903 else {
79072805 904 if (cGVOP->op_gv == defgv)
57ebbfd0 905 return laststatval;
a687059c
LW
906 if (dowarn)
907 warn("Stat on unopened file <%s>",
79072805
LW
908 GvENAME(cGVOP->op_gv));
909 statgv = Nullgv;
910 sv_setpv(statname,"");
57ebbfd0 911 return (laststatval = -1);
a687059c
LW
912 }
913 }
914 else {
79072805
LW
915 dPOPss;
916 PUTBACK;
917 statgv = Nullgv;
463ee0b2 918 sv_setpv(statname,SvPV(sv, na));
79072805 919 laststype = OP_STAT;
463ee0b2
LW
920 laststatval = stat(SvPV(sv, na),&statcache);
921 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
bee1dbe2
LW
922 warn(warn_nl, "stat");
923 return laststatval;
a687059c
LW
924 }
925}
926
79072805
LW
927I32
928my_lstat(ARGS)
929dARGS
c623bd54 930{
79072805
LW
931 dSP;
932 SV *sv;
933 if (op->op_flags & OPf_SPECIAL) {
934 EXTEND(sp,1);
935 if (cGVOP->op_gv == defgv) {
936 if (laststype != OP_LSTAT)
463ee0b2 937 croak("The stat preceding -l _ wasn't an lstat");
fe14fcc3
LW
938 return laststatval;
939 }
463ee0b2 940 croak("You can't use -l on a filehandle");
fe14fcc3 941 }
c623bd54 942
79072805
LW
943 laststype = OP_LSTAT;
944 statgv = Nullgv;
945 sv = POPs;
946 PUTBACK;
463ee0b2 947 sv_setpv(statname,SvPV(sv, na));
fe14fcc3 948#ifdef HAS_LSTAT
463ee0b2 949 laststatval = lstat(SvPV(sv, na),&statcache);
c623bd54 950#else
463ee0b2 951 laststatval = stat(SvPV(sv, na),&statcache);
c623bd54 952#endif
463ee0b2 953 if (laststatval < 0 && dowarn && strchr(SvPV(sv, na), '\n'))
bee1dbe2
LW
954 warn(warn_nl, "lstat");
955 return laststatval;
c623bd54
LW
956}
957
a687059c 958bool
79072805
LW
959do_aexec(really,mark,sp)
960SV *really;
961register SV **mark;
962register SV **sp;
a687059c 963{
a687059c 964 register char **a;
a687059c
LW
965 char *tmps;
966
79072805
LW
967 if (sp > mark) {
968 New(401,Argv, sp - mark + 1, char*);
bee1dbe2 969 a = Argv;
79072805
LW
970 while (++mark <= sp) {
971 if (*mark)
463ee0b2 972 *a++ = SvPVx(*mark, na);
a687059c
LW
973 else
974 *a++ = "";
975 }
976 *a = Nullch;
bee1dbe2 977 if (*Argv[0] != '/') /* will execvp use PATH? */
79072805 978 TAINT_ENV(); /* testing IFS here is overkill, probably */
463ee0b2 979 if (really && *(tmps = SvPV(really, na)))
bee1dbe2 980 execvp(tmps,Argv);
a687059c 981 else
bee1dbe2 982 execvp(Argv[0],Argv);
a687059c 983 }
bee1dbe2 984 do_execfree();
a687059c
LW
985 return FALSE;
986}
987
fe14fcc3 988void
ff8e2863
LW
989do_execfree()
990{
991 if (Argv) {
992 Safefree(Argv);
993 Argv = Null(char **);
994 }
995 if (Cmd) {
996 Safefree(Cmd);
997 Cmd = Nullch;
998 }
999}
1000
a687059c
LW
1001bool
1002do_exec(cmd)
1003char *cmd;
1004{
1005 register char **a;
1006 register char *s;
a687059c
LW
1007 char flags[10];
1008
a687059c
LW
1009 /* save an extra exec if possible */
1010
bf38876a
LW
1011#ifdef CSH
1012 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
a687059c 1013 strcpy(flags,"-c");
bf38876a 1014 s = cmd+cshlen+3;
a687059c
LW
1015 if (*s == 'f') {
1016 s++;
1017 strcat(flags,"f");
1018 }
1019 if (*s == ' ')
1020 s++;
1021 if (*s++ == '\'') {
1022 char *ncmd = s;
1023
1024 while (*s)
1025 s++;
1026 if (s[-1] == '\n')
1027 *--s = '\0';
1028 if (s[-1] == '\'') {
1029 *--s = '\0';
bf38876a 1030 execl(cshname,"csh", flags,ncmd,(char*)0);
a687059c
LW
1031 *s = '\'';
1032 return FALSE;
1033 }
1034 }
1035 }
bf38876a 1036#endif /* CSH */
a687059c
LW
1037
1038 /* see if there are shell metacharacters in it */
1039
99b89507
LW
1040 /*SUPPRESS 530*/
1041 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1
LW
1042 if (*s == '=')
1043 goto doshell;
a687059c 1044 for (s = cmd; *s; s++) {
93a17b20 1045 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1046 if (*s == '\n' && !s[1]) {
1047 *s = '\0';
1048 break;
1049 }
1050 doshell:
1051 execl("/bin/sh","sh","-c",cmd,(char*)0);
1052 return FALSE;
1053 }
1054 }
ff8e2863
LW
1055 New(402,Argv, (s - cmd) / 2 + 2, char*);
1056 Cmd = nsavestr(cmd, s-cmd);
1057 a = Argv;
1058 for (s = Cmd; *s;) {
99b89507 1059 while (*s && isSPACE(*s)) s++;
a687059c
LW
1060 if (*s)
1061 *(a++) = s;
99b89507 1062 while (*s && !isSPACE(*s)) s++;
a687059c
LW
1063 if (*s)
1064 *s++ = '\0';
1065 }
1066 *a = Nullch;
ff8e2863
LW
1067 if (Argv[0]) {
1068 execvp(Argv[0],Argv);
b1248f16 1069 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1070 do_execfree();
a687059c 1071 goto doshell;
b1248f16 1072 }
a687059c 1073 }
ff8e2863 1074 do_execfree();
a687059c
LW
1075 return FALSE;
1076}
1077
79072805
LW
1078I32
1079apply(type,mark,sp)
1080I32 type;
1081register SV **mark;
1082register SV **sp;
a687059c 1083{
79072805
LW
1084 register I32 val;
1085 register I32 val2;
1086 register I32 tot = 0;
a687059c 1087 char *s;
79072805 1088 SV **oldmark = mark;
a687059c 1089
463ee0b2
LW
1090 if (tainting) {
1091 while (++mark <= sp) {
1092 if (SvMAGICAL(*mark) && mg_find(*mark, 't'))
1093 tainted = TRUE;
1094 }
1095 mark = oldmark;
1096 }
a687059c 1097 switch (type) {
79072805
LW
1098 case OP_CHMOD:
1099 TAINT_PROPER("chmod");
1100 if (++mark <= sp) {
1101 tot = sp - mark;
463ee0b2 1102 val = SvIVx(*mark);
79072805 1103 while (++mark <= sp) {
463ee0b2 1104 if (chmod(SvPVx(*mark, na),val))
a687059c
LW
1105 tot--;
1106 }
1107 }
1108 break;
fe14fcc3 1109#ifdef HAS_CHOWN
79072805
LW
1110 case OP_CHOWN:
1111 TAINT_PROPER("chown");
1112 if (sp - mark > 2) {
1113 tot = sp - mark;
463ee0b2
LW
1114 val = SvIVx(*++mark);
1115 val2 = SvIVx(*++mark);
79072805 1116 while (++mark <= sp) {
463ee0b2 1117 if (chown(SvPVx(*mark, na),val,val2))
a687059c
LW
1118 tot--;
1119 }
1120 }
1121 break;
b1248f16 1122#endif
fe14fcc3 1123#ifdef HAS_KILL
79072805
LW
1124 case OP_KILL:
1125 TAINT_PROPER("kill");
463ee0b2 1126 s = SvPVx(*++mark, na);
79072805
LW
1127 tot = sp - mark;
1128 if (isUPPER(*s)) {
1129 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1130 s += 3;
1131 if (!(val = whichsig(s)))
463ee0b2 1132 croak("Unrecognized signal name \"%s\"",s);
79072805
LW
1133 }
1134 else
463ee0b2 1135 val = SvIVx(*mark);
79072805
LW
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
LW
1147 }
1148 else {
1149 while (++mark <= sp) {
463ee0b2 1150 if (kill(SvIVx(*mark),val))
79072805 1151 tot--;
a687059c
LW
1152 }
1153 }
1154 break;
b1248f16 1155#endif
79072805
LW
1156 case OP_UNLINK:
1157 TAINT_PROPER("unlink");
1158 tot = sp - mark;
1159 while (++mark <= sp) {
463ee0b2 1160 s = SvPVx(*mark, na);
a687059c
LW
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
c623bd54 1169 if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
a687059c 1170#endif
a687059c
LW
1171 tot--;
1172 else {
1173 if (UNLINK(s))
1174 tot--;
1175 }
1176 }
1177 }
1178 break;
79072805
LW
1179 case OP_UTIME:
1180 TAINT_PROPER("utime");
1181 if (sp - mark > 2) {
663a0e37
LW
1182#ifdef I_UTIME
1183 struct utimbuf utbuf;
1184#else
a687059c 1185 struct {
663a0e37
LW
1186 long actime;
1187 long modtime;
a687059c 1188 } utbuf;
663a0e37 1189#endif
a687059c 1190
afd9f252 1191 Zero(&utbuf, sizeof utbuf, char);
463ee0b2
LW
1192 utbuf.actime = SvIVx(*++mark); /* time accessed */
1193 utbuf.modtime = SvIVx(*++mark); /* time modified */
79072805
LW
1194 tot = sp - mark;
1195 while (++mark <= sp) {
463ee0b2 1196 if (utime(SvPVx(*mark, na),&utbuf))
a687059c
LW
1197 tot--;
1198 }
a687059c
LW
1199 }
1200 else
79072805 1201 tot = 0;
a687059c
LW
1202 break;
1203 }
1204 return tot;
1205}
1206
1207/* Do the permissions allow some operation? Assumes statcache already set. */
1208
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
LW
1261}
1262
79072805 1263I32
a687059c 1264ingroup(testgid,effective)
79072805
LW
1265I32 testgid;
1266I32 effective;
a687059c
LW
1267{
1268 if (testgid == (effective ? egid : gid))
1269 return TRUE;
fe14fcc3 1270#ifdef HAS_GETGROUPS
a687059c
LW
1271#ifndef NGROUPS
1272#define NGROUPS 32
1273#endif
1274 {
1c3d792e 1275 GROUPSTYPE gary[NGROUPS];
79072805 1276 I32 anum;
a687059c
LW
1277
1278 anum = getgroups(NGROUPS,gary);
1279 while (--anum >= 0)
1280 if (gary[anum] == testgid)
1281 return TRUE;
1282 }
1283#endif
1284 return FALSE;
1285}
c2ab57d4 1286
fe14fcc3 1287#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1288
79072805
LW
1289I32
1290do_ipcget(optype, mark, sp)
1291I32 optype;
1292SV **mark;
1293SV **sp;
c2ab57d4 1294{
c2ab57d4 1295 key_t key;
79072805 1296 I32 n, flags;
c2ab57d4 1297
463ee0b2
LW
1298 key = (key_t)SvNVx(*++mark);
1299 n = (optype == OP_MSGGET) ? 0 : SvIVx(*++mark);
1300 flags = SvIVx(*++mark);
c2ab57d4
LW
1301 errno = 0;
1302 switch (optype)
1303 {
fe14fcc3 1304#ifdef HAS_MSG
79072805 1305 case OP_MSGGET:
c2ab57d4 1306 return msgget(key, flags);
e5d73d77 1307#endif
fe14fcc3 1308#ifdef HAS_SEM
79072805 1309 case OP_SEMGET:
c2ab57d4 1310 return semget(key, n, flags);
e5d73d77 1311#endif
fe14fcc3 1312#ifdef HAS_SHM
79072805 1313 case OP_SHMGET:
c2ab57d4 1314 return shmget(key, n, flags);
e5d73d77 1315#endif
fe14fcc3 1316#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1317 default:
463ee0b2 1318 croak("%s not implemented", op_name[optype]);
e5d73d77 1319#endif
c2ab57d4
LW
1320 }
1321 return -1; /* should never happen */
1322}
1323
79072805
LW
1324I32
1325do_ipcctl(optype, mark, sp)
1326I32 optype;
1327SV **mark;
1328SV **sp;
c2ab57d4 1329{
79072805 1330 SV *astr;
c2ab57d4 1331 char *a;
79072805 1332 I32 id, n, cmd, infosize, getinfo, ret;
c2ab57d4 1333
463ee0b2
LW
1334 id = SvIVx(*++mark);
1335 n = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
1336 cmd = SvIVx(*++mark);
79072805 1337 astr = *++mark;
c2ab57d4
LW
1338 infosize = 0;
1339 getinfo = (cmd == IPC_STAT);
1340
1341 switch (optype)
1342 {
fe14fcc3 1343#ifdef HAS_MSG
79072805 1344 case OP_MSGCTL:
c2ab57d4
LW
1345 if (cmd == IPC_STAT || cmd == IPC_SET)
1346 infosize = sizeof(struct msqid_ds);
1347 break;
e5d73d77 1348#endif
fe14fcc3 1349#ifdef HAS_SHM
79072805 1350 case OP_SHMCTL:
c2ab57d4
LW
1351 if (cmd == IPC_STAT || cmd == IPC_SET)
1352 infosize = sizeof(struct shmid_ds);
1353 break;
e5d73d77 1354#endif
fe14fcc3 1355#ifdef HAS_SEM
79072805 1356 case OP_SEMCTL:
c2ab57d4
LW
1357 if (cmd == IPC_STAT || cmd == IPC_SET)
1358 infosize = sizeof(struct semid_ds);
1359 else if (cmd == GETALL || cmd == SETALL)
1360 {
1361 struct semid_ds semds;
1362 if (semctl(id, 0, IPC_STAT, &semds) == -1)
1363 return -1;
1364 getinfo = (cmd == GETALL);
6e21c824
LW
1365 infosize = semds.sem_nsems * sizeof(short);
1366 /* "short" is technically wrong but much more portable
1367 than guessing about u_?short(_t)? */
c2ab57d4
LW
1368 }
1369 break;
e5d73d77 1370#endif
fe14fcc3 1371#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1372 default:
463ee0b2 1373 croak("%s not implemented", op_name[optype]);
e5d73d77 1374#endif
c2ab57d4
LW
1375 }
1376
1377 if (infosize)
1378 {
1379 if (getinfo)
1380 {
463ee0b2
LW
1381 if (SvREADONLY(astr))
1382 croak("Can't %s to readonly var", op_name[optype]);
79072805 1383 SvGROW(astr, infosize+1);
463ee0b2 1384 a = SvPV(astr, na);
c2ab57d4
LW
1385 }
1386 else
1387 {
463ee0b2
LW
1388 STRLEN len;
1389 a = SvPV(astr, len);
1390 if (len != infosize)
1391 croak("Bad arg length for %s, is %d, should be %d",
1392 op_name[optype], len, infosize);
c2ab57d4
LW
1393 }
1394 }
1395 else
1396 {
463ee0b2 1397 I32 i = SvIV(astr);
c2ab57d4
LW
1398 a = (char *)i; /* ouch */
1399 }
1400 errno = 0;
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';
c2ab57d4
LW
1422 }
1423 return ret;
1424}
1425
79072805
LW
1426I32
1427do_msgsnd(mark, sp)
1428SV **mark;
1429SV **sp;
c2ab57d4 1430{
fe14fcc3 1431#ifdef HAS_MSG
79072805 1432 SV *mstr;
c2ab57d4 1433 char *mbuf;
79072805 1434 I32 id, msize, flags;
463ee0b2 1435 STRLEN len;
c2ab57d4 1436
463ee0b2 1437 id = SvIVx(*++mark);
79072805 1438 mstr = *++mark;
463ee0b2
LW
1439 flags = SvIVx(*++mark);
1440 mbuf = SvPV(mstr, len);
1441 if ((msize = len - sizeof(long)) < 0)
1442 croak("Arg too short for msgsnd");
c2ab57d4 1443 errno = 0;
bee1dbe2 1444 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77 1445#else
463ee0b2 1446 croak("msgsnd not implemented");
e5d73d77 1447#endif
c2ab57d4
LW
1448}
1449
79072805
LW
1450I32
1451do_msgrcv(mark, sp)
1452SV **mark;
1453SV **sp;
c2ab57d4 1454{
fe14fcc3 1455#ifdef HAS_MSG
79072805 1456 SV *mstr;
c2ab57d4
LW
1457 char *mbuf;
1458 long mtype;
79072805 1459 I32 id, msize, flags, ret;
463ee0b2 1460 STRLEN len;
79072805 1461
463ee0b2 1462 id = SvIVx(*++mark);
79072805 1463 mstr = *++mark;
463ee0b2
LW
1464 msize = SvIVx(*++mark);
1465 mtype = (long)SvIVx(*++mark);
1466 flags = SvIVx(*++mark);
1467 if (SvREADONLY(mstr))
1468 croak("Can't msgrcv to readonly var");
1469 mbuf = SvPV(mstr, len);
1470 if (len < sizeof(long)+msize+1) {
79072805 1471 SvGROW(mstr, sizeof(long)+msize+1);
463ee0b2 1472 mbuf = SvPV(mstr, len);
c2ab57d4
LW
1473 }
1474 errno = 0;
bee1dbe2 1475 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1476 if (ret >= 0) {
79072805
LW
1477 SvCUR_set(mstr, sizeof(long)+ret);
1478 *SvEND(mstr) = '\0';
c2ab57d4
LW
1479 }
1480 return ret;
e5d73d77 1481#else
463ee0b2 1482 croak("msgrcv not implemented");
e5d73d77 1483#endif
c2ab57d4
LW
1484}
1485
79072805
LW
1486I32
1487do_semop(mark, sp)
1488SV **mark;
1489SV **sp;
c2ab57d4 1490{
fe14fcc3 1491#ifdef HAS_SEM
79072805 1492 SV *opstr;
c2ab57d4 1493 char *opbuf;
463ee0b2
LW
1494 I32 id;
1495 STRLEN opsize;
c2ab57d4 1496
463ee0b2 1497 id = SvIVx(*++mark);
79072805 1498 opstr = *++mark;
463ee0b2 1499 opbuf = SvPV(opstr, opsize);
c2ab57d4
LW
1500 if (opsize < sizeof(struct sembuf)
1501 || (opsize % sizeof(struct sembuf)) != 0) {
1502 errno = EINVAL;
1503 return -1;
1504 }
1505 errno = 0;
6e21c824 1506 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77 1507#else
463ee0b2 1508 croak("semop not implemented");
e5d73d77 1509#endif
c2ab57d4
LW
1510}
1511
79072805
LW
1512I32
1513do_shmio(optype, mark, sp)
1514I32 optype;
1515SV **mark;
1516SV **sp;
c2ab57d4 1517{
fe14fcc3 1518#ifdef HAS_SHM
79072805 1519 SV *mstr;
c2ab57d4 1520 char *mbuf, *shm;
79072805 1521 I32 id, mpos, msize;
463ee0b2 1522 STRLEN len;
c2ab57d4 1523 struct shmid_ds shmds;
6e21c824 1524#ifndef VOIDSHMAT
c2ab57d4 1525 extern char *shmat();
6e21c824 1526#endif
c2ab57d4 1527
463ee0b2 1528 id = SvIVx(*++mark);
79072805 1529 mstr = *++mark;
463ee0b2
LW
1530 mpos = SvIVx(*++mark);
1531 msize = SvIVx(*++mark);
c2ab57d4
LW
1532 errno = 0;
1533 if (shmctl(id, IPC_STAT, &shmds) == -1)
1534 return -1;
1535 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1536 errno = EFAULT; /* can't do as caller requested */
1537 return -1;
1538 }
79072805 1539 shm = (char*)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;
463ee0b2 1542 mbuf = SvPV(mstr, len);
79072805 1543 if (optype == OP_SHMREAD) {
463ee0b2
LW
1544 if (SvREADONLY(mstr))
1545 croak("Can't shmread to readonly var");
1546 if (len < msize) {
79072805 1547 SvGROW(mstr, msize+1);
463ee0b2 1548 mbuf = SvPV(mstr, len);
c2ab57d4 1549 }
bee1dbe2 1550 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
1551 SvCUR_set(mstr, msize);
1552 *SvEND(mstr) = '\0';
c2ab57d4
LW
1553 }
1554 else {
79072805 1555 I32 n;
c2ab57d4 1556
463ee0b2 1557 if ((n = len) > msize)
c2ab57d4 1558 n = msize;
bee1dbe2 1559 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 1560 if (n < msize)
bee1dbe2 1561 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
1562 }
1563 return shmdt(shm);
e5d73d77 1564#else
463ee0b2 1565 croak("shm I/O not implemented");
e5d73d77 1566#endif
c2ab57d4
LW
1567}
1568
fe14fcc3 1569#endif /* SYSV IPC */