Imported Upstream version 4.0.40
[platform/upstream/mtools.git] / mcd.c
1 /*  Copyright 1986-1992 Emmet P. Gray.
2  *  Copyright 1996,1997,2000-2002,2009 Alain Knaff.
3  *  This file is part of mtools.
4  *
5  *  Mtools is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  Mtools is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * mcd.c: Change MSDOS directories
19  */
20
21 #include "sysincludes.h"
22 #include "mainloop.h"
23 #include "mtools.h"
24
25
26 static int mcd_callback(direntry_t *entry, MainParam_t *mp UNUSEDP)
27 {
28         FILE *fp;
29
30         if (!(fp = open_mcwd("w"))){
31                 fprintf(stderr,"mcd: Can't open mcwd .file for writing\n");
32                 return ERROR_ONE;
33         }
34
35         fprintPwd(fp, entry,0);
36         fprintf(fp, "\n");
37         fclose(fp);
38         return GOT_ONE | STOP_NOW;
39 }
40
41 static void usage(int ret) NORETURN;
42 static void usage(int ret)
43 {
44                 fprintf(stderr, "Mtools version %s, dated %s\n",
45                         mversion, mdate);
46                 fprintf(stderr, "Usage: %s: [-V] [-i image] msdosdirectory\n",
47                         progname);
48                 exit(ret);
49 }
50
51
52 void mcd(int argc, char **argv, int type UNUSEDP) NORETURN;
53 void mcd(int argc, char **argv, int type UNUSEDP)
54 {
55         struct MainParam_t mp;
56         int c;
57         
58         while ((c = getopt(argc, argv, "i:")) != EOF) {
59                 switch(c) {
60                 case 'i':
61                         set_cmd_line_image(optarg);
62                         break;
63                 case 'h':
64                         usage(0);
65                 default:
66                         usage(1);
67                 }
68         }
69
70         if (argc > optind + 1)
71                 usage(1);
72
73         init_mp(&mp);
74         mp.lookupflags = ACCEPT_DIR | NO_DOTS;
75         mp.dirCallback = mcd_callback;
76         if (argc == 1) {
77                 printf("%s\n", mp.mcwd);
78                 exit(0);
79         } else
80                 exit(main_loop(&mp, argv + optind, 1));
81 }