getlabel.vfat.c

来自「linux下的一个mount工具」· C语言 代码 · 共 82 行

C
82
字号
/* *  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 of the License, 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 Library 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. *//* *$Id: getlabel.vfat.c,v 1.1.1.1 2000/09/13 19:36:45 lyonel Exp $ *  * Copyright (c) 2000, Lyonel Vincent <vincentl@ec-lyon.fr> * * This file may be redistributed under the terms of the GNU Public * License. * * Taken from aeb's mount, 990619 */#include <stdio.h>#include <string.h>#include <ctype.h>#include <fcntl.h>#include <unistd.h>/* From inspection of a few FAT filesystems - aeb *//* Unfortunately I find almost the same thing on an extended partition; *    it looks like a primary has some directory entries where the extended *       has a partition table: IO.SYS, MSDOS.SYS, WINBOOT.SYS */struct vfat_super_block_struct {		    u_char    s_dummy[3];		    u_char    s_os[8];      /* "MSDOS5.0" or "MSWIN4.0" or "MSWIN4.1" */				                /* mtools-3.9.4 writes "MTOOL394" */		    u_char    s_dummy2[32];		    u_char    s_label[11];  /* for DOS? */		    u_char    s_fs[8];      /* "FAT12   " or "FAT16   " or all zero   */	                                /* OS/2 BM has "FAT * " here. */		    u_char    s_dummy3[9];		    u_char    s_label2[11]; /* for Windows? */		    u_char    s_fs2[8];         /* garbage or "FAT32   " */};typedef struct vfat_super_block_struct vfat_super_block;int vfat_getlabel(int fd, char *label) {	/* start with a test for ext2, taken from mount_guess_fstype */	int i;	vfat_super_block vfatsb;	if (fd < 0)		return 0;	if (lseek(fd, 0, SEEK_SET) != 0	    || read(fd, (char *) &vfatsb, sizeof(vfatsb)) != sizeof(vfatsb)) {		return 0;	}	if ((strncmp(vfatsb.s_os, "MSDOS", 5) &&		 strncmp(vfatsb.s_os, "MSWIN", 5) &&		 strncmp(vfatsb.s_os, "MTOOL", 5) &&		 strncmp(vfatsb.s_os, "mkdosfs", 7) &&		 strncmp(vfatsb.s_os, "kmkdosfs", 8))&&		 strncmp(vfatsb.s_fs, "FAT12   ", 8) &&	     strncmp(vfatsb.s_fs, "FAT16   ", 8) &&	     strncmp(vfatsb.s_fs2, "FAT32   ", 8))			return 0;	for(i=10; i>=0; i--)			if(vfatsb.s_label[i] == ' ') vfatsb.s_label[i] = '\0';			else break;	strncpy(label, vfatsb.s_label, sizeof(vfatsb.s_label));	return 1;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?