File: mmove.c

package info (click to toggle)
mtools 3.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,116 kB
  • ctags: 1,306
  • sloc: ansic: 11,489; sh: 2,052; makefile: 223; sed: 8
file content (285 lines) | stat: -rw-r--r-- 5,832 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * mmove.c
 * Renames/moves an MSDOS file
 *
 */


#define LOWERCASE

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "vfat.h"
#include "mainloop.h"
#include "plain_io.h"
#include "nameclash.h"
#include "file.h"
#include "fs.h"

/*
 * Preserve the file modification times after the fclose()
 */

typedef struct Arg_t {
	char *target;
	char *fromname;
	int nowarn;
	int single;
	int interactive;
	int verbose;
	int oldsyntax;
	MainParam_t mp;

	Stream_t *SrcDir;
	int entry;
	ClashHandling_t ch;
	Stream_t *targetDir;
} Arg_t;


/*
 * Open the named file for read, create the cluster chain, return the
 * directory structure or NULL on error.
 */
static struct directory *renameit(char *dosname,
				  char *longname,
				  void *arg0,
				  struct directory *dir)
{
	int entry;
	struct directory parentdir;
	Stream_t *Dir;
	Arg_t *arg = (Arg_t *) arg0;
	int fat;


	*dir = arg->mp.dir;
	strncpy(dir->name, dosname, 8);
	strncpy(dir->ext, dosname + 8, 3);

	if( (dir->attr & 0x10) && arg->targetDir){
		/* we have a directory here. Change its parent link */
		Dir = open_file(arg->targetDir, & arg->mp.dir);
		if(FileIsLocked(Dir)) {
			fprintf(stderr,
				"Cannot move directory \'%s%s\' into itself\n",
				dir->name,dir->ext);
			FREE(&Dir);
			return 0;
		}
		entry = 0;
		if(vfat_lookup(Dir,
			       &parentdir, &entry, 0, "..", ACCEPT_DIR,
			       NULL, NULL, NULL))
			fprintf(stderr," Directory has no parent entry\n");
		else {
			entry--; /* rewind entry */
			GET_DATA(arg->targetDir, 0, 0, 0, &fat);
			parentdir.start[1] = (fat >> 8) & 0xff;
			parentdir.start[0] = fat & 0xff;
			dir_write(Dir, entry, &parentdir);
			if(arg->verbose){
				fprintf(stderr,
					"Easy, isn't it? I wonder why DOS can't do this.\n");
			}
		}
		FREE(&Dir);
	}

	/* wipe out original entry */
	arg->mp.dir.name[0] = DELMARK;
	dir_write(arg->SrcDir, arg->entry, &arg->mp.dir);

	return dir;
}


static int rename_file(Stream_t *Dir, MainParam_t *mp, int entry)
/* rename a messy DOS file to another messy DOS file */
{
	int result;
	Stream_t *targetDir;
	char *shortname, *longname;

	Arg_t * arg = (Arg_t *) (mp->arg);

	arg->SrcDir = Dir;
	arg->entry = entry;
	if(arg->oldsyntax)
		targetDir = Dir;
	else
		targetDir = arg->targetDir;

	if (targetDir == Dir){
		arg->ch.ignore_entry = -1;
		arg->ch.source = entry;
	} else {
		arg->ch.ignore_entry = -1;
		arg->ch.source = -2;
	}


	if(arg->oldsyntax && !strcasecmp(mp->shortname, arg->fromname)){
		longname = mp->longname;
		shortname = arg->target;
	} else {
		longname = arg->target;
		shortname = 0;
	}

	result = mwrite_one(targetDir, longname, shortname,
			    renameit, (void *)arg, &arg->ch);
	if(result == 1)
		return GOT_ONE;
	else
		return MISSED_ONE;
}


static void usage(void)
{
	fprintf(stderr,
		"Mtools version %s, dated %s\n", mversion, mdate);
	fprintf(stderr,
		"Usage: %s [-itnmvV] file targetfile\n", progname);
	fprintf(stderr,
		"       %s [-itnmvV] file [files...] target_directory\n", 
		progname);
	exit(1);
}

void mmove(int argc, char **argv, int oldsyntax)
{
	Stream_t *SubDir,*Dir;
	Arg_t arg;
	int c, ret;
	char filename[VBUFSIZE];
	char spareOutname[VBUFSIZE];
	char shortname[13];
	char longname[VBUFSIZE];
	char def_drive;
	char *s;
	int i;
	int interactive;

	/* get command line options */

	init_clash_handling(& arg.ch);

	interactive = 0;


	/* get command line options */
	arg.verbose = 0;
	arg.nowarn = 0;
	arg.interactive = 0;
	while ((c = getopt(argc, argv, "invorsamORSAM")) != EOF) {
		switch (c) {
			case 'i':
				arg.interactive = 1;
				break;
			case 'n':
				arg.nowarn = 1;
				break;
			case 'v':	/* dummy option for mcopy */
				arg.verbose = 1;
				break;
			case '?':
				usage();
			default:
				if(handle_clash_options(&arg.ch, c))
					usage();
				break;
		}
	}

	if (argc - optind < 2)
		usage();


	/* only 1 file to rename... */
	arg.single = SINGLE;
	
	init_mp(&arg.mp);		
	arg.mp.arg = (void *) &arg;
	arg.mp.openflags = O_RDWR;

	/* look for a default drive */
	def_drive = '\0';
	for(i=optind; i<argc; i++)
		if(argv[i][0] && argv[i][1] == ':' ){
			if(!def_drive)
				def_drive = toupper(argv[i][0]);
			else if(def_drive != toupper(argv[i][0])){
				fprintf(stderr,
					"Cannot move files across different drives\n");
				exit(1);
			}
		}

	if(def_drive)
		*(arg.mp.mcwd) = def_drive;

	if (oldsyntax && (argc - optind != 2 || strpbrk(":/", argv[argc-1])))
		oldsyntax = 0;

	arg.oldsyntax = oldsyntax;

	if (!oldsyntax){
		Dir = open_subdir(&arg.mp, argv[argc-1], O_RDWR, 0, 1);
		if(!Dir){
			fprintf(stderr,"Bad target\n");
			exit(1);
		}
		get_name(argv[argc-1], filename, arg.mp.mcwd);
		SubDir = descend(Dir, filename, 0, 0, 1);
		if (!SubDir){
			/* the last parameter is not a directory: take
			 * it as the new name */
			if ( argc - optind != 2 ){
				fprintf(stderr,
					"%s: Too many arguments, or destination directory omitted\n", 
					argv[0]);
				exit(1);
			}
			
			arg.targetDir = Dir;
			arg.mp.outname = 0; /* toss away source name */
			arg.target = filename; /* store near name given as
						* target */
			arg.single = 1;
		} else {
			arg.targetDir = SubDir;
			FREE(&Dir);
			arg.mp.outname = arg.target = filename;
			arg.single = 0;
		}
	} else {
		arg.fromname = argv[optind];
		if(arg.fromname[0] && arg.fromname[1] == ':')
			arg.fromname += 2;
		s = strrchr(arg.fromname, '/');
		if(s)
			arg.fromname = s+1;
		arg.mp.outname = filename;
		arg.single = 0;
		arg.targetDir = NULL;
		arg.target = argv[argc-1];
	}

	arg.mp.callback = rename_file;

	arg.mp.lookupflags = 
	  ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN | NO_DOTS | arg.single;

	arg.mp.longname = longname;
	arg.mp.shortname = shortname;
	if(!arg.mp.outname)
		arg.mp.outname = spareOutname;

	ret=main_loop(&arg.mp, argv + optind, argc - optind - 1);
	FREE(&arg.targetDir);

	exit(ret);
}