Changes by: flatcap
Update of /cvsroot/linux-ntfs/linux-ntfs/ntfstools
In directory usw-pr-cvs1:/tmp/cvs-serv29422/ntfstools
Added Files:
ntfslabel.c
Log Message:
Matt Fanto's changes to retrieve the volume name and a utility to print it
--- NEW FILE ---
/*
* ntfslabel.c - Change the label on an NTFS partition
*
* Copyright (c) 2002 Matthew J. Fanto <fan...@cm...>
*
*
* This program/include file 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/include file 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 (in the main directory of the Linux-NTFS
* distribution in the file COPYING); if not, write to the Free Software
* Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <errno.h>
#include <mntent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "types.h"
#include "volume.h"
#define NTFS_MF_MOUNTED 1
const char *EXEC_NAME = "ntfslabel";
const char *AUTHOR = "Matthew Fanto";
const char *VERSION = "1.0";
ntfs_volume *vol;
void parse_options(int argc, char *argv[]);
void change_label(const char *device);
int check_mntent(const char *file);
void display_syntax();
void display_label(const char *device);
int main(int argc, char **argv)
{
if(argc && *argv)
fprintf(stderr, "%s v%s - %s\n",EXEC_NAME,VERSION,AUTHOR);
parse_options(argc, argv);
}
void parse_options(int argc, char *argv[])
{
int c;
if(argc < 2)
display_syntax();
while((c = getopt(argc, argv, "p:c:h")) != EOF)
switch(c){
case 'p':
display_label(optarg);
break;
case 'c':
change_label(optarg);
break;
case 'h':
display_syntax();
}
}
void display_syntax()
{
printf("Syntax: ntfslabel [options] device\n\n "
" -p, print the current label\n "
" -c, change the current label\n");
}
void display_label(const char *device)
{
vol = ntfs_mount(device,0);
if(!vol)
{
printf("Error: ntfs_mount() failed!");
exit(1);
}
printf("Volume Label: %s\n",vol->vol_name);
}
void change_label(const char *device)
{
printf("now changing label\n");
/*First we need to make sure the device is not mounted*/
check_mount(device);
}
/* Only check if the mount is readonly when it's the root filesystem. */
int check_mntent(const char *file)
{
FILE *m_f;
struct mntent *mnt;
int m_fd;
if ((m_f = setmntent(MOUNTED, "r")) == NULL)
return -errno;
while ((mnt = getmntent(m_f)) != NULL)
if (strcmp(file, mnt->mnt_fsname) == 0)
break;
endmntent(m_f);
if (mnt == 0)
return 0;
else
return NTFS_MF_MOUNTED;
}
int check_mount(const char *device)
{
int r;
struct stat sbuf;
/*First verify we are dealing with a valid block device.*/
if(stat(device,&sbuf)==-1)
{
fprintf(stderr,"Error: device %s is not a valid block device.\n",device);
exit(1);
}
if(r = check_mntent(device))
{
printf("Error: device %s is currently mounted.\n",device);
exit(1);
}
}
|