-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhich-blockdev.c
More file actions
158 lines (131 loc) · 3.59 KB
/
which-blockdev.c
File metadata and controls
158 lines (131 loc) · 3.59 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* find_block_device [path]
*
* This is a standalone version of a utility from busybox that finds the device
* that a particular filesystem is mounted on.
*
* Given a pathname it will resolve the block device mounted at the path. Without
* arguments, it will give the root filesystem (/) block device.
*
* -- William Schaub <wschaub@genesi-tech.com>
*/
/* vi: set sw=4 ts=4: */
/*
* Utility routines.
*
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#define DOT_OR_DOTDOT(s) ((s)[0] == '.' && (!(s)[1] || ((s)[1] == '.' && !(s)[2])))
char* safe_strncpy(char *dst, const char *src, size_t size);
void overlapping_strcpy(char *dst, const char *src);
char* xstrdup(const char *s);
/* Find block device /dev/XXX which contains specified file
* We handle /dev/dir/dir/dir too, at a cost of ~80 more bytes code */
/* Do not reallocate all this stuff on each recursion */
enum { DEVNAME_MAX = 256 };
struct arena {
struct stat st;
dev_t dev;
/* Was PATH_MAX, but we recurse _/dev_. We can assume
* people are not crazy enough to have mega-deep tree there */
char devpath[DEVNAME_MAX];
};
static char *find_block_device_in_dir(struct arena *ap)
{
DIR *dir;
struct dirent *entry;
char *retpath = NULL;
int len, rem;
len = strlen(ap->devpath);
rem = DEVNAME_MAX-2 - len;
if (rem <= 0)
return NULL;
dir = opendir(ap->devpath);
if (!dir)
return NULL;
ap->devpath[len++] = '/';
while ((entry = readdir(dir)) != NULL) {
safe_strncpy(ap->devpath + len, entry->d_name, rem);
/* lstat: do not follow links */
if (lstat(ap->devpath, &ap->st) != 0)
continue;
if (S_ISBLK(ap->st.st_mode) && ap->st.st_rdev == ap->dev) {
retpath = xstrdup(ap->devpath);
break;
}
if (S_ISDIR(ap->st.st_mode)) {
/* Do not recurse for '.' and '..' */
if (DOT_OR_DOTDOT(entry->d_name))
continue;
retpath = find_block_device_in_dir(ap);
if (retpath)
break;
}
}
closedir(dir);
return retpath;
}
char* find_block_device(const char *path)
{
struct arena a;
if (stat(path, &a.st) != 0)
return NULL;
a.dev = S_ISBLK(a.st.st_mode) ? a.st.st_rdev : a.st.st_dev;
strcpy(a.devpath, "/dev");
return find_block_device_in_dir(&a);
}
/* Like strncpy but make sure the resulting string is always 0 terminated. */
char* safe_strncpy(char *dst, const char *src, size_t size)
{
if (!size) return dst;
dst[--size] = '\0';
return strncpy(dst, src, size);
}
/* Like strcpy but can copy overlapping strings. */
void overlapping_strcpy(char *dst, const char *src)
{
/* Cheap optimization for dst == src case -
* better to have it here than in many callers.
*/
if (dst != src) {
while ((*dst = *src) != '\0') {
dst++;
src++;
}
}
}
// Die if we can't copy a string to freshly allocated memory.
char* xstrdup(const char *s)
{
char *t;
if (s == NULL)
return NULL;
t = strdup(s);
if (t == NULL) {
perror("xstrdup ran out of memory\n");
abort();
}
return t;
}
int main(int argc, char **argv) {
const char *rootfs = "/";
char *result;
if(argc > 1 )
result = find_block_device(argv[1]);
else
result = find_block_device(rootfs);
/* not likely but better safe than sorry */
if(result == NULL)
return 1;
printf("%s",result);
return 0;
}