1*a77546fbSCy Schubert /*- 2*a77546fbSCy Schubert * SPDX-License-Identifier: BSD-3-Clause 3*a77546fbSCy Schubert * 4*a77546fbSCy Schubert * Copyright (c) 1990, 1993 5*a77546fbSCy Schubert * The Regents of the University of California. All rights reserved. 6*a77546fbSCy Schubert * Copyright (c) 2017, 2018 7*a77546fbSCy Schubert * Cyril S. E. Schubert. All rights reserved. 8*a77546fbSCy Schubert * 9*a77546fbSCy Schubert * This code is derived from software contributed to Berkeley by 10*a77546fbSCy Schubert * Chris Torek. 11*a77546fbSCy Schubert * 12*a77546fbSCy Schubert * Redistribution and use in source and binary forms, with or without 13*a77546fbSCy Schubert * modification, are permitted provided that the following conditions 14*a77546fbSCy Schubert * are met: 15*a77546fbSCy Schubert * 1. Redistributions of source code must retain the above copyright 16*a77546fbSCy Schubert * notice, this list of conditions and the following disclaimer. 17*a77546fbSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 18*a77546fbSCy Schubert * notice, this list of conditions and the following disclaimer in the 19*a77546fbSCy Schubert * documentation and/or other materials provided with the distribution. 20*a77546fbSCy Schubert * 3. Neither the name of the University nor the names of its contributors 21*a77546fbSCy Schubert * may be used to endorse or promote products derived from this software 22*a77546fbSCy Schubert * without specific prior written permission. 23*a77546fbSCy Schubert * 24*a77546fbSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25*a77546fbSCy Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26*a77546fbSCy Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27*a77546fbSCy Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28*a77546fbSCy Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29*a77546fbSCy Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30*a77546fbSCy Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31*a77546fbSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32*a77546fbSCy Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33*a77546fbSCy Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34*a77546fbSCy Schubert * SUCH DAMAGE. 35*a77546fbSCy Schubert */ 36*a77546fbSCy Schubert 37*a77546fbSCy Schubert #include <sys/cdefs.h> 38*a77546fbSCy Schubert __FBSDID("$FreeBSD$"); 39*a77546fbSCy Schubert 40*a77546fbSCy Schubert #include "namespace.h" 41*a77546fbSCy Schubert #include <errno.h> 42*a77546fbSCy Schubert #include <unistd.h> 43*a77546fbSCy Schubert #include <stdint.h> 44*a77546fbSCy Schubert #include <stdio.h> 45*a77546fbSCy Schubert #include "un-namespace.h" 46*a77546fbSCy Schubert #include "libc_private.h" 47*a77546fbSCy Schubert #include "local.h" 48*a77546fbSCy Schubert 49*a77546fbSCy Schubert static inline char * 50*a77546fbSCy Schubert _gets_s(char *buf, rsize_t n) 51*a77546fbSCy Schubert { 52*a77546fbSCy Schubert int c; 53*a77546fbSCy Schubert char *s; 54*a77546fbSCy Schubert 55*a77546fbSCy Schubert ORIENT(stdin, -1); 56*a77546fbSCy Schubert for (s = buf, n--; (c = __sgetc(stdin)) != '\n' && n > 0 ; n--) { 57*a77546fbSCy Schubert if (c == EOF) { 58*a77546fbSCy Schubert if (s == buf) { 59*a77546fbSCy Schubert return (NULL); 60*a77546fbSCy Schubert } else 61*a77546fbSCy Schubert break; 62*a77546fbSCy Schubert } else 63*a77546fbSCy Schubert *s++ = c; 64*a77546fbSCy Schubert } 65*a77546fbSCy Schubert 66*a77546fbSCy Schubert /* 67*a77546fbSCy Schubert * If end of buffer reached, discard until \n or eof. 68*a77546fbSCy Schubert * Then throw an error. 69*a77546fbSCy Schubert */ 70*a77546fbSCy Schubert if (n == 0) { 71*a77546fbSCy Schubert /* discard */ 72*a77546fbSCy Schubert while ((c = __sgetc(stdin)) != '\n' && c != EOF); 73*a77546fbSCy Schubert /* throw the error after lock released prior to exit */ 74*a77546fbSCy Schubert __throw_constraint_handler_s("gets_s : end of buffer", E2BIG); 75*a77546fbSCy Schubert return (NULL); 76*a77546fbSCy Schubert } 77*a77546fbSCy Schubert *s = 0; 78*a77546fbSCy Schubert return (buf); 79*a77546fbSCy Schubert } 80*a77546fbSCy Schubert 81*a77546fbSCy Schubert /* ISO/IEC 9899:2011 K.3.7.4.1 */ 82*a77546fbSCy Schubert char * 83*a77546fbSCy Schubert gets_s(char *buf, rsize_t n) 84*a77546fbSCy Schubert { 85*a77546fbSCy Schubert char *ret; 86*a77546fbSCy Schubert if (buf == NULL) { 87*a77546fbSCy Schubert __throw_constraint_handler_s("gets_s : str is NULL", EINVAL); 88*a77546fbSCy Schubert return(NULL); 89*a77546fbSCy Schubert } else if (n > RSIZE_MAX) { 90*a77546fbSCy Schubert __throw_constraint_handler_s("gets_s : n > RSIZE_MAX", 91*a77546fbSCy Schubert EINVAL); 92*a77546fbSCy Schubert return(NULL); 93*a77546fbSCy Schubert } else if (n == 0) { 94*a77546fbSCy Schubert __throw_constraint_handler_s("gets_s : n == 0", EINVAL); 95*a77546fbSCy Schubert return(NULL); 96*a77546fbSCy Schubert } 97*a77546fbSCy Schubert 98*a77546fbSCy Schubert FLOCKFILE_CANCELSAFE(stdin); 99*a77546fbSCy Schubert ret = _gets_s(buf, n); 100*a77546fbSCy Schubert FUNLOCKFILE_CANCELSAFE(); 101*a77546fbSCy Schubert return (ret); 102*a77546fbSCy Schubert } 103