sex

simple editor extended using libtermbox
git clone git://z3bra.org/sex
Log | Files | Refs | README

commit 9e6662fa43f6c1c6620b72a70a6e7e670beb56e8
parent b5eeba8daea9d3976388f46981fcb5aa20d3db28
Author: z3bra <willyatmailoodotorg>
Date:   Thu Nov 19 14:29:42 2015

Add tail queue to load the file

Diffstat:
 sex.c | 41 +++++++++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 8 deletions(-)

diff --git a/sex.c b/sex.c @@ -1,6 +1,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <sys/mman.h> +#include <sys/queue.h> #include <termbox.h> #include <unistd.h> #include <string.h> @@ -15,8 +16,7 @@ struct ln_s { char *p; size_t len; - struct ln_s *prev; - struct ln_s *next; + TAILQ_ENTRY(ln_s) entries; }; struct file_s { @@ -29,6 +29,7 @@ struct file_s { }; static struct file_s f; +TAILQ_HEAD(ln_s_head, ln_s) head; void cleanup(void) { @@ -50,9 +51,10 @@ init_termbox(void) void open_file(char *path) { - size_t i; + size_t i, last_eol; int d; struct stat s; + struct ln_s *tmp; f.path = path; @@ -69,13 +71,23 @@ open_file(char *path) if (f.map == MAP_FAILED) err(1, "mmap()"); - i = f.size; f.p = f.map; - while (--i) - if (*++f.map == '\n') { - *f.map = '\0'; + TAILQ_INIT(&head); + + for (i = last_eol = 0; i < f.size; ++i) { + if (*(f.map + i) == '\n') { + tmp = malloc(sizeof(*tmp)); + if (tmp == NULL) { + perror("malloc failed"); + exit(1); + } + tmp->p = f.map + last_eol; + tmp->len = i - last_eol + 1; + last_eol = i + 1; + TAILQ_INSERT_TAIL(&head, tmp, entries); } + } } void @@ -83,6 +95,17 @@ edit(char *path) { open_file(path); } +ssize_t +write_file(int d) +{ + ssize_t s = 0; + struct ln_s *tmp; + TAILQ_FOREACH(tmp, &head, entries) + s += write(d, tmp->p, tmp->len); + + return s; +} + int main(int argc, char **argv) { @@ -99,8 +122,10 @@ main(int argc, char **argv) /* init_termbox(); */ atexit(cleanup); - while (*argv) + while (*argv) { edit(*argv++); + write_file(1); + } return 0; }