This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use PERL_UNUSED_RESULT.
[perl5.git] / doio.c
1 /*    doio.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4  *    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  *  Far below them they saw the white waters pour into a foaming bowl, and
13  *  then swirl darkly about a deep oval basin in the rocks, until they found
14  *  their way out again through a narrow gate, and flowed away, fuming and
15  *  chattering, into calmer and more level reaches.
16  *
17  *     [p.684 of _The Lord of the Rings_, IV/vi: "The Forbidden Pool"]
18  */
19
20 /* This file contains functions that do the actual I/O on behalf of ops.
21  * For example, pp_print() calls the do_print() function in this file for
22  * each argument needing printing.
23  */
24
25 #include "EXTERN.h"
26 #define PERL_IN_DOIO_C
27 #include "perl.h"
28
29 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
30 #ifndef HAS_SEM
31 #include <sys/ipc.h>
32 #endif
33 #ifdef HAS_MSG
34 #include <sys/msg.h>
35 #endif
36 #ifdef HAS_SHM
37 #include <sys/shm.h>
38 # ifndef HAS_SHMAT_PROTOTYPE
39     extern Shmat_t shmat (int, char *, int);
40 # endif
41 #endif
42 #endif
43
44 #ifdef I_UTIME
45 #  if defined(_MSC_VER) || defined(__MINGW32__)
46 #    include <sys/utime.h>
47 #  else
48 #    include <utime.h>
49 #  endif
50 #endif
51
52 #ifdef O_EXCL
53 #  define OPEN_EXCL O_EXCL
54 #else
55 #  define OPEN_EXCL 0
56 #endif
57
58 #define PERL_MODE_MAX 8
59 #define PERL_FLAGS_MAX 10
60
61 #include <signal.h>
62
63 static IO *
64 S_openn_setup(pTHX_ GV *gv, char *mode, PerlIO **saveifp, PerlIO **saveofp,
65               int *savefd,  char *savetype)
66 {
67     dVAR;
68     IO * const io = GvIOn(gv);
69
70     PERL_ARGS_ASSERT_OPENN_SETUP;
71
72     *saveifp = NULL;
73     *saveofp = NULL;
74     *savefd = -1;
75     *savetype = IoTYPE_CLOSED;
76
77     Zero(mode,sizeof(mode),char);
78     PL_forkprocess = 1;         /* assume true if no fork */
79
80     /* If currently open - close before we re-open */
81     if (IoIFP(io)) {
82         if (IoTYPE(io) == IoTYPE_STD) {
83             /* This is a clone of one of STD* handles */
84         }
85         else {
86             const int old_fd = PerlIO_fileno(IoIFP(io));
87
88             if (old_fd >= 0 && old_fd <= PL_maxsysfd) {
89                 /* This is one of the original STD* handles */
90                 *saveifp  = IoIFP(io);
91                 *saveofp  = IoOFP(io);
92                 *savetype = IoTYPE(io);
93                 *savefd   = old_fd;
94             }
95             else {
96                 int result;
97
98                 if (IoTYPE(io) == IoTYPE_PIPE)
99                     result = PerlProc_pclose(IoIFP(io));
100                 else if (IoIFP(io) != IoOFP(io)) {
101                     if (IoOFP(io)) {
102                         result = PerlIO_close(IoOFP(io));
103                         PerlIO_close(IoIFP(io)); /* clear stdio, fd already closed */
104                     }
105                     else
106                         result = PerlIO_close(IoIFP(io));
107                 }
108                 else
109                     result = PerlIO_close(IoIFP(io));
110
111                 if (result == EOF && old_fd > PL_maxsysfd) {
112                     /* Why is this not Perl_warn*() call ? */
113                     PerlIO_printf(Perl_error_log,
114                                   "Warning: unable to close filehandle %"HEKf" properly.\n",
115                                   HEKfARG(GvENAME_HEK(gv))
116                         );
117                 }
118             }
119         }
120         IoOFP(io) = IoIFP(io) = NULL;
121     }
122     return io;
123 }
124
125 bool
126 Perl_do_openn(pTHX_ GV *gv, const char *oname, I32 len, int as_raw,
127               int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
128               I32 num_svs)
129 {
130     PERL_ARGS_ASSERT_DO_OPENN;
131
132     if (as_raw) {
133         /* sysopen style args, i.e. integer mode and permissions */
134
135         if (num_svs != 0) {
136             Perl_croak(aTHX_ "panic: sysopen with multiple args, num_svs=%ld",
137                        (long) num_svs);
138         }
139         return do_open_raw(gv, oname, len, rawmode, rawperm);
140     }
141     return do_open6(gv, oname, len, supplied_fp, svp, num_svs);
142 }
143
144 bool
145 Perl_do_open_raw(pTHX_ GV *gv, const char *oname, STRLEN len,
146                  int rawmode, int rawperm)
147 {
148     dVAR;
149     PerlIO *saveifp;
150     PerlIO *saveofp;
151     int savefd;
152     char savetype;
153     char mode[PERL_MODE_MAX];   /* file mode ("r\0", "rb\0", "ab\0" etc.) */
154     IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
155     int writing = 0;
156     PerlIO *fp;
157
158     PERL_ARGS_ASSERT_DO_OPEN_RAW;
159
160     /* For ease of blame back to 5.000, keep the existing indenting. */
161     {
162         /* sysopen style args, i.e. integer mode and permissions */
163         STRLEN ix = 0;
164         const int appendtrunc =
165              0
166 #ifdef O_APPEND /* Not fully portable. */
167              |O_APPEND
168 #endif
169 #ifdef O_TRUNC  /* Not fully portable. */
170              |O_TRUNC
171 #endif
172              ;
173         const int modifyingmode = O_WRONLY|O_RDWR|O_CREAT|appendtrunc;
174         int ismodifying;
175         SV *namesv;
176
177         /* It's not always
178
179            O_RDONLY 0
180            O_WRONLY 1
181            O_RDWR   2
182
183            It might be (in OS/390 and Mac OS Classic it is)
184
185            O_WRONLY 1
186            O_RDONLY 2
187            O_RDWR   3
188
189            This means that simple & with O_RDWR would look
190            like O_RDONLY is present.  Therefore we have to
191            be more careful.
192         */
193         if ((ismodifying = (rawmode & modifyingmode))) {
194              if ((ismodifying & O_WRONLY) == O_WRONLY ||
195                  (ismodifying & O_RDWR)   == O_RDWR   ||
196                  (ismodifying & (O_CREAT|appendtrunc)))
197                   TAINT_PROPER("sysopen");
198         }
199         mode[ix++] = IoTYPE_NUMERIC; /* Marker to openn to use numeric "sysopen" */
200
201 #if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
202         rawmode |= O_LARGEFILE; /* Transparently largefiley. */
203 #endif
204
205         IoTYPE(io) = PerlIO_intmode2str(rawmode, &mode[ix], &writing);
206
207         namesv = newSVpvn_flags(oname, len, SVs_TEMP);
208         fp = PerlIO_openn(aTHX_ NULL, mode, -1, rawmode, rawperm, NULL, 1, &namesv);
209     }
210     return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
211                          savetype, writing, 0, NULL);
212 }
213
214 bool
215 Perl_do_open6(pTHX_ GV *gv, const char *oname, STRLEN len,
216               PerlIO *supplied_fp, SV **svp, U32 num_svs)
217 {
218     dVAR;
219     PerlIO *saveifp;
220     PerlIO *saveofp;
221     int savefd;
222     char savetype;
223     char mode[PERL_MODE_MAX];   /* file mode ("r\0", "rb\0", "ab\0" etc.) */
224     IO * const io = openn_setup(gv, mode, &saveifp, &saveofp, &savefd, &savetype);
225     int writing = 0;
226     PerlIO *fp;
227     bool was_fdopen = FALSE;
228     char *type  = NULL;
229
230     PERL_ARGS_ASSERT_DO_OPEN6;
231
232     /* For ease of blame back to 5.000, keep the existing indenting. */
233     {
234         /* Regular (non-sys) open */
235         char *name;
236         STRLEN olen = len;
237         char *tend;
238         int dodup = 0;
239         bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
240
241         /* Collect default raw/crlf info from the op */
242         if (PL_op && PL_op->op_type == OP_OPEN) {
243             /* set up IO layers */
244             const U8 flags = PL_op->op_private;
245             in_raw = (flags & OPpOPEN_IN_RAW);
246             in_crlf = (flags & OPpOPEN_IN_CRLF);
247             out_raw = (flags & OPpOPEN_OUT_RAW);
248             out_crlf = (flags & OPpOPEN_OUT_CRLF);
249         }
250
251         type = savepvn(oname, len);
252         tend = type+len;
253         SAVEFREEPV(type);
254
255         /* Lose leading and trailing white space */
256         while (isSPACE(*type))
257             type++;
258         while (tend > type && isSPACE(tend[-1]))
259             *--tend = '\0';
260
261         if (num_svs) {
262             const char *p;
263             STRLEN nlen = 0;
264             /* New style explicit name, type is just mode and layer info */
265 #ifdef USE_STDIO
266             if (SvROK(*svp) && !strchr(oname,'&')) {
267                 if (ckWARN(WARN_IO))
268                     Perl_warner(aTHX_ packWARN(WARN_IO),
269                             "Can't open a reference");
270                 SETERRNO(EINVAL, LIB_INVARG);
271                 fp = NULL;
272                 goto say_false;
273             }
274 #endif /* USE_STDIO */
275             p = (SvOK(*svp) || SvGMAGICAL(*svp)) ? SvPV(*svp, nlen) : NULL;
276
277             if (p && !IS_SAFE_PATHNAME(p, nlen, "open")) {
278                 fp = NULL;
279                 goto say_false;
280             }
281
282             name = p ? savepvn(p, nlen) : savepvs("");
283
284             SAVEFREEPV(name);
285         }
286         else {
287             name = type;
288             len  = tend-type;
289         }
290         IoTYPE(io) = *type;
291         if ((*type == IoTYPE_RDWR) && /* scary */
292            (*(type+1) == IoTYPE_RDONLY || *(type+1) == IoTYPE_WRONLY) &&
293             ((!num_svs || (tend > type+1 && tend[-1] != IoTYPE_PIPE)))) {
294             TAINT_PROPER("open");
295             mode[1] = *type++;
296             writing = 1;
297         }
298
299         if (*type == IoTYPE_PIPE) {
300             if (num_svs) {
301                 if (type[1] != IoTYPE_STD) {
302                   unknown_open_mode:
303                     Perl_croak(aTHX_ "Unknown open() mode '%.*s'", (int)olen, oname);
304                 }
305                 type++;
306             }
307             do {
308                 type++;
309             } while (isSPACE(*type));
310             if (!num_svs) {
311                 name = type;
312                 len = tend-type;
313             }
314             if (*name == '\0') {
315                 /* command is missing 19990114 */
316                 if (ckWARN(WARN_PIPE))
317                     Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
318                 errno = EPIPE;
319                 fp = NULL;
320                 goto say_false;
321             }
322             if (!(*name == '-' && name[1] == '\0') || num_svs)
323                 TAINT_ENV();
324             TAINT_PROPER("piped open");
325             if (!num_svs && name[len-1] == '|') {
326                 name[--len] = '\0' ;
327                 if (ckWARN(WARN_PIPE))
328                     Perl_warner(aTHX_ packWARN(WARN_PIPE), "Can't open bidirectional pipe");
329             }
330             mode[0] = 'w';
331             writing = 1;
332             if (out_raw)
333                 mode[1] = 'b';
334             else if (out_crlf)
335                 mode[1] = 't';
336             if (num_svs > 1) {
337                 fp = PerlProc_popen_list(mode, num_svs, svp);
338             }
339             else {
340                 fp = PerlProc_popen(name,mode);
341             }
342             if (num_svs) {
343                 if (*type) {
344                     if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
345                         fp = NULL;
346                         goto say_false;
347                     }
348                 }
349             }
350         } /* IoTYPE_PIPE */
351         else if (*type == IoTYPE_WRONLY) {
352             TAINT_PROPER("open");
353             type++;
354             if (*type == IoTYPE_WRONLY) {
355                 /* Two IoTYPE_WRONLYs in a row make for an IoTYPE_APPEND. */
356                 mode[0] = IoTYPE(io) = IoTYPE_APPEND;
357                 type++;
358             }
359             else {
360                 mode[0] = 'w';
361             }
362             writing = 1;
363
364             if (out_raw)
365                 mode[1] = 'b';
366             else if (out_crlf)
367                 mode[1] = 't';
368             if (*type == '&') {
369               duplicity:
370                 dodup = PERLIO_DUP_FD;
371                 type++;
372                 if (*type == '=') {
373                     dodup = 0;
374                     type++;
375                 }
376                 if (!num_svs && !*type && supplied_fp) {
377                     /* "<+&" etc. is used by typemaps */
378                     fp = supplied_fp;
379                 }
380                 else {
381                     PerlIO *that_fp = NULL;
382                     int wanted_fd;
383                     if (num_svs > 1) {
384                         /* diag_listed_as: More than one argument to '%s' open */
385                         Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
386                     }
387                     while (isSPACE(*type))
388                         type++;
389                     if (num_svs && (
390                              SvIOK(*svp)
391                           || (SvPOKp(*svp) && looks_like_number(*svp))
392                        )) {
393                         wanted_fd = SvUV(*svp);
394                         num_svs = 0;
395                     }
396                     else if (isDIGIT(*type)) {
397                         wanted_fd = atoi(type);
398                     }
399                     else {
400                         const IO* thatio;
401                         if (num_svs) {
402                             thatio = sv_2io(*svp);
403                         }
404                         else {
405                             GV * const thatgv = gv_fetchpvn_flags(type, tend - type,
406                                                        0, SVt_PVIO);
407                             thatio = GvIO(thatgv);
408                         }
409                         if (!thatio) {
410 #ifdef EINVAL
411                             SETERRNO(EINVAL,SS_IVCHAN);
412 #endif
413                             fp = NULL;
414                             goto say_false;
415                         }
416                         if ((that_fp = IoIFP(thatio))) {
417                             /* Flush stdio buffer before dup. --mjd
418                              * Unfortunately SEEK_CURing 0 seems to
419                              * be optimized away on most platforms;
420                              * only Solaris and Linux seem to flush
421                              * on that. --jhi */
422                             /* On the other hand, do all platforms
423                              * take gracefully to flushing a read-only
424                              * filehandle?  Perhaps we should do
425                              * fsetpos(src)+fgetpos(dst)?  --nik */
426                             PerlIO_flush(that_fp);
427                             wanted_fd = PerlIO_fileno(that_fp);
428                             /* When dup()ing STDIN, STDOUT or STDERR
429                              * explicitly set appropriate access mode */
430                             if (that_fp == PerlIO_stdout()
431                                 || that_fp == PerlIO_stderr())
432                                 IoTYPE(io) = IoTYPE_WRONLY;
433                             else if (that_fp == PerlIO_stdin())
434                                 IoTYPE(io) = IoTYPE_RDONLY;
435                             /* When dup()ing a socket, say result is
436                              * one as well */
437                             else if (IoTYPE(thatio) == IoTYPE_SOCKET)
438                                 IoTYPE(io) = IoTYPE_SOCKET;
439                         }
440                         else
441                             wanted_fd = -1;
442                     }
443                     if (!num_svs)
444                         type = NULL;
445                     if (that_fp) {
446                         fp = PerlIO_fdupopen(aTHX_ that_fp, NULL, dodup);
447                     }
448                     else {
449                         if (dodup)
450                             wanted_fd = PerlLIO_dup(wanted_fd);
451                         else
452                             was_fdopen = TRUE;
453                         if (!(fp = PerlIO_openn(aTHX_ type,mode,wanted_fd,0,0,NULL,num_svs,svp))) {
454                             if (dodup && wanted_fd >= 0)
455                                 PerlLIO_close(wanted_fd);
456                         }
457                     }
458                 }
459             } /* & */
460             else {
461                 while (isSPACE(*type))
462                     type++;
463                 if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
464                     type++;
465                     fp = PerlIO_stdout();
466                     IoTYPE(io) = IoTYPE_STD;
467                     if (num_svs > 1) {
468                         /* diag_listed_as: More than one argument to '%s' open */
469                         Perl_croak(aTHX_ "More than one argument to '>%c' open",IoTYPE_STD);
470                     }
471                 }
472                 else  {
473                     if (num_svs) {
474                         fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
475                     }
476                     else {
477                         SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
478                         type = NULL;
479                         fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
480                     }
481                 }
482             } /* !& */
483             if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
484                goto unknown_open_mode;
485         } /* IoTYPE_WRONLY */
486         else if (*type == IoTYPE_RDONLY) {
487             do {
488                 type++;
489             } while (isSPACE(*type));
490             mode[0] = 'r';
491             if (in_raw)
492                 mode[1] = 'b';
493             else if (in_crlf)
494                 mode[1] = 't';
495             if (*type == '&') {
496                 goto duplicity;
497             }
498             if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
499                 type++;
500                 fp = PerlIO_stdin();
501                 IoTYPE(io) = IoTYPE_STD;
502                 if (num_svs > 1) {
503                     /* diag_listed_as: More than one argument to '%s' open */
504                     Perl_croak(aTHX_ "More than one argument to '<%c' open",IoTYPE_STD);
505                 }
506             }
507             else {
508                 if (num_svs) {
509                     fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
510                 }
511                 else {
512                     SV *namesv  = newSVpvn_flags(type, tend - type, SVs_TEMP);
513                     type = NULL;
514                     fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
515                 }
516             }
517             if (!fp && type && *type && *type != ':' && !isIDFIRST(*type))
518                goto unknown_open_mode;
519         } /* IoTYPE_RDONLY */
520         else if ((num_svs && /* '-|...' or '...|' */
521                   type[0] == IoTYPE_STD && type[1] == IoTYPE_PIPE) ||
522                  (!num_svs && tend > type+1 && tend[-1] == IoTYPE_PIPE)) {
523             if (num_svs) {
524                 type += 2;   /* skip over '-|' */
525             }
526             else {
527                 *--tend = '\0';
528                 while (tend > type && isSPACE(tend[-1]))
529                     *--tend = '\0';
530                 for (; isSPACE(*type); type++)
531                     ;
532                 name = type;
533                 len  = tend-type;
534             }
535             if (*name == '\0') {
536                 /* command is missing 19990114 */
537                 if (ckWARN(WARN_PIPE))
538                     Perl_warner(aTHX_ packWARN(WARN_PIPE), "Missing command in piped open");
539                 errno = EPIPE;
540                 fp = NULL;
541                 goto say_false;
542             }
543             if (!(*name == '-' && name[1] == '\0') || num_svs)
544                 TAINT_ENV();
545             TAINT_PROPER("piped open");
546             mode[0] = 'r';
547
548             if (in_raw)
549                 mode[1] = 'b';
550             else if (in_crlf)
551                 mode[1] = 't';
552
553             if (num_svs > 1) {
554                 fp = PerlProc_popen_list(mode,num_svs,svp);
555             }
556             else {
557                 fp = PerlProc_popen(name,mode);
558             }
559             IoTYPE(io) = IoTYPE_PIPE;
560             if (num_svs) {
561                 while (isSPACE(*type))
562                     type++;
563                 if (*type) {
564                     if (PerlIO_apply_layers(aTHX_ fp, mode, type) != 0) {
565                         fp = NULL;
566                         goto say_false;
567                     }
568                 }
569             }
570         }
571         else { /* layer(Args) */
572             if (num_svs)
573                 goto unknown_open_mode;
574             name = type;
575             IoTYPE(io) = IoTYPE_RDONLY;
576             for (; isSPACE(*name); name++)
577                 ;
578             mode[0] = 'r';
579
580             if (in_raw)
581                 mode[1] = 'b';
582             else if (in_crlf)
583                 mode[1] = 't';
584
585             if (*name == '-' && name[1] == '\0') {
586                 fp = PerlIO_stdin();
587                 IoTYPE(io) = IoTYPE_STD;
588             }
589             else {
590                 if (num_svs) {
591                     fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,num_svs,svp);
592                 }
593                 else {
594                     SV *namesv = newSVpvn_flags(type, tend - type, SVs_TEMP);
595                     type = NULL;
596                     fp = PerlIO_openn(aTHX_ type,mode,-1,0,0,NULL,1,&namesv);
597                 }
598             }
599         }
600     }
601
602   say_false:
603     return openn_cleanup(gv, io, fp, mode, oname, saveifp, saveofp, savefd,
604                          savetype, writing, was_fdopen, type);
605 }
606
607 /* Yes, this is ugly, but it's private, and I don't see a cleaner way to
608    simplify the two-headed public interface of do_openn. */
609 static bool
610 S_openn_cleanup(pTHX_ GV *gv, IO *io, PerlIO *fp, char *mode, const char *oname,
611                 PerlIO *saveifp, PerlIO *saveofp, int savefd, char savetype,
612                 int writing, bool was_fdopen, const char *type)
613 {
614     int fd;
615
616     PERL_ARGS_ASSERT_OPENN_CLEANUP;
617
618     if (!fp) {
619         if (IoTYPE(io) == IoTYPE_RDONLY && ckWARN(WARN_NEWLINE)
620             && should_warn_nl(oname)
621             
622         )
623         {
624             GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
625             Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "open");
626             GCC_DIAG_RESTORE;
627         }
628         goto say_false;
629     }
630
631     if (ckWARN(WARN_IO)) {
632         if ((IoTYPE(io) == IoTYPE_RDONLY) &&
633             (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
634                 Perl_warner(aTHX_ packWARN(WARN_IO),
635                             "Filehandle STD%s reopened as %"HEKf
636                             " only for input",
637                             ((fp == PerlIO_stdout()) ? "OUT" : "ERR"),
638                             HEKfARG(GvENAME_HEK(gv)));
639         }
640         else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdin()) {
641                 Perl_warner(aTHX_ packWARN(WARN_IO),
642                     "Filehandle STDIN reopened as %"HEKf" only for output",
643                      HEKfARG(GvENAME_HEK(gv))
644                 );
645         }
646     }
647
648     fd = PerlIO_fileno(fp);
649     /* Do NOT do: "if (fd < 0) goto say_false;" here.  If there is no
650      * fd assume it isn't a socket - this covers PerlIO::scalar -
651      * otherwise unless we "know" the type probe for socket-ness.
652      */
653     if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD && fd >= 0) {
654         if (PerlLIO_fstat(fd,&PL_statbuf) < 0) {
655             /* If PerlIO claims to have fd we had better be able to fstat() it. */
656             (void) PerlIO_close(fp);
657             goto say_false;
658         }
659 #ifndef PERL_MICRO
660         if (S_ISSOCK(PL_statbuf.st_mode))
661             IoTYPE(io) = IoTYPE_SOCKET; /* in case a socket was passed in to us */
662 #ifdef HAS_SOCKET
663         else if (
664             !(PL_statbuf.st_mode & S_IFMT)
665             && IoTYPE(io) != IoTYPE_WRONLY  /* Dups of STD* filehandles already have */
666             && IoTYPE(io) != IoTYPE_RDONLY  /* type so they aren't marked as sockets */
667         ) {                                 /* on OS's that return 0 on fstat()ed pipe */
668              char tmpbuf[256];
669              Sock_size_t buflen = sizeof tmpbuf;
670              if (PerlSock_getsockname(fd, (struct sockaddr *)tmpbuf, &buflen) >= 0
671                       || errno != ENOTSOCK)
672                     IoTYPE(io) = IoTYPE_SOCKET; /* some OS's return 0 on fstat()ed socket */
673                                                 /* but some return 0 for streams too, sigh */
674         }
675 #endif /* HAS_SOCKET */
676 #endif /* !PERL_MICRO */
677     }
678
679     /* Eeek - FIXME !!!
680      * If this is a standard handle we discard all the layer stuff
681      * and just dup the fd into whatever was on the handle before !
682      */
683
684     if (saveifp) {              /* must use old fp? */
685         /* If fd is less that PL_maxsysfd i.e. STDIN..STDERR
686            then dup the new fileno down
687          */
688         if (saveofp) {
689             PerlIO_flush(saveofp);      /* emulate PerlIO_close() */
690             if (saveofp != saveifp) {   /* was a socket? */
691                 PerlIO_close(saveofp);
692             }
693         }
694         if (savefd != fd) {
695             /* Still a small can-of-worms here if (say) PerlIO::scalar
696                is assigned to (say) STDOUT - for now let dup2() fail
697                and provide the error
698              */
699             if (fd < 0) {
700                 SETERRNO(EBADF,RMS_IFI);
701                 goto say_false;
702             } else if (PerlLIO_dup2(fd, savefd) < 0) {
703                 (void)PerlIO_close(fp);
704                 goto say_false;
705             }
706 #ifdef VMS
707             if (savefd != PerlIO_fileno(PerlIO_stdin())) {
708                 char newname[FILENAME_MAX+1];
709                 if (PerlIO_getname(fp, newname)) {
710                     if (fd == PerlIO_fileno(PerlIO_stdout()))
711                         vmssetuserlnm("SYS$OUTPUT", newname);
712                     if (fd == PerlIO_fileno(PerlIO_stderr()))
713                         vmssetuserlnm("SYS$ERROR", newname);
714                 }
715             }
716 #endif
717
718 #if !defined(WIN32)
719            /* PL_fdpid isn't used on Windows, so avoid this useless work.
720             * XXX Probably the same for a lot of other places. */
721             {
722                 Pid_t pid;
723                 SV *sv;
724
725                 sv = *av_fetch(PL_fdpid,fd,TRUE);
726                 SvUPGRADE(sv, SVt_IV);
727                 pid = SvIVX(sv);
728                 SvIV_set(sv, 0);
729                 sv = *av_fetch(PL_fdpid,savefd,TRUE);
730                 SvUPGRADE(sv, SVt_IV);
731                 SvIV_set(sv, pid);
732             }
733 #endif
734
735             if (was_fdopen) {
736                 /* need to close fp without closing underlying fd */
737                 int ofd = PerlIO_fileno(fp);
738                 int dupfd = ofd >= 0 ? PerlLIO_dup(ofd) : -1;
739 #if defined(HAS_FCNTL) && defined(F_SETFD)
740                 /* Assume if we have F_SETFD we have F_GETFD */
741                 int coe = ofd >= 0 ? fcntl(ofd, F_GETFD) : -1;
742                 if (coe < 0) {
743                     if (dupfd >= 0)
744                         PerlLIO_close(dupfd);
745                     goto say_false;
746                 }
747 #endif
748                 if (ofd < 0 || dupfd < 0) {
749                     if (dupfd >= 0)
750                         PerlLIO_close(dupfd);
751                     goto say_false;
752                 }
753                 PerlIO_close(fp);
754                 PerlLIO_dup2(dupfd, ofd);
755 #if defined(HAS_FCNTL) && defined(F_SETFD)
756                 /* The dup trick has lost close-on-exec on ofd */
757                 fcntl(ofd,F_SETFD, coe);
758 #endif
759                 PerlLIO_close(dupfd);
760             }
761             else
762                 PerlIO_close(fp);
763         }
764         fp = saveifp;
765         PerlIO_clearerr(fp);
766         fd = PerlIO_fileno(fp);
767     }
768 #if defined(HAS_FCNTL) && defined(F_SETFD)
769     if (fd >= 0) {
770         if (fcntl(fd, F_SETFD, fd > PL_maxsysfd) < 0) {
771             PerlLIO_close(fd);
772             goto say_false;
773         }
774     }
775 #endif
776     IoIFP(io) = fp;
777
778     IoFLAGS(io) &= ~IOf_NOLINE;
779     if (writing) {
780         if (IoTYPE(io) == IoTYPE_SOCKET
781             || (IoTYPE(io) == IoTYPE_WRONLY && fd >= 0 && S_ISCHR(PL_statbuf.st_mode)) ) {
782             char *s = mode;
783             if (*s == IoTYPE_IMPLICIT || *s == IoTYPE_NUMERIC)
784               s++;
785             *s = 'w';
786             if (!(IoOFP(io) = PerlIO_openn(aTHX_ type,s,fd,0,0,NULL,0,NULL))) {
787                 PerlIO_close(fp);
788                 IoIFP(io) = NULL;
789                 goto say_false;
790             }
791         }
792         else
793             IoOFP(io) = fp;
794     }
795     return TRUE;
796
797 say_false:
798     IoIFP(io) = saveifp;
799     IoOFP(io) = saveofp;
800     IoTYPE(io) = savetype;
801     return FALSE;
802 }
803
804 PerlIO *
805 Perl_nextargv(pTHX_ GV *gv)
806 {
807     dVAR;
808     IO * const io = GvIOp(gv);
809
810     PERL_ARGS_ASSERT_NEXTARGV;
811
812     if (!PL_argvoutgv)
813         PL_argvoutgv = gv_fetchpvs("ARGVOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO);
814     if (io && (IoFLAGS(io) & IOf_ARGV) && (IoFLAGS(io) & IOf_START)) {
815         IoFLAGS(io) &= ~IOf_START;
816         if (PL_inplace) {
817             assert(PL_defoutgv);
818             Perl_av_create_and_push(aTHX_ &PL_argvout_stack,
819                                     SvREFCNT_inc_simple_NN(PL_defoutgv));
820         }
821     }
822     if (PL_filemode & (S_ISUID|S_ISGID)) {
823         PerlIO_flush(IoIFP(GvIOn(PL_argvoutgv)));  /* chmod must follow last write */
824 #ifdef HAS_FCHMOD
825         if (PL_lastfd != -1)
826             (void)fchmod(PL_lastfd,PL_filemode);
827 #else
828         (void)PerlLIO_chmod(PL_oldname,PL_filemode);
829 #endif
830     }
831     PL_lastfd = -1;
832     PL_filemode = 0;
833     if (!GvAV(gv))
834         return NULL;
835     while (av_tindex(GvAV(gv)) >= 0) {
836         STRLEN oldlen;
837         SV *const sv = av_shift(GvAV(gv));
838         SAVEFREESV(sv);
839         SvTAINTED_off(GvSVn(gv)); /* previous tainting irrelevant */
840         sv_setsv(GvSVn(gv),sv);
841         SvSETMAGIC(GvSV(gv));
842         PL_oldname = SvPVx(GvSV(gv), oldlen);
843         if (LIKELY(!PL_inplace)) {
844             if (do_open6(gv, PL_oldname, oldlen, NULL, NULL, 0)) {
845                 return IoIFP(GvIOp(gv));
846             }
847         }
848         else {
849             /* This very long block ends with return IoIFP(GvIOp(gv));
850                Both this block and the block above fall through on open
851                failure to the warning code, and then the while loop above tries
852                the next entry. */
853             if (do_open_raw(gv, PL_oldname, oldlen, O_RDONLY, 0)) {
854 #ifndef FLEXFILENAMES
855                 int filedev;
856                 int fileino;
857 #endif
858                 Uid_t fileuid;
859                 Gid_t filegid;
860
861                 TAINT_PROPER("inplace open");
862                 if (oldlen == 1 && *PL_oldname == '-') {
863                     setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL,
864                                           SVt_PVIO));
865                     return IoIFP(GvIOp(gv));
866                 }
867 #ifndef FLEXFILENAMES
868                 filedev = PL_statbuf.st_dev;
869                 fileino = PL_statbuf.st_ino;
870 #endif
871                 PL_filemode = PL_statbuf.st_mode;
872                 fileuid = PL_statbuf.st_uid;
873                 filegid = PL_statbuf.st_gid;
874                 if (!S_ISREG(PL_filemode)) {
875                     Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
876                                      "Can't do inplace edit: %s is not a regular file",
877                                      PL_oldname );
878                     do_close(gv,FALSE);
879                     continue;
880                 }
881                 if (*PL_inplace && strNE(PL_inplace, "*")) {
882                     const char *star = strchr(PL_inplace, '*');
883                     if (star) {
884                         const char *begin = PL_inplace;
885                         sv_setpvs(sv, "");
886                         do {
887                             sv_catpvn(sv, begin, star - begin);
888                             sv_catpvn(sv, PL_oldname, oldlen);
889                             begin = ++star;
890                         } while ((star = strchr(begin, '*')));
891                         if (*begin)
892                             sv_catpv(sv,begin);
893                     }
894                     else {
895                         sv_catpv(sv,PL_inplace);
896                     }
897 #ifndef FLEXFILENAMES
898                     if ((PerlLIO_stat(SvPVX_const(sv),&PL_statbuf) >= 0
899                          && PL_statbuf.st_dev == filedev
900                          && PL_statbuf.st_ino == fileino)
901 #ifdef DJGPP
902                         || ((_djstat_fail_bits & _STFAIL_TRUENAME)!=0)
903 #endif
904                       )
905                     {
906                         Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
907                                          "Can't do inplace edit: %"SVf" would not be unique",
908                                          SVfARG(sv));
909                         do_close(gv,FALSE);
910                         continue;
911                     }
912 #endif
913 #ifdef HAS_RENAME
914 #if !defined(DOSISH) && !defined(__CYGWIN__)
915                     if (PerlLIO_rename(PL_oldname,SvPVX_const(sv)) < 0) {
916                         Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
917                                          "Can't rename %s to %"SVf": %s, skipping file",
918                                          PL_oldname, SVfARG(sv), Strerror(errno));
919                         do_close(gv,FALSE);
920                         continue;
921                     }
922 #else
923                     do_close(gv,FALSE);
924                     (void)PerlLIO_unlink(SvPVX_const(sv));
925                     (void)PerlLIO_rename(PL_oldname,SvPVX_const(sv));
926                     do_open_raw(gv, SvPVX_const(sv), SvCUR(sv), O_RDONLY, 0);
927 #endif /* DOSISH */
928 #else
929                     (void)UNLINK(SvPVX_const(sv));
930                     if (link(PL_oldname,SvPVX_const(sv)) < 0) {
931                         Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
932                                          "Can't rename %s to %"SVf": %s, skipping file",
933                                          PL_oldname, SVfARG(sv), Strerror(errno) );
934                         do_close(gv,FALSE);
935                         continue;
936                     }
937                     (void)UNLINK(PL_oldname);
938 #endif
939                 }
940                 else {
941 #if !defined(DOSISH) && !defined(AMIGAOS)
942 #  ifndef VMS  /* Don't delete; use automatic file versioning */
943                     if (UNLINK(PL_oldname) < 0) {
944                         Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE),
945                                          "Can't remove %s: %s, skipping file",
946                                          PL_oldname, Strerror(errno) );
947                         do_close(gv,FALSE);
948                         continue;
949                     }
950 #  endif
951 #else
952                     Perl_croak(aTHX_ "Can't do inplace edit without backup");
953 #endif
954                 }
955
956                 sv_setpvn(sv,PL_oldname,oldlen);
957                 SETERRNO(0,0);          /* in case sprintf set errno */
958                 if (!Perl_do_open_raw(aTHX_ PL_argvoutgv, SvPVX_const(sv),
959                                       SvCUR(sv),
960 #ifdef VMS
961                                       O_WRONLY|O_CREAT|O_TRUNC, 0
962 #else
963                                       O_WRONLY|O_CREAT|OPEN_EXCL, 0600
964 #endif
965                         )) {
966                     Perl_ck_warner_d(aTHX_ packWARN(WARN_INPLACE), "Can't do inplace edit on %s: %s",
967                                      PL_oldname, Strerror(errno) );
968                     do_close(gv,FALSE);
969                     continue;
970                 }
971                 setdefout(PL_argvoutgv);
972                 PL_lastfd = PerlIO_fileno(IoIFP(GvIOp(PL_argvoutgv)));
973                 if (PL_lastfd >= 0) {
974                     (void)PerlLIO_fstat(PL_lastfd,&PL_statbuf);
975 #ifdef HAS_FCHMOD
976                     (void)fchmod(PL_lastfd,PL_filemode);
977 #else
978                     (void)PerlLIO_chmod(PL_oldname,PL_filemode);
979 #endif
980                     if (fileuid != PL_statbuf.st_uid || filegid != PL_statbuf.st_gid) {
981                         /* XXX silently ignore failures */
982 #ifdef HAS_FCHOWN
983                         PERL_UNUSED_RESULT(fchown(PL_lastfd,fileuid,filegid));
984 #else
985 #ifdef HAS_CHOWN
986                         PERL_UNUSED_RESULT(PerlLIO_chown(PL_oldname,fileuid,filegid));
987 #endif
988 #endif
989                     }
990                 }
991                 return IoIFP(GvIOp(gv));
992             }
993         } /* successful do_open_raw(), PL_inplace non-NULL */
994
995         if (ckWARN_d(WARN_INPLACE)) {
996             const int eno = errno;
997             if (PerlLIO_stat(PL_oldname, &PL_statbuf) >= 0
998                 && !S_ISREG(PL_statbuf.st_mode)) {
999                 Perl_warner(aTHX_ packWARN(WARN_INPLACE),
1000                             "Can't do inplace edit: %s is not a regular file",
1001                             PL_oldname);
1002             }
1003             else {
1004                 Perl_warner(aTHX_ packWARN(WARN_INPLACE), "Can't open %s: %s",
1005                             PL_oldname, Strerror(eno));
1006             }
1007         }
1008     }
1009     if (io && (IoFLAGS(io) & IOf_ARGV))
1010         IoFLAGS(io) |= IOf_START;
1011     if (PL_inplace) {
1012         (void)do_close(PL_argvoutgv,FALSE);
1013         if (io && (IoFLAGS(io) & IOf_ARGV)
1014             && PL_argvout_stack && AvFILLp(PL_argvout_stack) >= 0)
1015         {
1016             GV * const oldout = MUTABLE_GV(av_pop(PL_argvout_stack));
1017             setdefout(oldout);
1018             SvREFCNT_dec_NN(oldout);
1019             return NULL;
1020         }
1021         setdefout(gv_fetchpvs("STDOUT", GV_ADD|GV_NOTQUAL, SVt_PVIO));
1022     }
1023     return NULL;
1024 }
1025
1026 /* explicit renamed to avoid C++ conflict    -- kja */
1027 bool
1028 Perl_do_close(pTHX_ GV *gv, bool not_implicit)
1029 {
1030     dVAR;
1031     bool retval;
1032     IO *io;
1033
1034     if (!gv)
1035         gv = PL_argvgv;
1036     if (!gv || !isGV_with_GP(gv)) {
1037         if (not_implicit)
1038             SETERRNO(EBADF,SS_IVCHAN);
1039         return FALSE;
1040     }
1041     io = GvIO(gv);
1042     if (!io) {          /* never opened */
1043         if (not_implicit) {
1044             report_evil_fh(gv);
1045             SETERRNO(EBADF,SS_IVCHAN);
1046         }
1047         return FALSE;
1048     }
1049     retval = io_close(io, not_implicit);
1050     if (not_implicit) {
1051         IoLINES(io) = 0;
1052         IoPAGE(io) = 0;
1053         IoLINES_LEFT(io) = IoPAGE_LEN(io);
1054     }
1055     IoTYPE(io) = IoTYPE_CLOSED;
1056     return retval;
1057 }
1058
1059 bool
1060 Perl_io_close(pTHX_ IO *io, bool not_implicit)
1061 {
1062     dVAR;
1063     bool retval = FALSE;
1064
1065     PERL_ARGS_ASSERT_IO_CLOSE;
1066
1067     if (IoIFP(io)) {
1068         if (IoTYPE(io) == IoTYPE_PIPE) {
1069             const int status = PerlProc_pclose(IoIFP(io));
1070             if (not_implicit) {
1071                 STATUS_NATIVE_CHILD_SET(status);
1072                 retval = (STATUS_UNIX == 0);
1073             }
1074             else {
1075                 retval = (status != -1);
1076             }
1077         }
1078         else if (IoTYPE(io) == IoTYPE_STD)
1079             retval = TRUE;
1080         else {
1081             if (IoOFP(io) && IoOFP(io) != IoIFP(io)) {          /* a socket */
1082                 const bool prev_err = PerlIO_error(IoOFP(io));
1083                 retval = (PerlIO_close(IoOFP(io)) != EOF && !prev_err);
1084                 PerlIO_close(IoIFP(io));        /* clear stdio, fd already closed */
1085             }
1086             else {
1087                 const bool prev_err = PerlIO_error(IoIFP(io));
1088                 retval = (PerlIO_close(IoIFP(io)) != EOF && !prev_err);
1089             }
1090         }
1091         IoOFP(io) = IoIFP(io) = NULL;
1092     }
1093     else if (not_implicit) {
1094         SETERRNO(EBADF,SS_IVCHAN);
1095     }
1096
1097     return retval;
1098 }
1099
1100 bool
1101 Perl_do_eof(pTHX_ GV *gv)
1102 {
1103     dVAR;
1104     IO * const io = GvIO(gv);
1105
1106     PERL_ARGS_ASSERT_DO_EOF;
1107
1108     if (!io)
1109         return TRUE;
1110     else if (IoTYPE(io) == IoTYPE_WRONLY)
1111         report_wrongway_fh(gv, '>');
1112
1113     while (IoIFP(io)) {
1114         if (PerlIO_has_cntptr(IoIFP(io))) {     /* (the code works without this) */
1115             if (PerlIO_get_cnt(IoIFP(io)) > 0)  /* cheat a little, since */
1116                 return FALSE;                   /* this is the most usual case */
1117         }
1118
1119         {
1120              /* getc and ungetc can stomp on errno */
1121             dSAVE_ERRNO;
1122             const int ch = PerlIO_getc(IoIFP(io));
1123             if (ch != EOF) {
1124                 (void)PerlIO_ungetc(IoIFP(io),ch);
1125                 RESTORE_ERRNO;
1126                 return FALSE;
1127             }
1128             RESTORE_ERRNO;
1129         }
1130
1131         if (PerlIO_has_cntptr(IoIFP(io)) && PerlIO_canset_cnt(IoIFP(io))) {
1132             if (PerlIO_get_cnt(IoIFP(io)) < -1)
1133                 PerlIO_set_cnt(IoIFP(io),-1);
1134         }
1135         if (PL_op->op_flags & OPf_SPECIAL) { /* not necessarily a real EOF yet? */
1136             if (gv != PL_argvgv || !nextargv(gv))       /* get another fp handy */
1137                 return TRUE;
1138         }
1139         else
1140             return TRUE;                /* normal fp, definitely end of file */
1141     }
1142     return TRUE;
1143 }
1144
1145 Off_t
1146 Perl_do_tell(pTHX_ GV *gv)
1147 {
1148     dVAR;
1149     IO *const io = GvIO(gv);
1150     PerlIO *fp;
1151
1152     PERL_ARGS_ASSERT_DO_TELL;
1153
1154     if (io && (fp = IoIFP(io))) {
1155         return PerlIO_tell(fp);
1156     }
1157     report_evil_fh(gv);
1158     SETERRNO(EBADF,RMS_IFI);
1159     return (Off_t)-1;
1160 }
1161
1162 bool
1163 Perl_do_seek(pTHX_ GV *gv, Off_t pos, int whence)
1164 {
1165     dVAR;
1166     IO *const io = GvIO(gv);
1167     PerlIO *fp;
1168
1169     if (io && (fp = IoIFP(io))) {
1170         return PerlIO_seek(fp, pos, whence) >= 0;
1171     }
1172     report_evil_fh(gv);
1173     SETERRNO(EBADF,RMS_IFI);
1174     return FALSE;
1175 }
1176
1177 Off_t
1178 Perl_do_sysseek(pTHX_ GV *gv, Off_t pos, int whence)
1179 {
1180     dVAR;
1181     IO *const io = GvIO(gv);
1182     PerlIO *fp;
1183
1184     PERL_ARGS_ASSERT_DO_SYSSEEK;
1185
1186     if (io && (fp = IoIFP(io))) {
1187         int fd = PerlIO_fileno(fp);
1188         if (fd >= 0) {
1189             return PerlLIO_lseek(fd, pos, whence);
1190         }
1191     }
1192     report_evil_fh(gv);
1193     SETERRNO(EBADF,RMS_IFI);
1194     return (Off_t)-1;
1195 }
1196
1197 int
1198 Perl_mode_from_discipline(pTHX_ const char *s, STRLEN len)
1199 {
1200     int mode = O_BINARY;
1201     if (s) {
1202         while (*s) {
1203             if (*s == ':') {
1204                 switch (s[1]) {
1205                 case 'r':
1206                     if (s[2] == 'a' && s[3] == 'w'
1207                         && (!s[4] || s[4] == ':' || isSPACE(s[4])))
1208                     {
1209                         mode = O_BINARY;
1210                         s += 4;
1211                         len -= 4;
1212                         break;
1213                     }
1214                     /* FALLTHROUGH */
1215                 case 'c':
1216                     if (s[2] == 'r' && s[3] == 'l' && s[4] == 'f'
1217                         && (!s[5] || s[5] == ':' || isSPACE(s[5])))
1218                     {
1219                         mode = O_TEXT;
1220                         s += 5;
1221                         len -= 5;
1222                         break;
1223                     }
1224                     /* FALLTHROUGH */
1225                 default:
1226                     goto fail_discipline;
1227                 }
1228             }
1229             else if (isSPACE(*s)) {
1230                 ++s;
1231                 --len;
1232             }
1233             else {
1234                 const char *end;
1235 fail_discipline:
1236                 end = strchr(s+1, ':');
1237                 if (!end)
1238                     end = s+len;
1239 #ifndef PERLIO_LAYERS
1240                 Perl_croak(aTHX_ "IO layers (like '%.*s') unavailable", end-s, s);
1241 #else
1242                 len -= end-s;
1243                 s = end;
1244 #endif
1245             }
1246         }
1247     }
1248     return mode;
1249 }
1250
1251 #if !defined(HAS_TRUNCATE) && !defined(HAS_CHSIZE)
1252 I32
1253 my_chsize(int fd, Off_t length)
1254 {
1255 #ifdef F_FREESP
1256         /* code courtesy of William Kucharski */
1257 #define HAS_CHSIZE
1258
1259     Stat_t filebuf;
1260
1261     if (PerlLIO_fstat(fd, &filebuf) < 0)
1262         return -1;
1263
1264     if (filebuf.st_size < length) {
1265
1266         /* extend file length */
1267
1268         if ((PerlLIO_lseek(fd, (length - 1), 0)) < 0)
1269             return -1;
1270
1271         /* write a "0" byte */
1272
1273         if ((PerlLIO_write(fd, "", 1)) != 1)
1274             return -1;
1275     }
1276     else {
1277         /* truncate length */
1278         struct flock fl;
1279         fl.l_whence = 0;
1280         fl.l_len = 0;
1281         fl.l_start = length;
1282         fl.l_type = F_WRLCK;    /* write lock on file space */
1283
1284         /*
1285         * This relies on the UNDOCUMENTED F_FREESP argument to
1286         * fcntl(2), which truncates the file so that it ends at the
1287         * position indicated by fl.l_start.
1288         *
1289         * Will minor miracles never cease?
1290         */
1291
1292         if (fcntl(fd, F_FREESP, &fl) < 0)
1293             return -1;
1294
1295     }
1296     return 0;
1297 #else
1298     Perl_croak_nocontext("truncate not implemented");
1299 #endif /* F_FREESP */
1300     return -1;
1301 }
1302 #endif /* !HAS_TRUNCATE && !HAS_CHSIZE */
1303
1304 bool
1305 Perl_do_print(pTHX_ SV *sv, PerlIO *fp)
1306 {
1307     dVAR;
1308
1309     PERL_ARGS_ASSERT_DO_PRINT;
1310
1311     /* assuming fp is checked earlier */
1312     if (!sv)
1313         return TRUE;
1314     if (SvTYPE(sv) == SVt_IV && SvIOK(sv)) {
1315         assert(!SvGMAGICAL(sv));
1316         if (SvIsUV(sv))
1317             PerlIO_printf(fp, "%"UVuf, (UV)SvUVX(sv));
1318         else
1319             PerlIO_printf(fp, "%"IVdf, (IV)SvIVX(sv));
1320         return !PerlIO_error(fp);
1321     }
1322     else {
1323         STRLEN len;
1324         /* Do this first to trigger any overloading.  */
1325         const char *tmps = SvPV_const(sv, len);
1326         U8 *tmpbuf = NULL;
1327         bool happy = TRUE;
1328
1329         if (PerlIO_isutf8(fp)) { /* If the stream is utf8 ... */
1330             if (!SvUTF8(sv)) {  /* Convert to utf8 if necessary */
1331                 /* We don't modify the original scalar.  */
1332                 tmpbuf = bytes_to_utf8((const U8*) tmps, &len);
1333                 tmps = (char *) tmpbuf;
1334             }
1335             else if (ckWARN4_d(WARN_UTF8, WARN_SURROGATE, WARN_NON_UNICODE, WARN_NONCHAR)) {
1336                 (void) check_utf8_print((const U8*) tmps, len);
1337             }
1338         } /* else stream isn't utf8 */
1339         else if (DO_UTF8(sv)) { /* But if is utf8 internally, attempt to
1340                                    convert to bytes */
1341             STRLEN tmplen = len;
1342             bool utf8 = TRUE;
1343             U8 * const result = bytes_from_utf8((const U8*) tmps, &tmplen, &utf8);
1344             if (!utf8) {
1345
1346                 /* Here, succeeded in downgrading from utf8.  Set up to below
1347                  * output the converted value */
1348                 tmpbuf = result;
1349                 tmps = (char *) tmpbuf;
1350                 len = tmplen;
1351             }
1352             else {  /* Non-utf8 output stream, but string only representable in
1353                        utf8 */
1354                 assert((char *)result == tmps);
1355                 Perl_ck_warner_d(aTHX_ packWARN(WARN_UTF8),
1356                                  "Wide character in %s",
1357                                    PL_op ? OP_DESC(PL_op) : "print"
1358                                 );
1359                     /* Could also check that isn't one of the things to avoid
1360                      * in utf8 by using check_utf8_print(), but not doing so,
1361                      * since the stream isn't a UTF8 stream */
1362             }
1363         }
1364         /* To detect whether the process is about to overstep its
1365          * filesize limit we would need getrlimit().  We could then
1366          * also transparently raise the limit with setrlimit() --
1367          * but only until the system hard limit/the filesystem limit,
1368          * at which we would get EPERM.  Note that when using buffered
1369          * io the write failure can be delayed until the flush/close. --jhi */
1370         if (len && (PerlIO_write(fp,tmps,len) == 0))
1371             happy = FALSE;
1372         Safefree(tmpbuf);
1373         return happy ? !PerlIO_error(fp) : FALSE;
1374     }
1375 }
1376
1377 I32
1378 Perl_my_stat_flags(pTHX_ const U32 flags)
1379 {
1380     dVAR;
1381     dSP;
1382     IO *io;
1383     GV* gv;
1384
1385     if (PL_op->op_flags & OPf_REF) {
1386         gv = cGVOP_gv;
1387       do_fstat:
1388         if (gv == PL_defgv)
1389             return PL_laststatval;
1390         io = GvIO(gv);
1391         do_fstat_have_io:
1392         PL_laststype = OP_STAT;
1393         PL_statgv = gv ? gv : (GV *)io;
1394         sv_setpvs(PL_statname, "");
1395         if(io) {
1396             if (IoIFP(io)) {
1397                 int fd = PerlIO_fileno(IoIFP(io));
1398                 if (fd >= 0) {
1399                     return (PL_laststatval = PerlLIO_fstat(fd, &PL_statcache));
1400                 }
1401             } else if (IoDIRP(io)) {
1402                 return (PL_laststatval = PerlLIO_fstat(my_dirfd(IoDIRP(io)), &PL_statcache));
1403             }
1404         }
1405         PL_laststatval = -1;
1406         report_evil_fh(gv);
1407         return -1;
1408     }
1409     else if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1410              == OPpFT_STACKED)
1411         return PL_laststatval;
1412     else {
1413         SV* const sv = TOPs;
1414         const char *s;
1415         STRLEN len;
1416         if ((gv = MAYBE_DEREF_GV_flags(sv,flags))) {
1417             goto do_fstat;
1418         }
1419         else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO) {
1420             io = MUTABLE_IO(SvRV(sv));
1421             gv = NULL;
1422             goto do_fstat_have_io;
1423         }
1424
1425         s = SvPV_flags_const(sv, len, flags);
1426         PL_statgv = NULL;
1427         sv_setpvn(PL_statname, s, len);
1428         s = SvPVX_const(PL_statname);           /* s now NUL-terminated */
1429         PL_laststype = OP_STAT;
1430         PL_laststatval = PerlLIO_stat(s, &PL_statcache);
1431         if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(s)) {
1432             GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1433             Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "stat");
1434             GCC_DIAG_RESTORE;
1435         }
1436         return PL_laststatval;
1437     }
1438 }
1439
1440
1441 I32
1442 Perl_my_lstat_flags(pTHX_ const U32 flags)
1443 {
1444     dVAR;
1445     static const char* const no_prev_lstat = "The stat preceding -l _ wasn't an lstat";
1446     dSP;
1447     const char *file;
1448     SV* const sv = TOPs;
1449     bool isio = FALSE;
1450     if (PL_op->op_flags & OPf_REF) {
1451         if (cGVOP_gv == PL_defgv) {
1452             if (PL_laststype != OP_LSTAT)
1453                 Perl_croak(aTHX_ "%s", no_prev_lstat);
1454             return PL_laststatval;
1455         }
1456         PL_laststatval = -1;
1457         if (ckWARN(WARN_IO)) {
1458             /* diag_listed_as: Use of -l on filehandle%s */
1459             Perl_warner(aTHX_ packWARN(WARN_IO),
1460                              "Use of -l on filehandle %"HEKf,
1461                               HEKfARG(GvENAME_HEK(cGVOP_gv)));
1462         }
1463         return -1;
1464     }
1465     if ((PL_op->op_private & (OPpFT_STACKED|OPpFT_AFTER_t))
1466              == OPpFT_STACKED) {
1467       if (PL_laststype != OP_LSTAT)
1468         Perl_croak(aTHX_ "%s", no_prev_lstat);
1469       return PL_laststatval;
1470     }
1471
1472     PL_laststype = OP_LSTAT;
1473     PL_statgv = NULL;
1474     if ( (  (SvROK(sv) && (  isGV_with_GP(SvRV(sv))
1475                           || (isio = SvTYPE(SvRV(sv)) == SVt_PVIO)  )
1476             )
1477          || isGV_with_GP(sv)
1478          )
1479       && ckWARN(WARN_IO)) {
1480         if (isio)
1481             /* diag_listed_as: Use of -l on filehandle%s */
1482             Perl_warner(aTHX_ packWARN(WARN_IO),
1483                              "Use of -l on filehandle");
1484         else
1485             /* diag_listed_as: Use of -l on filehandle%s */
1486             Perl_warner(aTHX_ packWARN(WARN_IO),
1487                              "Use of -l on filehandle %"HEKf,
1488                               GvENAME_HEK((const GV *)
1489                                           (SvROK(sv) ? SvRV(sv) : sv)));
1490     }
1491     file = SvPV_flags_const_nolen(sv, flags);
1492     sv_setpv(PL_statname,file);
1493     PL_laststatval = PerlLIO_lstat(file,&PL_statcache);
1494     if (PL_laststatval < 0 && ckWARN(WARN_NEWLINE) && should_warn_nl(file)) {
1495         GCC_DIAG_IGNORE(-Wformat-nonliteral); /* PL_warn_nl is constant */
1496         Perl_warner(aTHX_ packWARN(WARN_NEWLINE), PL_warn_nl, "lstat");
1497         GCC_DIAG_RESTORE;
1498     }
1499     return PL_laststatval;
1500 }
1501
1502 static void
1503 S_exec_failed(pTHX_ const char *cmd, int fd, int do_report)
1504 {
1505     const int e = errno;
1506     PERL_ARGS_ASSERT_EXEC_FAILED;
1507     if (ckWARN(WARN_EXEC))
1508         Perl_warner(aTHX_ packWARN(WARN_EXEC), "Can't exec \"%s\": %s",
1509                     cmd, Strerror(e));
1510     if (do_report) {
1511         /* XXX silently ignore failures */
1512         PERL_UNUSED_RESULT(PerlLIO_write(fd, (void*)&e, sizeof(int)));
1513         PerlLIO_close(fd);
1514     }
1515 }
1516
1517 bool
1518 Perl_do_aexec5(pTHX_ SV *really, SV **mark, SV **sp,
1519                int fd, int do_report)
1520 {
1521     dVAR;
1522     PERL_ARGS_ASSERT_DO_AEXEC5;
1523 #if defined(__SYMBIAN32__) || defined(__LIBCATAMOUNT__)
1524     Perl_croak(aTHX_ "exec? I'm not *that* kind of operating system");
1525 #else
1526     if (sp > mark) {
1527         const char **a;
1528         const char *tmps = NULL;
1529         Newx(PL_Argv, sp - mark + 1, const char*);
1530         a = PL_Argv;
1531
1532         while (++mark <= sp) {
1533             if (*mark)
1534                 *a++ = SvPV_nolen_const(*mark);
1535             else
1536                 *a++ = "";
1537         }
1538         *a = NULL;
1539         if (really)
1540             tmps = SvPV_nolen_const(really);
1541         if ((!really && *PL_Argv[0] != '/') ||
1542             (really && *tmps != '/'))           /* will execvp use PATH? */
1543             TAINT_ENV();                /* testing IFS here is overkill, probably */
1544         PERL_FPU_PRE_EXEC
1545         if (really && *tmps)
1546             PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
1547         else
1548             PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1549         PERL_FPU_POST_EXEC
1550         S_exec_failed(aTHX_ (really ? tmps : PL_Argv[0]), fd, do_report);
1551     }
1552     do_execfree();
1553 #endif
1554     return FALSE;
1555 }
1556
1557 void
1558 Perl_do_execfree(pTHX)
1559 {
1560     dVAR;
1561     Safefree(PL_Argv);
1562     PL_Argv = NULL;
1563     Safefree(PL_Cmd);
1564     PL_Cmd = NULL;
1565 }
1566
1567 #ifdef PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1568
1569 bool
1570 Perl_do_exec3(pTHX_ const char *incmd, int fd, int do_report)
1571 {
1572     dVAR;
1573     const char **a;
1574     char *s;
1575     char *buf;
1576     char *cmd;
1577     /* Make a copy so we can change it */
1578     const Size_t cmdlen = strlen(incmd) + 1;
1579
1580     PERL_ARGS_ASSERT_DO_EXEC3;
1581
1582     Newx(buf, cmdlen, char);
1583     cmd = buf;
1584     memcpy(cmd, incmd, cmdlen);
1585
1586     while (*cmd && isSPACE(*cmd))
1587         cmd++;
1588
1589     /* save an extra exec if possible */
1590
1591 #ifdef CSH
1592     {
1593         char flags[PERL_FLAGS_MAX];
1594         if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
1595             strnEQ(cmd+PL_cshlen," -c",3)) {
1596           my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
1597           s = cmd+PL_cshlen+3;
1598           if (*s == 'f') {
1599               s++;
1600               my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
1601           }
1602           if (*s == ' ')
1603               s++;
1604           if (*s++ == '\'') {
1605               char * const ncmd = s;
1606
1607               while (*s)
1608                   s++;
1609               if (s[-1] == '\n')
1610                   *--s = '\0';
1611               if (s[-1] == '\'') {
1612                   *--s = '\0';
1613                   PERL_FPU_PRE_EXEC
1614                   PerlProc_execl(PL_cshname, "csh", flags, ncmd, (char*)NULL);
1615                   PERL_FPU_POST_EXEC
1616                   *s = '\'';
1617                   S_exec_failed(aTHX_ PL_cshname, fd, do_report);
1618                   Safefree(buf);
1619                   return FALSE;
1620               }
1621           }
1622         }
1623     }
1624 #endif /* CSH */
1625
1626     /* see if there are shell metacharacters in it */
1627
1628     if (*cmd == '.' && isSPACE(cmd[1]))
1629         goto doshell;
1630
1631     if (strnEQ(cmd,"exec",4) && isSPACE(cmd[4]))
1632         goto doshell;
1633
1634     s = cmd;
1635     while (isWORDCHAR(*s))
1636         s++;    /* catch VAR=val gizmo */
1637     if (*s == '=')
1638         goto doshell;
1639
1640     for (s = cmd; *s; s++) {
1641         if (*s != ' ' && !isALPHA(*s) &&
1642             strchr("$&*(){}[]'\";\\|?<>~`\n",*s)) {
1643             if (*s == '\n' && !s[1]) {
1644                 *s = '\0';
1645                 break;
1646             }
1647             /* handle the 2>&1 construct at the end */
1648             if (*s == '>' && s[1] == '&' && s[2] == '1'
1649                 && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2])
1650                 && (!s[3] || isSPACE(s[3])))
1651             {
1652                 const char *t = s + 3;
1653
1654                 while (*t && isSPACE(*t))
1655                     ++t;
1656                 if (!*t && (PerlLIO_dup2(1,2) != -1)) {
1657                     s[-2] = '\0';
1658                     break;
1659                 }
1660             }
1661           doshell:
1662             PERL_FPU_PRE_EXEC
1663             PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char *)NULL);
1664             PERL_FPU_POST_EXEC
1665             S_exec_failed(aTHX_ PL_sh_path, fd, do_report);
1666             Safefree(buf);
1667             return FALSE;
1668         }
1669     }
1670
1671     Newx(PL_Argv, (s - cmd) / 2 + 2, const char*);
1672     PL_Cmd = savepvn(cmd, s-cmd);
1673     a = PL_Argv;
1674     for (s = PL_Cmd; *s;) {
1675         while (isSPACE(*s))
1676             s++;
1677         if (*s)
1678             *(a++) = s;
1679         while (*s && !isSPACE(*s))
1680             s++;
1681         if (*s)
1682             *s++ = '\0';
1683     }
1684     *a = NULL;
1685     if (PL_Argv[0]) {
1686         PERL_FPU_PRE_EXEC
1687         PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
1688         PERL_FPU_POST_EXEC
1689         if (errno == ENOEXEC) {         /* for system V NIH syndrome */
1690             do_execfree();
1691             goto doshell;
1692         }
1693         S_exec_failed(aTHX_ PL_Argv[0], fd, do_report);
1694     }
1695     do_execfree();
1696     Safefree(buf);
1697     return FALSE;
1698 }
1699
1700 #endif /* OS2 || WIN32 */
1701
1702 #ifdef VMS
1703 #include <starlet.h> /* for sys$delprc */
1704 #endif
1705
1706 I32
1707 Perl_apply(pTHX_ I32 type, SV **mark, SV **sp)
1708 {
1709     dVAR;
1710     I32 val;
1711     I32 tot = 0;
1712     const char *const what = PL_op_name[type];
1713     const char *s;
1714     STRLEN len;
1715     SV ** const oldmark = mark;
1716     bool killgp = FALSE;
1717
1718     PERL_ARGS_ASSERT_APPLY;
1719
1720     PERL_UNUSED_VAR(what); /* may not be used depending on compile options */
1721
1722     /* Doing this ahead of the switch statement preserves the old behaviour,
1723        where attempting to use kill as a taint test test would fail on
1724        platforms where kill was not defined.  */
1725 #ifndef HAS_KILL
1726     if (type == OP_KILL)
1727         Perl_die(aTHX_ PL_no_func, what);
1728 #endif
1729 #ifndef HAS_CHOWN
1730     if (type == OP_CHOWN)
1731         Perl_die(aTHX_ PL_no_func, what);
1732 #endif
1733
1734
1735 #define APPLY_TAINT_PROPER() \
1736     STMT_START {                                                        \
1737         if (TAINT_get) { TAINT_PROPER(what); }                          \
1738     } STMT_END
1739
1740     /* This is a first heuristic; it doesn't catch tainting magic. */
1741     if (TAINTING_get) {
1742         while (++mark <= sp) {
1743             if (SvTAINTED(*mark)) {
1744                 TAINT;
1745                 break;
1746             }
1747         }
1748         mark = oldmark;
1749     }
1750     switch (type) {
1751     case OP_CHMOD:
1752         APPLY_TAINT_PROPER();
1753         if (++mark <= sp) {
1754             val = SvIV(*mark);
1755             APPLY_TAINT_PROPER();
1756             tot = sp - mark;
1757             while (++mark <= sp) {
1758                 GV* gv;
1759                 if ((gv = MAYBE_DEREF_GV(*mark))) {
1760                     if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1761 #ifdef HAS_FCHMOD
1762                         int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1763                         APPLY_TAINT_PROPER();
1764                         if (fd < 0) {
1765                             SETERRNO(EBADF,RMS_IFI);
1766                             tot--;
1767                         } else if (fchmod(fd, val))
1768                             tot--;
1769 #else
1770                         Perl_die(aTHX_ PL_no_func, "fchmod");
1771 #endif
1772                     }
1773                     else {
1774                         tot--;
1775                     }
1776                 }
1777                 else {
1778                     const char *name = SvPV_nomg_const(*mark, len);
1779                     APPLY_TAINT_PROPER();
1780                     if (!IS_SAFE_PATHNAME(name, len, "chmod") ||
1781                         PerlLIO_chmod(name, val)) {
1782                         tot--;
1783                     }
1784                 }
1785             }
1786         }
1787         break;
1788 #ifdef HAS_CHOWN
1789     case OP_CHOWN:
1790         APPLY_TAINT_PROPER();
1791         if (sp - mark > 2) {
1792             I32 val2;
1793             val = SvIVx(*++mark);
1794             val2 = SvIVx(*++mark);
1795             APPLY_TAINT_PROPER();
1796             tot = sp - mark;
1797             while (++mark <= sp) {
1798                 GV* gv;
1799                 if ((gv = MAYBE_DEREF_GV(*mark))) {
1800                     if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1801 #ifdef HAS_FCHOWN
1802                         int fd = PerlIO_fileno(IoIFP(GvIOn(gv)));
1803                         APPLY_TAINT_PROPER();
1804                         if (fd < 0) {
1805                             SETERRNO(EBADF,RMS_IFI);
1806                             tot--;
1807                         } else if (fchown(fd, val, val2))
1808                             tot--;
1809 #else
1810                         Perl_die(aTHX_ PL_no_func, "fchown");
1811 #endif
1812                     }
1813                     else {
1814                         tot--;
1815                     }
1816                 }
1817                 else {
1818                     const char *name = SvPV_nomg_const(*mark, len);
1819                     APPLY_TAINT_PROPER();
1820                     if (!IS_SAFE_PATHNAME(name, len, "chown") ||
1821                         PerlLIO_chown(name, val, val2)) {
1822                         tot--;
1823                     }
1824                 }
1825             }
1826         }
1827         break;
1828 #endif
1829 /*
1830 XXX Should we make lchown() directly available from perl?
1831 For now, we'll let Configure test for HAS_LCHOWN, but do
1832 nothing in the core.
1833     --AD  5/1998
1834 */
1835 #ifdef HAS_KILL
1836     case OP_KILL:
1837         APPLY_TAINT_PROPER();
1838         if (mark == sp)
1839             break;
1840         s = SvPVx_const(*++mark, len);
1841         if (*s == '-' && isALPHA(s[1]))
1842         {
1843             s++;
1844             len--;
1845             killgp = TRUE;
1846         }
1847         if (isALPHA(*s)) {
1848             if (*s == 'S' && s[1] == 'I' && s[2] == 'G') {
1849                 s += 3;
1850                 len -= 3;
1851             }
1852            if ((val = whichsig_pvn(s, len)) < 0)
1853                Perl_croak(aTHX_ "Unrecognized signal name \"%"SVf"\"", SVfARG(*mark));
1854         }
1855         else
1856         {
1857             val = SvIV(*mark);
1858             if (val < 0)
1859             {
1860                 killgp = TRUE;
1861                 val = -val;
1862             }
1863         }
1864         APPLY_TAINT_PROPER();
1865         tot = sp - mark;
1866 #ifdef VMS
1867         /* kill() doesn't do process groups (job trees?) under VMS */
1868         if (val == SIGKILL) {
1869             /* Use native sys$delprc() to insure that target process is
1870              * deleted; supervisor-mode images don't pay attention to
1871              * CRTL's emulation of Unix-style signals and kill()
1872              */
1873             while (++mark <= sp) {
1874                 I32 proc;
1875                 unsigned long int __vmssts;
1876                 SvGETMAGIC(*mark);
1877                 if (!(SvIOK(*mark) || SvNOK(*mark) || looks_like_number(*mark)))
1878                     Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1879                 proc = SvIV_nomg(*mark);
1880                 APPLY_TAINT_PROPER();
1881                 if (!((__vmssts = sys$delprc(&proc,0)) & 1)) {
1882                     tot--;
1883                     switch (__vmssts) {
1884                         case SS$_NONEXPR:
1885                         case SS$_NOSUCHNODE:
1886                             SETERRNO(ESRCH,__vmssts);
1887                             break;
1888                         case SS$_NOPRIV:
1889                             SETERRNO(EPERM,__vmssts);
1890                             break;
1891                         default:
1892                             SETERRNO(EVMSERR,__vmssts);
1893                     }
1894                 }
1895             }
1896             PERL_ASYNC_CHECK();
1897             break;
1898         }
1899 #endif
1900         while (++mark <= sp) {
1901             Pid_t proc;
1902             SvGETMAGIC(*mark);
1903             if (!(SvNIOK(*mark) || looks_like_number(*mark)))
1904                 Perl_croak(aTHX_ "Can't kill a non-numeric process ID");
1905             proc = SvIV_nomg(*mark);
1906             APPLY_TAINT_PROPER();
1907 #ifdef HAS_KILLPG
1908             /* use killpg in preference, as the killpg() wrapper for Win32
1909              * understands process groups, but the kill() wrapper doesn't */
1910             if (killgp ? PerlProc_killpg(proc, val)
1911                        : PerlProc_kill(proc, val))
1912 #else
1913             if (PerlProc_kill(killgp ? -proc: proc, val))
1914 #endif
1915                 tot--;
1916         }
1917         PERL_ASYNC_CHECK();
1918         break;
1919 #endif
1920     case OP_UNLINK:
1921         APPLY_TAINT_PROPER();
1922         tot = sp - mark;
1923         while (++mark <= sp) {
1924             s = SvPV_const(*mark, len);
1925             APPLY_TAINT_PROPER();
1926             if (!IS_SAFE_PATHNAME(s, len, "unlink")) {
1927                 tot--;
1928             }
1929             else if (PL_unsafe) {
1930                 if (UNLINK(s))
1931                     tot--;
1932             }
1933             else {      /* don't let root wipe out directories without -U */
1934                 if (PerlLIO_lstat(s,&PL_statbuf) < 0)
1935                     tot--;
1936                 else if (S_ISDIR(PL_statbuf.st_mode)) {
1937                     tot--;
1938                     SETERRNO(EISDIR, SS_NOPRIV);
1939                 }
1940                 else {
1941                     if (UNLINK(s))
1942                         tot--;
1943                 }
1944             }
1945         }
1946         break;
1947 #if defined(HAS_UTIME) || defined(HAS_FUTIMES)
1948     case OP_UTIME:
1949         APPLY_TAINT_PROPER();
1950         if (sp - mark > 2) {
1951 #if defined(HAS_FUTIMES)
1952             struct timeval utbuf[2];
1953             void *utbufp = utbuf;
1954 #elif defined(I_UTIME) || defined(VMS)
1955             struct utimbuf utbuf;
1956             struct utimbuf *utbufp = &utbuf;
1957 #else
1958             struct {
1959                 Time_t  actime;
1960                 Time_t  modtime;
1961             } utbuf;
1962             void *utbufp = &utbuf;
1963 #endif
1964
1965            SV* const accessed = *++mark;
1966            SV* const modified = *++mark;
1967
1968            /* Be like C, and if both times are undefined, let the C
1969             * library figure out what to do.  This usually means
1970             * "current time". */
1971
1972            if ( accessed == &PL_sv_undef && modified == &PL_sv_undef )
1973                 utbufp = NULL;
1974            else {
1975                 Zero(&utbuf, sizeof utbuf, char);
1976 #ifdef HAS_FUTIMES
1977                 utbuf[0].tv_sec = (long)SvIV(accessed);  /* time accessed */
1978                 utbuf[0].tv_usec = 0;
1979                 utbuf[1].tv_sec = (long)SvIV(modified);  /* time modified */
1980                 utbuf[1].tv_usec = 0;
1981 #elif defined(BIG_TIME)
1982                 utbuf.actime = (Time_t)SvNV(accessed);  /* time accessed */
1983                 utbuf.modtime = (Time_t)SvNV(modified); /* time modified */
1984 #else
1985                 utbuf.actime = (Time_t)SvIV(accessed);  /* time accessed */
1986                 utbuf.modtime = (Time_t)SvIV(modified); /* time modified */
1987 #endif
1988             }
1989             APPLY_TAINT_PROPER();
1990             tot = sp - mark;
1991             while (++mark <= sp) {
1992                 GV* gv;
1993                 if ((gv = MAYBE_DEREF_GV(*mark))) {
1994                     if (GvIO(gv) && IoIFP(GvIOp(gv))) {
1995 #ifdef HAS_FUTIMES
1996                         int fd =  PerlIO_fileno(IoIFP(GvIOn(gv)));
1997                         APPLY_TAINT_PROPER();
1998                         if (fd < 0) {
1999                             SETERRNO(EBADF,RMS_IFI);
2000                             tot--;
2001                         } else if (futimes(fd, (struct timeval *) utbufp))
2002                             tot--;
2003 #else
2004                         Perl_die(aTHX_ PL_no_func, "futimes");
2005 #endif
2006                     }
2007                     else {
2008                         tot--;
2009                     }
2010                 }
2011                 else {
2012                     const char * const name = SvPV_nomg_const(*mark, len);
2013                     APPLY_TAINT_PROPER();
2014                     if (!IS_SAFE_PATHNAME(name, len, "utime")) {
2015                         tot--;
2016                     }
2017                     else
2018 #ifdef HAS_FUTIMES
2019                     if (utimes(name, (struct timeval *)utbufp))
2020 #else
2021                     if (PerlLIO_utime(name, utbufp))
2022 #endif
2023                         tot--;
2024                 }
2025
2026             }
2027         }
2028         else
2029             tot = 0;
2030         break;
2031 #endif
2032     }
2033     return tot;
2034
2035 #undef APPLY_TAINT_PROPER
2036 }
2037
2038 /* Do the permissions allow some operation?  Assumes statcache already set. */
2039 #ifndef VMS /* VMS' cando is in vms.c */
2040 bool
2041 Perl_cando(pTHX_ Mode_t mode, bool effective, const Stat_t *statbufp)
2042 /* effective is a flag, true for EUID, or for checking if the effective gid
2043  *  is in the list of groups returned from getgroups().
2044  */
2045 {
2046     dVAR;
2047
2048     PERL_ARGS_ASSERT_CANDO;
2049
2050 #ifdef DOSISH
2051     /* [Comments and code from Len Reed]
2052      * MS-DOS "user" is similar to UNIX's "superuser," but can't write
2053      * to write-protected files.  The execute permission bit is set
2054      * by the Microsoft C library stat() function for the following:
2055      *          .exe files
2056      *          .com files
2057      *          .bat files
2058      *          directories
2059      * All files and directories are readable.
2060      * Directories and special files, e.g. "CON", cannot be
2061      * write-protected.
2062      * [Comment by Tom Dinger -- a directory can have the write-protect
2063      *          bit set in the file system, but DOS permits changes to
2064      *          the directory anyway.  In addition, all bets are off
2065      *          here for networked software, such as Novell and
2066      *          Sun's PC-NFS.]
2067      */
2068
2069      /* Atari stat() does pretty much the same thing. we set x_bit_set_in_stat
2070       * too so it will actually look into the files for magic numbers
2071       */
2072      return (mode & statbufp->st_mode) ? TRUE : FALSE;
2073
2074 #else /* ! DOSISH */
2075 # ifdef __CYGWIN__
2076     if (ingroup(544,effective)) {     /* member of Administrators */
2077 # else
2078     if ((effective ? PerlProc_geteuid() : PerlProc_getuid()) == 0) {    /* root is special */
2079 # endif
2080         if (mode == S_IXUSR) {
2081             if (statbufp->st_mode & 0111 || S_ISDIR(statbufp->st_mode))
2082                 return TRUE;
2083         }
2084         else
2085             return TRUE;                /* root reads and writes anything */
2086         return FALSE;
2087     }
2088     if (statbufp->st_uid == (effective ? PerlProc_geteuid() : PerlProc_getuid()) ) {
2089         if (statbufp->st_mode & mode)
2090             return TRUE;        /* ok as "user" */
2091     }
2092     else if (ingroup(statbufp->st_gid,effective)) {
2093         if (statbufp->st_mode & mode >> 3)
2094             return TRUE;        /* ok as "group" */
2095     }
2096     else if (statbufp->st_mode & mode >> 6)
2097         return TRUE;    /* ok as "other" */
2098     return FALSE;
2099 #endif /* ! DOSISH */
2100 }
2101 #endif /* ! VMS */
2102
2103 static bool
2104 S_ingroup(pTHX_ Gid_t testgid, bool effective)
2105 {
2106     dVAR;
2107     if (testgid == (effective ? PerlProc_getegid() : PerlProc_getgid()))
2108         return TRUE;
2109 #ifdef HAS_GETGROUPS
2110     {
2111         Groups_t *gary = NULL;
2112         I32 anum;
2113         bool rc = FALSE;
2114
2115         anum = getgroups(0, gary);
2116         if (anum > 0) {
2117             Newx(gary, anum, Groups_t);
2118             anum = getgroups(anum, gary);
2119             while (--anum >= 0)
2120                 if (gary[anum] == testgid) {
2121                     rc = TRUE;
2122                     break;
2123                 }
2124
2125             Safefree(gary);
2126         }
2127         return rc;
2128     }
2129 #else
2130     return FALSE;
2131 #endif
2132 }
2133
2134 #if defined(HAS_MSG) || defined(HAS_SEM) || defined(HAS_SHM)
2135
2136 I32
2137 Perl_do_ipcget(pTHX_ I32 optype, SV **mark, SV **sp)
2138 {
2139     dVAR;
2140     const key_t key = (key_t)SvNVx(*++mark);
2141     SV *nsv = optype == OP_MSGGET ? NULL : *++mark;
2142     const I32 flags = SvIVx(*++mark);
2143
2144     PERL_ARGS_ASSERT_DO_IPCGET;
2145     PERL_UNUSED_ARG(sp);
2146
2147     SETERRNO(0,0);
2148     switch (optype)
2149     {
2150 #ifdef HAS_MSG
2151     case OP_MSGGET:
2152         return msgget(key, flags);
2153 #endif
2154 #ifdef HAS_SEM
2155     case OP_SEMGET:
2156         return semget(key, (int) SvIV(nsv), flags);
2157 #endif
2158 #ifdef HAS_SHM
2159     case OP_SHMGET:
2160         return shmget(key, (size_t) SvUV(nsv), flags);
2161 #endif
2162 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2163     default:
2164         /* diag_listed_as: msg%s not implemented */
2165         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2166 #endif
2167     }
2168     return -1;                  /* should never happen */
2169 }
2170
2171 I32
2172 Perl_do_ipcctl(pTHX_ I32 optype, SV **mark, SV **sp)
2173 {
2174     dVAR;
2175     char *a;
2176     I32 ret = -1;
2177     const I32 id  = SvIVx(*++mark);
2178 #ifdef Semctl
2179     const I32 n   = (optype == OP_SEMCTL) ? SvIVx(*++mark) : 0;
2180 #endif
2181     const I32 cmd = SvIVx(*++mark);
2182     SV * const astr = *++mark;
2183     STRLEN infosize = 0;
2184     I32 getinfo = (cmd == IPC_STAT);
2185
2186     PERL_ARGS_ASSERT_DO_IPCCTL;
2187     PERL_UNUSED_ARG(sp);
2188
2189     switch (optype)
2190     {
2191 #ifdef HAS_MSG
2192     case OP_MSGCTL:
2193         if (cmd == IPC_STAT || cmd == IPC_SET)
2194             infosize = sizeof(struct msqid_ds);
2195         break;
2196 #endif
2197 #ifdef HAS_SHM
2198     case OP_SHMCTL:
2199         if (cmd == IPC_STAT || cmd == IPC_SET)
2200             infosize = sizeof(struct shmid_ds);
2201         break;
2202 #endif
2203 #ifdef HAS_SEM
2204     case OP_SEMCTL:
2205 #ifdef Semctl
2206         if (cmd == IPC_STAT || cmd == IPC_SET)
2207             infosize = sizeof(struct semid_ds);
2208         else if (cmd == GETALL || cmd == SETALL)
2209         {
2210             struct semid_ds semds;
2211             union semun semun;
2212 #ifdef EXTRA_F_IN_SEMUN_BUF
2213             semun.buff = &semds;
2214 #else
2215             semun.buf = &semds;
2216 #endif
2217             getinfo = (cmd == GETALL);
2218             if (Semctl(id, 0, IPC_STAT, semun) == -1)
2219                 return -1;
2220             infosize = semds.sem_nsems * sizeof(short);
2221                 /* "short" is technically wrong but much more portable
2222                    than guessing about u_?short(_t)? */
2223         }
2224 #else
2225         /* diag_listed_as: sem%s not implemented */
2226         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2227 #endif
2228         break;
2229 #endif
2230 #if !defined(HAS_MSG) || !defined(HAS_SEM) || !defined(HAS_SHM)
2231     default:
2232         /* diag_listed_as: shm%s not implemented */
2233         Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2234 #endif
2235     }
2236
2237     if (infosize)
2238     {
2239         if (getinfo)
2240         {
2241             SvPV_force_nolen(astr);
2242             a = SvGROW(astr, infosize+1);
2243         }
2244         else
2245         {
2246             STRLEN len;
2247             a = SvPV(astr, len);
2248             if (len != infosize)
2249                 Perl_croak(aTHX_ "Bad arg length for %s, is %lu, should be %ld",
2250                       PL_op_desc[optype],
2251                       (unsigned long)len,
2252                       (long)infosize);
2253         }
2254     }
2255     else
2256     {
2257         const IV i = SvIV(astr);
2258         a = INT2PTR(char *,i);          /* ouch */
2259     }
2260     SETERRNO(0,0);
2261     switch (optype)
2262     {
2263 #ifdef HAS_MSG
2264     case OP_MSGCTL:
2265         ret = msgctl(id, cmd, (struct msqid_ds *)a);
2266         break;
2267 #endif
2268 #ifdef HAS_SEM
2269     case OP_SEMCTL: {
2270 #ifdef Semctl
2271             union semun unsemds;
2272
2273             if(cmd == SETVAL) {
2274                 unsemds.val = PTR2nat(a);
2275             }
2276             else {
2277 #ifdef EXTRA_F_IN_SEMUN_BUF
2278                 unsemds.buff = (struct semid_ds *)a;
2279 #else
2280                 unsemds.buf = (struct semid_ds *)a;
2281 #endif
2282             }
2283             ret = Semctl(id, n, cmd, unsemds);
2284 #else
2285             /* diag_listed_as: sem%s not implemented */
2286             Perl_croak(aTHX_ "%s not implemented", PL_op_desc[optype]);
2287 #endif
2288         }
2289         break;
2290 #endif
2291 #ifdef HAS_SHM
2292     case OP_SHMCTL:
2293         ret = shmctl(id, cmd, (struct shmid_ds *)a);
2294         break;
2295 #endif
2296     }
2297     if (getinfo && ret >= 0) {
2298         SvCUR_set(astr, infosize);
2299         *SvEND(astr) = '\0';
2300         SvSETMAGIC(astr);
2301     }
2302     return ret;
2303 }
2304
2305 I32
2306 Perl_do_msgsnd(pTHX_ SV **mark, SV **sp)
2307 {
2308     dVAR;
2309 #ifdef HAS_MSG
2310     STRLEN len;
2311     const I32 id = SvIVx(*++mark);
2312     SV * const mstr = *++mark;
2313     const I32 flags = SvIVx(*++mark);
2314     const char * const mbuf = SvPV_const(mstr, len);
2315     const I32 msize = len - sizeof(long);
2316
2317     PERL_ARGS_ASSERT_DO_MSGSND;
2318     PERL_UNUSED_ARG(sp);
2319
2320     if (msize < 0)
2321         Perl_croak(aTHX_ "Arg too short for msgsnd");
2322     SETERRNO(0,0);
2323     return msgsnd(id, (struct msgbuf *)mbuf, msize, flags);
2324 #else
2325     PERL_UNUSED_ARG(sp);
2326     PERL_UNUSED_ARG(mark);
2327     /* diag_listed_as: msg%s not implemented */
2328     Perl_croak(aTHX_ "msgsnd not implemented");
2329     return -1;
2330 #endif
2331 }
2332
2333 I32
2334 Perl_do_msgrcv(pTHX_ SV **mark, SV **sp)
2335 {
2336 #ifdef HAS_MSG
2337     dVAR;
2338     char *mbuf;
2339     long mtype;
2340     I32 msize, flags, ret;
2341     const I32 id = SvIVx(*++mark);
2342     SV * const mstr = *++mark;
2343
2344     PERL_ARGS_ASSERT_DO_MSGRCV;
2345     PERL_UNUSED_ARG(sp);
2346
2347     /* suppress warning when reading into undef var --jhi */
2348     if (! SvOK(mstr))
2349         sv_setpvs(mstr, "");
2350     msize = SvIVx(*++mark);
2351     mtype = (long)SvIVx(*++mark);
2352     flags = SvIVx(*++mark);
2353     SvPV_force_nolen(mstr);
2354     mbuf = SvGROW(mstr, sizeof(long)+msize+1);
2355
2356     SETERRNO(0,0);
2357     ret = msgrcv(id, (struct msgbuf *)mbuf, msize, mtype, flags);
2358     if (ret >= 0) {
2359         SvCUR_set(mstr, sizeof(long)+ret);
2360         *SvEND(mstr) = '\0';
2361         /* who knows who has been playing with this message? */
2362         SvTAINTED_on(mstr);
2363     }
2364     return ret;
2365 #else
2366     PERL_UNUSED_ARG(sp);
2367     PERL_UNUSED_ARG(mark);
2368     /* diag_listed_as: msg%s not implemented */
2369     Perl_croak(aTHX_ "msgrcv not implemented");
2370     return -1;
2371 #endif
2372 }
2373
2374 I32
2375 Perl_do_semop(pTHX_ SV **mark, SV **sp)
2376 {
2377 #ifdef HAS_SEM
2378     dVAR;
2379     STRLEN opsize;
2380     const I32 id = SvIVx(*++mark);
2381     SV * const opstr = *++mark;
2382     const char * const opbuf = SvPV_const(opstr, opsize);
2383
2384     PERL_ARGS_ASSERT_DO_SEMOP;
2385     PERL_UNUSED_ARG(sp);
2386
2387     if (opsize < 3 * SHORTSIZE
2388         || (opsize % (3 * SHORTSIZE))) {
2389         SETERRNO(EINVAL,LIB_INVARG);
2390         return -1;
2391     }
2392     SETERRNO(0,0);
2393     /* We can't assume that sizeof(struct sembuf) == 3 * sizeof(short). */
2394     {
2395         const int nsops  = opsize / (3 * sizeof (short));
2396         int i      = nsops;
2397         short * const ops = (short *) opbuf;
2398         short *o   = ops;
2399         struct sembuf *temps, *t;
2400         I32 result;
2401
2402         Newx (temps, nsops, struct sembuf);
2403         t = temps;
2404         while (i--) {
2405             t->sem_num = *o++;
2406             t->sem_op  = *o++;
2407             t->sem_flg = *o++;
2408             t++;
2409         }
2410         result = semop(id, temps, nsops);
2411         Safefree(temps);
2412         return result;
2413     }
2414 #else
2415     /* diag_listed_as: sem%s not implemented */
2416     Perl_croak(aTHX_ "semop not implemented");
2417 #endif
2418 }
2419
2420 I32
2421 Perl_do_shmio(pTHX_ I32 optype, SV **mark, SV **sp)
2422 {
2423 #ifdef HAS_SHM
2424     dVAR;
2425     char *shm;
2426     struct shmid_ds shmds;
2427     const I32 id = SvIVx(*++mark);
2428     SV * const mstr = *++mark;
2429     const I32 mpos = SvIVx(*++mark);
2430     const I32 msize = SvIVx(*++mark);
2431
2432     PERL_ARGS_ASSERT_DO_SHMIO;
2433     PERL_UNUSED_ARG(sp);
2434
2435     SETERRNO(0,0);
2436     if (shmctl(id, IPC_STAT, &shmds) == -1)
2437         return -1;
2438     if (mpos < 0 || msize < 0
2439         || (size_t)mpos + msize > (size_t)shmds.shm_segsz) {
2440         SETERRNO(EFAULT,SS_ACCVIO);             /* can't do as caller requested */
2441         return -1;
2442     }
2443     shm = (char *)shmat(id, NULL, (optype == OP_SHMREAD) ? SHM_RDONLY : 0);
2444     if (shm == (char *)-1)      /* I hate System V IPC, I really do */
2445         return -1;
2446     if (optype == OP_SHMREAD) {
2447         char *mbuf;
2448         /* suppress warning when reading into undef var (tchrist 3/Mar/00) */
2449         SvGETMAGIC(mstr);
2450         SvUPGRADE(mstr, SVt_PV);
2451         if (! SvOK(mstr))
2452             sv_setpvs(mstr, "");
2453         SvPOK_only(mstr);
2454         mbuf = SvGROW(mstr, (STRLEN)msize+1);
2455
2456         Copy(shm + mpos, mbuf, msize, char);
2457         SvCUR_set(mstr, msize);
2458         *SvEND(mstr) = '\0';
2459         SvSETMAGIC(mstr);
2460         /* who knows who has been playing with this shared memory? */
2461         SvTAINTED_on(mstr);
2462     }
2463     else {
2464         STRLEN len;
2465
2466         const char *mbuf = SvPV_const(mstr, len);
2467         const I32 n = ((I32)len > msize) ? msize : (I32)len;
2468         Copy(mbuf, shm + mpos, n, char);
2469         if (n < msize)
2470             memzero(shm + mpos + n, msize - n);
2471     }
2472     return shmdt(shm);
2473 #else
2474     /* diag_listed_as: shm%s not implemented */
2475     Perl_croak(aTHX_ "shm I/O not implemented");
2476     return -1;
2477 #endif
2478 }
2479
2480 #endif /* SYSV IPC */
2481
2482 /*
2483 =head1 IO Functions
2484
2485 =for apidoc start_glob
2486
2487 Function called by C<do_readline> to spawn a glob (or do the glob inside
2488 perl on VMS).  This code used to be inline, but now perl uses C<File::Glob>
2489 this glob starter is only used by miniperl during the build process.
2490 Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
2491
2492 =cut
2493 */
2494
2495 PerlIO *
2496 Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
2497 {
2498     dVAR;
2499     SV * const tmpcmd = newSV(0);
2500     PerlIO *fp;
2501     STRLEN len;
2502     const char *s = SvPV(tmpglob, len);
2503
2504     PERL_ARGS_ASSERT_START_GLOB;
2505
2506     if (!IS_SAFE_SYSCALL(s, len, "pattern", "glob"))
2507         return NULL;
2508
2509     ENTER;
2510     SAVEFREESV(tmpcmd);
2511 #ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
2512            /* since spawning off a process is a real performance hit */
2513
2514 PerlIO * 
2515 Perl_vms_start_glob
2516    (pTHX_ SV *tmpglob,
2517     IO *io);
2518
2519     fp = Perl_vms_start_glob(aTHX_ tmpglob, io);
2520
2521 #else /* !VMS */
2522 #ifdef DOSISH
2523 #ifdef OS2
2524     sv_setpv(tmpcmd, "for a in ");
2525     sv_catsv(tmpcmd, tmpglob);
2526     sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
2527 #else
2528 #ifdef DJGPP
2529     sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
2530     sv_catsv(tmpcmd, tmpglob);
2531 #else
2532     sv_setpv(tmpcmd, "perlglob ");
2533     sv_catsv(tmpcmd, tmpglob);
2534     sv_catpv(tmpcmd, " |");
2535 #endif /* !DJGPP */
2536 #endif /* !OS2 */
2537 #else /* !DOSISH */
2538 #if defined(CSH)
2539     sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
2540     sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
2541     sv_catsv(tmpcmd, tmpglob);
2542     sv_catpv(tmpcmd, "' 2>/dev/null |");
2543 #else
2544     sv_setpv(tmpcmd, "echo ");
2545     sv_catsv(tmpcmd, tmpglob);
2546     sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
2547 #endif /* !CSH */
2548 #endif /* !DOSISH */
2549     {
2550         GV * const envgv = gv_fetchpvs("ENV", 0, SVt_PVHV);
2551         SV ** const home = hv_fetchs(GvHV(envgv), "HOME", 0);
2552         SV ** const path = hv_fetchs(GvHV(envgv), "PATH", 0);
2553         if (home && *home) SvGETMAGIC(*home);
2554         if (path && *path) SvGETMAGIC(*path);
2555         save_hash(gv_fetchpvs("ENV", 0, SVt_PVHV));
2556         if (home && *home) SvSETMAGIC(*home);
2557         if (path && *path) SvSETMAGIC(*path);
2558     }
2559     (void)do_open6(PL_last_in_gv, SvPVX_const(tmpcmd), SvCUR(tmpcmd),
2560                    NULL, NULL, 0);
2561     fp = IoIFP(io);
2562 #endif /* !VMS */
2563     LEAVE;
2564
2565     if (!fp && ckWARN(WARN_GLOB)) {
2566         Perl_warner(aTHX_ packWARN(WARN_GLOB), "glob failed (can't start child: %s)",
2567                     Strerror(errno));
2568     }
2569
2570     return fp;
2571 }
2572
2573 /*
2574  * Local variables:
2575  * c-indentation-style: bsd
2576  * c-basic-offset: 4
2577  * indent-tabs-mode: nil
2578  * End:
2579  *
2580  * ex: set ts=8 sts=4 sw=4 et:
2581  */