My Tech notes: Get Mounted File System Type C function
Subscribe

Unix Documentation

Free Online Unix Training Materials

Lists many links to free Unix training materials.

Pointers and Arrays Materials

Pointers and Arrays materials Explained for C beginners

C FAQ and General Questions C Interview Questions

Powered By

Free XML Skins for Blogger

Powered by Blogger

Wednesday, November 12, 2008

Get Mounted File System Type C function

How to get Mounted file system type using C program?
Here is trick to get mounted file system type or say this trick can be applied to get variable from linux; This is simple combination of "grep" and "awk" commands. check out below; this function takes mounted point name like where you have mounted the disk example
/dev/sda1 is being mounted at /mnt/test so the input argument to this function is test and the output should get is /dev/sda1 filesytem type.
If you type mount command you will get the file type

on my system with one pen drive; it shows
dev/sda1 on /mnt/JetFlash type ext3 (rw)
input to this function is JetFlash and I will get return as ext3 which is file system type.
Description          : returns mounted FS type
char *get_fs_type(char *mount_point_name)
{
char temp[20];
char cmd[100],*ret_p;
FILE *fptr;


sprintf(cmd,"/sbin/fdisk -l | grep %s | awk '{print $6,$7}' > /var/mpt",mount_point_name);
system(cmd);

fptr = fopen("/var/mpt","r");
fgets(cmd,20,fptr);
fclose(fptr);
ret_p = (char*)malloc(strlen(cmd) + 1);
if(ret_p != NULL)
strcpy(ret_p,cmd);
else
strcpy(ret_p,"NULL");

return ret_p;
}



one word of caution
this function uses fgets to read from file; the returned output contains an appended new line character; so before using this returned output in other commands one must remove new line char using simple trick like;
temp_p[strlen(temp_p)-l] = '\0'

No comments:

Post a Comment