1 /* Copyright (C) 2004 Manuel Novoa III <mjn3@codepoet.org> 2 * 3 * GNU Library General Public License (LGPL) version 2 or later. 4 * 5 * Dedicated to Toni. See uClibc/DEDICATION.mjn3 for details. 6 */ 7 8 #include "_stdio.h" 9 10 11 #ifdef __DO_UNLOCKED 12 13 fgets_unlocked(char * __restrict s,int n,register FILE * __restrict stream)14char *fgets_unlocked(char *__restrict s, int n, 15 register FILE * __restrict stream) 16 { 17 register char *p; 18 int c; 19 20 __STDIO_STREAM_VALIDATE(stream); 21 22 /* Should we assert here? Or set errno? Or just fail... */ 23 if (n <= 0) { 24 /* __set_errno(EINVAL); */ 25 goto ERROR; 26 } 27 28 p = s; 29 30 while (--n) { 31 if (__STDIO_STREAM_CAN_USE_BUFFER_GET(stream)) { 32 if ((*p++ = __STDIO_STREAM_BUFFER_GET(stream)) == '\n') { 33 break; 34 } 35 } else { 36 if ((c = __fgetc_unlocked(stream)) == EOF) { 37 if (__FERROR_UNLOCKED(stream)) { 38 goto ERROR; 39 } 40 break; 41 } 42 if ((*p++ = c) == '\n') { 43 break; 44 } 45 } 46 } 47 48 if (p > s) { 49 *p = 0; 50 return s; 51 } 52 53 ERROR: 54 return NULL; 55 } 56 libc_hidden_def(fgets_unlocked) 57 58 #ifndef __UCLIBC_HAS_THREADS__ 59 strong_alias(fgets_unlocked,fgets) 60 libc_hidden_def(fgets) 61 #endif 62 63 #elif defined __UCLIBC_HAS_THREADS__ 64 65 char *fgets(char *__restrict s, int n, 66 register FILE * __restrict stream) 67 { 68 char *retval; 69 __STDIO_AUTO_THREADLOCK_VAR; 70 71 __STDIO_AUTO_THREADLOCK(stream); 72 73 retval = fgets_unlocked(s, n, stream); 74 75 __STDIO_AUTO_THREADUNLOCK(stream); 76 77 return retval; 78 } 79 libc_hidden_def(fgets) 80 81 #endif 82