aux.c 3.1 KB

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