File: mmd.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 (177 lines) | stat: -rw-r--r-- 3,325 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
/*
 * mmd.c
 * Makes an MSDOS directory
 */


#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;
	int nowarn;
	int interactive;
	int verbose;
	int silent;
	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 *makeeit(char *dosname,
				 char *longname,
				 void *arg0,
				 struct directory *dir)
{
	Stream_t *Target;
	time_t now;
	Arg_t *arg = (Arg_t *) arg0;
	int fat;

	/* will it fit? At least one sector must be free */
	if (! getfree(arg->targetDir)) {
		fprintf(stderr,"Disk full\n");
		return NULL;
	}
	
	/* find out current time */
	time(&now);
	mk_entry(".", 0x10, 1, 0, now, dir);
	Target = open_file(arg->targetDir, dir);	
	if(!Target){
		fprintf(stderr,"Could not open Target\n");
		return NULL;
	}

	dir_grow(Target, 0);
	/* this allocates the first cluster for our directory */

	GET_DATA(arg->targetDir, 0, 0, 0, &fat);
	mk_entry("..         ", 0x10, fat, 0, now, dir);
	dir_write(Target, 1, dir);

	GET_DATA(Target, 0, 0, 0, &fat);
	mk_entry(".          ", 0x10, fat, 0, now, dir);
	dir_write(Target, 0, dir);

	mk_entry(dosname, 0x10, fat, 0, now, dir);
	FREE(&Target);
	return dir;
}


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 mmd(int argc, char **argv, int type)
{
	int ret = 0;
	Stream_t *Dir;
	Arg_t arg;
	int c;
	char filename[VBUFSIZE];
	int i;

	/* get command line options */

	init_clash_handling(& arg.ch);

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

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

	init_mp(&arg.mp);
	arg.mp.arg = (void *) &arg;
	arg.mp.openflags = O_RDWR;
	ret = 0;

	for(i=optind; i < argc; i++){
		if(got_signal)
			break;
		Dir = open_subdir(&arg.mp, argv[i], O_RDWR, 0, 0);
		if(!Dir){
			fprintf(stderr,"Could not open parent directory\n");
			exit(1);
		}
		arg.targetDir = Dir;
		get_name(argv[i], filename, arg.mp.mcwd);
		if(!filename[0]){
			if(!arg.silent)
				fprintf(stderr,"Empty filename\n");
			ret |= MISSED_ONE;
			FREE(&Dir);
			continue;
		}
		if(mwrite_one(Dir, filename,0,
			      makeeit, (void *)&arg, &arg.ch)<=0){
			if(!arg.silent)
				fprintf(stderr,"Could not create %s\n", 
					argv[i]);
			ret |= MISSED_ONE;
		} else
			ret |= GOT_ONE;
		FREE(&Dir);
	}

	if ((ret & GOT_ONE) && ( ret & MISSED_ONE))
		exit(2);
	if (ret & MISSED_ONE)
		exit(1);
	exit(0);
}