119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project/* 219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * e2label.c - Print or change the volume label on an ext2 fs 319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * 419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Written by Andries Brouwer (aeb@cwi.nl), 970714 53984b61df41c68966bdfbb2a5e5a45ef4b9a536cDmitry Shmidt * 619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * Copyright 1997, 1998 by Theodore Ts'o. 719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * 819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %Begin-Header% 919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * This file may be redistributed under the terms of the GNU Public 1019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * License. 1119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project * %End-Header% 1219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project */ 1319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 1419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdio.h> 1519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <string.h> 1619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <fcntl.h> 1719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <ctype.h> 1819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <termios.h> 1919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <time.h> 2019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#ifdef HAVE_GETOPT_H 2119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <getopt.h> 2219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#else 2319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectextern char *optarg; 2419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectextern int optind; 2519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif 2619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#ifdef HAVE_UNISTD_H 2719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <unistd.h> 2819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif 2919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#ifdef HAVE_STDLIB_H 3019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <stdlib.h> 3119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif 3219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#ifdef HAVE_ERRNO_H 3319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include <errno.h> 3419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#endif 3519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#include "nls-enable.h" 3619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 3719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#define EXT2_SUPER_MAGIC 0xEF53 3819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 3919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project#define VOLNAMSZ 16 4019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 4119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectstruct ext2_super_block { 4219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char s_dummy0[56]; 4319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project unsigned char s_magic[2]; 4419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char s_dummy1[62]; 4519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char s_volume_name[VOLNAMSZ]; 4619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char s_last_mounted[64]; 4719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char s_dummy2[824]; 4819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} sb; 4919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 5019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectstatic int open_e2fs (char *dev, int mode) 5119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 5219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project int fd; 5319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 5419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fd = open(dev, mode); 5519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (fd < 0) { 5619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project perror(dev); 5719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: cannot open %s\n"), dev); 5819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 5919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 6019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (lseek(fd, 1024, SEEK_SET) != 1024) { 6119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project perror(dev); 6219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: cannot seek to superblock\n")); 6319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 6419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 6519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (read(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { 6619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project perror(dev); 6719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: error reading superblock\n")); 6819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 6919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 7019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (sb.s_magic[0] + 256*sb.s_magic[1] != EXT2_SUPER_MAGIC) { 7119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: not an ext2 filesystem\n")); 7219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 7319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 7419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 7519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return fd; 7619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 7719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 7819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectstatic void print_label (char *dev) 7919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 8019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project char label[VOLNAMSZ+1]; 8119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 8219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project open_e2fs (dev, O_RDONLY); 8319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project strncpy(label, sb.s_volume_name, VOLNAMSZ); 8419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project label[VOLNAMSZ] = 0; 8519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project printf("%s\n", label); 8619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 8719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 8819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectstatic void change_label (char *dev, char *label) 8919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 9019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project int fd; 9119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 9219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fd = open_e2fs(dev, O_RDWR); 9319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project memset(sb.s_volume_name, 0, VOLNAMSZ); 9419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project strncpy(sb.s_volume_name, label, VOLNAMSZ); 9519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (strlen(label) > VOLNAMSZ) 9619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf(stderr, _("Warning: label too long, truncating.\n")); 9719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (lseek(fd, 1024, SEEK_SET) != 1024) { 9819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project perror(dev); 9919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: cannot seek to superblock again\n")); 10019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 10119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 10219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (write(fd, (char *) &sb, sizeof(sb)) != sizeof(sb)) { 10319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project perror(dev); 10419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf (stderr, _("e2label: error writing superblock\n")); 10519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 10619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 10719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 10819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project 10919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Projectint main (int argc, char ** argv) 11019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project{ 11119dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project if (argc == 2) 11219dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project print_label(argv[1]); 11319dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project else if (argc == 3) 11419dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project change_label(argv[1], argv[2]); 11519dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project else { 11619dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project fprintf(stderr, _("Usage: e2label device [newlabel]\n")); 11719dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project exit(1); 11819dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project } 11919dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project return 0; 12019dacda2b02bb08c0ffb649f84526b249c749279The Android Open Source Project} 121