1997-07-15 03:28:55 +08:00
|
|
|
/*
|
|
|
|
* e2label.c - Print or change the volume label on an ext2 fs
|
|
|
|
*
|
1998-03-30 09:20:55 +08:00
|
|
|
* Written by Andries Brouwer (aeb@cwi.nl), 970714
|
2008-08-28 11:07:54 +08:00
|
|
|
*
|
1998-03-30 09:20:55 +08:00
|
|
|
* Copyright 1997, 1998 by Theodore Ts'o.
|
1997-07-15 03:28:55 +08:00
|
|
|
*
|
1998-03-30 09:20:55 +08:00
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
1997-07-15 03:28:55 +08:00
|
|
|
*/
|
|
|
|
|
2011-09-19 05:34:37 +08:00
|
|
|
#include "config.h"
|
1997-07-15 03:28:55 +08:00
|
|
|
#include <stdio.h>
|
1997-11-04 03:46:49 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
Many files:
badblocks.c, dumpe2fs.c, e2label.c, mke2fs.c, tune2fs.c, uuidgen.c:
For platforms that don't define optarg.h, manually define optarg and
optind.
ChangeLog, main.c:
main.c: For platforms that don't define optarg.h, manually define
optarg and optind.
ChangeLog, unix.c:
unix.c: For platforms that don't define optarg.h, manually define
optarg and optind.
2000-04-04 00:22:35 +08:00
|
|
|
#else
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
1997-11-04 03:46:49 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
2015-07-13 09:55:32 +08:00
|
|
|
#include "support/nls-enable.h"
|
1997-07-15 03:28:55 +08:00
|
|
|
|
|
|
|
#define EXT2_SUPER_MAGIC 0xEF53
|
|
|
|
|
|
|
|
#define VOLNAMSZ 16
|
|
|
|
|
|
|
|
struct ext2_super_block {
|
|
|
|
char s_dummy0[56];
|
|
|
|
unsigned char s_magic[2];
|
|
|
|
char s_dummy1[62];
|
|
|
|
char s_volume_name[VOLNAMSZ];
|
|
|
|
char s_last_mounted[64];
|
|
|
|
char s_dummy2[824];
|
|
|
|
} sb;
|
|
|
|
|
1998-03-30 09:20:55 +08:00
|
|
|
static int open_e2fs (char *dev, int mode)
|
|
|
|
{
|
1997-07-15 03:28:55 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open(dev, mode);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror(dev);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: cannot open %s\n"), dev);
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (lseek(fd, 1024, SEEK_SET) != 1024) {
|
|
|
|
perror(dev);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: cannot seek to superblock\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
|
|
|
|
perror(dev);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: error reading superblock\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: not an ext2 filesystem\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
1998-03-30 09:20:55 +08:00
|
|
|
static void print_label (char *dev)
|
|
|
|
{
|
1997-07-15 03:28:55 +08:00
|
|
|
char label[VOLNAMSZ+1];
|
|
|
|
|
|
|
|
open_e2fs (dev, O_RDONLY);
|
2020-01-15 05:42:18 +08:00
|
|
|
snprintf(label, sizeof(label), "%.*s", EXT2_LEN_STR(sb.s_volume_name));
|
1998-03-30 09:20:55 +08:00
|
|
|
label[VOLNAMSZ] = 0;
|
1997-07-15 03:28:55 +08:00
|
|
|
printf("%s\n", label);
|
|
|
|
}
|
|
|
|
|
1998-03-30 09:20:55 +08:00
|
|
|
static void change_label (char *dev, char *label)
|
|
|
|
{
|
1997-07-15 03:28:55 +08:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open_e2fs(dev, O_RDWR);
|
|
|
|
memset(sb.s_volume_name, 0, VOLNAMSZ);
|
|
|
|
strncpy(sb.s_volume_name, label, VOLNAMSZ);
|
1998-03-30 09:20:55 +08:00
|
|
|
if (strlen(label) > VOLNAMSZ)
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf(stderr, _("Warning: label too long, truncating.\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
if (lseek(fd, 1024, SEEK_SET) != 1024) {
|
|
|
|
perror(dev);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: cannot seek to superblock again\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
|
|
|
|
perror(dev);
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf (stderr, _("e2label: error writing superblock\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-30 09:20:55 +08:00
|
|
|
int main (int argc, char ** argv)
|
|
|
|
{
|
1997-07-15 03:28:55 +08:00
|
|
|
if (argc == 2)
|
|
|
|
print_label(argv[1]);
|
|
|
|
else if (argc == 3)
|
|
|
|
change_label(argv[1], argv[2]);
|
|
|
|
else {
|
Many files:
badblocks.c, chattr.c, dumpe2fs.c, e2label.c, findsuper.c, fsck.c,
get_device_by_label.c, lsattr.c, mke2fs.c, mklost+found.c,
nls-enable.h, partinfo.c, tune2fs.c, uuidgen.c: Add
Internationalization support as suggested by Marco d'Itri
<md@linux.it>.
2000-02-08 08:47:55 +08:00
|
|
|
fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
|
1997-07-15 03:28:55 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|