beta-0.89.2
[luatex.git] / source / texk / kpathsea / win32 / getdestdir.c
blob281e3540fbdae657d289b16d04d33f4ce36646b2
1 /* getdestdir.c
3 Copyright 2000, 2015 Akira Kakuto.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, see <http://www.gnu.org/licenses/>.
18 from mktexmf:
19 argv[0] = "Dummy", argv[1] = "source", argv[2] = path
20 from mktexpk:
21 argv[0] = "Dummy", argv[1] = "pk", argv[2] = path, argv[3] = mode
22 from mktextfm:
23 argv[0] = "Dummy", argv[1] = "tfm", argv[2] = path
26 #include <kpathsea/kpathsea.h>
27 #include "mktex.h"
29 #define NUMBUF 32
30 #define LENBUF 128
31 #define MPATH 256
32 #define FTOP "fonts"
34 /* error message */
35 static void
36 fatal (const char *s)
38 fprintf (stderr, "%s\n", s);
41 char *
42 getdestdir (int ac, char **av)
44 static char buff[MPATH];
45 char *pb[NUMBUF];
46 char *p, *q;
47 char spec[32];
48 int i, Num = 0, ispk, k;
49 char *topdir;
51 ispk = 0;
53 for (i = 0; i < NUMBUF; i++) {
54 if (!(pb[i] = (char *) malloc (LENBUF))) {
55 fatal ("Memory allocation error.");
56 return (NULL);
60 if (ac != 3 && ac != 4) {
61 fatal ("Argument error.");
62 for (k = 0; k < NUMBUF; k++)
63 free (pb[k]);
64 return (NULL);
67 strcpy (spec, av[1]);
69 normalize (av[2]); /* path */
71 p = av[2];
72 q = buff;
74 /* UNC name support */
76 if (p[0] == '/' && p[1] == '/') {
77 *q++ = *p++;
78 *q++ = *p++;
81 while (*p) {
82 if (*p == '/') {
83 *q = *p;
84 while (*p == '/')
85 p++;
86 p--;
87 } else {
88 *q = *p;
90 p++;
91 q++;
93 *q = '\0'; /* now path name of ${name}.mf is in buff. */
95 #ifdef TEST
96 return xstrdup(buff);
97 #endif
99 if (ac == 4)
100 ispk = 1; /* called from mktexpk */
102 if (!(p = strrchr (buff, '/'))) {
103 fatal ("Invalid path name.");
104 for (k = 0; k < NUMBUF; k++)
105 free (pb[k]);
106 return (NULL);
109 *p = '\0'; /* get directory name */
111 for (i = 0; i < NUMBUF; i++) {
112 if (!(p = strrchr (buff, '/'))) {
113 fatal ("Invalid path name.");
114 for (k = 0; k < NUMBUF; k++)
115 free (pb[k]);
116 return (NULL);
118 *p = '\0';
119 p++;
120 strcpy (pb[i], p);
121 if (!stricmp (pb[i], FTOP)) {
122 Num = i;
123 break;
127 Num -= 2;
128 if (Num < 0) {
129 fprintf (stderr, "Font resources should be under a directory ");
130 fprintf (stderr, "with the name \"fonts\".\n");
131 fprintf (stderr, "Furthermore, there must be at least two directories ");
132 fprintf (stderr, "under the directory \"fonts\".\n");
133 fatal ("Invalid path name.");
134 for (k = 0; k < NUMBUF; k++)
135 free (pb[k]);
136 return (NULL);
139 topdir = kpse_var_value ("MAKETEXPK_TOP_DIR");
140 if (topdir && *topdir && ispk) {
141 normalize (topdir);
142 i = (int)strlen (topdir);
143 while(topdir[i - 1] == '/')
144 i--;
145 topdir[i] = '\0';
147 if(!is_dir(topdir)) {
148 if(make_dir_p(topdir)) {
149 fprintf(stderr, "Failed to access %s.\n", topdir);
150 return NULL;
153 #ifdef TEST
154 printf ("%s\n", topdir);
155 #endif
156 if (strnicmp (&topdir[i - 3], "/pk", 3) != 0) {
157 strcat (topdir, "/pk");
158 if (!is_dir(topdir)) {
159 if (make_dir(topdir)) {
160 fprintf(stderr, "Faild to access %s.\n", topdir);
161 for (k = 0; k < NUMBUF; k++)
162 free (pb[k]);
163 return (NULL);
167 strcpy (buff, topdir);
168 free(topdir);
169 } else {
170 if((topdir = kpse_var_value("TEXMFVAR")) != NULL) {
171 normalize (topdir);
172 i = (int)strlen (topdir);
173 while(topdir[i - 1] == '/')
174 i--;
175 topdir[i] = '\0';
177 if(!is_dir(topdir)) {
178 if(make_dir_p(topdir)) {
179 fprintf(stderr, "Failed to access %s.\n", topdir);
180 return NULL;
184 strcpy(buff, topdir);
185 free(topdir);
188 strcat (buff, "/fonts");
189 if (!is_dir (buff)) {
190 if (make_dir (buff)) {
191 for (k = 0; k < NUMBUF; k++)
192 free (pb[k]);
193 return (NULL);
196 strcat (buff, "/");
197 strcat (buff, spec);
198 if (!is_dir (buff)) {
199 if (make_dir (buff)) {
200 for (k = 0; k < NUMBUF; k++)
201 free (pb[k]);
202 return (NULL);
207 if (ispk) {
208 strcat (buff, "/");
209 strcat (buff, av[3]);
210 if (!is_dir (buff)) {
211 if (make_dir (buff)) {
212 for (k = 0; k < NUMBUF; k++)
213 free (pb[k]);
214 return (NULL);
219 for (i = Num; i > -1; i--) {
220 strcat (buff, "/");
221 strcat (buff, pb[i]);
222 if (is_dir (buff))
223 continue;
224 else {
225 if (make_dir (buff)) {
226 for (k = 0; k < NUMBUF; k++)
227 free (pb[k]);
228 return (NULL);
233 for (k = 0; k < NUMBUF; k++)
234 free (pb[k]);
236 p = xstrdup(buff);
237 return p;
240 #ifdef TEST
242 main (int ac, char *av[])
244 kpse_set_program_name (av[0], "getdestdir");
245 printf ("%s", getdestdir (ac, av));
246 return 0;
248 #endif