Commit b68d24fa authored by Sergej Chodarev's avatar Sergej Chodarev
Browse files

Remove use of nonstandard getline function

parent 1f904eff
CC = gcc CC = gcc
CFLAGS = -std=gnu11 -Wall -pedantic CFLAGS = -std=c11 -Wall -pedantic -g
LDFLAGS = LDFLAGS =
TARGET = notwork TARGET = notwork
......
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "wall.h" #include "wall.h"
void empty_input_buffer() { void empty_input_buffer() {
...@@ -19,20 +20,23 @@ void show_wall_ui(wall_t *wall) { ...@@ -19,20 +20,23 @@ void show_wall_ui(wall_t *wall) {
} }
void write_post_ui(wall_t *wall) { void write_post_ui(wall_t *wall) {
char *text = NULL; char text[MAX_POST_LENGTH + 2]; // leave space for newline and 0
size_t length = 0; size_t newline_pos = 0;
int read;
printf("Enter your post:\n"); printf("Enter your post:\n");
read = getline(&text, &length, stdin); fgets(text, MAX_POST_LENGTH + 2, stdin);
text[read - 1] = '\0'; // remove the trailing newline
newline_pos = strcspn(text, "\n");
if (newline_pos <= MAX_POST_LENGTH) {
text[newline_pos] = '\0'; // remove the trailing newline
} else { // Newline not found, so input must be longer then the limit
printf("Post longer then %d characters. It would be truncated.\n",
MAX_POST_LENGTH);
text[MAX_POST_LENGTH] = 0;
empty_input_buffer();
}
post_t *post = create_post(text); post_t *post = create_post(text);
free(text);
if (post != NULL) {
add_post(wall, post); add_post(wall, post);
} else {
printf("Try to fit your post into %d characters.", MAX_POST_LENGTH);
}
} }
post_t *select_post_ui(wall_t *wall, char *question) { post_t *select_post_ui(wall_t *wall, char *question) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment