plan9.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = (Rune *)L"~~sam~~";
  11. Rune *left[]= {
  12. (Rune *)L"{[(<«",
  13. (Rune *)L"\n",
  14. (Rune *)L"'\"`",
  15. 0
  16. };
  17. Rune *right[]= {
  18. (Rune *)L"}])>»",
  19. (Rune *)L"\n",
  20. (Rune *)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, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
  54. {
  55. Dir *dirb;
  56. dirb = dirstat(name);
  57. if(dirb == nil)
  58. return -1;
  59. if(dev)
  60. *dev = dirb->type|(dirb->dev<<16);
  61. if(id)
  62. *id = dirb->qid.path;
  63. if(time)
  64. *time = dirb->mtime;
  65. if(length)
  66. *length = dirb->length;
  67. if(appendonly)
  68. *appendonly = dirb->mode & DMAPPEND;
  69. free(dirb);
  70. return 1;
  71. }
  72. int
  73. statfd(int fd, ulong *dev, uvlong *id, long *time, long *length, long *appendonly)
  74. {
  75. Dir *dirb;
  76. dirb = dirfstat(fd);
  77. if(dirb == nil)
  78. return -1;
  79. if(dev)
  80. *dev = dirb->type|(dirb->dev<<16);
  81. if(id)
  82. *id = dirb->qid.path;
  83. if(time)
  84. *time = dirb->mtime;
  85. if(length)
  86. *length = dirb->length;
  87. if(appendonly)
  88. *appendonly = dirb->mode & DMAPPEND;
  89. free(dirb);
  90. return 1;
  91. }
  92. void
  93. notifyf(void *a, char *s)
  94. {
  95. USED(a);
  96. if(bpipeok && strcmp(s, "sys: write on closed pipe") == 0)
  97. noted(NCONT);
  98. if(strcmp(s, "interrupt") == 0)
  99. noted(NCONT);
  100. panicking = 1;
  101. rescue();
  102. noted(NDFLT);
  103. }
  104. char*
  105. waitfor(int pid)
  106. {
  107. Waitmsg *w;
  108. static char msg[ERRMAX];
  109. while((w = wait()) != nil){
  110. if(w->pid != pid){
  111. free(w);
  112. continue;
  113. }
  114. strecpy(msg, msg+sizeof msg, w->msg);
  115. free(w);
  116. return msg;
  117. }
  118. rerrstr(msg, sizeof msg);
  119. return msg;
  120. }
  121. void
  122. samerr(char *buf)
  123. {
  124. sprint(buf, "%s/sam.err", TMPDIR);
  125. }
  126. void*
  127. emalloc(ulong n)
  128. {
  129. void *p;
  130. p = malloc(n);
  131. if(p == 0)
  132. panic("malloc fails");
  133. memset(p, 0, n);
  134. return p;
  135. }
  136. void*
  137. erealloc(void *p, ulong n)
  138. {
  139. p = realloc(p, n);
  140. if(p == 0)
  141. panic("realloc fails");
  142. return p;
  143. }