123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- /*
- * This file is part of the UCB release of Plan 9. It is subject to the license
- * terms in the LICENSE file found in the top-level directory of this
- * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
- * part of the UCB release of Plan 9, including this file, may be copied,
- * modified, propagated, or distributed except according to the terms contained
- * in the LICENSE file.
- */
- #include <u.h>
- #include <libc.h>
- #include <../boot/boot.h>
- int
- sendmsg(int fd, char *msg)
- {
- int n;
- n = strlen(msg);
- if(write(fd, msg, n) != n)
- return -1;
- return 0;
- }
- void
- warning(char *s)
- {
- char buf[ERRMAX];
- buf[0] = '\0';
- errstr(buf, sizeof buf);
- fprint(2, "boot: %s: %s\n", s, buf);
- }
- void
- fatal(char *s)
- {
- char buf[ERRMAX];
- buf[0] = '\0';
- errstr(buf, sizeof buf);
- fprint(2, "boot: %s: %s\n", s, buf);
- exits(0);
- }
- int
- readfile(char *name, char *buf, int len)
- {
- int f, n;
- buf[0] = 0;
- f = open(name, OREAD);
- if(f < 0){
- fprint(2, "readfile: cannot open %s (%r)\n", name);
- return -1;
- }
- n = read(f, buf, len-1);
- if(n >= 0)
- buf[n] = 0;
- close(f);
- return 0;
- }
- int
- writefile(char *name, char *buf, int len)
- {
- int f, n;
- f = open(name, OWRITE);
- if(f < 0)
- return -1;
- n = write(f, buf, len);
- close(f);
- return (n != len) ? -1 : 0;
- }
- void
- setenv(char *name, char *val)
- {
- int f;
- char ename[64];
- snprint(ename, sizeof ename, "#e/%s", name);
- f = create(ename, 1, 0666);
- if(f < 0){
- fprint(2, "create %s: %r\n", ename);
- return;
- }
- write(f, val, strlen(val));
- close(f);
- }
- void
- srvcreate(char *name, int fd)
- {
- char *srvname;
- int f;
- char buf[64];
- srvname = strrchr(name, '/');
- if(srvname)
- srvname++;
- else
- srvname = name;
- snprint(buf, sizeof buf, "#s/%s", srvname);
- f = create(buf, 1, 0666);
- if(f < 0)
- fatal(buf);
- sprint(buf, "%d", fd);
- if(write(f, buf, strlen(buf)) != strlen(buf))
- fatal("write");
- close(f);
- }
- void
- catchint(void *a, char *note)
- {
- USED(a);
- if(strcmp(note, "alarm") == 0)
- noted(NCONT);
- noted(NDFLT);
- }
- int
- outin(char *prompt, char *def, int len)
- {
- int n;
- char buf[256];
- if(len >= sizeof buf)
- len = sizeof(buf)-1;
- if(cpuflag){
- notify(catchint);
- alarm(15*1000);
- }
- print("%s[%s]: ", prompt, *def ? def : "no default");
- memset(buf, 0, sizeof buf);
- n = read(0, buf, len);
- if(cpuflag){
- alarm(0);
- notify(0);
- }
- if(n < 0){
- return 1;
- }
- if (n > 1) {
- strncpy(def, buf, len);
- }
- return n;
- }
|