ACPI / PNP: Avoid conflicting resource reservations
[linux/fpc-iii.git] / tools / lib / api / fs / findfs.c
blob49946cb6d7afa9194360c4f5672ea2a5595df181
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdbool.h>
6 #include <sys/vfs.h>
8 #include "findfs.h"
10 /* verify that a mountpoint is actually the type we want */
12 int valid_mountpoint(const char *mount, long magic)
14 struct statfs st_fs;
16 if (statfs(mount, &st_fs) < 0)
17 return -ENOENT;
18 else if ((long)st_fs.f_type != magic)
19 return -ENOENT;
21 return 0;
24 /* find the path to a mounted file system */
25 const char *find_mountpoint(const char *fstype, long magic,
26 char *mountpoint, int len,
27 const char * const *known_mountpoints)
29 const char * const *ptr;
30 char format[128];
31 char type[100];
32 FILE *fp;
34 if (known_mountpoints) {
35 ptr = known_mountpoints;
36 while (*ptr) {
37 if (valid_mountpoint(*ptr, magic) == 0) {
38 strncpy(mountpoint, *ptr, len - 1);
39 mountpoint[len-1] = 0;
40 return mountpoint;
42 ptr++;
46 /* give up and parse /proc/mounts */
47 fp = fopen("/proc/mounts", "r");
48 if (fp == NULL)
49 return NULL;
51 snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len);
53 while (fscanf(fp, format, mountpoint, type) == 2) {
54 if (strcmp(type, fstype) == 0)
55 break;
57 fclose(fp);
59 if (strcmp(type, fstype) != 0)
60 return NULL;
62 return mountpoint;