remove <utime.h> include from <fuse.h>
[fuse.git] / example / fioclient.c
blob5f05525f25ced1405c54862992b756e584521a62
1 /*
2 FUSE fioclient: FUSE ioctl example client
3 Copyright (C) 2008 SUSE Linux Products GmbH
4 Copyright (C) 2008 Tejun Heo <teheo@suse.de>
6 This program can be distributed under the terms of the GNU GPL.
7 See the file COPYING.
9 gcc -Wall fioclient.c -o fioclient
12 #include <sys/types.h>
13 #include <sys/fcntl.h>
14 #include <sys/stat.h>
15 #include <sys/ioctl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include "fioc.h"
22 const char *usage =
23 "Usage: fioclient FIOC_FILE COMMAND\n"
24 "\n"
25 "COMMANDS\n"
26 " s [SIZE] : get size if SIZE is omitted, set size otherwise\n"
27 " r SIZE [OFF] : read SIZE bytes @ OFF (dfl 0) and output to stdout\n"
28 " w SIZE [OFF] : write SIZE bytes @ OFF (dfl 0) from stdin\n"
29 "\n";
31 static int do_rw(int fd, int is_read, size_t size, off_t offset,
32 size_t *prev_size, size_t *new_size)
34 struct fioc_rw_arg arg = { .offset = offset };
35 ssize_t ret;
37 arg.buf = calloc(1, size);
38 if (!arg.buf) {
39 fprintf(stderr, "failed to allocated %zu bytes\n", size);
40 return -1;
43 if (is_read) {
44 arg.size = size;
45 ret = ioctl(fd, FIOC_READ, &arg);
46 if (ret >= 0)
47 fwrite(arg.buf, 1, ret, stdout);
48 } else {
49 arg.size = fread(arg.buf, 1, size, stdin);
50 fprintf(stderr, "Writing %zu bytes\n", arg.size);
51 ret = ioctl(fd, FIOC_WRITE, &arg);
54 if (ret >= 0) {
55 *prev_size = arg.prev_size;
56 *new_size = arg.new_size;
57 } else
58 perror("ioctl");
60 free(arg.buf);
61 return ret;
64 int main(int argc, char **argv)
66 size_t param[2] = { };
67 size_t size, prev_size = 0, new_size = 0;
68 char cmd;
69 int fd, i, rc;
71 if (argc < 3)
72 goto usage;
74 fd = open(argv[1], O_RDWR);
75 if (fd < 0) {
76 perror("open");
77 return 1;
80 cmd = tolower(argv[2][0]);
81 argc -= 3;
82 argv += 3;
84 for (i = 0; i < argc; i++) {
85 char *endp;
86 param[i] = strtoul(argv[i], &endp, 0);
87 if (endp == argv[i] || *endp != '\0')
88 goto usage;
91 switch (cmd) {
92 case 's':
93 if (!argc) {
94 if (ioctl(fd, FIOC_GET_SIZE, &size)) {
95 perror("ioctl");
96 return 1;
98 printf("%zu\n", size);
99 } else {
100 size = param[0];
101 if (ioctl(fd, FIOC_SET_SIZE, &size)) {
102 perror("ioctl");
103 return 1;
106 return 0;
108 case 'r':
109 case 'w':
110 rc = do_rw(fd, cmd == 'r', param[0], param[1],
111 &prev_size, &new_size);
112 if (rc < 0)
113 return 1;
114 fprintf(stderr, "transferred %d bytes (%zu -> %zu)\n",
115 rc, prev_size, new_size);
116 return 0;
119 usage:
120 fprintf(stderr, "%s", usage);
121 return 1;