aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-01-21 11:43:35 +0100
committerGustav Sörnäs <gustav@sornas.net>2021-01-21 11:54:26 +0100
commit49a5b684555dcd0bd101d546dc843e5f4f815e5c (patch)
tree6bdef346ac2c50b03389072867323104ad4fae90
parent66456b8a0b622bb63297f97eea6349de07db0ae5 (diff)
downloadpintos-linked-list-49a5b684555dcd0bd101d546dc843e5f4f815e5c.tar.gz
add read helpers
-rw-r--r--main.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/main.c b/main.c
index 05f17ac..abe0ef6 100644
--- a/main.c
+++ b/main.c
@@ -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) {
}