Whamcloud - gitweb
Many files:
[tools/e2fsprogs.git] / misc / e2label.c
1 /*
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 <stdio.h>
15 #include <string.h>
16 #include <fcntl.h>
17 #include <ctype.h>
18 #include <termios.h>
19 #include <time.h>
20 #ifdef HAVE_GETOPT_H
21 #include <getopt.h>
22 #endif
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32
33 #define EXT2_SUPER_MAGIC 0xEF53
34
35 #define VOLNAMSZ 16
36
37 struct ext2_super_block {
38         char  s_dummy0[56];
39         unsigned char  s_magic[2];
40         char  s_dummy1[62];
41         char  s_volume_name[VOLNAMSZ];
42         char  s_last_mounted[64];
43         char  s_dummy2[824];
44 } sb;
45
46 static int open_e2fs (char *dev, int mode)
47 {
48         int fd;
49
50         fd = open(dev, mode);
51         if (fd < 0) {
52              perror(dev);
53              fprintf (stderr, "e2label: cannot open %s\n", dev);
54              exit(1);
55         }
56         if (lseek(fd, 1024, SEEK_SET) != 1024) {
57              perror(dev);
58              fprintf (stderr, "e2label: cannot seek to superblock\n");
59              exit(1);
60         }
61         if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
62              perror(dev);
63              fprintf (stderr, "e2label: error reading superblock\n");
64              exit(1);
65         }
66         if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) {
67              fprintf (stderr, "e2label: not an ext2 filesystem\n");
68              exit(1);
69         }
70
71         return fd;
72 }
73
74 static void print_label (char *dev)
75 {
76         char label[VOLNAMSZ+1];
77
78         open_e2fs (dev, O_RDONLY);
79         strncpy(label, sb.s_volume_name, VOLNAMSZ);
80         label[VOLNAMSZ] = 0;
81         printf("%s\n", label);
82 }
83
84 static void change_label (char *dev, char *label)
85 {
86         int fd;
87
88         fd = open_e2fs(dev, O_RDWR);
89         memset(sb.s_volume_name, 0, VOLNAMSZ);
90         strncpy(sb.s_volume_name, label, VOLNAMSZ);
91         if (strlen(label) > VOLNAMSZ)
92                 fprintf(stderr, "Warning: label too long, truncating.\n");
93         if (lseek(fd, 1024, SEEK_SET) != 1024) {
94              perror(dev);
95              fprintf (stderr, "e2label: cannot seek to superblock again\n");
96              exit(1);
97         }
98         if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) {
99              perror(dev);
100              fprintf (stderr, "e2label: error writing superblock\n");
101              exit(1);
102         }
103 }
104
105 int main (int argc, char ** argv)
106 {
107         if (argc == 2)
108              print_label(argv[1]);
109         else if (argc == 3)
110              change_label(argv[1], argv[2]);
111         else {
112              fprintf(stderr, "Usage: e2label device [newlabel]\n");
113              exit(1);
114         }
115         return 0;
116 }