import source from 3.0.10
[external/dosfstools.git] / src / dosfslabel.c
1 /* dosfslabel.c - User interface
2
3    Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch>
4    Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5    Copyright (C) 2007 Red Hat, Inc.
6
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20    On Debian systems, the complete text of the GNU General Public License
21    can be found in /usr/share/common-licenses/GPL-3 file.
22 */
23
24 #include "version.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <getopt.h>
32
33 #include "common.h"
34 #include "dosfsck.h"
35 #include "io.h"
36 #include "boot.h"
37 #include "fat.h"
38 #include "file.h"
39 #include "check.h"
40
41
42 int interactive = 0,rw = 0,list = 0,test = 0,verbose = 0,write_immed = 0;
43 int atari_format = 0;
44 unsigned n_files = 0;
45 void *mem_queue = NULL;
46
47
48 static void usage(int error)
49 {
50     FILE *f = error ? stderr : stdout;
51     int status = error ? 1 : 0;
52
53     fprintf(f,"usage: dosfslabel device [label]\n");
54     exit(status);
55 }
56
57 /*
58  * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant
59  * of MS-DOS filesystem by default.
60  */
61 static void check_atari( void )
62 {
63 #ifdef __mc68000__
64     FILE *f;
65     char line[128], *p;
66
67     if (!(f = fopen( "/proc/hardware", "r" ))) {
68         perror( "/proc/hardware" );
69         return;
70     }
71
72     while( fgets( line, sizeof(line), f ) ) {
73         if (strncmp( line, "Model:", 6 ) == 0) {
74             p = line + 6;
75             p += strspn( p, " \t" );
76             if (strncmp( p, "Atari ", 6 ) == 0)
77                 atari_format = 1;
78             break;
79         }
80     }
81     fclose( f );
82 #endif
83 }
84
85
86 int main(int argc, char *argv[])
87 {
88     DOS_FS fs;
89     rw = 0;
90
91     char *device = NULL;
92     char *label = NULL;
93
94     check_atari();
95
96     if (argc < 2 || argc > 3)
97         usage(1);
98
99     if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
100         usage(0);
101     else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) {
102         printf( "dosfslabel " VERSION ", " VERSION_DATE ", FAT32, LFN\n" );
103         exit(0);
104     }
105
106     device = argv[1];
107     if (argc == 3) {
108         label = argv[2];
109         if (strlen(label) > 11) {
110             fprintf(stderr,
111                     "dosfslabel: labels can be no longer than 11 characters\n");
112             exit(1);
113         }
114         rw = 1;
115     }
116
117     fs_open(device, rw);
118     read_boot(&fs);
119     if (!rw) {
120         fprintf(stdout, "%s\n", fs.label);
121         exit(0);
122     }
123
124     write_label(&fs, label);
125     fs_close(rw);
126     return 0;
127 }