File: mainloop.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 (174 lines) | stat: -rw-r--r-- 3,274 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
/*
 * mainloop.c
 * Iterating over all the command line parameters, and matching patterns
 * where needed
 */

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

static int unix_loop(MainParam_t *mp, char *arg)
{
	char *tmp;
	int ret;
	int isdir;

	mp->File = NULL;
	if((mp->lookupflags & DO_OPEN)){
		mp->File = SimpleFileOpen(0, 0, arg, O_RDONLY, 0, 0);

		if(!mp->File){

			perror(arg);
#if 0
			tmp = strrchr(arg,'/');
			if(tmp)
				tmp++;
			else
				tmp = arg;
			strncpy(mp->filename, tmp, VBUFSIZE);
			mp->filename[VBUFSIZE-1] = '\0';
#endif
			return MISSED_ONE;
		}
		GET_DATA(mp->File, 0, 0, &isdir, 0);
		if(isdir)
			return IS_MATCH;
	}

	if(mp->outname){
		tmp = strrchr(arg,'/');
		if(tmp)
			tmp++;
		else
			tmp = arg;
		strncpy(mp->outname, tmp, VBUFSIZE-1);
		mp->outname[VBUFSIZE-1]='\0';
	}
	ret = mp->unixcallback(arg,mp) | IS_MATCH;
	FREE(&mp->File);
	return ret;
}

static int checkForDot(MainParam_t *mp, const char *name)
{
	if((mp->lookupflags & NO_DOTS) &&
	   (!name[0] ||
	    !strcmp(name,".") || 
	    !strcmp(name,"..")))
		return 1;
	return 0;
}
		
		
static int dos_loop(MainParam_t *mp, char *arg)
{
	Stream_t *Dir;
	int ret;
	int have_one;
	int entry;
	const char *filename;

	have_one = MISSED_ONE;
	ret = 0;

	Dir =  open_subdir(mp, arg, mp->openflags, 0, 0);
	if(!Dir)
		return MISSED_ONE;
	entry = 0;

	filename = mp->filename;
	if(!filename[0])
		filename="*";
	if(checkForDot(mp, filename)){
		fprintf(stderr,
			"This command cannot be applied to \".\" or \"..\"\n");
		FREE(&Dir);
		return MISSED_ONE;
	}

	while(entry >= 0){
		if(got_signal)
			break;
		mp->File = NULL;
		if(vfat_lookup(Dir,
			       & (mp->dir), &entry, 0,
			       filename,
			       mp->lookupflags,
			       mp->outname,
			       mp->shortname,
			       mp->longname) == 0 ){
			if(checkForDot(mp,mp->outname))
				ret |= MISSED_ONE;
			else {
				if(mp->lookupflags & DO_OPEN)
					mp->File = open_file(Dir, &mp->dir);
				if(got_signal)
					break;
				have_one = IS_MATCH;
				ret |= mp->callback(Dir, mp, entry - 1);
				FREE(&mp->File);
			}
		}		
		if (fat_error(Dir))
			ret |= MISSED_ONE;
		if(ret & NEXT_DISK)
			break;
		if(mp->fast_quit && (ret & MISSED_ONE))
			break;
		
	}
	FREE(&Dir);
	return ret | have_one;
}

int main_loop(MainParam_t *mp, char **argv, int argc)
{
	int i;
	int ret, Bret;
	
	Bret = 0;

	for (i = 0; i < argc; i++) {
		if ( got_signal )
			break;
		if (mp->unixcallback && (!argv[i][0] || argv[i][1] != ':' ))
			ret = unix_loop(mp, argv[i]);
		else if (mp->newdoscallback)
			ret = mp->newdoscallback(argv[i], mp) ;
		else
			ret = dos_loop(mp, argv[i]);
		
		if (! (ret & IS_MATCH) ) {
			fprintf(stderr, "%s: File \"%s\" not found\n",
				progname, argv[i]);
			ret |= MISSED_ONE;
		}
		Bret |= ret;
		if(mp->fast_quit && (Bret & MISSED_ONE))
			break;
	}
	if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
		return 2;
	if (Bret & MISSED_ONE)
		return 1;
	return 0;
}


void init_mp(MainParam_t *mp)
{
	fix_mcwd(mp->mcwd);

	mp->unixcallback = NULL;
	mp->newdoscallback = NULL;
	mp->pathname = mp->outname = mp->shortname = mp->longname = 0;
	mp->newdrive_cb = mp->olddrive_cb = 0;
	mp->fast_quit = 0;
}