aux.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. */
  42. int
  43. sendmsg(int fd, char *msg)
  44. {
  45. int n;
  46. n = strlen(msg);
  47. if(write(fd, msg, n) != n)
  48. return -1;
  49. return 0;
  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 buf[ERRMAX];
  63. buf[0] = '\0';
  64. errstr(buf, sizeof buf);
  65. fprint(2, "boot: %s: %s\n", s, buf);
  66. exits(0);
  67. }
  68. int
  69. readfile(char *name, char *buf, int len)
  70. {
  71. int f, n;
  72. buf[0] = 0;
  73. f = open(name, OREAD);
  74. if(f < 0)
  75. return -1;
  76. n = read(f, buf, len-1);
  77. if(n >= 0)
  78. buf[n] = 0;
  79. close(f);
  80. return 0;
  81. }
  82. int
  83. writefile(char *name, char *buf, int len)
  84. {
  85. int f, n;
  86. f = open(name, OWRITE);
  87. if(f < 0)
  88. return -1;
  89. n = write(f, buf, len);
  90. close(f);
  91. return (n != len) ? -1 : 0;
  92. }
  93. void
  94. setenv(char *name, char *val)
  95. {
  96. int f;
  97. char ename[64];
  98. snprint(ename, sizeof ename, "#e/%s", name);
  99. f = create(ename, 1, 0666);
  100. if(f < 0){
  101. fprint(2, "create %s: %r\n", ename);
  102. return;
  103. }
  104. write(f, val, strlen(val));
  105. close(f);
  106. }
  107. void
  108. srvcreate(char *name, int fd)
  109. {
  110. char *srvname;
  111. int f;
  112. char buf[64];
  113. srvname = strrchr(name, '/');
  114. if(srvname)
  115. srvname++;
  116. else
  117. srvname = name;
  118. snprint(buf, sizeof buf, "#s/%s", srvname);
  119. f = create(buf, 1, 0666);
  120. if(f < 0)
  121. fatal(buf);
  122. sprint(buf, "%d", fd);
  123. if(write(f, buf, strlen(buf)) != strlen(buf))
  124. fatal("write");
  125. close(f);
  126. }
  127. void
  128. catchint(void *a, char *note)
  129. {
  130. USED(a);
  131. if(strcmp(note, "alarm") == 0)
  132. noted(NCONT);
  133. noted(NDFLT);
  134. }
  135. int
  136. outin(char *prompt, char *def, int len)
  137. {
  138. int n;
  139. char buf[256];
  140. if(len >= sizeof buf)
  141. len = sizeof(buf)-1;
  142. if(cpuflag){
  143. notify(catchint);
  144. alarm(15*1000);
  145. }
  146. print("%s[%s]: ", prompt, *def ? def : "no default");
  147. memset(buf, 0, sizeof buf);
  148. n = read(0, buf, len);
  149. if(cpuflag){
  150. alarm(0);
  151. notify(0);
  152. }
  153. if(n < 0)
  154. return 1;
  155. if(n > 1){
  156. buf[n-1] = 0;
  157. strcpy(def, buf);
  158. }
  159. return n;
  160. }