xref: /aosp_15_r20/external/mtools/mkmanifest.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 1986-1992 Emmet P. Gray.
2*d5c9a868SElliott Hughes  *  Copyright 1996-1998,2001,2002,2007,2009 Alain Knaff.
3*d5c9a868SElliott Hughes  *  This file is part of mtools.
4*d5c9a868SElliott Hughes  *
5*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
6*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
7*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
8*d5c9a868SElliott Hughes  *  (at your option) any later version.
9*d5c9a868SElliott Hughes  *
10*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
11*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
14*d5c9a868SElliott Hughes  *
15*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
16*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
17*d5c9a868SElliott Hughes  *
18*d5c9a868SElliott Hughes  * A program to create a manifest (shipping list) that is a shell script
19*d5c9a868SElliott Hughes  * to return a Unix file name to it's original state after it has been
20*d5c9a868SElliott Hughes  * clobbered by MSDOS's file name restrictions.
21*d5c9a868SElliott Hughes  *
22*d5c9a868SElliott Hughes  *	This code also used in arc, mtools, and pcomm
23*d5c9a868SElliott Hughes  */
24*d5c9a868SElliott Hughes 
25*d5c9a868SElliott Hughes #include "sysincludes.h"
26*d5c9a868SElliott Hughes #include "msdos.h"
27*d5c9a868SElliott Hughes #include "mtools.h"
28*d5c9a868SElliott Hughes 
29*d5c9a868SElliott Hughes static char *dos_name2(const char *name);
30*d5c9a868SElliott Hughes 
main(int argc,char ** argv)31*d5c9a868SElliott Hughes int main(int argc, char **argv)
32*d5c9a868SElliott Hughes {
33*d5c9a868SElliott Hughes 	int i;
34*d5c9a868SElliott Hughes 	const char *name;
35*d5c9a868SElliott Hughes 	char *new_name;
36*d5c9a868SElliott Hughes 
37*d5c9a868SElliott Hughes 	/* print the version */
38*d5c9a868SElliott Hughes 	if(argc >= 2 && strcmp(argv[1], "-V") == 0) {
39*d5c9a868SElliott Hughes 		printf("Mtools version %s, dated %s\n", mversion, mdate);
40*d5c9a868SElliott Hughes 		return 0;
41*d5c9a868SElliott Hughes 	}
42*d5c9a868SElliott Hughes 
43*d5c9a868SElliott Hughes 	if (argc == 1) {
44*d5c9a868SElliott Hughes 		fprintf(stderr, "Usage: mkmanifest [-V] <list-of-files>\n");
45*d5c9a868SElliott Hughes 		return 1;
46*d5c9a868SElliott Hughes 	}
47*d5c9a868SElliott Hughes 
48*d5c9a868SElliott Hughes 	for (i=1; i<argc; i++) {
49*d5c9a868SElliott Hughes 		/* zap the leading path */
50*d5c9a868SElliott Hughes 		name = _basename(argv[i]);
51*d5c9a868SElliott Hughes 		/* create new name */
52*d5c9a868SElliott Hughes 		new_name = dos_name2(name);
53*d5c9a868SElliott Hughes 
54*d5c9a868SElliott Hughes 		if (strcasecmp(new_name, name))
55*d5c9a868SElliott Hughes 			printf("mv %s %s\n", new_name, name);
56*d5c9a868SElliott Hughes 	}
57*d5c9a868SElliott Hughes 	return 0;
58*d5c9a868SElliott Hughes }
59*d5c9a868SElliott Hughes 
dos_name2(const char * name)60*d5c9a868SElliott Hughes static char *dos_name2(const char *name)
61*d5c9a868SElliott Hughes {
62*d5c9a868SElliott Hughes 	static const char *dev[9] = {"con", "aux", "com1", "com2", "lpt1",
63*d5c9a868SElliott Hughes 				     "prn", "lpt2", "lpt3", "nul"};
64*d5c9a868SElliott Hughes 	char *s;
65*d5c9a868SElliott Hughes 	char *ext,*temp;
66*d5c9a868SElliott Hughes 	char buf[MAX_PATH];
67*d5c9a868SElliott Hughes 	int i, dot;
68*d5c9a868SElliott Hughes 	static char ans[13];
69*d5c9a868SElliott Hughes 
70*d5c9a868SElliott Hughes 	strncpy(buf, name, MAX_PATH-1);
71*d5c9a868SElliott Hughes 	temp = buf;
72*d5c9a868SElliott Hughes 					/* separate the name from extension */
73*d5c9a868SElliott Hughes 	ext = 0;
74*d5c9a868SElliott Hughes 	dot = 0;
75*d5c9a868SElliott Hughes 	for (i=(int)strlen(buf)-1; i>=0; i--) {
76*d5c9a868SElliott Hughes 		if (buf[i] == '.' && !dot) {
77*d5c9a868SElliott Hughes 			dot = 1;
78*d5c9a868SElliott Hughes 			buf[i] = '\0';
79*d5c9a868SElliott Hughes 			ext = &buf[i+1];
80*d5c9a868SElliott Hughes 		}
81*d5c9a868SElliott Hughes 		if (isupper((unsigned char)buf[i]))
82*d5c9a868SElliott Hughes 			buf[i] = ch_tolower(buf[i]);
83*d5c9a868SElliott Hughes 	}
84*d5c9a868SElliott Hughes 					/* if no name */
85*d5c9a868SElliott Hughes 	if (*temp == '\0')
86*d5c9a868SElliott Hughes 		strcpy(ans, "x");
87*d5c9a868SElliott Hughes 	else {
88*d5c9a868SElliott Hughes 		/* if name is a device */
89*d5c9a868SElliott Hughes 		for (i=0; i<9; i++) {
90*d5c9a868SElliott Hughes 			if (!strcasecmp(temp, dev[i]))
91*d5c9a868SElliott Hughes 				*temp = 'x';
92*d5c9a868SElliott Hughes 		}
93*d5c9a868SElliott Hughes 		/* name too long? */
94*d5c9a868SElliott Hughes 		if (strlen(temp) > 8)
95*d5c9a868SElliott Hughes 			*(temp+8) = '\0';
96*d5c9a868SElliott Hughes 		/* extension too long? */
97*d5c9a868SElliott Hughes 		if (ext && strlen(ext) > 3)
98*d5c9a868SElliott Hughes 			*(ext+3) = '\0';
99*d5c9a868SElliott Hughes 		/* illegal characters? */
100*d5c9a868SElliott Hughes 		while ((s = strpbrk(temp, "^+=/[]:',?*\\<>|\". ")))
101*d5c9a868SElliott Hughes 			*s = 'x';
102*d5c9a868SElliott Hughes 
103*d5c9a868SElliott Hughes 		while (ext && (s = strpbrk(ext, "^+=/[]:',?*\\<>|\". ")))
104*d5c9a868SElliott Hughes 			*s = 'x';
105*d5c9a868SElliott Hughes 		strncpy(ans, temp, 12);
106*d5c9a868SElliott Hughes 		ans[12] = '\0';
107*d5c9a868SElliott Hughes 	}
108*d5c9a868SElliott Hughes 	if (ext && *ext) {
109*d5c9a868SElliott Hughes 		strcat(ans, ".");
110*d5c9a868SElliott Hughes 		strcat(ans, ext);
111*d5c9a868SElliott Hughes 	}
112*d5c9a868SElliott Hughes 	return(ans);
113*d5c9a868SElliott Hughes }
114