libdraw: use multiple read() calls in openfont() to read .font file

font files might be bigger than the i/o unit, so do multiple reads
until eof to read it.
This commit is contained in:
cinap_lenrek 2015-03-01 05:45:22 +01:00
parent e725771b5d
commit 0467b41972

View file

@ -2,36 +2,43 @@
#include <libc.h>
#include <draw.h>
static char*
readfile(char *name)
{
enum { HUNK = 8*1024, };
int f, n, r;
char *s, *p;
n = 0;
r = -1;
if((s = malloc(HUNK)) != nil){
if((f = open(name, OREAD)) >= 0){
while((r = read(f, s+n, HUNK)) > 0){
n += r;
r = -1;
if((p = realloc(s, n+HUNK)) == nil)
break;
s = p;
}
close(f);
}
}
if(r < 0 || (p = realloc(s, n+1)) == nil){
free(s);
return nil;
}
p[n] = 0;
return p;
}
Font*
openfont(Display *d, char *name)
{
Font *fnt;
int fd, i, n;
char *buf;
Dir *dir;
fd = open(name, OREAD);
if(fd < 0)
return 0;
dir = dirfstat(fd);
if(dir == nil){
Err0:
close(fd);
return 0;
}
n = dir->length;
free(dir);
buf = malloc(n+1);
if(buf == 0)
goto Err0;
buf[n] = 0;
i = read(fd, buf, n);
close(fd);
if(i != n){
free(buf);
return 0;
}
if((buf = readfile(name)) == nil)
return nil;
fnt = buildfont(d, buf, name);
free(buf);
return fnt;