Imported Upstream version 4.0.18
[platform/upstream/mtools.git] / mshowfat.c
1 /*  Copyright 1997,2000-2002,2009,2011 Alain Knaff.
2  *  This file is part of mtools.
3  *
4  *  Mtools is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Mtools is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  * mcopy.c
18  * Copy an MSDOS files to and from Unix
19  *
20  */
21
22
23 #define LOWERCASE
24
25 #include "sysincludes.h"
26 #include "msdos.h"
27 #include "mtools.h"
28 #include "vfat.h"
29 #include "mainloop.h"
30 #include "plain_io.h"
31 #include "nameclash.h"
32 #include "file.h"
33 #include "fs.h"
34
35 typedef struct Arg_t {
36         MainParam_t mp;
37         off_t offset;
38 } Arg_t;
39
40 static int dos_showfat(direntry_t *entry, MainParam_t *mp)
41 {
42         Stream_t *File=mp->File;
43         Arg_t *arg = (Arg_t *) mp->arg;
44         fprintPwd(stdout, entry,0);
45         putchar(' ');
46         if(arg->offset == -1) {
47                 printFat(File);
48         } else {
49                 printFatWithOffset(File, arg->offset);
50         }
51         printf("\n");
52         return GOT_ONE;
53 }
54
55 static int unix_showfat(MainParam_t *mp)
56 {
57         fprintf(stderr,"File does not reside on a Dos fs\n");
58         return ERROR_ONE;
59 }
60
61
62 static void usage(int ret) NORETURN;
63 static void usage(int ret)
64 {
65         fprintf(stderr,
66                 "Mtools version %s, dated %s\n", mversion, mdate);
67         fprintf(stderr,
68                 "Usage: %s files\n", progname);
69         exit(ret);
70 }
71
72 void mshowfat(int argc, char **argv, int mtype)
73 {
74         Arg_t arg;
75         int c, ret;
76         
77         /* get command line options */
78         if(helpFlag(argc, argv))
79                 usage(0);
80         arg.offset = -1;
81         while ((c = getopt(argc, argv, "i:ho:")) != EOF) {
82                 switch (c) {
83                         case 'o':
84                                 arg.offset = str_to_offset(optarg);
85                                 break;
86                         case 'i':
87                                 set_cmd_line_image(optarg);
88                                 break;
89                         case 'h':
90                                 usage(0);
91                         case '?':
92                                 usage(1);
93                                 break;
94                 }
95         }
96
97         if (argc - optind < 1)
98                 usage(1);
99
100         /* only 1 file to handle... */
101         init_mp(&arg.mp);
102         arg.mp.arg = (void *) &arg;
103
104         arg.mp.callback = dos_showfat;
105         arg.mp.unixcallback = unix_showfat;
106
107         arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN;
108         ret=main_loop(&arg.mp, argv + optind, argc - optind);
109         exit(ret);
110 }