rezize2fs: fix memory leak when fixing up the orphan file inode
[e2fsprogs.git] / misc / e2label.c
blob910ccc77221a41d9f1d1be4b100d989a8d2c57e3
1 /*
2 * e2label.c - Print or change the volume label on an ext2 fs
4 * Written by Andries Brouwer (aeb@cwi.nl), 970714
6 * Copyright 1997, 1998 by Theodore Ts'o.
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
14 #include "config.h"
15 #include <stdio.h>
16 #include <string.h>
17 #include <fcntl.h>
18 #include <ctype.h>
19 #include <termios.h>
20 #include <time.h>
21 #ifdef HAVE_GETOPT_H
22 #include <getopt.h>
23 #else
24 extern char *optarg;
25 extern int optind;
26 #endif
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #include "support/nls-enable.h"
38 #define EXT2_SUPER_MAGIC 0xEF53
40 #define VOLNAMSZ 16
42 struct ext2_super_block {
43 char s_dummy0[56];
44 unsigned char s_magic[2];
45 char s_dummy1[62];
46 char s_volume_name[VOLNAMSZ];
47 char s_last_mounted[64];
48 char s_dummy2[824];
49 } sb;
51 static int open_e2fs (char *dev, int mode)
53 int fd;
55 fd = open(dev, mode);
56 if (fd < 0) {
57 perror(dev);
58 fprintf (stderr, _("e2label: cannot open %s\n"), dev);
59 exit(1);
61 if (lseek(fd, 1024, SEEK_SET) != 1024) {
62 perror(dev);
63 fprintf (stderr, _("e2label: cannot seek to superblock\n"));
64 exit(1);
66 if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
67 perror(dev);
68 fprintf (stderr, _("e2label: error reading superblock\n"));
69 exit(1);
71 if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
72 fprintf (stderr, _("e2label: not an ext2 filesystem\n"));
73 exit(1);
76 return fd;
79 static void print_label (char *dev)
81 char label[VOLNAMSZ+1];
83 open_e2fs (dev, O_RDONLY);
84 snprintf(label, sizeof(label), "%.*s", EXT2_LEN_STR(sb.s_volume_name));
85 label[VOLNAMSZ] = 0;
86 printf("%s\n", label);
89 static void change_label (char *dev, char *label)
91 int fd;
93 fd = open_e2fs(dev, O_RDWR);
94 memset(sb.s_volume_name, 0, VOLNAMSZ);
95 strncpy(sb.s_volume_name, label, VOLNAMSZ);
96 if (strlen(label) > VOLNAMSZ)
97 fprintf(stderr, _("Warning: label too long, truncating.\n"));
98 if (lseek(fd, 1024, SEEK_SET) != 1024) {
99 perror(dev);
100 fprintf (stderr, _("e2label: cannot seek to superblock again\n"));
101 exit(1);
103 if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
104 perror(dev);
105 fprintf (stderr, _("e2label: error writing superblock\n"));
106 exit(1);
110 int main (int argc, char ** argv)
112 if (argc == 2)
113 print_label(argv[1]);
114 else if (argc == 3)
115 change_label(argv[1], argv[2]);
116 else {
117 fprintf(stderr, _("Usage: e2label device [newlabel]\n"));
118 exit(1);
120 return 0;