1 /* @(#)fcons.c 2.20 10/11/06 Copyright 1986, 1995-2010 J. Schilling */ 2 /* 3 * Copyright (c) 1986, 1995-2010 J. Schilling 4 */ 5 /* 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License, Version 1.0 only 8 * (the "License"). You may not use this file except in compliance 9 * with the License. 10 * 11 * See the file CDDL.Schily.txt in this distribution for details. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file CDDL.Schily.txt from this distribution. 15 */ 16 17 #include "schilyio.h" 18 19 /* 20 * Note that because of a definition in schilyio.h we are using 21 * fseeko()/ftello() instead of fseek()/ftell() if available. 22 */ 23 24 LOCAL char *fmtab[] = { 25 "", /* 0 FI_NONE */ 26 "r", /* 1 FI_READ */ 27 "w", /* 2 FI_WRITE **1) */ 28 "r+", /* 3 FI_READ | FI_WRITE */ 29 "b", /* 4 FI_NONE | FI_BINARY */ 30 "rb", /* 5 FI_READ | FI_BINARY */ 31 "wb", /* 6 FI_WRITE | FI_BINARY **1) */ 32 "r+b", /* 7 FI_READ | FI_WRITE | FI_BINARY */ 33 34 /* + FI_APPEND */ "", /* 0 FI_NONE */ 35 /* ... */ "r", /* 1 FI_READ */ 36 "a", /* 2 FI_WRITE **1) */ 37 "a+", /* 3 FI_READ | FI_WRITE */ 38 "b", /* 4 FI_NONE | FI_BINARY */ 39 "rb", /* 5 FI_READ | FI_BINARY */ 40 "ab", /* 6 FI_WRITE | FI_BINARY **1) */ 41 "a+b", /* 7 FI_READ | FI_WRITE | FI_BINARY */ 42 }; 43 /* 44 * NOTES: 45 * 1) fdopen() guarantees not to create/trunc files in this case 46 * 47 * "w" will create/trunc files with fopen() 48 * "a" will create files with fopen() 49 */ 50 51 52 EXPORT FILE * 53 _fcons(fd, f, flag) 54 register FILE *fd; 55 int f; 56 int flag; 57 { 58 int my_gflag = _io_glflag; 59 60 if (fd == (FILE *)NULL) 61 fd = fdopen(f, 62 fmtab[flag&(FI_READ|FI_WRITE|FI_BINARY | FI_APPEND)]); 63 64 if (fd != (FILE *)NULL) { 65 if (flag & FI_APPEND) { 66 (void) fseek(fd, (off_t)0, SEEK_END); 67 } 68 if (flag & FI_UNBUF) { 69 setbuf(fd, NULL); 70 my_gflag |= _JS_IOUNBUF; 71 } 72 set_my_flag(fd, my_gflag); /* must clear it if fd is reused */ 73 return (fd); 74 } 75 if (flag & FI_CLOSE) 76 close(f); 77 78 return ((FILE *)NULL); 79 } 80