Compare commits
3 commits
5a0ce6d53c
...
ec3fc81544
Author | SHA1 | Date | |
---|---|---|---|
ec3fc81544 | |||
345ea44fb3 | |||
783e95a6ad |
4 changed files with 58 additions and 2 deletions
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
.PHONY: all clean
|
.PHONY: all
|
||||||
|
|
||||||
all: out out/sh
|
all: out out/sh
|
||||||
|
|
||||||
|
|
17
src/main.c
17
src/main.c
|
@ -1,15 +1,29 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
|
#include "reader.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
/*
|
||||||
size_t buffer_size;
|
size_t buffer_size;
|
||||||
size_t initial_buffer_size = 64;
|
size_t initial_buffer_size = 64;
|
||||||
char *input_buffer;
|
char *input_buffer;
|
||||||
char *initial_buffer_position;
|
char *initial_buffer_position;
|
||||||
unsigned int how_many_characters_to_read;
|
unsigned int how_many_characters_to_read;
|
||||||
|
*/
|
||||||
|
char *input_line;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
printf("$ ");
|
||||||
|
input_line = read_a_line(stdin);
|
||||||
|
if (input_line == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("command: '%s'\n", input_line);
|
||||||
|
|
||||||
|
free(input_line);
|
||||||
|
/*
|
||||||
buffer_size = initial_buffer_size;
|
buffer_size = initial_buffer_size;
|
||||||
how_many_characters_to_read = buffer_size + 1;
|
how_many_characters_to_read = buffer_size + 1;
|
||||||
input_buffer = malloc(how_many_characters_to_read);
|
input_buffer = malloc(how_many_characters_to_read);
|
||||||
|
@ -32,5 +46,6 @@ int main() {
|
||||||
printf("%s", input_buffer);
|
printf("%s", input_buffer);
|
||||||
|
|
||||||
free(input_buffer);
|
free(input_buffer);
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
33
src/reader.c
Normal file
33
src/reader.c
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
#include "reader.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
char *read_a_line(FILE *file_to_read) {
|
||||||
|
size_t initial_buffer_size = 64;
|
||||||
|
size_t buffer_size = initial_buffer_size;
|
||||||
|
unsigned int how_many_characters_to_read = buffer_size + 1;
|
||||||
|
char *input_buffer = malloc(how_many_characters_to_read);
|
||||||
|
char *initial_buffer_position = input_buffer;
|
||||||
|
|
||||||
|
while (fgets(input_buffer, how_many_characters_to_read, file_to_read) !=
|
||||||
|
NULL &&
|
||||||
|
input_buffer[strlen(input_buffer) - 1] != '\n') {
|
||||||
|
size_t new_buffer_size = buffer_size * 2;
|
||||||
|
if (buffer_size != initial_buffer_size) {
|
||||||
|
how_many_characters_to_read = buffer_size + 1;
|
||||||
|
}
|
||||||
|
input_buffer = realloc(initial_buffer_position, new_buffer_size + 1);
|
||||||
|
initial_buffer_position = input_buffer;
|
||||||
|
input_buffer += buffer_size;
|
||||||
|
buffer_size = new_buffer_size;
|
||||||
|
}
|
||||||
|
input_buffer = initial_buffer_position;
|
||||||
|
|
||||||
|
if (!feof(file_to_read)) {
|
||||||
|
return input_buffer;
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
8
src/reader.h
Normal file
8
src/reader.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef READER_H
|
||||||
|
#define READER_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
char *read_a_line(FILE *file_to_read);
|
||||||
|
|
||||||
|
#endif /* READER_H */
|
Loading…
Reference in a new issue