return PerlIO_tmpfile();
else {
char *name = SvPV_nolen(*args);
- if (*mode == '#') {
+ if (*mode == IoTYPE_NUMERIC) {
fd = PerlLIO_open3(name, imode, perm);
if (fd >= 0)
return PerlIO_fdopen(fd, (char *) mode + 1);
if (tab->Set_ptrcnt != NULL)
l->flags |= PERLIO_F_FASTGETS;
if (mode) {
- if (*mode == '#' || *mode == 'I')
+ if (*mode == IoTYPE_NUMERIC || *mode == IoTYPE_IMPLICIT)
mode++;
switch (*mode++) {
case 'r':
PerlIOUnix_oflags(const char *mode)
{
int oflags = -1;
- if (*mode == 'I' || *mode == '#')
+ if (*mode == IoTYPE_IMPLICIT || *mode == IoTYPE_NUMERIC)
mode++;
switch (*mode) {
case 'r':
return code;
}
+IV
+PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
+{
+ int fd = PerlIOSelf(f, PerlIOUnix)->fd;
+ Off_t new;
+ if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
+#ifdef ESPIPE
+ SETERRNO(ESPIPE, LIB_INVARG);
+#else
+ SETERRNO(EINVAL, LIB_INVARG);
+#endif
+ return -1;
+ }
+ new = PerlLIO_lseek(fd, offset, whence);
+ if (new == (Off_t) - 1)
+ {
+ return -1;
+ }
+ PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
+ return 0;
+}
+
PerlIO *
PerlIOUnix_open(pTHX_ PerlIO_funcs *self, PerlIO_list_t *layers,
IV n, const char *mode, int fd, int imode,
}
if (narg > 0) {
char *path = SvPV_nolen(*args);
- if (*mode == '#')
+ if (*mode == IoTYPE_NUMERIC)
mode++;
else {
imode = PerlIOUnix_oflags(mode);
}
}
if (fd >= 0) {
- if (*mode == 'I')
+ if (*mode == IoTYPE_IMPLICIT)
mode++;
if (!f) {
f = PerlIO_allocate(aTHX);
}
PerlIOUnix_setfd(aTHX_ f, fd, imode);
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
+ if (*mode == IoTYPE_APPEND)
+ PerlIOUnix_seek(aTHX_ f, 0, SEEK_END);
return f;
}
else {
}
}
-IV
-PerlIOUnix_seek(pTHX_ PerlIO *f, Off_t offset, int whence)
-{
- int fd = PerlIOSelf(f, PerlIOUnix)->fd;
- Off_t new;
- if (PerlIOBase(f)->flags & PERLIO_F_NOTREG) {
-#ifdef ESPIPE
- SETERRNO(ESPIPE, LIB_INVARG);
-#else
- SETERRNO(EINVAL, LIB_INVARG);
-#endif
- return -1;
- }
- new = PerlLIO_lseek(fd, offset, whence);
- if (new == (Off_t) - 1)
- {
- return -1;
- }
- PerlIOBase(f)->flags &= ~PERLIO_F_EOF;
- return 0;
-}
-
Off_t
PerlIOUnix_tell(pTHX_ PerlIO *f)
{
while (*mode) {
*tmode++ = *mode++;
}
-#ifdef PERLIO_USING_CRLF
+#if defined(PERLIO_USING_CRLF) || defined(__CYGWIN__)
*tmode++ = 'b';
#endif
*tmode = '\0';
else {
if (narg > 0) {
char *path = SvPV_nolen(*args);
- if (*mode == '#') {
+ if (*mode == IoTYPE_NUMERIC) {
mode++;
fd = PerlLIO_open3(path, imode, perm);
}
else {
- FILE *stdio = PerlSIO_fopen(path, mode);
+ FILE *stdio;
+ bool appended = FALSE;
+#ifdef __CYGWIN__
+ /* Cygwin wants its 'b' early. */
+ appended = TRUE;
+ mode = PerlIOStdio_mode(mode, tmode);
+#endif
+ stdio = PerlSIO_fopen(path, mode);
if (stdio) {
PerlIOStdio *s;
if (!f) {
f = PerlIO_allocate(aTHX);
}
- if ((f = PerlIO_push(aTHX_ f, self,
- (mode = PerlIOStdio_mode(mode, tmode)),
- PerlIOArg))) {
+ if (!appended)
+ mode = PerlIOStdio_mode(mode, tmode);
+ f = PerlIO_push(aTHX_ f, self, mode, PerlIOArg);
+ if (f) {
s = PerlIOSelf(f, PerlIOStdio);
s->stdio = stdio;
PerlIOUnix_refcnt_inc(fileno(s->stdio));
if (fd >= 0) {
FILE *stdio = NULL;
int init = 0;
- if (*mode == 'I') {
+ if (*mode == IoTYPE_IMPLICIT) {
init = 1;
mode++;
}
else {
PerlIO_funcs *tab = PerlIO_layer_fetch(aTHX_ layers, n - 1, PerlIO_default_btm());
int init = 0;
- if (*mode == 'I') {
+ if (*mode == IoTYPE_IMPLICIT) {
init = 1;
/*
* mode++;
f, PerlIOBase(f)->tab->name, (mode) ? mode : "(Null)",
PerlIOBase(f)->flags);
#endif
+ {
+ /* Enable the first CRLF capable layer you can find, but if none
+ * found, the one we just pushed is fine. This results in at
+ * any given moment at most one CRLF-capable layer being enabled
+ * in the whole layer stack. */
+ PerlIO *g = PerlIONext(f);
+ while (g && *g) {
+ PerlIOl *b = PerlIOBase(g);
+ if (b && b->tab == &PerlIO_crlf) {
+ if (!(b->flags & PERLIO_F_CRLF))
+ b->flags |= PERLIO_F_CRLF;
+ PerlIO_pop(aTHX_ f);
+ return code;
+ }
+ g = PerlIONext(g);
+ }
+ }
return code;
}