aux.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <../boot/boot.h>
  12. /*
  13. int
  14. plumb(char *dir, char *dest, int *efd, char *here)
  15. {
  16. char buf[128];
  17. char name[128];
  18. int n;
  19. sprint(name, "%s/clone", dir);
  20. efd[0] = open(name, ORDWR);
  21. if(efd[0] < 0)
  22. return -1;
  23. n = read(efd[0], buf, sizeof(buf)-1);
  24. if(n < 0){
  25. close(efd[0]);
  26. return -1;
  27. }
  28. buf[n] = 0;
  29. sprint(name, "%s/%s/data", dir, buf);
  30. if(here){
  31. sprint(buf, "announce %s", here);
  32. if(sendmsg(efd[0], buf) < 0){
  33. close(efd[0]);
  34. return -1;
  35. }
  36. }
  37. sprint(buf, "connect %s", dest);
  38. if(sendmsg(efd[0], buf) < 0){
  39. close(efd[0]);
  40. return -1;
  41. }
  42. efd[1] = open(name, ORDWR);
  43. if(efd[1] < 0){
  44. close(efd[0]);
  45. return -1;
  46. }
  47. return efd[1];
  48. }
  49. int
  50. sendmsg(int fd, char *msg)
  51. {
  52. int n;
  53. n = strlen(msg);
  54. if(write(fd, msg, n) != n)
  55. return -1;
  56. return 0;
  57. }
  58. */
  59. void
  60. warning(char *s)
  61. {
  62. char buf[ERRMAX];
  63. buf[0] = '\0';
  64. errstr(buf, sizeof buf);
  65. fprint(2, "boot: %s: %s\n", s, buf);
  66. }
  67. void
  68. fatal(char *s)
  69. {
  70. char *msg;
  71. char buf[ERRMAX];
  72. buf[0] = '\0';
  73. errstr(buf, sizeof buf);
  74. msg = smprint("%s: %s", s, buf);
  75. fprint(2, "boot: %s\n", msg);
  76. exits(msg); /* this will trigger a panic */
  77. }
  78. int
  79. readfile(char *name, char *buf, int len)
  80. {
  81. int f, n;
  82. buf[0] = 0;
  83. f = open(name, OREAD);
  84. if(f < 0)
  85. return -1;
  86. n = read(f, buf, len-1);
  87. if(n >= 0)
  88. buf[n] = 0;
  89. close(f);
  90. return 0;
  91. }
  92. int
  93. writefile(char *name, char *buf, int len)
  94. {
  95. int f, n;
  96. f = open(name, OWRITE);
  97. if(f < 0)
  98. return -1;
  99. n = write(f, buf, len);
  100. close(f);
  101. return (n != len) ? -1 : 0;
  102. }
  103. void
  104. setenv(char *name, char *val)
  105. {
  106. int f;
  107. char ename[64];
  108. snprint(ename, sizeof ename, "#e/%s", name);
  109. f = create(ename, 1, 0666);
  110. if(f < 0){
  111. fprint(2, "create %s: %r\n", ename);
  112. return;
  113. }
  114. write(f, val, strlen(val));
  115. close(f);
  116. }
  117. void
  118. srvcreate(char *name, int fd)
  119. {
  120. char *srvname;
  121. int f;
  122. char buf[64];
  123. srvname = strrchr(name, '/');
  124. if(srvname)
  125. srvname++;
  126. else
  127. srvname = name;
  128. snprint(buf, sizeof buf, "#s/%s", srvname);
  129. f = create(buf, 1, 0666);
  130. if(f < 0)
  131. fatal(buf);
  132. sprint(buf, "%d", fd);
  133. if(write(f, buf, strlen(buf)) != strlen(buf))
  134. fatal("write");
  135. close(f);
  136. }
  137. void
  138. catchint(void *a, char *note)
  139. {
  140. USED(a);
  141. if(strcmp(note, "alarm") == 0)
  142. noted(NCONT);
  143. noted(NDFLT);
  144. }
  145. int
  146. outin(char *prompt, char *def, int len)
  147. {
  148. int n;
  149. char buf[256];
  150. if(len >= sizeof buf)
  151. len = sizeof(buf)-1;
  152. if(cpuflag){
  153. notify(catchint);
  154. alarm(15*1000);
  155. }
  156. print("%s[%s]: ", prompt, *def ? def : "no default");
  157. memset(buf, 0, sizeof buf);
  158. n = read(0, buf, len);
  159. if(cpuflag){
  160. alarm(0);
  161. notify(0);
  162. }
  163. if(n < 0)
  164. return 1;
  165. if(n > 1){
  166. buf[n-1] = 0;
  167. strcpy(def, buf);
  168. }
  169. return n;
  170. }