13d58139fSopenharmony_ci/* 23d58139fSopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved. 33d58139fSopenharmony_ci * Licensed under Mulan PSL v2. 43d58139fSopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2. 53d58139fSopenharmony_ci * You may obtain a copy of Mulan PSL v2 at: 63d58139fSopenharmony_ci * http://license.coscl.org.cn/MulanPSL2 73d58139fSopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 83d58139fSopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 93d58139fSopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 103d58139fSopenharmony_ci * See the Mulan PSL v2 for more details. 113d58139fSopenharmony_ci * Description: gets_s function 123d58139fSopenharmony_ci * Create: 2014-02-25 133d58139fSopenharmony_ci */ 143d58139fSopenharmony_ci 153d58139fSopenharmony_ci#include "securecutil.h" 163d58139fSopenharmony_ci 173d58139fSopenharmony_ci/* 183d58139fSopenharmony_ci * The parameter size is buffer size in byte 193d58139fSopenharmony_ci */ 203d58139fSopenharmony_ciSECUREC_INLINE void SecTrimCRLF(char *buffer, size_t size) 213d58139fSopenharmony_ci{ 223d58139fSopenharmony_ci size_t len = strlen(buffer); 233d58139fSopenharmony_ci --len; /* Unsigned integer wrapping is accepted and is checked afterwards */ 243d58139fSopenharmony_ci while (len < size && (buffer[len] == '\r' || buffer[len] == '\n')) { 253d58139fSopenharmony_ci buffer[len] = '\0'; 263d58139fSopenharmony_ci --len; /* Unsigned integer wrapping is accepted and is checked next loop */ 273d58139fSopenharmony_ci } 283d58139fSopenharmony_ci} 293d58139fSopenharmony_ci 303d58139fSopenharmony_ci/* 313d58139fSopenharmony_ci * <FUNCTION DESCRIPTION> 323d58139fSopenharmony_ci * The gets_s function reads at most one less than the number of characters 333d58139fSopenharmony_ci * specified by destMax from the std input stream, into the array pointed to by buffer 343d58139fSopenharmony_ci * The line consists of all characters up to and including 353d58139fSopenharmony_ci * the first newline character ('\n'). gets_s then replaces the newline 363d58139fSopenharmony_ci * character with a null character ('\0') before returning the line. 373d58139fSopenharmony_ci * If the first character read is the end-of-file character, a null character 383d58139fSopenharmony_ci * is stored at the beginning of buffer and NULL is returned. 393d58139fSopenharmony_ci * 403d58139fSopenharmony_ci * <INPUT PARAMETERS> 413d58139fSopenharmony_ci * buffer Storage location for input string. 423d58139fSopenharmony_ci * destMax The size of the buffer. 433d58139fSopenharmony_ci * 443d58139fSopenharmony_ci * <OUTPUT PARAMETERS> 453d58139fSopenharmony_ci * buffer is updated 463d58139fSopenharmony_ci * 473d58139fSopenharmony_ci * <RETURN VALUE> 483d58139fSopenharmony_ci * buffer Successful operation 493d58139fSopenharmony_ci * NULL Improper parameter or read fail 503d58139fSopenharmony_ci */ 513d58139fSopenharmony_cichar *gets_s(char *buffer, size_t destMax) 523d58139fSopenharmony_ci{ 533d58139fSopenharmony_ci#ifdef SECUREC_COMPATIBLE_WIN_FORMAT 543d58139fSopenharmony_ci size_t bufferSize = ((destMax == (size_t)(-1)) ? SECUREC_STRING_MAX_LEN : destMax); 553d58139fSopenharmony_ci#else 563d58139fSopenharmony_ci size_t bufferSize = destMax; 573d58139fSopenharmony_ci#endif 583d58139fSopenharmony_ci 593d58139fSopenharmony_ci if (buffer == NULL || bufferSize == 0 || bufferSize > SECUREC_STRING_MAX_LEN) { 603d58139fSopenharmony_ci SECUREC_ERROR_INVALID_PARAMTER("gets_s"); 613d58139fSopenharmony_ci return NULL; 623d58139fSopenharmony_ci } 633d58139fSopenharmony_ci 643d58139fSopenharmony_ci if (fgets(buffer, (int)bufferSize, SECUREC_STREAM_STDIN) != NULL) { 653d58139fSopenharmony_ci SecTrimCRLF(buffer, bufferSize); 663d58139fSopenharmony_ci return buffer; 673d58139fSopenharmony_ci } 683d58139fSopenharmony_ci 693d58139fSopenharmony_ci return NULL; 703d58139fSopenharmony_ci} 713d58139fSopenharmony_ci 72