diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-01-21 11:43:35 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-01-21 11:54:26 +0100 |
| commit | 49a5b684555dcd0bd101d546dc843e5f4f815e5c (patch) | |
| tree | 6bdef346ac2c50b03389072867323104ad4fae90 | |
| parent | 66456b8a0b622bb63297f97eea6349de07db0ae5 (diff) | |
| download | pintos-linked-list-49a5b684555dcd0bd101d546dc843e5f4f815e5c.tar.gz | |
add read helpers
| -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) { } |
