Changeset View
Changeset View
Standalone View
Standalone View
lib/libc/stdio/gets_s.c
/*- | |||||
* SPDX-License-Identifier: BSD-3-Clause | |||||
* | |||||
* Copyright (c) 1990, 1993 | |||||
* The Regents of the University of California. All rights reserved. | |||||
* | |||||
* This code is derived from software contributed to Berkeley by | |||||
* Chris Torek. | |||||
* | |||||
* Redistribution and use in source and binary forms, with or without | |||||
* modification, are permitted provided that the following conditions | |||||
* are met: | |||||
* 1. Redistributions of source code must retain the above copyright | |||||
* notice, this list of conditions and the following disclaimer. | |||||
* 2. Redistributions in binary form must reproduce the above copyright | |||||
* notice, this list of conditions and the following disclaimer in the | |||||
* documentation and/or other materials provided with the distribution. | |||||
* 3. Neither the name of the University nor the names of its contributors | |||||
ed: Do you want this code to be 3-clause BSD licensed on your behalf as well? If so, leave this as… | |||||
Not Done Inline ActionsI think I'll do that. I won't mark this done until it is. cy: I think I'll do that. I won't mark this done until it is. | |||||
* may be used to endorse or promote products derived from this software | |||||
* without specific prior written permission. | |||||
* | |||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |||||
* SUCH DAMAGE. | |||||
*/ | |||||
#include <sys/cdefs.h> | |||||
__FBSDID("$FreeBSD$"); | |||||
#include "namespace.h" | |||||
#include <errno.h> | |||||
#include <unistd.h> | |||||
#include <stdint.h> | |||||
#include <stdio.h> | |||||
#include "un-namespace.h" | |||||
#include "libc_private.h" | |||||
#include "local.h" | |||||
static inline char * | |||||
_gets_s(char *buf, rsize_t n) | |||||
{ | |||||
int c; | |||||
char *s; | |||||
ORIENT(stdin, -1); | |||||
for (s = buf, n--; (c = __sgetc(stdin)) != '\n' && n > 0 ; n--) { | |||||
if (c == EOF) { | |||||
if (s == buf) { | |||||
return (NULL); | |||||
Not Done Inline ActionsStyle nit: space after return (several times) jhb: Style nit: space after return (several times) | |||||
} else | |||||
break; | |||||
} else | |||||
*s++ = c; | |||||
} | |||||
/* | |||||
* If end of buffer reached, discard until \n or eof. | |||||
* Then throw an error. | |||||
*/ | |||||
if (n == 0) { | |||||
/* discard */ | |||||
while ((c = __sgetc(stdin)) != '\n' && c != EOF); | |||||
Done Inline ActionsI'd suggest putting { }'s around this block to prevent potential 'trailing else' issues from being introduced when refactoring. ed: I'd suggest putting { }'s around this block to prevent potential 'trailing else' issues from… | |||||
Not Done Inline ActionsFixed. cy: Fixed. | |||||
/* throw the error after lock released prior to exit */ | |||||
__throw_constraint_handler_s("gets_s : end of buffer", E2BIG); | |||||
return (NULL); | |||||
} | |||||
*s = 0; | |||||
return (buf); | |||||
} | |||||
/* ISO/IEC 9899:2011 K.3.7.4.1 */ | |||||
char * | |||||
Done Inline ActionsThere is no need to have these empty lined in the comment block, right? ed: There is no need to have these empty lined in the comment block, right? | |||||
gets_s(char *buf, rsize_t n) | |||||
{ | |||||
char *ret; | |||||
if (buf == NULL) { | |||||
Done Inline ActionsUnnecessary space. ed: Unnecessary space. | |||||
__throw_constraint_handler_s("gets_s : str is NULL", EINVAL); | |||||
return(NULL); | |||||
Done Inline ActionsYou can remove one pair of parentheses here. ed: You can remove one pair of parentheses here. | |||||
} else if (n > RSIZE_MAX) { | |||||
Done Inline ActionsI actually think this is simpler if you just avoid the goto and duplicate the one line to unlock: FUNLOCKFILE_CANCELSAFE(); __throw_constraint_handler_s("gets_s : end of buffer", E2BIG); return (NULL); You can then remove the 'throw_error' variable and the 'end:' label. jhb: I actually think this is simpler if you just avoid the goto and duplicate the one line to… | |||||
__throw_constraint_handler_s("gets_s : n > RSIZE_MAX", | |||||
EINVAL); | |||||
return(NULL); | |||||
} else if (n == 0) { | |||||
__throw_constraint_handler_s("gets_s : n == 0", EINVAL); | |||||
return(NULL); | |||||
} | |||||
FLOCKFILE_CANCELSAFE(stdin); | |||||
ret = _gets_s(buf, n); | |||||
FUNLOCKFILE_CANCELSAFE(); | |||||
return (ret); | |||||
} |
Do you want this code to be 3-clause BSD licensed on your behalf as well? If so, leave this as is. Otherwise, remove your name from the copyright above and add a second (2-clause BSD?) license to this source file.