]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/bitsy/params.c
Libflac: Tell it that we have stdint.h so it finds SIZE_MAX
[plan9front.git] / sys / src / cmd / bitsy / params.c
1 #include <u.h>
2 #include <libc.h>
3
4 void
5 erase(char *part)
6 {
7         char file[256];
8         int fd;
9
10         snprint(file, sizeof file, "%sctl", part);
11         fd = open(file, ORDWR);
12         if(fd < 0)
13                 return;
14         fprint(fd, "erase");
15         close(fd);
16 }
17
18 char*
19 readfile(char *file)
20 {
21         char buf[512];
22         int n, fd;
23         uchar *p;
24
25         fd = open(file, OREAD);
26         if(fd < 0)
27                 sysfatal("opening %s: %r", file);
28         n = read(fd, buf, sizeof(buf)-1);
29         close(fd);
30         if(n < 0)
31                 return "";
32         buf[n] = 0;
33         for(p = (uchar*)buf; *p; p++)
34                 if(*p == 0xff){
35                         *p = 0;
36                         break;
37                 }
38         return strdup(buf);
39 }
40
41 void
42 writefile(char *file, char *data)
43 {
44         int fd;
45
46         fd = open(file, OWRITE);
47         if(fd < 0)
48                 fd = create(file, OWRITE, 0664);
49         if(fd < 0)
50                 return;
51         write(fd, data, strlen(data));
52         close(fd);
53 }
54
55 void
56 main(int argc, char **argv)
57 {
58         int from = 0;
59         char *params;
60         char *file = "/tmp/tmpparams";
61         char *part;
62
63         ARGBEGIN {
64         case 'f':
65                 from++;
66                 break;
67         } ARGEND;
68
69         if(argc)
70                 part = argv[0];
71         else
72                 part = "/dev/flash/user";
73
74         if(from){
75                 params = readfile(part);
76                 writefile(file, params);
77         } else {
78                 params = readfile(file);
79                 erase(part);
80                 writefile(part, params);
81                 free(params);
82         }
83 }