New API: part-to-partnum

This converts a partition device name (eg. /dev/sda1) to a partition
number (eg. 1).  This is useful in conjunction with the parted APIs
that mostly take a disk device + partnum.
This commit is contained in:
Richard W.M. Jones
2011-10-25 12:46:05 +01:00
parent 1e891d9007
commit 47412f137f
3 changed files with 43 additions and 2 deletions

View File

@@ -220,3 +220,28 @@ do_part_to_dev (const char *part)
return r;
}
int
do_part_to_partnum (const char *part)
{
int err = 1;
size_t n = strlen (part);
while (n >= 1 && c_isdigit (part[n-1])) {
err = 0;
n--;
}
if (err) {
reply_with_error ("device name is not a partition");
return -1;
}
int r;
if (sscanf (&part[n], "%d", &r) != 1) {
reply_with_error ("could not parse number");
return -1;
}
return r;
}