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