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:
parent
e725771b5d
commit
0467b41972
1 changed files with 31 additions and 24 deletions
|
@ -2,36 +2,43 @@
|
||||||
#include <libc.h>
|
#include <libc.h>
|
||||||
#include <draw.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*
|
Font*
|
||||||
openfont(Display *d, char *name)
|
openfont(Display *d, char *name)
|
||||||
{
|
{
|
||||||
Font *fnt;
|
Font *fnt;
|
||||||
int fd, i, n;
|
|
||||||
char *buf;
|
char *buf;
|
||||||
Dir *dir;
|
|
||||||
|
|
||||||
fd = open(name, OREAD);
|
if((buf = readfile(name)) == nil)
|
||||||
if(fd < 0)
|
return nil;
|
||||||
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;
|
|
||||||
}
|
|
||||||
fnt = buildfont(d, buf, name);
|
fnt = buildfont(d, buf, name);
|
||||||
free(buf);
|
free(buf);
|
||||||
return fnt;
|
return fnt;
|
||||||
|
|
Loading…
Reference in a new issue