t2ex/t2ex_source/lib/libc/src/stdio/stdio_init.c | bare source | permlink (0.02 seconds) |
1: /* 2: *---------------------------------------------------------------------- 3: * T2EX Software Package 4: * 5: * Copyright 2012 by Ken Sakamura. 6: * This software is distributed under the latest version of T-License 2.x. 7: *---------------------------------------------------------------------- 8: * 9: * Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12. 10: * Modified by T-Engine Forum at 2014/01/08. 11: * Modified by TRON Forum(http://www.tron.org/) at 2015/06/04. 12: * 13: *---------------------------------------------------------------------- 14: */ 15: /* 16: * This software package is available for use, modification, 17: * and redistribution in accordance with the terms of the attached 18: * T-License 2.x. 19: * If you want to redistribute the source code, you need to attach 20: * the T-License 2.x document. 21: * There's no obligation to publish the content, and no obligation 22: * to disclose it to the TRON Forum if you have modified the 23: * software package. 24: * You can also distribute the modified source code. In this case, 25: * please register the modification to T-Kernel traceability service. 26: * People can know the history of modifications by the service, 27: * and can be sure that the version you have inherited some 28: * modification of a particular version or not. 29: * 30: * http://trace.tron.org/tk/?lang=en 31: * http://trace.tron.org/tk/?lang=ja 32: * 33: * As per the provisions of the T-License 2.x, TRON Forum ensures that 34: * the portion of the software that is copyrighted by Ken Sakamura or 35: * the TRON Forum does not infringe the copyrights of a third party. 36: * However, it does not make any warranty other than this. 37: * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held 38: * responsible for any consequences or damages caused directly or 39: * indirectly by the use of this software package. 40: * 41: * The source codes in bsd_source.tar.gz in this software package are 42: * derived from NetBSD or OpenBSD and not covered under T-License 2.x. 43: * They need to be changed or redistributed according to the 44: * representation of each source header. 45: */ 46: 47: /* 48: * @(#)stdio_init.c 49: * 50: */ 51: 52: #include <basic.h> 53: #include <stdio.h> 54: #include <t2ex/util.h> 55: 56: IMPORT int __sdidinit; 57: IMPORT void __sinit(); 58: 59: struct glue { 60: struct glue *next; 61: int niobs; 62: FILE *iobs; 63: }; 64: 65: IMPORT struct glue __sglue; 66: 67: LOCAL void (*cleanup)(void); 68: 69: LOCAL FastUMLock mlock; 70: 71: EXPORT ER libc_stdio_init( void ) 72: { 73: ER er; 74: 75: if (mlock.id == 0) { 76: er = CreateUMLock( &mlock, (UB*)"t2ek" ); 77: if (er < 0) goto e1; 78: } 79: 80: __sinit(); 81: return 0; 82: 83: e1: mlock.id = 0; 84: return er; 85: } 86: 87: EXPORT ER libc_stdio_cleanup( void ) 88: { 89: if (cleanup) { 90: cleanup(); 91: cleanup = NULL; 92: } 93: __sdidinit = 0; 94: 95: if (mlock.id) { 96: DeleteUMLock( &mlock ); 97: mlock.id = 0; 98: } 99: 100: return 0; 101: } 102: 103: EXPORT void __atexit_register_cleanup( void (*f)(void) ) 104: { 105: cleanup = f; 106: } 107: 108: LOCAL int fp2no( FILE *fp ) 109: { 110: int n; 111: struct glue *g; 112: 113: n = 0; 114: for (g = &__sglue; g != NULL; g = g->next) { 115: if (g->iobs <= fp && fp < g->iobs + g->niobs) { 116: return n + (fp - g->iobs); 117: } 118: n += g->niobs; 119: } 120: return -1; 121: } 122: 123: EXPORT void flockfile(FILE *fp) 124: { 125: int n; 126: 127: if (!__sdidinit || mlock.id == 0) goto e1; 128: 129: n = fp2no( fp ); 130: if (n < 0) goto e1; 131: 132: UMLock( &mlock, n ); 133: 134: e1: ; 135: } 136: 137: EXPORT void funlockfile(FILE *fp) 138: { 139: int n; 140: 141: if (!__sdidinit || mlock.id == 0) goto e1; 142: 143: n = fp2no( fp ); 144: if (n < 0) goto e1; 145: 146: UMUnlock( &mlock, n ); 147: 148: e1: ; 149: } 150: 151: #define lock_no_findfp 31 152: 153: EXPORT void __libc_lock_findfp( void ) 154: { 155: int n; 156: 157: if (mlock.id == 0) goto e1; 158: 159: n = lock_no_findfp; 160: 161: UMLock( &mlock, n ); 162: 163: e1: ; 164: } 165: 166: EXPORT void __libc_unlock_findfp( void ) 167: { 168: int n; 169: 170: if (mlock.id == 0) goto e1; 171: 172: n = lock_no_findfp; 173: 174: UMUnlock( &mlock, n ); 175: 176: e1: ; 177: } 178: 179: EXPORT int isatty( int fd ) 180: { 181: int r; 182: 183: r = (0 <= fd && fd <= 2); 184: return r; 185: } 186: