aboutsummaryrefslogtreecommitdiff
path: root/examples/ini_file_create.c
diff options
context:
space:
mode:
authorClecio Jung2023-03-23 13:16:18 -0300
committerClecio Jung2023-03-23 13:16:18 -0300
commit308925f81dcac05471e818865a94061f3421f217 (patch)
tree48e4bd797a0a6dd04b971c20598c5476d6cf5fed /examples/ini_file_create.c
parent26f25a80f790a014115dd25e9619d059716bdfbd (diff)
downloadlibini-308925f81dcac05471e818865a94061f3421f217.tar.xz
libini-308925f81dcac05471e818865a94061f3421f217.tar.zst
Simplifying memory management
Diffstat (limited to 'examples/ini_file_create.c')
-rw-r--r--examples/ini_file_create.c17
1 files changed, 5 insertions, 12 deletions
diff --git a/examples/ini_file_create.c b/examples/ini_file_create.c
index 8910e9f..e1cfd21 100644
--- a/examples/ini_file_create.c
+++ b/examples/ini_file_create.c
@@ -6,6 +6,7 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include "../ini_file.h"
@@ -14,22 +15,14 @@
/* This function reads a string of size MAX_STRING_SIZE from stdin.
* It returns 0 if no valid string was retrieved. */
int get_string_from_stdin(const char *const prompt, char *string) {
- int i, empty = 0;
fputs(prompt, stdout);
if (fgets(string, MAX_STRING_SIZE, stdin) == NULL) {
return 0;
}
- /* Replace \n characters for \0 and check if string is empty (only composed of spaces) */
- for (i = 0; string[i] != '\0'; i++) {
- if (string[i] == '\n') {
- string[i] = '\0';
- break;
- }
- if (!isspace(string[i])) {
- empty = 1;
- }
- }
- return empty;
+ /* Replace space and new line characters by \0 */
+ string[strcspn(string, " \t\r\n")] = '\0';
+ /* Check if string is empty */
+ return (string[0] != '\0');
}
/*------------------------------------------------------------------------------