Skip to content

Instantly share code, notes, and snippets.

@wezm
Created October 29, 2009 05:19
Show Gist options
  • Save wezm/221173 to your computer and use it in GitHub Desktop.
Save wezm/221173 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <blkid/blkid.h> /* Linux, requires libblkid-dev on Debian/Ubuntu */
/* Small program to print the device name that a file resides on.
Uses stat to get the device id then devname or blkid_devno_to_devname
to convert the dev id to a device name */
int main(int argc, char **argv)
{
struct stat s;
char *name;
if(argc < 1) {
printf("Usage: %s file\n", argv[0]);
return EXIT_FAILURE;
}
if(lstat(argv[1], &s) != 0) {
printf("Unable to stat %s\n", argv[1]);
return EXIT_FAILURE;
}
/* BSD */
/*name = devname(s.st_dev, S_IFBLK);*/
name = blkid_devno_to_devname(s.st_dev);
if(name == NULL) {
printf("Unable to get devname\n");
return EXIT_FAILURE;
}
printf("%s\n", name);
return EXIT_SUCCESS;
}
/* Compile on Linux: gcc -o devname -Wall -lblkid dev.c
Mac OS X/BSD: gcc -o devname -Wall dev.c */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment