/******************************************************************************* * * fstype.c * * AUTHOR: * Dan Harkless * * COPYRIGHT: * This file is Copyright (C) 2016 by Dan Harkless, and is released under the * GNU General Public License . * * USAGE: * % fstype [-n] [...] * * DESCRIPTION: * Prints what kind of filesystem each given file lives in (e.g. autofs, cdfs, * ext2/3/4, hfs, jfs, nfs, tmpfs, ufs, etc.). Unrecognized filesystem types * will be printed as their hex magic numbers, and -n can be used to force * numeric output for all filesystem types, known and unknown. * * COMPILATION: * AIX/Solaris/...: % cc fstype.c -o fstype * HP-UX 9.x: % cc -Ae -DHPUX_9 fstype.c -o fstype * HP-UX 10.x: % cc -Ae fstype.c -o fstype * Linux: % gcc -DLINUX fstype.c -o fstype * * DATE MODIFICATION * ========== ================================================================== * 2016-03-14 Ported to 64-bit Linux; added the latest filesystem magic * numbers from /usr/include/linux/magic.h, the statfs man page, and * otherwise unrepresented filesystems found via 'mount'; replaced * spaces in output fstypes; changed the "unknown fstype" message to * output the magic number as hex; and added a -n option to force * outputting magic numbers rather than filesystem name strings. * 2001-12-27 Ported to Linux (somewhat inelegantly). * 1999-05-07 Original. * *******************************************************************************/ #include /* for errno */ #include /* for fprintf(), etc. */ #include /* for strrchr(), etc. */ #ifdef HPUX_9 #include /* for MOUNT_CDFS, etc. */ #include /* for statfs() and struct statfs */ #define NUMERIC_STR "0x%x" #define STAT_V_FS statfs #define UNKNOWN_STR "" #elif defined LINUX #include /* for statfs() */ #define STAT_V_FS statfs /* It used to be necessary to duplicate all the magic number #defines from /usr/include/linux/magic.h or we'd be forced to avoid any standard C library #includes and instead use Kernel syscalls to do our output. This appears to have been fixed at some point, but so that we'll continue to be compilable on a range of Linuxes and not have to put an #ifdef around each case statement, we'll go ahead and duplicate the magic.h #defines here (with their original comments, but alphabetized). Some of these, like STACK_END_MAGIC, are probably not filesystems, but it won't hurt to treat all of them as potentially so. */ #define ADFS_SUPER_MAGIC 0xadf5 #define AFFS_SUPER_MAGIC 0xadff #define AFS_SUPER_MAGIC 0x5346414F #define ANON_INODE_FS_MAGIC 0x09041934 #define AUTOFS_SUPER_MAGIC 0x0187 #define BDEVFS_MAGIC 0x62646576 #define BEFS_SUPER_MAGIC 0x42465331 #define BFS_MAGIC 0x1BADFACE #define BINFMTFS_MAGIC 0x42494e4d #define BTRFS_SUPER_MAGIC 0x9123683E #define BTRFS_TEST_MAGIC 0x73727279 #define CGROUP_SUPER_MAGIC 0x27e0eb #define CIFS_MAGIC_NUMBER 0xFF534D42 #define CODA_SUPER_MAGIC 0x73757245 #define COH_SUPER_MAGIC 0x012FF7B7 #define CONFIGFS_MAGIC 0x62656570 #define CRAMFS_MAGIC 0x28cd3d45 /* some random number */ #define CRAMFS_MAGIC_WEND 0x453dcd28 /* magic number with the wrong endianess */ #define DEBUGFS_MAGIC 0x64626720 #define DEVFS_SUPER_MAGIC 0x1373 #define DEVPTS_SUPER_MAGIC 0x1cd1 #define ECRYPTFS_SUPER_MAGIC 0xf15f #define EFIVARFS_MAGIC 0xde5e81e4 #define EFS_SUPER_MAGIC 0x414A53 #define EXT_SUPER_MAGIC 0x137D #define EXT2_OLD_SUPER_MAGIC 0xEF51 #define EXT2_SUPER_MAGIC 0xEF53 #define EXT3_SUPER_MAGIC 0xEF53 #define EXT4_SUPER_MAGIC 0xEF53 #define F2FS_SUPER_MAGIC 0xF2F52010 #define FUSE_GVFSD_FUSE_MAGIC 0x65735546 #define FUSECTL_MAGIC 0x65735543 #define FUTEXFS_SUPER_MAGIC 0xBAD1DEA #define HFS_SUPER_MAGIC 0x4244 #define HOSTFS_SUPER_MAGIC 0x00c0ffee #define HPFS_SUPER_MAGIC 0xf995e849 #define HUGETLBFS_MAGIC 0x958458f6 /* some random number */ #define ISOFS_SUPER_MAGIC 0x9660 #define JFFS2_SUPER_MAGIC 0x72b6 #define JFS_SUPER_MAGIC 0x3153464a #define MINIX_SUPER_MAGIC 0x137F /* minix v1 fs, 14 char names */ #define MINIX_SUPER_MAGIC2 0x138F /* minix v1 fs, 30 char names */ #define MINIX2_SUPER_MAGIC 0x2468 /* minix v2 fs, 14 char names */ #define MINIX2_SUPER_MAGIC2 0x2478 /* minix v2 fs, 30 char names */ #define MINIX3_SUPER_MAGIC 0x4d5a /* minix v3 fs, 60 char names */ #define MQUEUE_MAGIC 0x19800202 #define MSDOS_SUPER_MAGIC 0x4d44 /* MD */ #define MTD_INODE_FS_MAGIC 0x11307854 #define NCP_SUPER_MAGIC 0x564c /* Guess, what 0x564c is :-) */ #define NFS_SUPER_MAGIC 0x6969 #define NFSD_MAGIC 0x6e667364 #define NILFS_SUPER_MAGIC 0x3434 #define NTFS_SB_MAGIC 0x5346544e #define OPENPROM_SUPER_MAGIC 0x9fa1 #define PIPEFS_MAGIC 0x50495045 #define PROC_SUPER_MAGIC 0x9fa0 #define PSTOREFS_MAGIC 0x6165676C #define QNX4_SUPER_MAGIC 0x002f /* qnx4 fs detection */ #define QNX6_SUPER_MAGIC 0x68191122 /* qnx6 fs detection */ #define RAMFS_MAGIC 0x858458f6 /* some random number */ #define REISERFS_SUPER_MAGIC 0x52654973 #define ROMFS_MAGIC 0x7275 #define RPC_PIPEFS_MAGIC 0x67596969 #define SECURITYFS_MAGIC 0x73636673 #define SELINUX_MAGIC 0xf97cff8c #define SMACK_MAGIC 0x43415d53 /* "SMAC" */ #define SMB_SUPER_MAGIC 0x517B #define SOCKFS_MAGIC 0x534F434B #define SQUASHFS_MAGIC 0x73717368 #define STACK_END_MAGIC 0x57AC6E9D #define SYSFS_MAGIC 0x62656572 #define SYSV2_SUPER_MAGIC 0x012FF7B6 #define SYSV4_SUPER_MAGIC 0x012FF7B5 #define TMPFS_MAGIC 0x01021994 #define UDF_SUPER_MAGIC 0x15013346 #define UFS_MAGIC 0x00011954 #define USBDEVICE_SUPER_MAGIC 0x9fa2 #define V9FS_MAGIC 0x01021997 #define VXFS_SUPER_MAGIC 0xa501FCF5 #define XENFS_SUPER_MAGIC 0xabba1974 #define XENIX_SUPER_MAGIC 0x012FF7B4 #define XFS_SUPER_MAGIC 0x58465342 #define _XIAFS_SUPER_MAGIC 0x012FD16D /* Not sure if all other UNIXes have the f_fsid field as an fsid_t and vary it between 32 and 64 bits with the platform __WORDSIZE. */ #if (__WORDSIZE == 64) #define NUMERIC_STR "0x%lx" #define UNKNOWN_STR "" #else #define NUMERIC_STR "0x%x" #define UNKNOWN_STR "" #endif #else #include /* must precede */ #include /* for statvfs() and struct statvfs */ #define NUMERIC_STR "0x%x" #define STAT_V_FS statvfs #define UNKNOWN_STR "" #endif #define INT_MIN_STR "-2147483648" #define OK 0 int main(int argc, char** argv) { char* fstype_str; char* last_slash_ptr = strrchr(argv[0], '/'); char* our_program_name; int first_filename_index = 1; int i; int numeric = 0; struct STAT_V_FS file_stat; if (last_slash_ptr == NULL) our_program_name = argv[0]; else our_program_name = last_slash_ptr + 1; if (argc > 1 && strcmp(argv[1], "-n") == OK) { numeric = 1; first_filename_index = 2; } if (argc == 1 || (argc == 2 && numeric)) { fprintf(stderr, "Usage: %s [-n] [...]\n", our_program_name); return 1; } else { for (i = first_filename_index; i < argc; i++) if (STAT_V_FS(argv[i], &file_stat) != OK) fprintf(stderr, "%s: \"%s\": %s.\n", our_program_name, argv[i], strerror(errno)); else { #ifdef HPUX_9 char fstype_arr[sizeof(UNKNOWN_STR) + 15 + 1]; if (numeric) { sprintf(fstype_arr, NUMERIC_STR, file_stat.f_fsid[1]); fstype_str = fstype_arr; goto print_fstype; /* clearer than 'continue' statement */ } switch (file_stat.f_fsid[1]) { case MOUNT_CDFS: fstype_str = "cdfs"; break; case MOUNT_DCFS: fstype_str = "dcfs"; break; case MOUNT_NFS: fstype_str = "nfs"; break; case MOUNT_PC: fstype_str = "pc"; break; case MOUNT_UFS: fstype_str = "ufs"; break; default: sprintf(fstype_arr, UNKNOWN_STR, file_stat.f_fsid[1]); fstype_str = fstype_arr; } #elif LINUX char fstype_arr[sizeof(UNKNOWN_STR) + sizeof(INT_MIN_STR)]; if (numeric) { sprintf(fstype_arr, NUMERIC_STR, file_stat.f_type); fstype_str = fstype_arr; goto print_fstype; /* clearer than 'continue' statement */ } switch (file_stat.f_type) { case ADFS_SUPER_MAGIC: fstype_str = "adfs"; break; case AFFS_SUPER_MAGIC: fstype_str = "affs"; break; case AFS_SUPER_MAGIC: fstype_str = "afs"; break; case ANON_INODE_FS_MAGIC: fstype_str = "anon_inode_fs"; break; case AUTOFS_SUPER_MAGIC: fstype_str = "autofs"; break; case BDEVFS_MAGIC: fstype_str = "bdevfs"; break; case BEFS_SUPER_MAGIC: fstype_str = "befs"; break; case BFS_MAGIC: fstype_str = "bfs"; break; case BINFMTFS_MAGIC: fstype_str = "binfmtfs"; break; case BTRFS_SUPER_MAGIC: fstype_str = "btrfs"; break; case BTRFS_TEST_MAGIC: fstype_str = "btrfs_test"; break; case CGROUP_SUPER_MAGIC: fstype_str = "cgroup"; break; case CIFS_MAGIC_NUMBER: fstype_str = "cifs"; break; case CODA_SUPER_MAGIC: fstype_str = "coda"; break; case COH_SUPER_MAGIC: fstype_str = "coh"; break; case CONFIGFS_MAGIC: fstype_str = "configfs"; break; case CRAMFS_MAGIC: fstype_str = "cramfs"; break; case CRAMFS_MAGIC_WEND: fstype_str = "cramfs_wrong_endianness"; break; case DEBUGFS_MAGIC: fstype_str = "debugfs"; break; case DEVFS_SUPER_MAGIC: fstype_str = "devfs"; break; case DEVPTS_SUPER_MAGIC: fstype_str = "devpts"; break; case ECRYPTFS_SUPER_MAGIC: fstype_str = "ecryptfs"; break; case EFIVARFS_MAGIC: fstype_str = "efivarfs"; break; case EFS_SUPER_MAGIC: fstype_str = "efs"; break; case EXT_SUPER_MAGIC: fstype_str = "ext"; break; case EXT2_OLD_SUPER_MAGIC: fstype_str = "ext2_old"; break; case EXT2_SUPER_MAGIC: /* case EXT3_SUPER_MAGIC: case EXT4_SUPER_MAGIC: */ fstype_str = "ext2/3/4"; break; case F2FS_SUPER_MAGIC: fstype_str = "f2fs"; break; case FUSE_GVFSD_FUSE_MAGIC: fstype_str = "fuse.gvfsd-fuse"; break; case FUSECTL_MAGIC: fstype_str = "fusectl"; break; case FUTEXFS_SUPER_MAGIC: fstype_str = "futexfs"; break; case HFS_SUPER_MAGIC: fstype_str = "hfs"; break; case HOSTFS_SUPER_MAGIC: fstype_str = "hostfs"; break; case HPFS_SUPER_MAGIC: fstype_str = "hpfs"; break; case HUGETLBFS_MAGIC: fstype_str = "hugetlbfs"; break; case ISOFS_SUPER_MAGIC: fstype_str = "isofs"; break; case JFFS2_SUPER_MAGIC: fstype_str = "jffs2"; break; case JFS_SUPER_MAGIC: fstype_str = "jfs"; break; case MINIX_SUPER_MAGIC: fstype_str = "minix_v1_14-char._names"; break; case MINIX_SUPER_MAGIC2: fstype_str = "minix_v1_30-char._names"; break; case MINIX2_SUPER_MAGIC: fstype_str = "minix_v2_14-char._names"; break; case MINIX2_SUPER_MAGIC2: fstype_str = "minix_v2_30-char._names"; break; case MINIX3_SUPER_MAGIC: fstype_str = "minix_v3_60-char._names"; break; case MQUEUE_MAGIC: fstype_str = "mqueue"; break; case MSDOS_SUPER_MAGIC: fstype_str = "msdos"; break; case MTD_INODE_FS_MAGIC: fstype_str = "mtd_inode_fs"; break; case NCP_SUPER_MAGIC: fstype_str = "ncp"; break; case NFS_SUPER_MAGIC: fstype_str = "nfs"; break; case NFSD_MAGIC: fstype_str = "nfsd"; break; case NILFS_SUPER_MAGIC: fstype_str = "nilfs"; break; case NTFS_SB_MAGIC: fstype_str = "ntfs"; break; case OPENPROM_SUPER_MAGIC: fstype_str = "openprom"; break; case PIPEFS_MAGIC: fstype_str = "pipefs"; break; case PROC_SUPER_MAGIC: fstype_str = "proc"; break; case PSTOREFS_MAGIC: fstype_str = "pstorefs"; break; case QNX4_SUPER_MAGIC: fstype_str = "qnx4"; break; case QNX6_SUPER_MAGIC: fstype_str = "qnx6"; break; case RAMFS_MAGIC: fstype_str = "ramfs"; break; case REISERFS_SUPER_MAGIC: fstype_str = "reiserfs"; break; case ROMFS_MAGIC: fstype_str = "romfs"; break; case RPC_PIPEFS_MAGIC: fstype_str = "rpc_pipefs"; break; case SECURITYFS_MAGIC: fstype_str = "securityfs"; break; case SELINUX_MAGIC: fstype_str = "selinux"; break; case SMACK_MAGIC: fstype_str = "smack"; break; case SMB_SUPER_MAGIC: fstype_str = "smb"; break; case SOCKFS_MAGIC: fstype_str = "sockfs"; break; case SQUASHFS_MAGIC: fstype_str = "squashfs"; break; case STACK_END_MAGIC: fstype_str = "stack_end"; break; case SYSFS_MAGIC: fstype_str = "sysfs"; break; case SYSV2_SUPER_MAGIC: fstype_str = "sysv2"; break; case SYSV4_SUPER_MAGIC: fstype_str = "sysv4"; break; case TMPFS_MAGIC: fstype_str = "tmpfs"; break; case UDF_SUPER_MAGIC: fstype_str = "udf"; break; case UFS_MAGIC: fstype_str = "ufs"; break; case USBDEVICE_SUPER_MAGIC: fstype_str = "usbdevice"; break; case V9FS_MAGIC: fstype_str = "v9fs"; break; case VXFS_SUPER_MAGIC: fstype_str = "vxfs"; break; case XENFS_SUPER_MAGIC: fstype_str = "xenfs"; break; case XENIX_SUPER_MAGIC: fstype_str = "xenix"; break; case XFS_SUPER_MAGIC: fstype_str = "xfs"; break; case _XIAFS_SUPER_MAGIC: fstype_str = "xiafs"; break; default: sprintf(fstype_arr, UNKNOWN_STR, file_stat.f_type); fstype_str = fstype_arr; } #else if (numeric) { sprintf(fstype_arr, NUMERIC_STR, file_stat.f_fsid); fstype_str = fstype_arr; } else fstype_str = file_stat.f_basetype; #endif print_fstype: printf("%s: %s\n", argv[i], fstype_str); } return 0; } }