plan9.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "sam.h"
  10. Rune samname[] = L"~~sam~~";
  11. Rune *left[]= {
  12. L"{[(<«",
  13. L"\n",
  14. L"'\"`",
  15. 0
  16. };
  17. Rune *right[]= {
  18. L"}])>»",
  19. L"\n",
  20. L"'\"`",
  21. 0
  22. };
  23. char RSAM[] = "sam";
  24. char SAMTERM[] = "/bin/aux/samterm";
  25. char HOME[] = "home";
  26. char TMPDIR[] = "/tmp";
  27. char SH[] = "rc";
  28. char SHPATH[] = "/bin/rc";
  29. char RX[] = "rx";
  30. char RXPATH[] = "/bin/rx";
  31. char SAMSAVECMD[] = "/bin/rc\n/sys/lib/samsave";
  32. void
  33. dprint(char *z, ...)
  34. {
  35. char buf[BLOCKSIZE];
  36. va_list arg;
  37. va_start(arg, z);
  38. vseprint(buf, &buf[BLOCKSIZE], z, arg);
  39. va_end(arg);
  40. termwrite(buf);
  41. }
  42. void
  43. print_ss(char *s, String *a, String *b)
  44. {
  45. dprint("?warning: %s: `%.*S' and `%.*S'\n", s, a->n, a->s, b->n, b->s);
  46. }
  47. void
  48. print_s(char *s, String *a)
  49. {
  50. dprint("?warning: %s `%.*S'\n", s, a->n, a->s);
  51. }
  52. int
  53. statfile(char *name, uint32_t *dev, uint64_t *id, int32_t *time,
  54. int32_t *length,
  55. int32_t *appendonly)
  56. {
  57. Dir *dirb;
  58. dirb = dirstat(name);
  59. if(dirb == nil)
  60. return -1;
  61. if(dev)
  62. *dev = dirb->type|(dirb->dev<<16);
  63. if(id)
  64. *id = dirb->qid.path;
  65. if(time)
  66. *time = dirb->mtime;
  67. if(length)
  68. *length = dirb->length;
  69. if(appendonly)
  70. *appendonly = dirb->mode & DMAPPEND;
  71. free(dirb);
  72. return 1;
  73. }
  74. int
  75. statfd(int fd, uint32_t *dev, uint64_t *id, int32_t *time, int32_t *length,
  76. int32_t *appendonly)
  77. {
  78. Dir *dirb;
  79. dirb = dirfstat(fd);
  80. if(dirb == nil)
  81. return -1;
  82. if(dev)
  83. *dev = dirb->type|(dirb->dev<<16);
  84. if(id)
  85. *id = dirb->qid.path;
  86. if(time)
  87. *time = dirb->mtime;
  88. if(length)
  89. *length = dirb->length;
  90. if(appendonly)
  91. *appendonly = dirb->mode & DMAPPEND;
  92. free(dirb);
  93. return 1;
  94. }
  95. void
  96. notifyf(void *a, char *s)
  97. {
  98. USED(a);
  99. if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0)
  100. noted(NCONT);
  101. if(strcmp(s, "interrupt") == 0)
  102. noted(NCONT);
  103. panicking = 1;
  104. rescue();
  105. noted(NDFLT);
  106. }
  107. char*
  108. waitfor(int pid)
  109. {
  110. Waitmsg *w;
  111. static char msg[ERRMAX];
  112. while((w = wait()) != nil){
  113. if(w->pid != pid){
  114. free(w);
  115. continue;
  116. }
  117. strecpy(msg, msg+sizeof msg, w->msg);
  118. free(w);
  119. return msg;
  120. }
  121. rerrstr(msg, sizeof msg);
  122. return msg;
  123. }
  124. void
  125. samerr(char *buf)
  126. {
  127. sprint(buf, "%s/sam.err", TMPDIR);
  128. }
  129. void*
  130. emalloc(uint32_t n)
  131. {
  132. void *p;
  133. p = malloc(n);
  134. if(p == 0)
  135. panic("malloc fails");
  136. memset(p, 0, n);
  137. return p;
  138. }
  139. void*
  140. erealloc(void *p, uint32_t n)
  141. {
  142. p = realloc(p, n);
  143. if(p == 0)
  144. panic("realloc fails");
  145. return p;
  146. }