myadm

Simple MySQL client for the terminal
git clone git://git.bitsmanent.org/myadm
Log | Files | Refs | README | LICENSE

commit 09aaf2e885c7b716e072f1d3118120589437aff6
parent 657572271386ef097d848124db9108a2b7d37c4a
Author: Claudio Alessi <smoppy@gmail.com>
Date:   Mon, 25 Apr 2016 13:42:04 +0200

Improve (and fix) fget/fput routines.

This fixes a weird bug which happened while editing a record and removing only
a single space. Then, the file turns out to be unchanged. Now it things works.

Diffstat:
Mmyadm.c | 30+++++++++++++-----------------
1 file changed, 13 insertions(+), 17 deletions(-)

diff --git a/myadm.c b/myadm.c @@ -366,33 +366,29 @@ escape(char *s, char c, int *nc) { char * fget(char *fn, int *sz) { - FILE *fp; + int fd; char *buf; - fp = fopen(fn, "rb"); - if(!fp) + fd = open(fn, O_RDONLY); + if(fd == -1) return NULL; - fseek(fp, 0, SEEK_END); - *sz = ftell(fp); - fseek(fp, 0, SEEK_SET); - - buf = ecalloc(1, (*sz)+1); - fread(buf, *sz, 1, fp); - fclose(fp); - buf[*sz] = '\0'; - + *sz = lseek(fd, 0, SEEK_END)+1; + lseek(fd, 0, SEEK_SET); + buf = ecalloc(1, *sz); + read(fd, buf, *sz); + close(fd); return buf; } int fput(char *fn, char *s, int size) { - FILE *fp; + int fd; - fp = fopen(fn, "w"); - if(!fp) + fd = open(fn, O_WRONLY | O_TRUNC); + if(fd == -1) return -1; - fwrite(s, size, 1, fp); - fclose(fp); + write(fd, s, size); + close(fd); return 0; }