int attribute_compat_text_section _IO_old_file_close_it (_IO_FILE *fp) { int write_status, close_status; if (!_IO_file_is_open (fp)) return EOF; write_status = _IO_old_do_flush (fp); _IO_unsave_markers (fp); close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0 ? _IO_SYSCLOSE (fp) : 0); /* Free buffer. */ _IO_setb (fp, NULL, NULL, 0); _IO_setg (fp, NULL, NULL, NULL); _IO_setp (fp, NULL, NULL); _IO_un_link ((struct _IO_FILE_plus *) fp); fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS; fp->_fileno = -1; fp->_old_offset = _IO_pos_BAD; return close_status ? close_status : write_status; }
void _IO_new_file_finish (_IO_FILE *fp, int dummy) { if (_IO_file_is_open (fp)) { _IO_do_flush (fp); if (!(fp->_flags & _IO_DELETE_DONT_CLOSE)) _IO_SYSCLOSE (fp); } _IO_default_finish (fp, 0); }
void attribute_compat_text_section _IO_old_file_finish (_IO_FILE *fp, int dummy) { if (_IO_file_is_open (fp)) { _IO_old_do_flush (fp); if (!(fp->_flags & _IO_DELETE_DONT_CLOSE)) _IO_SYSCLOSE (fp); } _IO_default_finish (fp, 0); }
attribute_compat_text_section _IO_old_file_attach (_IO_FILE *fp, int fd) { if (_IO_file_is_open (fp)) return NULL; fp->_fileno = fd; fp->_flags &= ~(_IO_NO_READS+_IO_NO_WRITES); fp->_flags |= _IO_DELETE_DONT_CLOSE; /* Get the current position of the file. */ /* We have to do that since that may be junk. */ fp->_old_offset = _IO_pos_BAD; if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_cur, _IOS_INPUT|_IOS_OUTPUT) == _IO_pos_BAD && errno != ESPIPE) return NULL; return fp; }
attribute_compat_text_section _IO_old_file_fopen (_IO_FILE *fp, const char *filename, const char *mode) { int oflags = 0, omode; int read_write, fdesc; int oprot = 0666; if (_IO_file_is_open (fp)) return 0; switch (*mode++) { case 'r': omode = O_RDONLY; read_write = _IO_NO_WRITES; break; case 'w': omode = O_WRONLY; oflags = O_CREAT|O_TRUNC; read_write = _IO_NO_READS; break; case 'a': omode = O_WRONLY; oflags = O_CREAT|O_APPEND; read_write = _IO_NO_READS|_IO_IS_APPENDING; break; default: __set_errno (EINVAL); return NULL; } if (mode[0] == '+' || (mode[0] == 'b' && mode[1] == '+')) { omode = O_RDWR; read_write &= _IO_IS_APPENDING; } fdesc = open (filename, omode|oflags, oprot); if (fdesc < 0) return NULL; fp->_fileno = fdesc; _IO_mask_flags (fp, read_write,_IO_NO_READS+_IO_NO_WRITES+_IO_IS_APPENDING); if (read_write & _IO_IS_APPENDING) if (_IO_SEEKOFF (fp, (_IO_off_t)0, _IO_seek_end, _IOS_INPUT|_IOS_OUTPUT) == _IO_pos_BAD && errno != ESPIPE) return NULL; _IO_link_in ((struct _IO_FILE_plus *) fp); return fp; }
int _IO_new_file_close_it (_IO_FILE *fp) { int write_status; if (!_IO_file_is_open (fp)) return EOF; if ((fp->_flags & _IO_NO_WRITES) == 0 && (fp->_flags & _IO_CURRENTLY_PUTTING) != 0) write_status = _IO_do_flush (fp); else write_status = 0; _IO_unsave_markers (fp); int close_status = ((fp->_flags2 & _IO_FLAGS2_NOCLOSE) == 0 ? _IO_SYSCLOSE (fp) : 0); /* Free buffer. */ #if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T if (fp->_mode > 0) { if (_IO_have_wbackup (fp)) _IO_free_wbackup_area (fp); _IO_wsetb (fp, NULL, NULL, 0); _IO_wsetg (fp, NULL, NULL, NULL); _IO_wsetp (fp, NULL, NULL); } #endif _IO_setb (fp, NULL, NULL, 0); _IO_setg (fp, NULL, NULL, NULL); _IO_setp (fp, NULL, NULL); _IO_un_link ((struct _IO_FILE_plus *) fp); fp->_flags = _IO_MAGIC|CLOSED_FILEBUF_FLAGS; fp->_fileno = -1; fp->_offset = _IO_pos_BAD; return close_status ? close_status : write_status; }
_IO_FILE * _IO_new_file_fopen (_IO_FILE *fp, const char *filename, const char *mode, int is32not64) { int oflags = 0, omode; int read_write; int oprot = 0666; int i; _IO_FILE *result; #ifdef _LIBC const char *cs; const char *last_recognized; #endif if (_IO_file_is_open (fp)) return 0; switch (*mode) { case 'r': omode = O_RDONLY; read_write = _IO_NO_WRITES; break; case 'w': omode = O_WRONLY; oflags = O_CREAT|O_TRUNC; read_write = _IO_NO_READS; break; case 'a': omode = O_WRONLY; oflags = O_CREAT|O_APPEND; read_write = _IO_NO_READS|_IO_IS_APPENDING; break; default: __set_errno (EINVAL); return NULL; } #ifdef _LIBC last_recognized = mode; #endif for (i = 1; i < 7; ++i) { switch (*++mode) { case '\0': break; case '+': omode = O_RDWR; read_write &= _IO_IS_APPENDING; #ifdef _LIBC last_recognized = mode; #endif continue; case 'x': oflags |= O_EXCL; #ifdef _LIBC last_recognized = mode; #endif continue; case 'b': #ifdef _LIBC last_recognized = mode; #endif continue; case 'm': fp->_flags2 |= _IO_FLAGS2_MMAP; continue; case 'c': fp->_flags2 |= _IO_FLAGS2_NOTCANCEL; continue; case 'e': #ifdef O_CLOEXEC oflags |= O_CLOEXEC; #endif fp->_flags2 |= _IO_FLAGS2_CLOEXEC; continue; default: /* Ignore. */ continue; } break; } result = _IO_file_open (fp, filename, omode|oflags, oprot, read_write, is32not64); if (result != NULL) { #ifndef __ASSUME_O_CLOEXEC if ((fp->_flags2 & _IO_FLAGS2_CLOEXEC) != 0 && __have_o_cloexec <= 0) { int fd = _IO_fileno (fp); if (__have_o_cloexec == 0) { int flags = __fcntl (fd, F_GETFD); __have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1; } if (__have_o_cloexec < 0) __fcntl (fd, F_SETFD, FD_CLOEXEC); } #endif /* Test whether the mode string specifies the conversion. */ cs = strstr (last_recognized + 1, ",ccs="); if (cs != NULL) { /* Yep. Load the appropriate conversions and set the orientation to wide. */ struct gconv_fcts fcts; struct _IO_codecvt *cc; char *endp = __strchrnul (cs + 5, ','); char *ccs = malloc (endp - (cs + 5) + 3); if (ccs == NULL) { int malloc_err = errno; /* Whatever malloc failed with. */ (void) _IO_file_close_it (fp); __set_errno (malloc_err); return NULL; } *((char *) __mempcpy (ccs, cs + 5, endp - (cs + 5))) = '\0'; strip (ccs, ccs); if (__wcsmbs_named_conv (&fcts, ccs[2] == '\0' ? upstr (ccs, cs + 5) : ccs) != 0) { /* Something went wrong, we cannot load the conversion modules. This means we cannot proceed since the user explicitly asked for these. */ (void) _IO_file_close_it (fp); free (ccs); __set_errno (EINVAL); return NULL; } free (ccs); assert (fcts.towc_nsteps == 1); assert (fcts.tomb_nsteps == 1); fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end; fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_write_base; /* Clear the state. We start all over again. */ memset (&fp->_wide_data->_IO_state, '\0', sizeof (__mbstate_t)); memset (&fp->_wide_data->_IO_last_state, '\0', sizeof (__mbstate_t)); cc = fp->_codecvt = &fp->_wide_data->_codecvt; /* The functions are always the same. */ *cc = __libio_codecvt; cc->__cd_in.__cd.__nsteps = fcts.towc_nsteps; cc->__cd_in.__cd.__steps = fcts.towc; cc->__cd_in.__cd.__data[0].__invocation_counter = 0; cc->__cd_in.__cd.__data[0].__internal_use = 1; cc->__cd_in.__cd.__data[0].__flags = __GCONV_IS_LAST; cc->__cd_in.__cd.__data[0].__statep = &result->_wide_data->_IO_state; cc->__cd_out.__cd.__nsteps = fcts.tomb_nsteps; cc->__cd_out.__cd.__steps = fcts.tomb; cc->__cd_out.__cd.__data[0].__invocation_counter = 0; cc->__cd_out.__cd.__data[0].__internal_use = 1; cc->__cd_out.__cd.__data[0].__flags = __GCONV_IS_LAST | __GCONV_TRANSLIT; cc->__cd_out.__cd.__data[0].__statep = &result->_wide_data->_IO_state; /* From now on use the wide character callback functions. */ _IO_JUMPS_FILE_plus (fp) = fp->_wide_data->_wide_vtable; /* Set the mode now. */ result->_mode = 1; } } return result; }