Exemple #1
0
/**
 * This function determines the action to be taken in case there is a problem
 * with target name (clash, illegal characters, or reserved)
 * The decision either comes from the default (ch), or the user will be
 * prompted if there is no default
 */
static __inline__ clash_action ask_namematch(doscp_t *cp,
					     dos_name_t *dosname,
					     char *longname,
					     int isprimary, 
					     ClashHandling_t *ch,
					     int no_overwrite,
					     int reason)
{
	/* User's answer letter (from keyboard). Only first letter is used,
	 * but we allocate space for 10 in order to account for extra garbage
	 * that user may enter
	 */
	char ans[10];

	/**
	 * Return value: action to be taken
	 */
	clash_action a;

	/**
	 * Should this decision be made permanent (do no longer ask same
	 * question)
	 */
	int perm;

	/**
	 * Buffer for shortname
	 */
	char name_buffer[4*13];

	/**
	 * Name to be printed
	 */
	char *name;

#define EXISTS 0
#define RESERVED 1
#define ILLEGALS 2

	static const char *reasons[]= {
		"already exists",
		"is reserved",
		"contains illegal character(s)"};

	a = ch->action[isprimary];

	if(a == NAMEMATCH_NONE && !opentty(1)) {
		/* no default, and no tty either . Skip the troublesome file */
		return NAMEMATCH_SKIP;
	}

	if (!isprimary)
		name = unix_normalize(cp, name_buffer, dosname);
	else
		name = longname;

	perm = 0;
	while (a == NAMEMATCH_NONE) {
		fprintf(stderr, "%s file name \"%s\" %s.\n",
			isprimary ? "Long" : "Short", name, reasons[reason]);
		fprintf(stderr,
			"a)utorename A)utorename-all r)ename R)ename-all ");
		if(!no_overwrite)
			fprintf(stderr,"o)verwrite O)verwrite-all");
		fprintf(stderr,
			"\ns)kip S)kip-all q)uit (aArR");
		if(!no_overwrite)
			fprintf(stderr,"oO");
		fprintf(stderr,"sSq): ");
		fflush(stderr);
		fflush(opentty(1));
		if (mtools_raw_tty) {
			int rep;
			rep = fgetc(opentty(1));			
			fputs("\n", stderr);
			if(rep == EOF)
				ans[0] = 'q';
			else
				ans[0] = rep;
		} else {
			fgets(ans, 9, opentty(0));
		}
		perm = isupper((unsigned char)ans[0]);
		switch(tolower((unsigned char)ans[0])) {
			case 'a':
				a = NAMEMATCH_AUTORENAME;
				break;
			case 'r':
				if(isprimary)
					a = NAMEMATCH_PRENAME;
				else
					a = NAMEMATCH_RENAME;
				break;
			case 'o':
				if(no_overwrite)
					continue;
				a = NAMEMATCH_OVERWRITE;
				break;
			case 's':
				a = NAMEMATCH_SKIP;
				break;
			case 'q':
				perm = 0;
				a = NAMEMATCH_QUIT;
				break;
			default:
				perm = 0;
		}
	}

	/* Keep track of this action in case this file collides again */
	ch->action[isprimary]  = a;
	if (perm)
		ch->namematch_default[isprimary] = a;

	/* if we were asked to overwrite be careful. We can't set the action
	 * to overwrite, else we get won't get a chance to specify another
	 * action, should overwrite fail. Indeed, we'll be caught in an
	 * infinite loop because overwrite will fail the same way for the
	 * second time */
	if(a == NAMEMATCH_OVERWRITE)
		ch->action[isprimary] = NAMEMATCH_NONE;
	return a;
}
static inline clash_action ask_namematch(char *name, int isprimary, 
					 ClashHandling_t *ch, int no_overwrite,
					 int reason)
{
	char ans[10];
	clash_action a;
	int perm;
	char unix_shortname[13];


#define EXISTS 0
#define RESERVED 1
#define ILLEGALS 2

	static const char *reasons[]= {
		"already exists",
		"is reserved",
		"contains illegal character(s)"};


	if (!isprimary)
		name = unix_normalize(unix_shortname, name, name+8);

	a = ch->action[isprimary];

	if(a == NAMEMATCH_NONE && !opentty(1)) {
		/* no default, and no tty either . Skip the troublesome file */
		return NAMEMATCH_SKIP;
	}

	perm = 0;
	while (a == NAMEMATCH_NONE) {
		fprintf(stderr, "%s file name \"%s\" %s.\n",
			isprimary ? "Long" : "Short", name, reasons[reason]);
		fprintf(stderr,
			"a)utorename A)utorename-all r)ename R)ename-all ");
		if(!no_overwrite)
			fprintf(stderr,"o)verwrite O)verwrite-all");
		fprintf(stderr,
			"\ns)kip S)kip-all q)uit (aArR");
		if(!no_overwrite)
			fprintf(stderr,"oO");
		fprintf(stderr,"sSq): ");
		fflush(stderr);
		fflush(opentty(1));
		if (mtools_raw_tty) {
			int rep;
			rep = fgetc(opentty(1));			
			fputs("\n", stderr);
			if(rep == EOF)
				ans[0] = 'q';
			else
				ans[0] = rep;
		} else {
			fgets(ans, 9, opentty(0));
		}
		perm = isupper((unsigned char)ans[0]);
		switch(tolower((unsigned char)ans[0])) {
			case 'a':
				a = NAMEMATCH_AUTORENAME;
				break;
			case 'r':
				if(isprimary)
					a = NAMEMATCH_PRENAME;
				else
					a = NAMEMATCH_RENAME;
				break;
			case 'o':
				if(no_overwrite)
					continue;
				a = NAMEMATCH_OVERWRITE;
				break;
			case 's':
				a = NAMEMATCH_SKIP;
				break;
			case 'q':
				perm = 0;
				a = NAMEMATCH_QUIT;
				break;
			default:
				perm = 0;
		}
	}

	/* Keep track of this action in case this file collides again */
	ch->action[isprimary]  = a;
	if (perm)
		ch->namematch_default[isprimary] = a;

	/* if we were asked to overwrite be careful. We can't set the action
	 * to overwrite, else we get won't get a chance to specify another
	 * action, should overwrite fail. Indeed, we'll be caught in an
	 * infinite loop because overwrite will fail the same way for the
	 * second time */
	if(a == NAMEMATCH_OVERWRITE)
		ch->action[isprimary] = NAMEMATCH_NONE;
	return a;
}