aux.c 2.6 KB

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