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