openfont.c 495 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <draw.h>
  4. Font*
  5. openfont(Display *d, char *name)
  6. {
  7. Font *fnt;
  8. int fd, i, n;
  9. char *buf;
  10. Dir *dir;
  11. fd = open(name, OREAD);
  12. if(fd < 0)
  13. return 0;
  14. dir = dirfstat(fd);
  15. if(dir == nil){
  16. Err0:
  17. close(fd);
  18. return 0;
  19. }
  20. n = dir->length;
  21. free(dir);
  22. buf = malloc(n+1);
  23. if(buf == 0)
  24. goto Err0;
  25. buf[n] = 0;
  26. i = read(fd, buf, n);
  27. close(fd);
  28. if(i != n){
  29. free(buf);
  30. return 0;
  31. }
  32. fnt = buildfont(d, buf, name);
  33. free(buf);
  34. return fnt;
  35. }