Debian Codegraph @

texk/kpathsea/win32/mktexlsr.c

raw
1 /* mktexlsr.c
2
3 Copyright 2000, 2019 Akira Kakuto.
4
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.
9
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.
14
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/>.
17
18 Web2C 2010 (2010/08/22 --ak)
19 */
20
21 #include <kpathsea/kpathsea.h>
22 #ifndef __MINGW32__
23 #include <kpathsea/dirent.h>
24 #endif
25 #include "mktex.h"
26
27 /* magic header */
28 #define HDL "%% ls-R -- filename database for kpathsea; do not change this line.\n"
29
30 #define VARTEXFONTS "c:/var/tex/fonts/ls-R"
31 #define TBUF 512
32
33 FILE *ls_R;
34
35 static void
36 search(char *name)
37 {
38 DIR *dp;
39 struct dirent *de;
40 char buff[TBUF];
41 int len;
42
43 normalize (name);
44
45 if((dp = opendir(name))) {
46 fprintf(ls_R, "\n%s:\n", name);
47 while((de = readdir(dp))) {
48 if(stricmp(de->d_name, ".") &&
49 stricmp(de->d_name, "..") &&
50 stricmp(de->d_name, ".bzr") &&
51 stricmp(de->d_name, ".git") &&
52 stricmp(de->d_name, ".hg") &&
53 stricmp(de->d_name, ".svn") &&
54 stricmp(de->d_name, "_bzr") &&
55 stricmp(de->d_name, "_git") &&
56 stricmp(de->d_name, "_hg") &&
57 stricmp(de->d_name, "_svn") &&
58 stricmp(de->d_name, "_darcs")) {
59 fprintf(ls_R, "%s\n", de->d_name);
60 }
61 }
62 closedir(dp);
63
64 len = (int)strlen(name);
65 strcpy(buff, name);
66 if(name[len-1] != '/') {
67 strcat(buff, "/");
68 len++;
69 }
70
71 dp = opendir(name);
72 while((de = readdir(dp))) {
73 if(stricmp(de->d_name, ".") &&
74 stricmp(de->d_name, "..") &&
75 stricmp(de->d_name, ".bzr") &&
76 stricmp(de->d_name, ".git") &&
77 stricmp(de->d_name, ".hg") &&
78 stricmp(de->d_name, ".svn") &&
79 stricmp(de->d_name, "_bzr") &&
80 stricmp(de->d_name, "_git") &&
81 stricmp(de->d_name, "_hg") &&
82 stricmp(de->d_name, "_svn") &&
83 stricmp(de->d_name, "_darcs")) {
84 #ifdef __MINGW32__
85 strcpy(buff + len, de->d_name);
86 if(!is_dir(buff))
87 continue;
88 #else
89 if(!de->d_isdir)
90 continue;
91 strcpy(buff + len, de->d_name);
92 #endif
93 search(buff);
94 }
95 }
96 closedir(dp);
97 }
98 }
99
100 int Quiet = 0;
101
102 char first_name[] = "./";
103
104 int main(int ac, char **av)
105 {
106 int cdrive, tdrive;
107 char ls_R_name[TBUF];
108 int i, numtree;
109 size_t len;
110 char *progname;
111 char **pathbuff;
112
113 kpse_set_program_name(av[0], NULL);
114 progname = kpse_program_name;
115
116 if(ac > 1 && (!strncmp(av[1], "-v", 2) || !strncmp(av[1], "--v", 3))) {
117 puts (kpathsea_version_string);
118 puts ("mktexlsr: (C version 1.1 --ak 2002-2015)");
119 exit (0);
120 }
121
122 if(ac > 1 && (!strncmp(av[1], "-h", 2) || !strncmp(av[1], "--h", 3))) {
123 printf("Usage: %s [--quiet|--silent] [DIRS ...]\n\n"
124 "Rebuild all necessary ls-R filename databases completely. If one or\n"
125 "more arguments DIRS are given, these are used as texmf directories to\n"
126 "build ls-R for. Else all directories in the search path for ls-R files\n"
127 "($TEXMFDBS) are used.", av[0]);
128 exit (0);
129 }
130
131 if(ac > 1) {
132 if(!strncmp(av[1], "-q", 2) || !strncmp(av[1], "--q", 3) ||
133 !strncmp(av[1], "-s", 2) || !strncmp(av[1], "--s", 3)) {
134 Quiet = 1;
135 ac--;
136 av++;
137 }
138 }
139
140 if(ac > 1) {
141 numtree = ac - 1;
142 pathbuff = xmalloc(numtree * sizeof(char *));
143 for(i=0; i < numtree; i++) {
144 pathbuff[i] = xstrdup(av[i+1]);
145 normalize (pathbuff[i]);
146 }
147 } else {
148 if (!(pathbuff = mkpaths (&numtree))) {
149 fprintf (stderr, "Maybe you are not using ls-R.\n");
150 exit (100);
151 }
152 }
153
154 for(i = 0; i < numtree; i++) {
155 if (strlen(pathbuff[i]) > (TBUF - 6)) {
156 fprintf (stderr, "Too long a directory name.\n");
157 exit (100);
158 }
159 strcpy(ls_R_name, pathbuff[i]);
160 len = strlen(ls_R_name);
161 if(ls_R_name[len-1] != '/') strcat(ls_R_name, "/");
162 strcat(ls_R_name, "ls-R");
163
164 ls_R = fopen(ls_R_name, "wb");
165 if(!ls_R) {
166 fprintf(stdout, "Cannot open %s to write.\n", ls_R_name);
167 if(!stricmp(ls_R_name, VARTEXFONTS))
168 fprintf(stdout, " (Don't mind this message.)\n");
169 continue;
170 }
171
172 cdrive = _getdrive();
173
174 if(ls_R_name[1] == ':') {
175 tdrive = tolower(ls_R_name[0]) - 'a' + 1;
176 _chdrive(tdrive);
177 }
178
179 _chdir(pathbuff[i]);
180 if(!Quiet)
181 fprintf(stdout, "%s: Updating %s...\n", progname, ls_R_name);
182 fprintf(ls_R, HDL);
183 search(first_name);
184
185 fclose(ls_R);
186 if(!Quiet)
187 fprintf(stdout, "%s: Updated %s.\n", progname, ls_R_name);
188 _chdrive(cdrive);
189 }
190
191 if(!Quiet)
192 fprintf(stdout, "%s: Done.\n", progname);
193
194 for(i = 0; i < numtree; i++) {
195 free(pathbuff[i]);
196 }
197
198 free (pathbuff);
199
200 return 0;
201 }