cirilisp/util.c

32 lines
629 B
C
Raw Normal View History

#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;
}