This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.0 alpha 3
[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);
286 pid = SvIV(sv);
287 SvIV(sv) = 0;
288 sv = *av_fetch(fdpid,fd,TRUE);
289 SvUPGRADE(sv, SVt_IV);
290 SvIV(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
LW
346 while (av_len(GvAV(gv)) >= 0) {
347 sv = av_shift(GvAV(gv));
348 sv_setsv(GvSV(gv),sv);
349 SvSETMAGIC(GvSV(gv));
350 oldname = SvPVnx(GvSV(gv));
351 if (do_open(gv,oldname,SvCUR(GvSV(gv)))) {
a687059c 352 if (inplace) {
79072805 353 TAINT_PROPER("inplace open");
c623bd54 354 if (strEQ(oldname,"-")) {
79072805
LW
355 sv_free(sv);
356 defoutgv = gv_fetchpv("STDOUT",TRUE);
357 return GvIO(gv)->ifp;
c623bd54 358 }
99b89507 359#ifndef FLEXFILENAMES
c623bd54
LW
360 filedev = statbuf.st_dev;
361 fileino = statbuf.st_ino;
99b89507 362#endif
a687059c
LW
363 filemode = statbuf.st_mode;
364 fileuid = statbuf.st_uid;
365 filegid = statbuf.st_gid;
c623bd54
LW
366 if (!S_ISREG(filemode)) {
367 warn("Can't do inplace edit: %s is not a regular file",
368 oldname );
79072805
LW
369 do_close(gv,FALSE);
370 sv_free(sv);
c623bd54
LW
371 continue;
372 }
a687059c 373 if (*inplace) {
ff8e2863 374#ifdef SUFFIX
79072805 375 add_suffix(sv,inplace);
ff8e2863 376#else
79072805 377 sv_catpv(sv,inplace);
ff8e2863 378#endif
c623bd54 379#ifndef FLEXFILENAMES
79072805 380 if (stat(SvPV(sv),&statbuf) >= 0
c623bd54
LW
381 && statbuf.st_dev == filedev
382 && statbuf.st_ino == fileino ) {
383 warn("Can't do inplace edit: %s > 14 characters",
79072805
LW
384 SvPV(sv) );
385 do_close(gv,FALSE);
386 sv_free(sv);
c623bd54
LW
387 continue;
388 }
389#endif
fe14fcc3 390#ifdef HAS_RENAME
bee1dbe2 391#ifndef DOSISH
79072805 392 if (rename(oldname,SvPV(sv)) < 0) {
c623bd54 393 warn("Can't rename %s to %s: %s, skipping file",
79072805
LW
394 oldname, SvPV(sv), strerror(errno) );
395 do_close(gv,FALSE);
396 sv_free(sv);
c623bd54
LW
397 continue;
398 }
a687059c 399#else
79072805
LW
400 do_close(gv,FALSE);
401 (void)unlink(SvPV(sv));
402 (void)rename(oldname,SvPV(sv));
403 do_open(gv,SvPV(sv),SvCUR(GvSV(gv)));
ff8e2863
LW
404#endif /* MSDOS */
405#else
79072805
LW
406 (void)UNLINK(SvPV(sv));
407 if (link(oldname,SvPV(sv)) < 0) {
c623bd54 408 warn("Can't rename %s to %s: %s, skipping file",
79072805
LW
409 oldname, SvPV(sv), strerror(errno) );
410 do_close(gv,FALSE);
411 sv_free(sv);
c623bd54
LW
412 continue;
413 }
a687059c
LW
414 (void)UNLINK(oldname);
415#endif
416 }
417 else {
bee1dbe2 418#ifndef DOSISH
fe14fcc3
LW
419 if (UNLINK(oldname) < 0) {
420 warn("Can't rename %s to %s: %s, skipping file",
79072805
LW
421 oldname, SvPV(sv), strerror(errno) );
422 do_close(gv,FALSE);
423 sv_free(sv);
fe14fcc3
LW
424 continue;
425 }
ff8e2863
LW
426#else
427 fatal("Can't do inplace edit without backup");
428#endif
a687059c
LW
429 }
430
79072805
LW
431 sv_setpvn(sv,">",1);
432 sv_catpv(sv,oldname);
a687059c 433 errno = 0; /* in case sprintf set errno */
79072805 434 if (!do_open(argvoutgv,SvPV(sv),SvCUR(sv))) {
c623bd54
LW
435 warn("Can't do inplace edit on %s: %s",
436 oldname, strerror(errno) );
79072805
LW
437 do_close(gv,FALSE);
438 sv_free(sv);
fe14fcc3
LW
439 continue;
440 }
79072805
LW
441 defoutgv = argvoutgv;
442 lastfd = fileno(GvIO(argvoutgv)->ifp);
fe14fcc3
LW
443 (void)fstat(lastfd,&statbuf);
444#ifdef HAS_FCHMOD
445 (void)fchmod(lastfd,filemode);
a687059c
LW
446#else
447 (void)chmod(oldname,filemode);
448#endif
fe14fcc3
LW
449 if (fileuid != statbuf.st_uid || filegid != statbuf.st_gid) {
450#ifdef HAS_FCHOWN
451 (void)fchown(lastfd,fileuid,filegid);
a687059c 452#else
fe14fcc3
LW
453#ifdef HAS_CHOWN
454 (void)chown(oldname,fileuid,filegid);
a687059c 455#endif
b1248f16 456#endif
fe14fcc3 457 }
a687059c 458 }
79072805
LW
459 sv_free(sv);
460 return GvIO(gv)->ifp;
a687059c
LW
461 }
462 else
79072805
LW
463 fprintf(stderr,"Can't open %s: %s\n",SvPVn(sv), strerror(errno));
464 sv_free(sv);
a687059c
LW
465 }
466 if (inplace) {
79072805
LW
467 (void)do_close(argvoutgv,FALSE);
468 defoutgv = gv_fetchpv("STDOUT",TRUE);
a687059c
LW
469 }
470 return Nullfp;
471}
472
fe14fcc3 473#ifdef HAS_PIPE
afd9f252 474void
79072805
LW
475do_pipe(sv, rgv, wgv)
476SV *sv;
477GV *rgv;
478GV *wgv;
afd9f252 479{
79072805
LW
480 register IO *rstio;
481 register IO *wstio;
afd9f252
LW
482 int fd[2];
483
79072805 484 if (!rgv)
afd9f252 485 goto badexit;
79072805 486 if (!wgv)
afd9f252
LW
487 goto badexit;
488
79072805
LW
489 rstio = GvIO(rgv);
490 wstio = GvIO(wgv);
afd9f252
LW
491
492 if (!rstio)
79072805 493 rstio = GvIO(rgv) = newIO();
afd9f252 494 else if (rstio->ifp)
79072805 495 do_close(rgv,FALSE);
afd9f252 496 if (!wstio)
79072805 497 wstio = GvIO(wgv) = newIO();
afd9f252 498 else if (wstio->ifp)
79072805 499 do_close(wgv,FALSE);
afd9f252
LW
500
501 if (pipe(fd) < 0)
502 goto badexit;
503 rstio->ifp = fdopen(fd[0], "r");
504 wstio->ofp = fdopen(fd[1], "w");
505 wstio->ifp = wstio->ofp;
506 rstio->type = '<';
507 wstio->type = '>';
fe14fcc3
LW
508 if (!rstio->ifp || !wstio->ofp) {
509 if (rstio->ifp) fclose(rstio->ifp);
510 else close(fd[0]);
511 if (wstio->ofp) fclose(wstio->ofp);
512 else close(fd[1]);
513 goto badexit;
514 }
afd9f252 515
79072805 516 sv_setsv(sv,&sv_yes);
afd9f252
LW
517 return;
518
519badexit:
79072805 520 sv_setsv(sv,&sv_undef);
afd9f252
LW
521 return;
522}
b1248f16 523#endif
afd9f252 524
a687059c 525bool
79072805
LW
526do_close(gv,explicit)
527GV *gv;
a687059c
LW
528bool explicit;
529{
530 bool retval = FALSE;
79072805 531 register IO *io;
a687059c
LW
532 int status;
533
79072805
LW
534 if (!gv)
535 gv = argvgv;
536 if (!gv) {
99b89507 537 errno = EBADF;
c2ab57d4 538 return FALSE;
99b89507 539 }
79072805
LW
540 io = GvIO(gv);
541 if (!io) { /* never opened */
a687059c 542 if (dowarn && explicit)
79072805 543 warn("Close on unopened file <%s>",GvENAME(gv));
a687059c
LW
544 return FALSE;
545 }
79072805
LW
546 if (io->ifp) {
547 if (io->type == '|') {
548 status = my_pclose(io->ifp);
c623bd54 549 retval = (status == 0);
b1248f16 550 statusvalue = (unsigned short)status & 0xffff;
a687059c 551 }
79072805 552 else if (io->type == '-')
a687059c
LW
553 retval = TRUE;
554 else {
79072805
LW
555 if (io->ofp && io->ofp != io->ifp) { /* a socket */
556 retval = (fclose(io->ofp) != EOF);
557 fclose(io->ifp); /* clear stdio, fd already closed */
c2ab57d4
LW
558 }
559 else
79072805 560 retval = (fclose(io->ifp) != EOF);
a687059c 561 }
79072805
LW
562 io->ofp = io->ifp = Nullfp;
563 }
564 if (explicit) {
565 io->lines = 0;
566 io->page = 0;
567 io->lines_left = io->page_len;
a687059c 568 }
79072805 569 io->type = ' ';
a687059c
LW
570 return retval;
571}
572
573bool
79072805
LW
574do_eof(gv)
575GV *gv;
a687059c 576{
79072805 577 register IO *io;
a687059c
LW
578 int ch;
579
79072805 580 io = GvIO(gv);
a687059c 581
79072805 582 if (!io)
a687059c
LW
583 return TRUE;
584
79072805 585 while (io->ifp) {
a687059c
LW
586
587#ifdef STDSTDIO /* (the code works without this) */
79072805 588 if (io->ifp->_cnt > 0) /* cheat a little, since */
a687059c
LW
589 return FALSE; /* this is the most usual case */
590#endif
591
79072805 592 ch = getc(io->ifp);
a687059c 593 if (ch != EOF) {
79072805 594 (void)ungetc(ch, io->ifp);
a687059c
LW
595 return FALSE;
596 }
fe14fcc3 597#ifdef STDSTDIO
79072805
LW
598 if (io->ifp->_cnt < -1)
599 io->ifp->_cnt = -1;
fe14fcc3 600#endif
79072805
LW
601 if (gv == argvgv) { /* not necessarily a real EOF yet? */
602 if (!nextargv(argvgv)) /* get another fp handy */
a687059c
LW
603 return TRUE;
604 }
605 else
606 return TRUE; /* normal fp, definitely end of file */
607 }
608 return TRUE;
609}
610
611long
79072805
LW
612do_tell(gv)
613GV *gv;
a687059c 614{
79072805 615 register IO *io;
a687059c 616
79072805 617 if (!gv)
a687059c
LW
618 goto phooey;
619
79072805
LW
620 io = GvIO(gv);
621 if (!io || !io->ifp)
a687059c
LW
622 goto phooey;
623
bee1dbe2 624#ifdef ULTRIX_STDIO_BOTCH
79072805
LW
625 if (feof(io->ifp))
626 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 627#endif
a687059c 628
79072805 629 return ftell(io->ifp);
a687059c
LW
630
631phooey:
632 if (dowarn)
633 warn("tell() on unopened file");
99b89507 634 errno = EBADF;
a687059c
LW
635 return -1L;
636}
637
638bool
79072805
LW
639do_seek(gv, pos, whence)
640GV *gv;
a687059c
LW
641long pos;
642int whence;
643{
79072805 644 register IO *io;
a687059c 645
79072805 646 if (!gv)
a687059c
LW
647 goto nuts;
648
79072805
LW
649 io = GvIO(gv);
650 if (!io || !io->ifp)
a687059c
LW
651 goto nuts;
652
bee1dbe2 653#ifdef ULTRIX_STDIO_BOTCH
79072805
LW
654 if (feof(io->ifp))
655 (void)fseek (io->ifp, 0L, 2); /* ultrix 1.2 workaround */
bee1dbe2 656#endif
a687059c 657
79072805 658 return fseek(io->ifp, pos, whence) >= 0;
a687059c
LW
659
660nuts:
661 if (dowarn)
662 warn("seek() on unopened file");
99b89507 663 errno = EBADF;
a687059c
LW
664 return FALSE;
665}
666
79072805
LW
667I32
668do_ctl(optype,gv,func,argstr)
669I32 optype;
670GV *gv;
671I32 func;
672SV *argstr;
a687059c 673{
79072805 674 register IO *io;
a687059c 675 register char *s;
79072805 676 I32 retval;
a687059c 677
79072805 678 if (!gv || !argstr || !(io = GvIO(gv)) || !io->ifp) {
99b89507 679 errno = EBADF; /* well, sort of... */
a687059c 680 return -1;
99b89507 681 }
a687059c 682
79072805
LW
683 if (SvPOK(argstr) || !SvNIOK(argstr)) {
684 if (!SvPOK(argstr))
685 s = SvPVn(argstr);
a687059c
LW
686
687#ifdef IOCPARM_MASK
688#ifndef IOCPARM_LEN
689#define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
690#endif
691#endif
692#ifdef IOCPARM_LEN
693 retval = IOCPARM_LEN(func); /* on BSDish systes we're safe */
694#else
695 retval = 256; /* otherwise guess at what's safe */
696#endif
79072805
LW
697 if (SvCUR(argstr) < retval) {
698 Sv_Grow(argstr,retval+1);
699 SvCUR_set(argstr, retval);
a687059c
LW
700 }
701
79072805
LW
702 s = SvPV(argstr);
703 s[SvCUR(argstr)] = 17; /* a little sanity check here */
a687059c
LW
704 }
705 else {
79072805 706 retval = SvIVn(argstr);
bee1dbe2 707#ifdef DOSISH
c2ab57d4
LW
708 s = (char*)(long)retval; /* ouch */
709#else
a687059c 710 s = (char*)retval; /* ouch */
c2ab57d4 711#endif
a687059c
LW
712 }
713
714#ifndef lint
79072805
LW
715 if (optype == OP_IOCTL)
716 retval = ioctl(fileno(io->ifp), func, s);
a687059c 717 else
bee1dbe2 718#ifdef DOSISH
57ebbfd0
LW
719 fatal("fcntl is not implemented");
720#else
fe14fcc3 721#ifdef HAS_FCNTL
79072805 722 retval = fcntl(fileno(io->ifp), func, s);
a687059c
LW
723#else
724 fatal("fcntl is not implemented");
725#endif
57ebbfd0 726#endif
a687059c
LW
727#else /* lint */
728 retval = 0;
729#endif /* lint */
730
79072805
LW
731 if (SvPOK(argstr)) {
732 if (s[SvCUR(argstr)] != 17)
a687059c 733 fatal("Return value overflowed string");
79072805 734 s[SvCUR(argstr)] = 0; /* put our null back */
a687059c
LW
735 }
736 return retval;
737}
738
79072805 739#if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE) && defined(FFt_FREESP)
c2ab57d4 740 /* code courtesy of William Kucharski */
fe14fcc3 741#define HAS_CHSIZE
6eb13c3b 742
79072805
LW
743I32 chsize(fd, length)
744I32 fd; /* file descriptor */
6eb13c3b
LW
745off_t length; /* length to set file to */
746{
747 extern long lseek();
748 struct flock fl;
749 struct stat filebuf;
750
751 if (fstat(fd, &filebuf) < 0)
752 return -1;
753
754 if (filebuf.st_size < length) {
755
756 /* extend file length */
757
758 if ((lseek(fd, (length - 1), 0)) < 0)
759 return -1;
760
761 /* write a "0" byte */
762
763 if ((write(fd, "", 1)) != 1)
764 return -1;
765 }
766 else {
767 /* truncate length */
768
769 fl.l_whence = 0;
770 fl.l_len = 0;
771 fl.l_start = length;
79072805 772 fl.l_type = FFt_WRLCK; /* write lock on file space */
6eb13c3b
LW
773
774 /*
79072805 775 * This relies on the UNDOCUMENTED FFt_FREESP argument to
6eb13c3b
LW
776 * fcntl(2), which truncates the file so that it ends at the
777 * position indicated by fl.l_start.
778 *
779 * Will minor miracles never cease?
780 */
781
79072805 782 if (fcntl(fd, FFt_FREESP, &fl) < 0)
6eb13c3b
LW
783 return -1;
784
785 }
786
787 return 0;
788}
79072805 789#endif /* FFt_FREESP */
ff8e2863 790
79072805
LW
791I32
792looks_like_number(sv)
793SV *sv;
a687059c
LW
794{
795 register char *s;
796 register char *send;
797
79072805 798 if (!SvPOK(sv))
a687059c 799 return TRUE;
79072805
LW
800 s = SvPV(sv);
801 send = s + SvCUR(sv);
99b89507 802 while (isSPACE(*s))
a687059c
LW
803 s++;
804 if (s >= send)
805 return FALSE;
806 if (*s == '+' || *s == '-')
807 s++;
99b89507 808 while (isDIGIT(*s))
a687059c
LW
809 s++;
810 if (s == send)
811 return TRUE;
812 if (*s == '.')
813 s++;
79072805 814 else if (s == SvPV(sv))
a687059c 815 return FALSE;
99b89507 816 while (isDIGIT(*s))
a687059c
LW
817 s++;
818 if (s == send)
819 return TRUE;
820 if (*s == 'e' || *s == 'E') {
821 s++;
822 if (*s == '+' || *s == '-')
823 s++;
99b89507 824 while (isDIGIT(*s))
a687059c
LW
825 s++;
826 }
99b89507 827 while (isSPACE(*s))
a687059c
LW
828 s++;
829 if (s >= send)
830 return TRUE;
831 return FALSE;
832}
833
834bool
79072805
LW
835do_print(sv,fp)
836register SV *sv;
a687059c
LW
837FILE *fp;
838{
839 register char *tmps;
79072805 840 SV* tmpstr;
a687059c 841
79072805
LW
842 /* assuming fp is checked earlier */
843 if (!sv)
844 return TRUE;
845 if (ofmt) {
846 if (SvMAGICAL(sv))
847 mg_get(sv);
848 if (SvIOK(sv) && SvIV(sv) != 0) {
849 fprintf(fp, ofmt, (double)SvIV(sv));
850 return !ferror(fp);
851 }
852 if ( (SvNOK(sv) && SvNV(sv) != 0.0)
853 || (looks_like_number(sv) && sv_2nv(sv) != 0.0) ) {
854 fprintf(fp, ofmt, SvNV(sv));
855 return !ferror(fp);
856 }
a687059c 857 }
79072805
LW
858 switch (SvTYPE(sv)) {
859 case SVt_NULL:
ff8e2863 860 return TRUE;
79072805
LW
861 case SVt_REF:
862 fprintf(fp, "%s", sv_2pv(sv));
ff8e2863 863 return !ferror(fp);
79072805
LW
864 case SVt_IV:
865 if (SvMAGICAL(sv))
866 mg_get(sv);
867 fprintf(fp, "%d", SvIV(sv));
868 return !ferror(fp);
869 default:
870 tmps = SvPVn(sv);
871 break;
ff8e2863 872 }
79072805 873 if (SvCUR(sv) && (fwrite(tmps,1,SvCUR(sv),fp) == 0 || ferror(fp)))
a687059c 874 return FALSE;
79072805 875 return TRUE;
a687059c
LW
876}
877
79072805
LW
878I32
879my_stat(ARGS)
880dARGS
a687059c 881{
79072805
LW
882 dSP;
883 IO *io;
884
885 if (op->op_flags & OPf_SPECIAL) {
886 EXTEND(sp,1);
887 io = GvIO(cGVOP->op_gv);
888 if (io && io->ifp) {
889 statgv = cGVOP->op_gv;
890 sv_setpv(statname,"");
891 laststype = OP_STAT;
892 return (laststatval = fstat(fileno(io->ifp), &statcache));
a687059c
LW
893 }
894 else {
79072805 895 if (cGVOP->op_gv == defgv)
57ebbfd0 896 return laststatval;
a687059c
LW
897 if (dowarn)
898 warn("Stat on unopened file <%s>",
79072805
LW
899 GvENAME(cGVOP->op_gv));
900 statgv = Nullgv;
901 sv_setpv(statname,"");
57ebbfd0 902 return (laststatval = -1);
a687059c
LW
903 }
904 }
905 else {
79072805
LW
906 dPOPss;
907 PUTBACK;
908 statgv = Nullgv;
909 sv_setpv(statname,SvPVn(sv));
910 laststype = OP_STAT;
911 laststatval = stat(SvPVn(sv),&statcache);
93a17b20 912 if (laststatval < 0 && dowarn && strchr(SvPVn(sv), '\n'))
bee1dbe2
LW
913 warn(warn_nl, "stat");
914 return laststatval;
a687059c
LW
915 }
916}
917
79072805
LW
918I32
919my_lstat(ARGS)
920dARGS
c623bd54 921{
79072805
LW
922 dSP;
923 SV *sv;
924 if (op->op_flags & OPf_SPECIAL) {
925 EXTEND(sp,1);
926 if (cGVOP->op_gv == defgv) {
927 if (laststype != OP_LSTAT)
fe14fcc3
LW
928 fatal("The stat preceding -l _ wasn't an lstat");
929 return laststatval;
930 }
931 fatal("You can't use -l on a filehandle");
932 }
c623bd54 933
79072805
LW
934 laststype = OP_LSTAT;
935 statgv = Nullgv;
936 sv = POPs;
937 PUTBACK;
938 sv_setpv(statname,SvPVn(sv));
fe14fcc3 939#ifdef HAS_LSTAT
79072805 940 laststatval = lstat(SvPVn(sv),&statcache);
c623bd54 941#else
79072805 942 laststatval = stat(SvPVn(sv),&statcache);
c623bd54 943#endif
93a17b20 944 if (laststatval < 0 && dowarn && strchr(SvPVn(sv), '\n'))
bee1dbe2
LW
945 warn(warn_nl, "lstat");
946 return laststatval;
c623bd54
LW
947}
948
a687059c 949bool
79072805
LW
950do_aexec(really,mark,sp)
951SV *really;
952register SV **mark;
953register SV **sp;
a687059c 954{
a687059c 955 register char **a;
a687059c
LW
956 char *tmps;
957
79072805
LW
958 if (sp > mark) {
959 New(401,Argv, sp - mark + 1, char*);
bee1dbe2 960 a = Argv;
79072805
LW
961 while (++mark <= sp) {
962 if (*mark)
963 *a++ = SvPVnx(*mark);
a687059c
LW
964 else
965 *a++ = "";
966 }
967 *a = Nullch;
bee1dbe2 968 if (*Argv[0] != '/') /* will execvp use PATH? */
79072805
LW
969 TAINT_ENV(); /* testing IFS here is overkill, probably */
970 if (really && *(tmps = SvPVn(really)))
bee1dbe2 971 execvp(tmps,Argv);
a687059c 972 else
bee1dbe2 973 execvp(Argv[0],Argv);
a687059c 974 }
bee1dbe2 975 do_execfree();
a687059c
LW
976 return FALSE;
977}
978
fe14fcc3 979void
ff8e2863
LW
980do_execfree()
981{
982 if (Argv) {
983 Safefree(Argv);
984 Argv = Null(char **);
985 }
986 if (Cmd) {
987 Safefree(Cmd);
988 Cmd = Nullch;
989 }
990}
991
a687059c
LW
992bool
993do_exec(cmd)
994char *cmd;
995{
996 register char **a;
997 register char *s;
a687059c
LW
998 char flags[10];
999
a687059c
LW
1000 /* save an extra exec if possible */
1001
bf38876a
LW
1002#ifdef CSH
1003 if (strnEQ(cmd,cshname,cshlen) && strnEQ(cmd+cshlen," -c",3)) {
a687059c 1004 strcpy(flags,"-c");
bf38876a 1005 s = cmd+cshlen+3;
a687059c
LW
1006 if (*s == 'f') {
1007 s++;
1008 strcat(flags,"f");
1009 }
1010 if (*s == ' ')
1011 s++;
1012 if (*s++ == '\'') {
1013 char *ncmd = s;
1014
1015 while (*s)
1016 s++;
1017 if (s[-1] == '\n')
1018 *--s = '\0';
1019 if (s[-1] == '\'') {
1020 *--s = '\0';
bf38876a 1021 execl(cshname,"csh", flags,ncmd,(char*)0);
a687059c
LW
1022 *s = '\'';
1023 return FALSE;
1024 }
1025 }
1026 }
bf38876a 1027#endif /* CSH */
a687059c
LW
1028
1029 /* see if there are shell metacharacters in it */
1030
99b89507
LW
1031 /*SUPPRESS 530*/
1032 for (s = cmd; *s && isALPHA(*s); s++) ; /* catch VAR=val gizmo */
63f2c1e1
LW
1033 if (*s == '=')
1034 goto doshell;
a687059c 1035 for (s = cmd; *s; s++) {
93a17b20 1036 if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
a687059c
LW
1037 if (*s == '\n' && !s[1]) {
1038 *s = '\0';
1039 break;
1040 }
1041 doshell:
1042 execl("/bin/sh","sh","-c",cmd,(char*)0);
1043 return FALSE;
1044 }
1045 }
ff8e2863
LW
1046 New(402,Argv, (s - cmd) / 2 + 2, char*);
1047 Cmd = nsavestr(cmd, s-cmd);
1048 a = Argv;
1049 for (s = Cmd; *s;) {
99b89507 1050 while (*s && isSPACE(*s)) s++;
a687059c
LW
1051 if (*s)
1052 *(a++) = s;
99b89507 1053 while (*s && !isSPACE(*s)) s++;
a687059c
LW
1054 if (*s)
1055 *s++ = '\0';
1056 }
1057 *a = Nullch;
ff8e2863
LW
1058 if (Argv[0]) {
1059 execvp(Argv[0],Argv);
b1248f16 1060 if (errno == ENOEXEC) { /* for system V NIH syndrome */
ff8e2863 1061 do_execfree();
a687059c 1062 goto doshell;
b1248f16 1063 }
a687059c 1064 }
ff8e2863 1065 do_execfree();
a687059c
LW
1066 return FALSE;
1067}
1068
79072805
LW
1069I32
1070apply(type,mark,sp)
1071I32 type;
1072register SV **mark;
1073register SV **sp;
a687059c 1074{
79072805
LW
1075 register I32 val;
1076 register I32 val2;
1077 register I32 tot = 0;
a687059c 1078 char *s;
79072805 1079 SV **oldmark = mark;
a687059c
LW
1080
1081#ifdef TAINT
79072805
LW
1082 while (++mark <= sp)
1083 TAINT_IF((*mark)->sv_tainted);
1084 mark = oldmark;
a687059c
LW
1085#endif
1086 switch (type) {
79072805
LW
1087 case OP_CHMOD:
1088 TAINT_PROPER("chmod");
1089 if (++mark <= sp) {
1090 tot = sp - mark;
1091 val = SvIVnx(*mark);
1092 while (++mark <= sp) {
1093 if (chmod(SvPVnx(*mark),val))
a687059c
LW
1094 tot--;
1095 }
1096 }
1097 break;
fe14fcc3 1098#ifdef HAS_CHOWN
79072805
LW
1099 case OP_CHOWN:
1100 TAINT_PROPER("chown");
1101 if (sp - mark > 2) {
1102 tot = sp - mark;
1103 val = SvIVnx(*++mark);
1104 val2 = SvIVnx(*++mark);
1105 while (++mark <= sp) {
1106 if (chown(SvPVnx(*mark),val,val2))
a687059c
LW
1107 tot--;
1108 }
1109 }
1110 break;
b1248f16 1111#endif
fe14fcc3 1112#ifdef HAS_KILL
79072805
LW
1113 case OP_KILL:
1114 TAINT_PROPER("kill");
1115 s = SvPVnx(*++mark);
1116 tot = sp - mark;
1117 if (isUPPER(*s)) {
1118 if (*s == 'S' && s[1] == 'I' && s[2] == 'G')
1119 s += 3;
1120 if (!(val = whichsig(s)))
1121 fatal("Unrecognized signal name \"%s\"",s);
1122 }
1123 else
1124 val = SvIVnx(*mark);
1125 if (val < 0) {
1126 val = -val;
1127 while (++mark <= sp) {
1128 I32 proc = SvIVnx(*mark);
fe14fcc3 1129#ifdef HAS_KILLPG
79072805 1130 if (killpg(proc,val)) /* BSD */
a687059c 1131#else
79072805 1132 if (kill(-proc,val)) /* SYSV */
a687059c 1133#endif
79072805 1134 tot--;
a687059c 1135 }
79072805
LW
1136 }
1137 else {
1138 while (++mark <= sp) {
1139 if (kill(SvIVnx(*mark),val))
1140 tot--;
a687059c
LW
1141 }
1142 }
1143 break;
b1248f16 1144#endif
79072805
LW
1145 case OP_UNLINK:
1146 TAINT_PROPER("unlink");
1147 tot = sp - mark;
1148 while (++mark <= sp) {
1149 s = SvPVnx(*mark);
a687059c
LW
1150 if (euid || unsafe) {
1151 if (UNLINK(s))
1152 tot--;
1153 }
1154 else { /* don't let root wipe out directories without -U */
fe14fcc3 1155#ifdef HAS_LSTAT
c623bd54 1156 if (lstat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
a687059c 1157#else
c623bd54 1158 if (stat(s,&statbuf) < 0 || S_ISDIR(statbuf.st_mode))
a687059c 1159#endif
a687059c
LW
1160 tot--;
1161 else {
1162 if (UNLINK(s))
1163 tot--;
1164 }
1165 }
1166 }
1167 break;
79072805
LW
1168 case OP_UTIME:
1169 TAINT_PROPER("utime");
1170 if (sp - mark > 2) {
663a0e37
LW
1171#ifdef I_UTIME
1172 struct utimbuf utbuf;
1173#else
a687059c 1174 struct {
663a0e37
LW
1175 long actime;
1176 long modtime;
a687059c 1177 } utbuf;
663a0e37 1178#endif
a687059c 1179
afd9f252 1180 Zero(&utbuf, sizeof utbuf, char);
79072805
LW
1181 utbuf.actime = SvIVnx(*++mark); /* time accessed */
1182 utbuf.modtime = SvIVnx(*++mark); /* time modified */
1183 tot = sp - mark;
1184 while (++mark <= sp) {
1185 if (utime(SvPVnx(*mark),&utbuf))
a687059c
LW
1186 tot--;
1187 }
a687059c
LW
1188 }
1189 else
79072805 1190 tot = 0;
a687059c
LW
1191 break;
1192 }
1193 return tot;
1194}
1195
1196/* Do the permissions allow some operation? Assumes statcache already set. */
1197
79072805 1198I32
a687059c 1199cando(bit, effective, statbufp)
79072805
LW
1200I32 bit;
1201I32 effective;
a687059c
LW
1202register struct stat *statbufp;
1203{
bee1dbe2 1204#ifdef DOSISH
fe14fcc3
LW
1205 /* [Comments and code from Len Reed]
1206 * MS-DOS "user" is similar to UNIX's "superuser," but can't write
1207 * to write-protected files. The execute permission bit is set
1208 * by the Miscrosoft C library stat() function for the following:
1209 * .exe files
1210 * .com files
1211 * .bat files
1212 * directories
1213 * All files and directories are readable.
1214 * Directories and special files, e.g. "CON", cannot be
1215 * write-protected.
1216 * [Comment by Tom Dinger -- a directory can have the write-protect
1217 * bit set in the file system, but DOS permits changes to
1218 * the directory anyway. In addition, all bets are off
1219 * here for networked software, such as Novell and
1220 * Sun's PC-NFS.]
1221 */
1222
bee1dbe2
LW
1223 /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
1224 * too so it will actually look into the files for magic numbers
1225 */
fe14fcc3
LW
1226 return (bit & statbufp->st_mode) ? TRUE : FALSE;
1227
1228#else /* ! MSDOS */
a687059c 1229 if ((effective ? euid : uid) == 0) { /* root is special */
c623bd54
LW
1230 if (bit == S_IXUSR) {
1231 if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
a687059c
LW
1232 return TRUE;
1233 }
1234 else
1235 return TRUE; /* root reads and writes anything */
1236 return FALSE;
1237 }
1238 if (statbufp->st_uid == (effective ? euid : uid) ) {
1239 if (statbufp->st_mode & bit)
1240 return TRUE; /* ok as "user" */
1241 }
79072805 1242 else if (ingroup((I32)statbufp->st_gid,effective)) {
a687059c
LW
1243 if (statbufp->st_mode & bit >> 3)
1244 return TRUE; /* ok as "group" */
1245 }
1246 else if (statbufp->st_mode & bit >> 6)
1247 return TRUE; /* ok as "other" */
1248 return FALSE;
fe14fcc3 1249#endif /* ! MSDOS */
a687059c
LW
1250}
1251
79072805 1252I32
a687059c 1253ingroup(testgid,effective)
79072805
LW
1254I32 testgid;
1255I32 effective;
a687059c
LW
1256{
1257 if (testgid == (effective ? egid : gid))
1258 return TRUE;
fe14fcc3 1259#ifdef HAS_GETGROUPS
a687059c
LW
1260#ifndef NGROUPS
1261#define NGROUPS 32
1262#endif
1263 {
1c3d792e 1264 GROUPSTYPE gary[NGROUPS];
79072805 1265 I32 anum;
a687059c
LW
1266
1267 anum = getgroups(NGROUPS,gary);
1268 while (--anum >= 0)
1269 if (gary[anum] == testgid)
1270 return TRUE;
1271 }
1272#endif
1273 return FALSE;
1274}
c2ab57d4 1275
fe14fcc3 1276#if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
c2ab57d4 1277
79072805
LW
1278I32
1279do_ipcget(optype, mark, sp)
1280I32 optype;
1281SV **mark;
1282SV **sp;
c2ab57d4 1283{
c2ab57d4 1284 key_t key;
79072805 1285 I32 n, flags;
c2ab57d4 1286
79072805
LW
1287 key = (key_t)SvNVnx(*++mark);
1288 n = (optype == OP_MSGGET) ? 0 : SvIVnx(*++mark);
1289 flags = SvIVnx(*++mark);
c2ab57d4
LW
1290 errno = 0;
1291 switch (optype)
1292 {
fe14fcc3 1293#ifdef HAS_MSG
79072805 1294 case OP_MSGGET:
c2ab57d4 1295 return msgget(key, flags);
e5d73d77 1296#endif
fe14fcc3 1297#ifdef HAS_SEM
79072805 1298 case OP_SEMGET:
c2ab57d4 1299 return semget(key, n, flags);
e5d73d77 1300#endif
fe14fcc3 1301#ifdef HAS_SHM
79072805 1302 case OP_SHMGET:
c2ab57d4 1303 return shmget(key, n, flags);
e5d73d77 1304#endif
fe14fcc3 1305#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1306 default:
79072805 1307 fatal("%s not implemented", op_name[optype]);
e5d73d77 1308#endif
c2ab57d4
LW
1309 }
1310 return -1; /* should never happen */
1311}
1312
79072805
LW
1313I32
1314do_ipcctl(optype, mark, sp)
1315I32 optype;
1316SV **mark;
1317SV **sp;
c2ab57d4 1318{
79072805 1319 SV *astr;
c2ab57d4 1320 char *a;
79072805 1321 I32 id, n, cmd, infosize, getinfo, ret;
c2ab57d4 1322
79072805
LW
1323 id = SvIVnx(*++mark);
1324 n = (optype == OP_SEMCTL) ? SvIVnx(*++mark) : 0;
1325 cmd = SvIVnx(*++mark);
1326 astr = *++mark;
c2ab57d4
LW
1327 infosize = 0;
1328 getinfo = (cmd == IPC_STAT);
1329
1330 switch (optype)
1331 {
fe14fcc3 1332#ifdef HAS_MSG
79072805 1333 case OP_MSGCTL:
c2ab57d4
LW
1334 if (cmd == IPC_STAT || cmd == IPC_SET)
1335 infosize = sizeof(struct msqid_ds);
1336 break;
e5d73d77 1337#endif
fe14fcc3 1338#ifdef HAS_SHM
79072805 1339 case OP_SHMCTL:
c2ab57d4
LW
1340 if (cmd == IPC_STAT || cmd == IPC_SET)
1341 infosize = sizeof(struct shmid_ds);
1342 break;
e5d73d77 1343#endif
fe14fcc3 1344#ifdef HAS_SEM
79072805 1345 case OP_SEMCTL:
c2ab57d4
LW
1346 if (cmd == IPC_STAT || cmd == IPC_SET)
1347 infosize = sizeof(struct semid_ds);
1348 else if (cmd == GETALL || cmd == SETALL)
1349 {
1350 struct semid_ds semds;
1351 if (semctl(id, 0, IPC_STAT, &semds) == -1)
1352 return -1;
1353 getinfo = (cmd == GETALL);
6e21c824
LW
1354 infosize = semds.sem_nsems * sizeof(short);
1355 /* "short" is technically wrong but much more portable
1356 than guessing about u_?short(_t)? */
c2ab57d4
LW
1357 }
1358 break;
e5d73d77 1359#endif
fe14fcc3 1360#if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
e5d73d77 1361 default:
79072805 1362 fatal("%s not implemented", op_name[optype]);
e5d73d77 1363#endif
c2ab57d4
LW
1364 }
1365
1366 if (infosize)
1367 {
1368 if (getinfo)
1369 {
79072805
LW
1370 SvGROW(astr, infosize+1);
1371 a = SvPVn(astr);
c2ab57d4
LW
1372 }
1373 else
1374 {
79072805
LW
1375 a = SvPVn(astr);
1376 if (SvCUR(astr) != infosize)
c2ab57d4
LW
1377 {
1378 errno = EINVAL;
1379 return -1;
1380 }
1381 }
1382 }
1383 else
1384 {
79072805 1385 I32 i = SvIVn(astr);
c2ab57d4
LW
1386 a = (char *)i; /* ouch */
1387 }
1388 errno = 0;
1389 switch (optype)
1390 {
fe14fcc3 1391#ifdef HAS_MSG
79072805 1392 case OP_MSGCTL:
bee1dbe2 1393 ret = msgctl(id, cmd, (struct msqid_ds *)a);
c2ab57d4 1394 break;
e5d73d77 1395#endif
fe14fcc3 1396#ifdef HAS_SEM
79072805
LW
1397 case OP_SEMCTL:
1398 ret = semctl(id, n, cmd, (struct semid_ds *)a);
c2ab57d4 1399 break;
e5d73d77 1400#endif
fe14fcc3 1401#ifdef HAS_SHM
79072805 1402 case OP_SHMCTL:
bee1dbe2 1403 ret = shmctl(id, cmd, (struct shmid_ds *)a);
c2ab57d4 1404 break;
e5d73d77 1405#endif
c2ab57d4
LW
1406 }
1407 if (getinfo && ret >= 0) {
79072805
LW
1408 SvCUR_set(astr, infosize);
1409 *SvEND(astr) = '\0';
c2ab57d4
LW
1410 }
1411 return ret;
1412}
1413
79072805
LW
1414I32
1415do_msgsnd(mark, sp)
1416SV **mark;
1417SV **sp;
c2ab57d4 1418{
fe14fcc3 1419#ifdef HAS_MSG
79072805 1420 SV *mstr;
c2ab57d4 1421 char *mbuf;
79072805 1422 I32 id, msize, flags;
c2ab57d4 1423
79072805
LW
1424 id = SvIVnx(*++mark);
1425 mstr = *++mark;
1426 flags = SvIVnx(*++mark);
1427 mbuf = SvPVn(mstr);
1428 if ((msize = SvCUR(mstr) - sizeof(long)) < 0) {
c2ab57d4
LW
1429 errno = EINVAL;
1430 return -1;
1431 }
1432 errno = 0;
bee1dbe2 1433 return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
e5d73d77
LW
1434#else
1435 fatal("msgsnd not implemented");
1436#endif
c2ab57d4
LW
1437}
1438
79072805
LW
1439I32
1440do_msgrcv(mark, sp)
1441SV **mark;
1442SV **sp;
c2ab57d4 1443{
fe14fcc3 1444#ifdef HAS_MSG
79072805 1445 SV *mstr;
c2ab57d4
LW
1446 char *mbuf;
1447 long mtype;
79072805
LW
1448 I32 id, msize, flags, ret;
1449
1450 id = SvIVnx(*++mark);
1451 mstr = *++mark;
1452 msize = SvIVnx(*++mark);
1453 mtype = (long)SvIVnx(*++mark);
1454 flags = SvIVnx(*++mark);
1455 mbuf = SvPVn(mstr);
1456 if (SvCUR(mstr) < sizeof(long)+msize+1) {
1457 SvGROW(mstr, sizeof(long)+msize+1);
1458 mbuf = SvPVn(mstr);
c2ab57d4
LW
1459 }
1460 errno = 0;
bee1dbe2 1461 ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
c2ab57d4 1462 if (ret >= 0) {
79072805
LW
1463 SvCUR_set(mstr, sizeof(long)+ret);
1464 *SvEND(mstr) = '\0';
c2ab57d4
LW
1465 }
1466 return ret;
e5d73d77
LW
1467#else
1468 fatal("msgrcv not implemented");
1469#endif
c2ab57d4
LW
1470}
1471
79072805
LW
1472I32
1473do_semop(mark, sp)
1474SV **mark;
1475SV **sp;
c2ab57d4 1476{
fe14fcc3 1477#ifdef HAS_SEM
79072805 1478 SV *opstr;
c2ab57d4 1479 char *opbuf;
79072805 1480 I32 id, opsize;
c2ab57d4 1481
79072805
LW
1482 id = SvIVnx(*++mark);
1483 opstr = *++mark;
1484 opbuf = SvPVn(opstr);
1485 opsize = SvCUR(opstr);
c2ab57d4
LW
1486 if (opsize < sizeof(struct sembuf)
1487 || (opsize % sizeof(struct sembuf)) != 0) {
1488 errno = EINVAL;
1489 return -1;
1490 }
1491 errno = 0;
6e21c824 1492 return semop(id, (struct sembuf *)opbuf, opsize/sizeof(struct sembuf));
e5d73d77
LW
1493#else
1494 fatal("semop not implemented");
1495#endif
c2ab57d4
LW
1496}
1497
79072805
LW
1498I32
1499do_shmio(optype, mark, sp)
1500I32 optype;
1501SV **mark;
1502SV **sp;
c2ab57d4 1503{
fe14fcc3 1504#ifdef HAS_SHM
79072805 1505 SV *mstr;
c2ab57d4 1506 char *mbuf, *shm;
79072805 1507 I32 id, mpos, msize;
c2ab57d4 1508 struct shmid_ds shmds;
6e21c824 1509#ifndef VOIDSHMAT
c2ab57d4 1510 extern char *shmat();
6e21c824 1511#endif
c2ab57d4 1512
79072805
LW
1513 id = SvIVnx(*++mark);
1514 mstr = *++mark;
1515 mpos = SvIVnx(*++mark);
1516 msize = SvIVnx(*++mark);
c2ab57d4
LW
1517 errno = 0;
1518 if (shmctl(id, IPC_STAT, &shmds) == -1)
1519 return -1;
1520 if (mpos < 0 || msize < 0 || mpos + msize > shmds.shm_segsz) {
1521 errno = EFAULT; /* can't do as caller requested */
1522 return -1;
1523 }
79072805 1524 shm = (char*)shmat(id, (char*)NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
c2ab57d4
LW
1525 if (shm == (char *)-1) /* I hate System V IPC, I really do */
1526 return -1;
79072805
LW
1527 mbuf = SvPVn(mstr);
1528 if (optype == OP_SHMREAD) {
1529 if (SvCUR(mstr) < msize) {
1530 SvGROW(mstr, msize+1);
1531 mbuf = SvPVn(mstr);
c2ab57d4 1532 }
bee1dbe2 1533 Copy(shm + mpos, mbuf, msize, char);
79072805
LW
1534 SvCUR_set(mstr, msize);
1535 *SvEND(mstr) = '\0';
c2ab57d4
LW
1536 }
1537 else {
79072805 1538 I32 n;
c2ab57d4 1539
79072805 1540 if ((n = SvCUR(mstr)) > msize)
c2ab57d4 1541 n = msize;
bee1dbe2 1542 Copy(mbuf, shm + mpos, n, char);
c2ab57d4 1543 if (n < msize)
bee1dbe2 1544 memzero(shm + mpos + n, msize - n);
c2ab57d4
LW
1545 }
1546 return shmdt(shm);
e5d73d77
LW
1547#else
1548 fatal("shm I/O not implemented");
1549#endif
c2ab57d4
LW
1550}
1551
fe14fcc3 1552#endif /* SYSV IPC */