setvbuf.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * pANS stdio -- setvbuf
  11. */
  12. #include "iolib.h"
  13. int setvbuf(FILE *f, char *buf, int mode, int32_t size){
  14. if(f->state!=OPEN){
  15. f->state=ERR;
  16. return -1;
  17. }
  18. f->state=RDWR;
  19. switch(mode){
  20. case _IOLBF:
  21. f->flags|=LINEBUF;
  22. case _IOFBF:
  23. if(buf==0){
  24. buf=malloc(size);
  25. if(buf==0){
  26. f->state=ERR;
  27. return -1;
  28. }
  29. f->flags|=BALLOC;
  30. }
  31. f->bufl=size;
  32. break;
  33. case _IONBF:
  34. buf=f->unbuf;
  35. f->bufl=0;
  36. break;
  37. }
  38. f->rp=f->wp=f->lp=f->buf=buf;
  39. f->state=RDWR;
  40. return 0;
  41. }
  42. static int
  43. isatty(int fd)
  44. {
  45. char buf[64];
  46. if(fd2path(fd, buf, sizeof buf) != 0)
  47. return 0;
  48. /* might be /mnt/term/dev/cons */
  49. return strlen(buf) >= 9 && strcmp(buf+strlen(buf)-9, "/dev/cons") == 0;
  50. }
  51. int _IO_setvbuf(FILE *f){
  52. //static int isatty(int);
  53. if(f==stderr || (f==stdout && isatty(1)))
  54. return setvbuf(f, (char *)0, _IOLBF, BUFSIZ);
  55. else return setvbuf(f, (char *)0, _IOFBF, BUFSIZ);
  56. }