aux.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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
  13. sendmsg(int fd, char *msg)
  14. {
  15. int n;
  16. n = strlen(msg);
  17. if(write(fd, msg, n) != n)
  18. return -1;
  19. return 0;
  20. }
  21. void
  22. warning(char *s)
  23. {
  24. char buf[ERRMAX];
  25. buf[0] = '\0';
  26. errstr(buf, sizeof buf);
  27. fprint(2, "boot: %s: %s\n", s, buf);
  28. }
  29. void
  30. fatal(char *s)
  31. {
  32. char buf[ERRMAX];
  33. buf[0] = '\0';
  34. errstr(buf, sizeof buf);
  35. fprint(2, "boot: %s: %s\n", s, buf);
  36. exits(0);
  37. }
  38. int
  39. readfile(char *name, char *buf, int len)
  40. {
  41. int f, n;
  42. buf[0] = 0;
  43. f = open(name, OREAD);
  44. if(f < 0){
  45. fprint(2, "readfile: cannot open %s (%r)\n", name);
  46. return -1;
  47. }
  48. n = read(f, buf, len-1);
  49. if(n >= 0)
  50. buf[n] = 0;
  51. close(f);
  52. return 0;
  53. }
  54. int
  55. writefile(char *name, char *buf, int len)
  56. {
  57. int f, n;
  58. f = open(name, OWRITE);
  59. if(f < 0)
  60. return -1;
  61. n = write(f, buf, len);
  62. close(f);
  63. return (n != len) ? -1 : 0;
  64. }
  65. void
  66. setenv(char *name, char *val)
  67. {
  68. int f;
  69. char ename[64];
  70. snprint(ename, sizeof ename, "#e/%s", name);
  71. f = create(ename, 1, 0666);
  72. if(f < 0){
  73. fprint(2, "create %s: %r\n", ename);
  74. return;
  75. }
  76. write(f, val, strlen(val));
  77. close(f);
  78. }
  79. void
  80. srvcreate(char *name, int fd)
  81. {
  82. char *srvname;
  83. int f;
  84. char buf[64];
  85. srvname = strrchr(name, '/');
  86. if(srvname)
  87. srvname++;
  88. else
  89. srvname = name;
  90. snprint(buf, sizeof buf, "#s/%s", srvname);
  91. f = create(buf, 1, 0666);
  92. if(f < 0)
  93. fatal(buf);
  94. sprint(buf, "%d", fd);
  95. if(write(f, buf, strlen(buf)) != strlen(buf))
  96. fatal("write");
  97. close(f);
  98. }
  99. void
  100. catchint(void *a, char *note)
  101. {
  102. USED(a);
  103. if(strcmp(note, "alarm") == 0)
  104. noted(NCONT);
  105. noted(NDFLT);
  106. }
  107. int
  108. outin(char *prompt, char *def, int len)
  109. {
  110. int n;
  111. char buf[256];
  112. if(len >= sizeof buf)
  113. len = sizeof(buf)-1;
  114. if(cpuflag){
  115. notify(catchint);
  116. alarm(15*1000);
  117. }
  118. print("%s[%s]: ", prompt, *def ? def : "no default");
  119. memset(buf, 0, sizeof buf);
  120. n = read(0, buf, len);
  121. if(cpuflag){
  122. alarm(0);
  123. notify(0);
  124. }
  125. if(n < 0){
  126. return 1;
  127. }
  128. if (n > 1) {
  129. strncpy(def, buf, len);
  130. }
  131. return n;
  132. }