Replace utils/mount.fuse "sh" script with a "C" program
[fuse.git] / example / hello.c
blob0676a390aedec69ebc282025b3b7db35af78ea8a
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
8 gcc -Wall `pkg-config fuse --cflags --libs` hello.c -o hello
9 */
11 #define FUSE_USE_VERSION 26
13 #include <fuse.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <fcntl.h>
19 static const char *hello_str = "Hello World!\n";
20 static const char *hello_path = "/hello";
22 static int hello_getattr(const char *path, struct stat *stbuf)
24 int res = 0;
26 memset(stbuf, 0, sizeof(struct stat));
27 if (strcmp(path, "/") == 0) {
28 stbuf->st_mode = S_IFDIR | 0755;
29 stbuf->st_nlink = 2;
30 } else if (strcmp(path, hello_path) == 0) {
31 stbuf->st_mode = S_IFREG | 0444;
32 stbuf->st_nlink = 1;
33 stbuf->st_size = strlen(hello_str);
34 } else
35 res = -ENOENT;
37 return res;
40 static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
41 off_t offset, struct fuse_file_info *fi)
43 (void) offset;
44 (void) fi;
46 if (strcmp(path, "/") != 0)
47 return -ENOENT;
49 filler(buf, ".", NULL, 0);
50 filler(buf, "..", NULL, 0);
51 filler(buf, hello_path + 1, NULL, 0);
53 return 0;
56 static int hello_open(const char *path, struct fuse_file_info *fi)
58 if (strcmp(path, hello_path) != 0)
59 return -ENOENT;
61 if ((fi->flags & 3) != O_RDONLY)
62 return -EACCES;
64 return 0;
67 static int hello_read(const char *path, char *buf, size_t size, off_t offset,
68 struct fuse_file_info *fi)
70 size_t len;
71 (void) fi;
72 if(strcmp(path, hello_path) != 0)
73 return -ENOENT;
75 len = strlen(hello_str);
76 if (offset < len) {
77 if (offset + size > len)
78 size = len - offset;
79 memcpy(buf, hello_str + offset, size);
80 } else
81 size = 0;
83 return size;
86 static struct fuse_operations hello_oper = {
87 .getattr = hello_getattr,
88 .readdir = hello_readdir,
89 .open = hello_open,
90 .read = hello_read,
93 int main(int argc, char *argv[])
95 return fuse_main(argc, argv, &hello_oper, NULL);