cirilisp/util.c

32 lines
629 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "util.h"
ssize_t _bytesRead;
size_t _nbytes = 2048;
char *_buffer = NULL;
char *readline(char *prompt)
{
if (_buffer == NULL)
{
_buffer = (char *) malloc(_nbytes + 1);
}
fputs(prompt, stdout);
_bytesRead = getline(&_buffer, &_nbytes, stdin);
if (_bytesRead == -1)
{
return NULL;
}
char *cpy = malloc(strlen(_buffer)+1);
strcpy(cpy, _buffer);
cpy[strlen(cpy)-1] = '\0';
// Уклања завршни њу-лајн или ЕОФ у стрингу и копира га на ново место
return cpy;
}