Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Unified Diff: rootdev.c

Issue 2814040: rootdev.c: Added -d option to strip trailing partition number (Closed) Base URL: http://src.chromium.org/git/rootdev.git
Patch Set: rootdev.c: Address comment from Bill. (--len in nl) Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698