~siborgium/dumbme

5dd06b59e12a2617385e93463316e65ab6b53c40 — Sergey Smirnykh 11 months ago 5f38e00
handle fork errs, early $EDITOR check, Makefile tweak
2 files changed, 19 insertions(+), 6 deletions(-)

M Makefile
M dumbme.c
M Makefile => Makefile +6 -2
@@ 2,8 2,12 @@
CC?=gcc
CFLAGS?=-Wall -Wextra -Wconversion -Werror -Wuninitialized -Winit-self -Wno-unused-result -pedantic -pipe -O2 -march=native -fomit-frame-pointer

dumbme:
	$(CC) $(CFLAGS) dumbme.c -o dumbme
all: dumbme

dumbme: dumbme.c
	$(CC) $< $(CFLAGS) -o $@

clean:
	rm dumbme

.PHONY: all clean

M dumbme.c => dumbme.c +13 -4
@@ 38,6 38,13 @@ int main() {
    char*             lines_2_tmp;
    size_t            lines_2_capacity;

    editor = getenv ("EDITOR");
    if (!editor || !strlen (editor)) {
        fputs ("$EDITOR is not set\n", stderr);
        exit (-1);
    }
    editor = strdup (editor);

    lines_1 = read_lines (STDIN_FILENO);
    assert (lines_1);



@@ 72,10 79,12 @@ int main() {
    fclose (tmp_stream);

    // Run editor
    if ((editor_pid = fork()) == 0) {
        editor = getenv ("EDITOR");
        assert (editor);
        editor = strdup (editor);
    editor_pid = fork();
    switch (editor_pid) {
    case -1:
        perror ("Error: fork failed");
        exit (-1);
    case 0:
        err = dup2 (STDOUT_FILENO, STDIN_FILENO);
        assert (err != -1);
        if (execlp (editor, editor, tmp_file_name, (char*)0) == -1) {