Index: rootdev.c |
diff --git a/rootdev.c b/rootdev.c |
index 053745d41b750a1d05c36b63f9952b9ecb7a2f81..c3dc175d8fc99221c44f1231f5996b9bffe37d24 100644 |
--- a/rootdev.c |
+++ b/rootdev.c |
@@ -13,7 +13,7 @@ |
static int |
-find_dev_recursive(char *dirnamebuf, int number) { |
+find_dev_recursive(char *dirnamebuf, int number, int deviceOnly) { |
DIR *dp; |
struct dirent *dir; |
struct stat s; |
@@ -31,10 +31,23 @@ find_dev_recursive(char *dirnamebuf, int number) { |
strcpy(dirnamebuf+dirnamelen+1, dir->d_name); |
if (lstat(dirnamebuf, &s) < 0) |
continue; |
- if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number) |
+ if ((s.st_mode & S_IFMT) == S_IFBLK && s.st_rdev == number){ |
+ if (deviceOnly) { |
+ int len = strlen(dirnamebuf); |
+ char c = 0; |
+ do { |
+ c = dirnamebuf[len-1]; |
+ --len; |
+ }while(c > 0 && c < 9 && len > 0); |
+ /* arm has "p" for partition */ |
+ if (dirnamebuf[len-1] == 'p') |
+ --len; |
+ dirnamebuf[len]='\0'; |
+ } |
return 1; |
+ } |
if ((s.st_mode & S_IFMT) == S_IFDIR && |
- find_dev_recursive(dirnamebuf, number)) |
+ find_dev_recursive(dirnamebuf, number, deviceOnly)) |
return 1; |
} |
dirnamebuf[dirnamelen] = 0; |
@@ -42,28 +55,46 @@ find_dev_recursive(char *dirnamebuf, int number) { |
return 0; |
} |
+void usage(){ |
+ printf ("rootdev \n\t-d (for device only)\n"); |
+} |
+ |
int main(int argc, char *argv[]) { |
struct stat s; |
char *file = "/"; |
static char name[PATH_MAX+1]; |
- |
- if (argc > 1) |
- file = argv[1]; |
+ int deviceOnly=0; |
+ int c; |
+ extern char *optarg; |
+ extern int optind, optopt; |
+ while ((c = getopt(argc, argv, "hd")) != -1) { |
+ switch(c) { |
+ case 'd': |
+ deviceOnly=1; |
+ break; |
+ case 'h': |
+ default: |
+ usage(); |
+ return 1; |
+ } |
+ } |
+ if (argc - optind >= 1) |
+ file = argv[optind]; |
if (stat(file, &s) < 0) |
err(1, "unable to stat %s", file); |
- |
+ |
if (!s.st_dev) |
err(1, "unknown device number 0"); |
strcpy(name, "/dev"); |
- if (!find_dev_recursive(name, s.st_dev)) { |
+ if (!find_dev_recursive(name, s.st_dev, deviceOnly)) { |
fprintf(stderr, "unable to find match\n"); |
return 1; |
} |
printf("%s\n", name); |
- |
+ |
return 0; |
} |