FILE *xtmpfile(char **namep, const char *postfix_long, const char *postfix_short, int do_unlink) { char *templ = xtmptemplate(postfix_long, postfix_short); errno = 0; int fd = mkstemp(templ); if (fd < 0) fatal("cannot create temporary file: %1", strerror(errno)); errno = 0; FILE *fp = fdopen(fd, FOPEN_RWB); // many callers of xtmpfile use binary I/O if (!fp) fatal("fdopen: %1", strerror(errno)); if (do_unlink) add_tmp_file(templ); if (namep) *namep = templ; else a_delete templ; return fp; }
IO_FileChannel__Channel IO_FileChannel__OpenUnbuffered(Object__String xfile, OOC_UINT32 mode) { int flags = 0; char* fname = (char*)OS_Path__Encode(xfile); int fd; Object__String8 tmpName = NULL; if ((mode & rdwr_mask) == rdwr_mask) { flags |= O_RDWR; } else if (mode & rd_mask) { flags |= O_RDONLY; } else if (mode & wr_mask) { flags |= O_WRONLY; } if (mode & creat_mask) { flags |= O_CREAT; } if (mode & excl_mask) { flags |= O_EXCL; } if (mode & trunc_mask) { flags |= O_TRUNC; } if (mode & append_mask) { flags |= O_APPEND; } #ifdef O_BINARY flags |= O_BINARY; #endif if (mode & tmp_mask) { char tname[PATH_MAX+16]; int count = 0; flags |= (O_CREAT|O_EXCL); do { /* repeat trying to open the file until an unused file is found, or an error occured */ if (count) { (void)sprintf(tname, "%s^%d", fname, count); } else { (void)sprintf(tname, "%s^", fname); } fd = open(tname, flags, DEFAULT_MASK); count++; } while ((fd < 0) && (errno == EEXIST)); tmpName = Object__NewLatin1((OOC_CHAR8*)tname, PATH_MAX+16); } else { fd = open(fname, flags, DEFAULT_MASK); } if (fd < 0) { IO_StdChannels__IOError(xfile); } else { IO_FileChannel__Channel ch = RT0__NewObject(OOC_TYPE_DESCR(IO_FileChannel,ChannelDesc)); IO__ByteChannelDesc_INIT((IO__ByteChannel)ch); ch->fd = fd; ch->origName = xfile; ch->tmpName = tmpName; if (tmpName) { add_tmp_file(ch); } else { ch->tmpIndex = -1; } return ch; } }