setvbuf.c 954 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * pANS stdio -- setvbuf
  3. */
  4. #include "iolib.h"
  5. int setvbuf(FILE *f, char *buf, int mode, long size){
  6. if(f->state!=OPEN){
  7. f->state=ERR;
  8. return -1;
  9. }
  10. f->state=RDWR;
  11. switch(mode){
  12. case _IOLBF:
  13. f->flags|=LINEBUF;
  14. case _IOFBF:
  15. if(buf==0){
  16. buf=malloc(size);
  17. if(buf==0){
  18. f->state=ERR;
  19. return -1;
  20. }
  21. f->flags|=BALLOC;
  22. }
  23. f->bufl=size;
  24. break;
  25. case _IONBF:
  26. buf=f->unbuf;
  27. f->bufl=0;
  28. break;
  29. }
  30. f->rp=f->wp=f->lp=f->buf=buf;
  31. f->state=RDWR;
  32. return 0;
  33. }
  34. int _IO_setvbuf(FILE *f){
  35. static int isatty(int);
  36. if(f==stderr || (f==stdout && isatty(1)))
  37. return setvbuf(f, (char *)0, _IOLBF, BUFSIZ);
  38. else return setvbuf(f, (char *)0, _IOFBF, BUFSIZ);
  39. }
  40. static int
  41. isatty(int fd)
  42. {
  43. Dir *d1, *d2;
  44. int ret;
  45. d1 = dirfstat(fd);
  46. d2 = dirstat("/dev/cons");
  47. ret = 0;
  48. if(d1!=nil && d2!=nil)
  49. ret = (d1->qid.path == d2->qid.path) &&
  50. (d1->type == d2->type) &&
  51. (d1->dev == d2->dev);
  52. free(d1);
  53. free(d2);
  54. return 0;
  55. }