Advertisement
Guest User

find_mnt_dev.c

a guest
Aug 19th, 2011
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. // Copyright (C) 2011  Pali Rohar  pali dot rohar at gmail dot com
  2. // License: GPL
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <limits.h>
  8. #include <errno.h>
  9.  
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13.  
  14. #include <libgen.h>
  15.  
  16. #include <mntent.h>
  17.  
  18. char * find_mnt_dir(const char * dir) {
  19.  
  20.     char save_cwd[PATH_MAX];
  21.     char last_cwd[PATH_MAX];
  22.     char cwd[PATH_MAX];
  23.     char * mnt_dir = NULL;
  24.     struct stat st;
  25.     __dev_t last_dev;
  26.     __ino_t last_ino;
  27.  
  28.     if ( ! getcwd(save_cwd, sizeof(save_cwd)) ) {
  29.  
  30.         fprintf(stderr, "Error: Cannot store working directory: %s\n", strerror(errno));
  31.         return NULL;
  32.  
  33.     }
  34.  
  35.     if ( stat(dir, &st) != 0 ) {
  36.  
  37.         fprintf(stderr, "Error: Cannot stat %s: %s\n", dir, strerror(errno));
  38.         return NULL;
  39.  
  40.     }
  41.  
  42.     if ( chdir(dir) == -1 ) {
  43.  
  44.         fprintf(stderr, "Error: Cannot change directory to %s: %s\n", dir, strerror(errno));
  45.         return NULL;
  46.  
  47.     }
  48.  
  49.     if ( ! getcwd(last_cwd, sizeof(last_cwd)) ) {
  50.  
  51.         fprintf(stderr, "Error: Cannot get working directory: %s\n", strerror(errno));
  52.  
  53.         if ( chdir(save_cwd) == -1 )
  54.             fprintf(stderr, "Error: Cannot restore working directory: %s\n", strerror(errno));
  55.  
  56.         return NULL;
  57.  
  58.     }
  59.  
  60.     last_dev = st.st_dev;
  61.     last_ino = st.st_ino;
  62.  
  63.     while ( 1 ) {
  64.  
  65.         if ( chdir("..") == -1 ) {
  66.  
  67.             fprintf(stderr, "Error: Cannot change working directory: %s\n", strerror(errno));
  68.             break;
  69.  
  70.         }
  71.  
  72.         if ( ! getcwd(cwd, sizeof(cwd)) ) {
  73.  
  74.             fprintf(stderr, "Error: Cannot get working directory: %s\n", strerror(errno));
  75.             break;
  76.  
  77.         }
  78.  
  79.         if ( stat(cwd, &st) != 0 ) {
  80.  
  81.             fprintf(stderr, "Error: Cannot stat %s: %s\n", cwd, strerror(errno));
  82.             break;
  83.  
  84.         }
  85.  
  86.         if ( last_dev != st.st_dev || last_ino == st.st_ino ) {
  87.  
  88.             mnt_dir = strdup(last_cwd);
  89.             break;
  90.  
  91.         }
  92.  
  93.         if ( strcmp(cwd, "/") == 0 ) {
  94.  
  95.             mnt_dir = strdup("/");
  96.             break;
  97.  
  98.         }
  99.  
  100.         last_dev = st.st_dev;
  101.         last_ino = st.st_ino;
  102.         strcpy(last_cwd, cwd);
  103.  
  104.     }
  105.  
  106.     if ( chdir(save_cwd) == -1 )
  107.         fprintf(stderr, "Error: Cannot restore working directory: %s\n", strerror(errno));
  108.  
  109.     return mnt_dir;
  110.  
  111. }
  112.  
  113. char * find_dev(const char * mnt_dir) {
  114.  
  115.     struct mntent * mnt;
  116.     char * dev = NULL;
  117.     FILE * file;
  118.  
  119.     file = setmntent("/etc/mtab", "r");
  120.  
  121.     if ( ! file ) {
  122.  
  123.         fprintf(stderr, "Error: Cannot open file /etc/mtab: %s\n", strerror(errno));
  124.         return NULL;
  125.  
  126.     }
  127.  
  128.     while ( mnt = getmntent(file) ) {
  129.  
  130.         if ( strcmp(mnt->mnt_dir, mnt_dir) == 0 ) {
  131.  
  132.             dev = strdup(mnt->mnt_fsname);
  133.             break;
  134.  
  135.         }
  136.  
  137.     }
  138.  
  139.     endmntent(file);
  140.  
  141.     return dev;
  142.  
  143. }
  144.  
  145. int main(int argc, char * argv[]) {
  146.  
  147.     struct stat st;
  148.     char * mnt_dir;
  149.     char * dev;
  150.  
  151.     if ( argc != 2 ) {
  152.  
  153.         printf("Usage: %s location\n", argv[0]);
  154.         return 1;
  155.  
  156.     }
  157.  
  158.     if ( stat(argv[1], &st) != 0 ) {
  159.  
  160.         fprintf(stderr, "Error: Cannot stat %s: %s\n", argv[1], strerror(errno));
  161.         return 1;
  162.  
  163.     }
  164.  
  165.     if ( ! S_ISDIR(st.st_mode) )
  166.         argv[1] = dirname(argv[1]);
  167.  
  168.     mnt_dir = find_mnt_dir(argv[1]);
  169.  
  170.     if ( ! mnt_dir ) {
  171.  
  172.         fprintf(stderr, "Error: Cannot find mount dir\n");
  173.         return 1;
  174.  
  175.     }
  176.  
  177.     dev = find_dev(mnt_dir);
  178.  
  179.     if ( ! dev ) {
  180.  
  181.         fprintf(stderr, "Error: Cannot find device for mount dir %s\n", mnt_dir);
  182.         free(mnt_dir);
  183.         return 1;
  184.  
  185.     }
  186.  
  187.     printf("%s\n", dev);
  188.  
  189.     free(mnt_dir);
  190.     free(dev);
  191.  
  192.     return 0;
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement