unstack - fix ipcvecs
[minix.git] / commands / diskctl / diskctl.c
blobaae8b9bff6bde532dc06489540c86cdc3f81882b
1 /* diskctl - control disk device driver parameters - by D.C. van Moolenbroek */
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 #include <fcntl.h>
8 static char *name, *dev;
10 static void usage(void)
12 fprintf(stderr, "usage: %s <device> <command> [args]\n"
13 "\n"
14 "supported commands:\n"
15 " getwcache return write cache status\n"
16 " setwcache [on|off] set write cache status\n"
17 " flush flush write cache\n",
18 name);
20 exit(EXIT_FAILURE);
23 static int open_dev(int flags)
25 int fd;
27 fd = open(dev, flags);
29 if (fd < 0) {
30 perror("open");
32 exit(EXIT_FAILURE);
35 return fd;
38 int main(int argc, char **argv)
40 int fd, val;
42 name = argv[0];
44 if (argc < 3) usage();
46 dev = argv[1];
48 if (!strcasecmp(argv[2], "getwcache")) {
49 if (argc != 3) usage();
51 fd = open_dev(O_RDONLY);
53 if (ioctl(fd, DIOCGETWC, &val) != 0) {
54 perror("ioctl");
56 return EXIT_FAILURE;
59 close(fd);
61 printf("write cache is %s\n", val ? "on" : "off");
63 else if (!strcasecmp(argv[2], "setwcache")) {
64 if (argc != 4) usage();
66 fd = open_dev(O_WRONLY);
68 if (!strcasecmp(argv[3], "on")) val = 1;
69 else if (!strcasecmp(argv[3], "off")) val = 0;
70 else usage();
72 if (ioctl(fd, DIOCSETWC, &val) != 0) {
73 perror("ioctl");
75 return EXIT_FAILURE;
78 close(fd);
80 printf("write cache %sabled\n", val ? "en" : "dis");
82 else if (!strcasecmp(argv[2], "flush")) {
83 if (argc != 3) usage();
85 fd = open_dev(O_WRONLY);
87 if (ioctl(fd, DIOCFLUSH, NULL) != 0) {
88 perror("ioctl");
90 return EXIT_FAILURE;
93 close(fd);
95 printf("write cache flushed\n");
97 else usage();
99 return EXIT_SUCCESS;