Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * e2label.c - Print or change the volume label on an ext2 fs |
| 3 | * |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 4 | * Written by Andries Brouwer (aeb@cwi.nl), 970714 |
| 5 | * |
| 6 | * Copyright 1997, 1998 by Theodore Ts'o. |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 7 | * |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 8 | * %Begin-Header% |
| 9 | * This file may be redistributed under the terms of the GNU Public |
| 10 | * License. |
| 11 | * %End-Header% |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 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> |
Theodore Ts'o | 373b833 | 2000-04-03 16:22:35 +0000 | [diff] [blame] | 22 | #else |
| 23 | extern char *optarg; |
| 24 | extern int optind; |
Theodore Ts'o | 80c0fc3 | 1997-11-03 19:46:49 +0000 | [diff] [blame] | 25 | #endif |
| 26 | #ifdef HAVE_UNISTD_H |
| 27 | #include <unistd.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_STDLIB_H |
| 30 | #include <stdlib.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_ERRNO_H |
| 33 | #include <errno.h> |
| 34 | #endif |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 35 | #include "nls-enable.h" |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 36 | |
| 37 | #define EXT2_SUPER_MAGIC 0xEF53 |
| 38 | |
| 39 | #define VOLNAMSZ 16 |
| 40 | |
| 41 | struct ext2_super_block { |
| 42 | char s_dummy0[56]; |
| 43 | unsigned char s_magic[2]; |
| 44 | char s_dummy1[62]; |
| 45 | char s_volume_name[VOLNAMSZ]; |
| 46 | char s_last_mounted[64]; |
| 47 | char s_dummy2[824]; |
| 48 | } sb; |
| 49 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 50 | static int open_e2fs (char *dev, int mode) |
| 51 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 52 | int fd; |
| 53 | |
| 54 | fd = open(dev, mode); |
| 55 | if (fd < 0) { |
| 56 | perror(dev); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 57 | fprintf (stderr, _("e2label: cannot open %s\n"), dev); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 58 | exit(1); |
| 59 | } |
| 60 | if (lseek(fd, 1024, SEEK_SET) != 1024) { |
| 61 | perror(dev); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 62 | fprintf (stderr, _("e2label: cannot seek to superblock\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 63 | exit(1); |
| 64 | } |
| 65 | if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { |
| 66 | perror(dev); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 67 | fprintf (stderr, _("e2label: error reading superblock\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 68 | exit(1); |
| 69 | } |
| 70 | if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 71 | fprintf (stderr, _("e2label: not an ext2 filesystem\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 72 | exit(1); |
| 73 | } |
| 74 | |
| 75 | return fd; |
| 76 | } |
| 77 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 78 | static void print_label (char *dev) |
| 79 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 80 | char label[VOLNAMSZ+1]; |
| 81 | |
| 82 | open_e2fs (dev, O_RDONLY); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 83 | strncpy(label, sb.s_volume_name, VOLNAMSZ); |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 84 | label[VOLNAMSZ] = 0; |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 85 | printf("%s\n", label); |
| 86 | } |
| 87 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 88 | static void change_label (char *dev, char *label) |
| 89 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 90 | int fd; |
| 91 | |
| 92 | fd = open_e2fs(dev, O_RDWR); |
| 93 | memset(sb.s_volume_name, 0, VOLNAMSZ); |
| 94 | strncpy(sb.s_volume_name, label, VOLNAMSZ); |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 95 | if (strlen(label) > VOLNAMSZ) |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 96 | fprintf(stderr, _("Warning: label too long, truncating.\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 97 | if (lseek(fd, 1024, SEEK_SET) != 1024) { |
| 98 | perror(dev); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 99 | fprintf (stderr, _("e2label: cannot seek to superblock again\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 100 | exit(1); |
| 101 | } |
| 102 | if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { |
| 103 | perror(dev); |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 104 | fprintf (stderr, _("e2label: error writing superblock\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 105 | exit(1); |
| 106 | } |
| 107 | } |
| 108 | |
Theodore Ts'o | a789d84 | 1998-03-30 01:20:55 +0000 | [diff] [blame] | 109 | int main (int argc, char ** argv) |
| 110 | { |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 111 | if (argc == 2) |
| 112 | print_label(argv[1]); |
| 113 | else if (argc == 3) |
| 114 | change_label(argv[1], argv[2]); |
| 115 | else { |
Theodore Ts'o | d9c56d3 | 2000-02-08 00:47:55 +0000 | [diff] [blame] | 116 | fprintf(stderr, _("Usage: e2label device [newlabel]\n")); |
Theodore Ts'o | ab6b8ab | 1997-07-14 19:28:55 +0000 | [diff] [blame] | 117 | exit(1); |
| 118 | } |
| 119 | return 0; |
| 120 | } |