blob: bc87a428266a1ef3a14838a837372808098eca57 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * e2label.c - Print or change the volume label on an ext2 fs
3 *
4 * Written by Andries Brouwer (aeb@cwi.nl), 970714
5 *
6 * Copyright 1997, 1998 by Theodore Ts'o.
7 *
8 * %Begin-Header%
9 * This file may be redistributed under the terms of the GNU Public
10 * License.
11 * %End-Header%
12 */
13
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
24extern char *optarg;
25extern 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 "nls-enable.h"
37
38#define EXT2_SUPER_MAGIC 0xEF53
39
40#define VOLNAMSZ 16
41
42struct 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;
50
51static int open_e2fs (char *dev, int mode)
52{
53 int fd;
54
55 fd = open(dev, mode);
56 if (fd < 0) {
57 perror(dev);
58 fprintf (stderr, _("e2label: cannot open %s\n"), dev);
59 exit(1);
60 }
61 if (lseek(fd, 1024, SEEK_SET) != 1024) {
62 perror(dev);
63 fprintf (stderr, _("e2label: cannot seek to superblock\n"));
64 exit(1);
65 }
66 if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
67 perror(dev);
68 fprintf (stderr, _("e2label: error reading superblock\n"));
69 exit(1);
70 }
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);
74 }
75
76 return fd;
77}
78
79static void print_label (char *dev)
80{
81 char label[VOLNAMSZ+1];
82
83 open_e2fs (dev, O_RDONLY);
84 strncpy(label, sb.s_volume_name, VOLNAMSZ);
85 label[VOLNAMSZ] = 0;
86 printf("%s\n", label);
87}
88
89static void change_label (char *dev, char *label)
90{
91 int fd;
92
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);
102 }
103 if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
104 perror(dev);
105 fprintf (stderr, _("e2label: error writing superblock\n"));
106 exit(1);
107 }
108}
109
110int main (int argc, char ** argv)
111{
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);
119 }
120 return 0;
121}