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