📄 mountlist.c
字号:
/* mountlist.c -- return a list of mounted filesystems Copyright (C) 1991, 1992, 1997-2002 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#if HAVE_CONFIG_H# include <config.h>#endif#include <stdio.h>#include <sys/types.h>#ifdef STDC_HEADERS# include <stdlib.h>#elsevoid free ();#endif#if defined STDC_HEADERS || defined HAVE_STRING_H# include <string.h>#else# include <strings.h>#endif#include "xalloc.h"#ifndef strstrchar *strstr ();#endif#include <errno.h>#ifndef errnoextern int errno;#endif#ifdef HAVE_FCNTL_H# include <fcntl.h>#endif#ifdef HAVE_UNISTD_H# include <unistd.h>#endif#if HAVE_SYS_PARAM_H# include <sys/param.h>#endif#if defined MOUNTED_GETFSSTAT /* OSF_1 and Darwin1.3.x */# if HAVE_SYS_UCRED_H# include <sys/ucred.h> /* needed by powerpc-apple-darwin1.3.7 */# endif# if HAVE_SYS_MOUNT_H# include <sys/mount.h># endif# if HAVE_SYS_FS_TYPES_H# include <sys/fs_types.h> /* needed by powerpc-apple-darwin1.3.7 */# endif# if HAVE_STRUCT_FSSTAT_F_FSTYPENAME# define FS_TYPE(Ent) ((Ent).f_fstypename)# else# define FS_TYPE(Ent) mnt_names[(Ent).f_type]# endif#endif /* MOUNTED_GETFSSTAT */#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */# include <mntent.h># if !defined MOUNTED# if defined _PATH_MOUNTED /* GNU libc */# define MOUNTED _PATH_MOUNTED# endif# if defined MNT_MNTTAB /* HP-UX. */# define MOUNTED MNT_MNTTAB# endif# if defined MNTTABNAME /* Dynix. */# define MOUNTED MNTTABNAME# endif# endif#endif#ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */# include <sys/mount.h>#endif#ifdef MOUNTED_GETMNT /* Ultrix. */# include <sys/mount.h># include <sys/fs_types.h>#endif#ifdef MOUNTED_FS_STAT_DEV /* BeOS. */# include <fs_info.h># include <dirent.h>#endif#ifdef MOUNTED_FREAD /* SVR2. */# include <mnttab.h>#endif#ifdef MOUNTED_FREAD_FSTYP /* SVR3. */# include <mnttab.h># include <sys/fstyp.h># include <sys/statfs.h>#endif#ifdef MOUNTED_LISTMNTENT# include <mntent.h>#endif#ifdef MOUNTED_GETMNTENT2 /* SVR4. */# include <sys/mnttab.h>#endif#ifdef MOUNTED_VMOUNT /* AIX. */# include <fshelp.h># include <sys/vfs.h>#endif#ifdef DOLPHIN/* So special that it's not worth putting this in autoconf. */# undef MOUNTED_FREAD_FSTYP# define MOUNTED_GETMNTTBL#endif#if HAVE_SYS_MNTENT_H/* This is to get MNTOPT_IGNORE on e.g. SVR4. */# include <sys/mntent.h>#endif#undef MNT_IGNORE#if defined MNTOPT_IGNORE && defined HAVE_HASMNTOPT# define MNT_IGNORE(M) hasmntopt ((M), MNTOPT_IGNORE)#else# define MNT_IGNORE(M) 0#endif#include "mountlist.h"#include "unlocked-io.h"#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. *//* Return the value of the hexadecimal number represented by CP. No prefix (like '0x') or suffix (like 'h') is expected to be part of CP. *//* FIXME: this can overflow */static intxatoi (char *cp){ int val; val = 0; while (*cp) { if (*cp >= 'a' && *cp <= 'f') val = val * 16 + *cp - 'a' + 10; else if (*cp >= 'A' && *cp <= 'F') val = val * 16 + *cp - 'A' + 10; else if (*cp >= '0' && *cp <= '9') val = val * 16 + *cp - '0'; else break; cp++; } return val;}#endif /* MOUNTED_GETMNTENT1. */#if MOUNTED_GETMNTINFO# if ! HAVE_F_FSTYPENAME_IN_STATFSstatic char *fstype_to_string (short t){ switch (t) {# ifdef MOUNT_PC case MOUNT_PC: return "pc";# endif# ifdef MOUNT_MFS case MOUNT_MFS: return "mfs";# endif# ifdef MOUNT_LO case MOUNT_LO: return "lo";# endif# ifdef MOUNT_TFS case MOUNT_TFS: return "tfs";# endif# ifdef MOUNT_TMP case MOUNT_TMP: return "tmp";# endif# ifdef MOUNT_UFS case MOUNT_UFS: return "ufs" ;# endif# ifdef MOUNT_NFS case MOUNT_NFS: return "nfs" ;# endif# ifdef MOUNT_MSDOS case MOUNT_MSDOS: return "msdos" ;# endif# ifdef MOUNT_LFS case MOUNT_LFS: return "lfs" ;# endif# ifdef MOUNT_LOFS case MOUNT_LOFS: return "lofs" ;# endif# ifdef MOUNT_FDESC case MOUNT_FDESC: return "fdesc" ;# endif# ifdef MOUNT_PORTAL case MOUNT_PORTAL: return "portal" ;# endif# ifdef MOUNT_NULL case MOUNT_NULL: return "null" ;# endif# ifdef MOUNT_UMAP case MOUNT_UMAP: return "umap" ;# endif# ifdef MOUNT_KERNFS case MOUNT_KERNFS: return "kernfs" ;# endif# ifdef MOUNT_PROCFS case MOUNT_PROCFS: return "procfs" ;# endif# ifdef MOUNT_AFS case MOUNT_AFS: return "afs" ;# endif# ifdef MOUNT_CD9660 case MOUNT_CD9660: return "cd9660" ;# endif# ifdef MOUNT_UNION case MOUNT_UNION: return "union" ;# endif# ifdef MOUNT_DEVFS case MOUNT_DEVFS: return "devfs" ;# endif# ifdef MOUNT_EXT2FS case MOUNT_EXT2FS: return "ext2fs" ;# endif default: return "?"; }}# endif /* ! HAVE_F_FSTYPENAME_IN_STATFS *//* __NetBSD__ || BSD_NET2 || __OpenBSD__ */static char *fsp_to_string (const struct statfs *fsp){# if defined HAVE_F_FSTYPENAME_IN_STATFS return (char *) (fsp->f_fstypename);# else return fstype_to_string (fsp->f_type);# endif}#endif /* MOUNTED_GETMNTINFO */#ifdef MOUNTED_VMOUNT /* AIX. */static char *fstype_to_string (int t){ struct vfs_ent *e; e = getvfsbytype (t); if (!e || !e->vfsent_name) return "none"; else return e->vfsent_name;}#endif /* MOUNTED_VMOUNT *//* Return a list of the currently mounted filesystems, or NULL on error. Add each entry to the tail of the list so that they stay in order. If NEED_FS_TYPE is nonzero, ensure that the filesystem type fields in the returned list are valid. Otherwise, they might not be. */struct mount_entry *read_filesystem_list (int need_fs_type){ struct mount_entry *mount_list; struct mount_entry *me; struct mount_entry **mtail = &mount_list;#ifdef MOUNTED_LISTMNTENT { struct tabmntent *mntlist, *p; struct mntent *mnt; struct mount_entry *me; /* the third and fourth arguments could be used to filter mounts, but Crays doesn't seem to have any mounts that we want to remove. Specifically, automount create normal NFS mounts. */ if(listmntent(&mntlist, KMTAB, NULL, NULL) < 0) return NULL; for (p = mntlist; p; p = p->next) { mnt = p->ment; me = (struct mount_entry*) xmalloc(sizeof (struct mount_entry)); me->me_devname = xstrdup(mnt->mnt_fsname); me->me_mountdir = xstrdup(mnt->mnt_dir); me->me_type = xstrdup(mnt->mnt_type); me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = -1; *mtail = me; mtail = &me->me_next; } freemntlist(mntlist); }#endif#ifdef MOUNTED_GETMNTENT1 /* 4.3BSD, SunOS, HP-UX, Dynix, Irix. */ { struct mntent *mnt; char *table = MOUNTED; FILE *fp; char *devopt; fp = setmntent (table, "r"); if (fp == NULL) return NULL; while ((mnt = getmntent (fp))) { me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry)); me->me_devname = xstrdup (mnt->mnt_fsname); me->me_mountdir = xstrdup (mnt->mnt_dir); me->me_type = xstrdup (mnt->mnt_type); me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); devopt = strstr (mnt->mnt_opts, "dev="); if (devopt) { if (devopt[4] == '0' && (devopt[5] == 'x' || devopt[5] == 'X')) me->me_dev = xatoi (devopt + 6); else me->me_dev = xatoi (devopt + 4); } else me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me; mtail = &me->me_next; } if (endmntent (fp) == 0) goto free_then_fail; }#endif /* MOUNTED_GETMNTENT1. */#ifdef MOUNTED_GETMNTINFO /* 4.4BSD. */ { struct statfs *fsp; int entries; entries = getmntinfo (&fsp, MNT_NOWAIT); if (entries < 0) return NULL; for (; entries-- > 0; fsp++) { char *fs_type = fsp_to_string (fsp); me = (struct mount_entry *) xmalloc (sizeof (struct mount_entry)); me->me_devname = xstrdup (fsp->f_mntfromname); me->me_mountdir = xstrdup (fsp->f_mntonname); me->me_type = fs_type; me->me_dummy = ME_DUMMY (me->me_devname, me->me_type); me->me_remote = ME_REMOTE (me->me_devname, me->me_type); me->me_dev = (dev_t) -1; /* Magic; means not known yet. */ /* Add to the linked list. */ *mtail = me;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -