aux.c 2.5 KB

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