-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadline.c
44 lines (34 loc) · 851 Bytes
/
readline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) 2014, Kevin Walsh. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License").
// Author: [email protected]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "readline.h"
void rl_initialize(void) { }
void rl_bind_key(char c, void *f) { }
void *rl_insert; // unused
void add_history(char *line) { }
char *readline(char *prompt)
{
printf("%s", prompt);
int have = 0, capacity = 2;
char *buf = NULL;
do {
capacity *= 2;
char *buf2 = realloc(buf, capacity);
if (!buf2) {
free(buf);
printf("out of memory\n");
return NULL;
}
buf = buf2;
if (!fgets(buf+have, capacity-have, stdin)) {
free(buf);
return NULL;
}
have += strlen(buf+have);
} while (buf[have-1] != '\n');
buf[have-1] = '\0';
return buf;
}