diff options
| -rw-r--r-- | main.c | 26 |
1 files changed, 26 insertions, 0 deletions
@@ -8,6 +8,32 @@ struct student { struct list_elem elem; }; +// Read a line from stdin, strip the eventual newline and return a pointer +// to the string. Remember to free the pointer. +char *read_input() { + char *buf = NULL; + size_t buf_size = 0; + size_t read = getline(&buf, &buf_size, stdin); + if (read == -1) { + free(buf); + buf = NULL; + } else if (read >= 1 && buf[read - 1] == '\n') { + buf[read - 1] = '\0'; + } + return buf; +} + +int read_int() { + char *buf = read_input(); + int res = -1; + if (!buf) { + printf("Failed to read int from stdin\n"); + } else { + res = atoi(buf); + } + free(buf); + return res; +} void insert (struct list *student_list) { } |
