binit.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. static Biobufhdr* wbufs[20];
  13. static int atexitflag;
  14. static
  15. void
  16. batexit(void)
  17. {
  18. Biobufhdr *bp;
  19. int i;
  20. for(i=0; i<nelem(wbufs); i++) {
  21. bp = wbufs[i];
  22. if(bp != 0) {
  23. wbufs[i] = 0;
  24. Bflush(bp);
  25. }
  26. }
  27. }
  28. static
  29. void
  30. deinstall(Biobufhdr *bp)
  31. {
  32. int i;
  33. for(i=0; i<nelem(wbufs); i++)
  34. if(wbufs[i] == bp)
  35. wbufs[i] = 0;
  36. }
  37. static
  38. void
  39. install(Biobufhdr *bp)
  40. {
  41. int i;
  42. deinstall(bp);
  43. for(i=0; i<nelem(wbufs); i++)
  44. if(wbufs[i] == 0) {
  45. wbufs[i] = bp;
  46. break;
  47. }
  48. if(atexitflag == 0) {
  49. atexitflag = 1;
  50. atexit(batexit);
  51. }
  52. }
  53. int
  54. Binits(Biobufhdr *bp, int f, int mode, uint8_t *p, int size)
  55. {
  56. p += Bungetsize; /* make room for Bungets */
  57. size -= Bungetsize;
  58. switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
  59. default:
  60. fprint(2, "Binits: unknown mode %d\n", mode);
  61. return Beof;
  62. case OREAD:
  63. bp->state = Bractive;
  64. bp->ocount = 0;
  65. break;
  66. case OWRITE:
  67. install(bp);
  68. bp->state = Bwactive;
  69. bp->ocount = -size;
  70. break;
  71. }
  72. bp->bbuf = p;
  73. bp->ebuf = p+size;
  74. bp->bsize = size;
  75. bp->icount = 0;
  76. bp->gbuf = bp->ebuf;
  77. bp->fid = f;
  78. bp->flag = 0;
  79. bp->rdline = 0;
  80. bp->offset = 0;
  81. bp->runesize = 0;
  82. return 0;
  83. }
  84. int
  85. Binit(Biobuf *bp, int f, int mode)
  86. {
  87. return Binits(bp, f, mode, bp->b, sizeof(bp->b));
  88. }
  89. Biobuf*
  90. Bopen(char *name, int mode)
  91. {
  92. Biobuf *bp;
  93. int f;
  94. switch(mode&~(OCEXEC|ORCLOSE|OTRUNC)) {
  95. default:
  96. fprint(2, "Bopen: unknown mode %#x\n", mode);
  97. return 0;
  98. case OREAD:
  99. f = open(name, mode);
  100. break;
  101. case OWRITE:
  102. f = create(name, mode, 0666);
  103. break;
  104. }
  105. if(f < 0)
  106. return 0;
  107. bp = malloc(sizeof(Biobuf));
  108. Binits(bp, f, mode, bp->b, sizeof(bp->b));
  109. bp->flag = Bmagic; /* mark bp open & malloced */
  110. return bp;
  111. }
  112. int
  113. Bterm(Biobufhdr *bp)
  114. {
  115. int r;
  116. deinstall(bp);
  117. r = Bflush(bp);
  118. if(bp->flag == Bmagic) {
  119. bp->flag = 0;
  120. close(bp->fid);
  121. bp->fid = -1; /* prevent accidents */
  122. free(bp);
  123. }
  124. /* otherwise opened with Binit(s) */
  125. return r;
  126. }