summaryrefslogtreecommitdiff
path: root/ext/-test-/file/fs.c
blob: ed1e64e1ebc49c6c6d5ef9dbaf6b78108e12e5f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "ruby/ruby.h"
#include "ruby/io.h"

#ifdef __linux__
# define HAVE_GETMNTENT
#endif

#ifdef HAVE_GETMNTENT
# include <stdio.h>
# include <mntent.h>
#endif

VALUE
get_fsname(VALUE self, VALUE str)
{
#ifdef HAVE_GETMNTENT
    const char *path;
    struct mntent mntbuf;
    static const int buflen = 4096;
    char *buf = alloca(buflen);
    int len = 0;
    FILE *fp;
#define FSNAME_LEN 100
    char name[FSNAME_LEN] = "";

    FilePathValue(str);
    path = RSTRING_PTR(str);
    fp = setmntent("/etc/mtab", "r"); 
    if (!fp) rb_sys_fail("setmntent(/etb/mtab)");;

    while (getmntent_r(fp, &mntbuf, buf, buflen)) {
	int i;
	char *mnt_dir = mntbuf.mnt_dir;
	for (i=0; mnt_dir[i]; i++) {
	    if (mnt_dir[i] != path[i]) {
		goto next_entry;
	    }
	}
	if (i >= len) {
	    len = i;
	    strlcpy(name, mntbuf.mnt_type, FSNAME_LEN);
	}
next_entry:
	;
    }
    endmntent(fp);

    if (!len) rb_sys_fail("no matching entry");;
    return rb_str_new_cstr(name);
#else
    return Qnil;
#endif
}

void
Init_fs(VALUE module)
{
    VALUE fs = rb_define_module_under(module, "Fs");
    rb_define_module_function(fs, "fsname", get_fsname, 1);
}