That seems a whole lot more complicated than just calling statfs and looking at f_mntfromname. For example:
#include <strings.h>
#include <sys/param.h>
#include <sys/mount.h>
#include <stdbool.h>
#include <ctype.h>
bool paths_on_same_device (const char *path_a, const char *path_b)
{
struct statfs sfs_a, sfs_b;
if (statfs (path_a, &sfs_a) || statfs (path_b, &sfs_b))
return false;
/* Check that the prefixes (which should be <prefix><unit>) of
f_mntfromname match. */
const char *pa = sfs_a.f_mntfromname, *pb = sfs_b.f_mntfromname;
// First check the prefix
do {
if (*pa != *pb)
return false;
++pa, ++pb;
} while (*pa && *pb && !isdigit (*pa));
// Now check the unit number
while (*pa && *pb && isdigit (*pa)) {
if (*pa != *pb)
return false;
++pa, ++pb;
}
// We need this in case we have, say, /dev/disk1 & /dev/disk10
return !isdigit (*pb);
}
I've not thoroughly tested it so there might be bugs.