/* doio.c
*
- * Copyright (c) 1991-2000, Larry Wall
+ * Copyright (c) 1991-2001, Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
Perl_do_open(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
int rawmode, int rawperm, PerlIO *supplied_fp)
{
- return do_open9(gv, name, len, as_raw, rawmode, rawperm,
- supplied_fp, Nullsv, 0);
+ return do_openn(gv, name, len, as_raw, rawmode, rawperm,
+ supplied_fp, (SV **) NULL, 0);
}
bool
int rawmode, int rawperm, PerlIO *supplied_fp, SV *svs,
I32 num_svs)
{
+ return do_openn(gv, name, len, as_raw, rawmode, rawperm,
+ supplied_fp, &svs, 1);
+}
+
+bool
+Perl_do_openn(pTHX_ GV *gv, register char *name, I32 len, int as_raw,
+ int rawmode, int rawperm, PerlIO *supplied_fp, SV **svp,
+ I32 num_svs)
+{
register IO *io = GvIOn(gv);
PerlIO *saveifp = Nullfp;
PerlIO *saveofp = Nullfp;
bool was_fdopen = FALSE;
bool in_raw = 0, in_crlf = 0, out_raw = 0, out_crlf = 0;
char *type = NULL;
- char *deftype = NULL;
char mode[4]; /* stdio file mode ("r\0", "rb\0", "r+b\0" etc.) */
+ SV *svs = (num_svs) ? *svp : Nullsv;
Zero(mode,sizeof(mode),char);
PL_forkprocess = 1; /* assume true if no fork */
if (as_raw) {
/* sysopen style args, i.e. integer mode and permissions */
+ if (num_svs != 0) {
+ Perl_croak(aTHX_ "panic:sysopen with multiple args");
+ }
#if defined(USE_64_BIT_RAWIO) && defined(O_LARGEFILE)
rawmode |= O_LARGEFILE;
len = tend-type;
}
IoTYPE(io) = *type;
- if (*type == IoTYPE_RDWR && (!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE)) { /* scary */
+ if ((*type == IoTYPE_RDWR) && ((!num_svs || tend > type+1 && tend[-1] != IoTYPE_PIPE))) { /* scary */
mode[1] = *type++;
writing = 1;
}
name = type;
len = tend-type;
}
- if (*name == '\0') { /* command is missing 19990114 */
+ if (*name == '\0') {
+ /* command is missing 19990114 */
if (ckWARN(WARN_PIPE))
Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
errno = EPIPE;
strcat(mode, "b");
else if (out_crlf)
strcat(mode, "t");
- fp = PerlProc_popen(name,mode);
+ if (num_svs > 1) {
+ fp = PerlProc_popen_list(mode, num_svs, svp);
+ }
+ else {
+ fp = PerlProc_popen(name,mode);
+ }
}
else if (*type == IoTYPE_WRONLY) {
TAINT_PROPER("open");
strcat(mode, "t");
if (*type == '&') {
- name = type;
duplicity:
- if (num_svs)
- goto unknown_desr;
dodup = 1;
- name++;
- if (*name == '=') {
+ type++;
+ if (*type == '=') {
dodup = 0;
- name++;
+ type++;
}
- if (!*name && supplied_fp)
+ if (!num_svs && !*type && supplied_fp)
+ /* "<+&" etc. is used by typemaps */
fp = supplied_fp;
else {
- /*SUPPRESS 530*/
- for (; isSPACE(*name); name++) ;
- if (isDIGIT(*name))
- fd = atoi(name);
+ if (num_svs > 1) {
+ Perl_croak(aTHX_ "More than one argument to '%c&' open",IoTYPE(io));
+ }
+ if (num_svs && SvIOK(*svp))
+ fd = SvUV(*svp);
+ else if (isDIGIT(*type)) {
+ /*SUPPRESS 530*/
+ for (; isSPACE(*type); type++) ;
+ fd = atoi(type);
+ }
else {
IO* thatio;
- gv = gv_fetchpv(name,FALSE,SVt_PVIO);
- thatio = GvIO(gv);
+ if (num_svs) {
+ thatio = sv_2io(*svp);
+ }
+ else {
+ GV *thatgv;
+ /*SUPPRESS 530*/
+ for (; isSPACE(*type); type++) ;
+ thatgv = gv_fetchpv(type,FALSE,SVt_PVIO);
+ thatio = GvIO(thatgv);
+ }
if (!thatio) {
#ifdef EINVAL
SETERRNO(EINVAL,SS$_IVCHAN);
}
}
else {
+ if (num_svs > 1) {
+ Perl_croak(aTHX_ "More than one argument to '>' open");
+ }
/*SUPPRESS 530*/
for (; isSPACE(*type); type++) ;
if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
}
}
else if (*type == IoTYPE_RDONLY) {
+ if (num_svs > 1) {
+ Perl_croak(aTHX_ "More than one argument to '<' open");
+ }
/*SUPPRESS 530*/
for (type++; isSPACE(*type); type++) ;
mode[0] = 'r';
strcat(mode, "t");
if (*type == '&') {
- name = type;
goto duplicity;
}
if (*type == IoTYPE_STD && (!type[1] || isSPACE(type[1]) || type[1] == ':')) {
name = type;
len = tend-type;
}
- if (*name == '\0') { /* command is missing 19990114 */
+ if (*name == '\0') {
+ /* command is missing 19990114 */
if (ckWARN(WARN_PIPE))
Perl_warner(aTHX_ WARN_PIPE, "Missing command in piped open");
errno = EPIPE;
strcat(mode, "b");
else if (in_crlf)
strcat(mode, "t");
- fp = PerlProc_popen(name,mode);
+ if (num_svs > 1) {
+ fp = PerlProc_popen_list(mode,num_svs,svp);
+ }
+ else {
+ fp = PerlProc_popen(name,mode);
+ }
IoTYPE(io) = IoTYPE_PIPE;
}
else {
Perl_warner(aTHX_ WARN_NEWLINE, PL_warn_nl, "open");
goto say_false;
}
+
+ if (ckWARN(WARN_IO)) {
+ if ((IoTYPE(io) == IoTYPE_RDONLY) &&
+ (fp == PerlIO_stdout() || fp == PerlIO_stderr())) {
+ Perl_warner(aTHX_ WARN_IO, "'std%s' opened only for input",
+ (fp == PerlIO_stdout()) ? "out" : "err");
+ }
+ else if ((IoTYPE(io) == IoTYPE_WRONLY) && fp == PerlIO_stdout()) {
+ Perl_warner(aTHX_ WARN_IO, "'stdin' opened only for output");
+ }
+ }
+
if (IoTYPE(io) && IoTYPE(io) != IoTYPE_PIPE && IoTYPE(io) != IoTYPE_STD) {
if (PerlLIO_fstat(PerlIO_fileno(fp),&PL_statbuf) < 0) {
(void)PerlIO_close(fp);
SV *sv;
PerlLIO_dup2(PerlIO_fileno(fp), fd);
+#ifdef VMS
+ if (fd != PerlIO_fileno(PerlIO_stdin())) {
+ char newname[FILENAME_MAX+1];
+ if (fgetname(fp, newname)) {
+ if (fd == PerlIO_fileno(PerlIO_stdout())) Perl_vmssetuserlnm("SYS$OUTPUT", newname);
+ if (fd == PerlIO_fileno(PerlIO_stderr())) Perl_vmssetuserlnm("SYS$ERROR", newname);
+ }
+ }
+#endif
LOCK_FDPID_MUTEX;
sv = *av_fetch(PL_fdpid,PerlIO_fileno(fp),TRUE);
(void)SvUPGRADE(sv, SVt_IV);
if (type) {
while (isSPACE(*type)) type++;
if (*type) {
+ errno = 0;
if (PerlIO_apply_layers(aTHX_ IoIFP(io),mode,type) != 0) {
goto say_false;
}
if (!io)
return TRUE;
- else if (ckWARN(WARN_IO)
- && (IoTYPE(io) == IoTYPE_WRONLY || IoIFP(io) == PerlIO_stdout()
- || IoIFP(io) == PerlIO_stderr()))
+ else if (ckWARN(WARN_IO) && (IoTYPE(io) == IoTYPE_WRONLY))
{
/* integrate to report_evil_fh()? */
char *name = NULL;
/* FALL THROUGH */
default:
if (PerlIO_isutf8(fp)) {
- tmps = SvPVutf8(sv, len);
- }
- else {
- if (DO_UTF8(sv))
- sv_utf8_downgrade(sv, FALSE);
- tmps = SvPV(sv, len);
+ if (!SvUTF8(sv))
+ sv_utf8_upgrade(sv = sv_mortalcopy(sv));
}
+ else if (DO_UTF8(sv))
+ sv_utf8_downgrade((sv = sv_mortalcopy(sv)), FALSE);
+ tmps = SvPV(sv, len);
break;
}
/* To detect whether the process is about to overstep its
I32
Perl_my_stat(pTHX)
{
- djSP;
+ dSP;
IO *io;
GV* gv;
I32
Perl_my_lstat(pTHX)
{
- djSP;
+ dSP;
SV *sv;
STRLEN n_a;
if (PL_op->op_flags & OPf_REF) {
*a++ = "";
}
*a = Nullch;
- if (*PL_Argv[0] != '/') /* will execvp use PATH? */
+ if (really)
+ tmps = SvPV(really, n_a);
+ if ((!really && *PL_Argv[0] != '/') ||
+ (really && *tmps != '/')) /* will execvp use PATH? */
TAINT_ENV(); /* testing IFS here is overkill, probably */
- if (really && *(tmps = SvPV(really, n_a)))
+ if (really && *tmps)
PerlProc_execvp(tmps,EXEC_ARGV_CAST(PL_Argv));
else
PerlProc_execvp(PL_Argv[0],EXEC_ARGV_CAST(PL_Argv));
if (ckWARN(WARN_EXEC))
Perl_warner(aTHX_ WARN_EXEC, "Can't exec \"%s\": %s",
- PL_Argv[0], Strerror(errno));
+ (really ? tmps : PL_Argv[0]), Strerror(errno));
if (do_report) {
int e = errno;
#endif /* SYSV IPC */
+/*
+=for apidoc start_glob
+Function called by C<do_readline> to spawn a glob (or do the glob inside
+perl on VMS). This code used to be inline, but now perl uses C<File::Glob>
+this glob starter is only used by miniperl during the build proccess.
+Moving it away shrinks pp_hot.c; shrinking pp_hot.c helps speed perl up.
+
+=cut
+*/
+
+PerlIO *
+Perl_start_glob (pTHX_ SV *tmpglob, IO *io)
+{
+ SV *tmpcmd = NEWSV(55, 0);
+ PerlIO *fp;
+ ENTER;
+ SAVEFREESV(tmpcmd);
+#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
+ /* since spawning off a process is a real performance hit */
+ {
+#include <descrip.h>
+#include <lib$routines.h>
+#include <nam.h>
+#include <rmsdef.h>
+ char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
+ char vmsspec[NAM$C_MAXRSS+1];
+ char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
+ char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
+ $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
+ PerlIO *tmpfp;
+ STRLEN i;
+ struct dsc$descriptor_s wilddsc
+ = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
+ struct dsc$descriptor_vs rsdsc
+ = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
+ unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
+
+ /* We could find out if there's an explicit dev/dir or version
+ by peeking into lib$find_file's internal context at
+ ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
+ but that's unsupported, so I don't want to do it now and
+ have it bite someone in the future. */
+ strcat(tmpfnam,PerlLIO_tmpnam(NULL));
+ cp = SvPV(tmpglob,i);
+ for (; i; i--) {
+ if (cp[i] == ';') hasver = 1;
+ if (cp[i] == '.') {
+ if (sts) hasver = 1;
+ else sts = 1;
+ }
+ if (cp[i] == '/') {
+ hasdir = isunix = 1;
+ break;
+ }
+ if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
+ hasdir = 1;
+ break;
+ }
+ }
+ if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
+ Stat_t st;
+ if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
+ ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
+ else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
+ if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
+ while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
+ &dfltdsc,NULL,NULL,NULL))&1)) {
+ end = rstr + (unsigned long int) *rslt;
+ if (!hasver) while (*end != ';') end--;
+ *(end++) = '\n'; *end = '\0';
+ for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
+ if (hasdir) {
+ if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
+ begin = rstr;
+ }
+ else {
+ begin = end;
+ while (*(--begin) != ']' && *begin != '>') ;
+ ++begin;
+ }
+ ok = (PerlIO_puts(tmpfp,begin) != EOF);
+ }
+ if (cxt) (void)lib$find_file_end(&cxt);
+ if (ok && sts != RMS$_NMF &&
+ sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
+ if (!ok) {
+ if (!(sts & 1)) {
+ SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
+ }
+ PerlIO_close(tmpfp);
+ fp = NULL;
+ }
+ else {
+ PerlIO_rewind(tmpfp);
+ IoTYPE(io) = IoTYPE_RDONLY;
+ IoIFP(io) = fp = tmpfp;
+ IoFLAGS(io) &= ~IOf_UNTAINT; /* maybe redundant */
+ }
+ }
+ }
+#else /* !VMS */
+#ifdef MACOS_TRADITIONAL
+ sv_setpv(tmpcmd, "glob ");
+ sv_catsv(tmpcmd, tmpglob);
+ sv_catpv(tmpcmd, " |");
+#else
+#ifdef DOSISH
+#ifdef OS2
+ sv_setpv(tmpcmd, "for a in ");
+ sv_catsv(tmpcmd, tmpglob);
+ sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
+#else
+#ifdef DJGPP
+ sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
+ sv_catsv(tmpcmd, tmpglob);
+#else
+ sv_setpv(tmpcmd, "perlglob ");
+ sv_catsv(tmpcmd, tmpglob);
+ sv_catpv(tmpcmd, " |");
+#endif /* !DJGPP */
+#endif /* !OS2 */
+#else /* !DOSISH */
+#if defined(CSH)
+ sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
+ sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
+ sv_catsv(tmpcmd, tmpglob);
+ sv_catpv(tmpcmd, "' 2>/dev/null |");
+#else
+ sv_setpv(tmpcmd, "echo ");
+ sv_catsv(tmpcmd, tmpglob);
+#if 'z' - 'a' == 25
+ sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
+#else
+ sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
+#endif
+#endif /* !CSH */
+#endif /* !DOSISH */
+#endif /* MACOS_TRADITIONAL */
+ (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
+ FALSE, O_RDONLY, 0, Nullfp);
+ fp = IoIFP(io);
+#endif /* !VMS */
+ LEAVE;
+ return fp;
+}