summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/win32/mktexlsr.c
blob: 6273ed55949a684f1b44dfd11aee801a8c1cd470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* mktexlsr.c

   Copyright 2000, 2019 Akira Kakuto.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this library; if not, see <http://www.gnu.org/licenses/>.

   Web2C 2010 (2010/08/22 --ak)
*/

#include <kpathsea/kpathsea.h>
#ifndef __MINGW32__
#include <kpathsea/dirent.h>
#endif
#include "mktex.h"

/* magic header */
#define HDL "%% ls-R -- filename database for kpathsea; do not change this line.\n"

#define VARTEXFONTS "c:/var/tex/fonts/ls-R"
#define TBUF 512

FILE *ls_R;

static void
search(char *name)
{
  DIR *dp;
  struct dirent *de;
  char   buff[TBUF];
  int    len;

  normalize (name);

  if((dp = opendir(name))) {
    fprintf(ls_R, "\n%s:\n", name);
    while((de = readdir(dp))) {
      if(stricmp(de->d_name, ".") &&
         stricmp(de->d_name, "..") &&
         stricmp(de->d_name, ".bzr") &&
         stricmp(de->d_name, ".git") &&
         stricmp(de->d_name, ".hg") &&
         stricmp(de->d_name, ".svn") &&
         stricmp(de->d_name, "_bzr") &&
         stricmp(de->d_name, "_git") &&
         stricmp(de->d_name, "_hg") &&
         stricmp(de->d_name, "_svn") &&
         stricmp(de->d_name, "_darcs")) {
        fprintf(ls_R, "%s\n", de->d_name);
      }
    }
    closedir(dp);

    len = (int)strlen(name);
    strcpy(buff, name);
    if(name[len-1] != '/') {
      strcat(buff, "/");
      len++;
    }

    dp = opendir(name);
    while((de = readdir(dp))) {
      if(stricmp(de->d_name, ".") &&
         stricmp(de->d_name, "..") &&
         stricmp(de->d_name, ".bzr") &&
         stricmp(de->d_name, ".git") &&
         stricmp(de->d_name, ".hg") &&
         stricmp(de->d_name, ".svn") &&
         stricmp(de->d_name, "_bzr") &&
         stricmp(de->d_name, "_git") &&
         stricmp(de->d_name, "_hg") &&
         stricmp(de->d_name, "_svn") &&
         stricmp(de->d_name, "_darcs")) {
#ifdef __MINGW32__
        strcpy(buff + len, de->d_name);
        if(!is_dir(buff))
          continue;
#else
        if(!de->d_isdir)
          continue;
        strcpy(buff + len, de->d_name);
#endif          
        search(buff);
      }
    }
    closedir(dp);
  }
}

int Quiet = 0;

char first_name[] = "./";

int main(int ac, char **av)
{
  int cdrive, tdrive;
  char ls_R_name[TBUF];
  int i, numtree;
  size_t len;
  char *progname;
  char **pathbuff;

  kpse_set_program_name(av[0], NULL);
  progname = kpse_program_name;

  if(ac > 1 && (!strncmp(av[1], "-v", 2) || !strncmp(av[1], "--v", 3))) {
    puts (kpathsea_version_string);
    puts ("mktexlsr: (C version 1.1 --ak 2002-2015)");
    exit (0);
  }

  if(ac > 1 && (!strncmp(av[1], "-h", 2) || !strncmp(av[1], "--h", 3))) {
    printf("Usage: %s [--quiet|--silent] [DIRS ...]\n\n"
"Rebuild all necessary ls-R filename databases completely. If one or\n"
"more arguments DIRS are given, these are used as texmf directories to\n"
"build ls-R for. Else all directories in the search path for ls-R files\n"
"($TEXMFDBS) are used.", av[0]);
    exit (0);
  }

  if(ac > 1) {
    if(!strncmp(av[1], "-q", 2) || !strncmp(av[1], "--q", 3) ||
       !strncmp(av[1], "-s", 2) || !strncmp(av[1], "--s", 3)) {
      Quiet = 1;
      ac--;
      av++;
    }
  }

  if(ac > 1) {
    numtree = ac - 1;
    pathbuff = xmalloc(numtree * sizeof(char *));
    for(i=0; i < numtree; i++) {
      pathbuff[i] = xstrdup(av[i+1]);
      normalize (pathbuff[i]);
    }
  } else {
    if (!(pathbuff = mkpaths (&numtree))) {
      fprintf (stderr, "Maybe you are not using ls-R.\n");
      exit (100);
    }
  }

  for(i = 0; i < numtree; i++) {
    if (strlen(pathbuff[i]) > (TBUF - 6)) {
      fprintf (stderr, "Too long a directory name.\n");
      exit (100);
    }
    strcpy(ls_R_name, pathbuff[i]);
    len = strlen(ls_R_name);
    if(ls_R_name[len-1] != '/') strcat(ls_R_name, "/");
    strcat(ls_R_name, "ls-R");

    ls_R = fopen(ls_R_name, "wb");
    if(!ls_R) {
      fprintf(stdout, "Cannot open %s to write.\n", ls_R_name);
      if(!stricmp(ls_R_name, VARTEXFONTS))
        fprintf(stdout, "       (Don't mind this message.)\n");
      continue;
    }

    cdrive = _getdrive();

    if(ls_R_name[1] == ':') {
      tdrive = tolower(ls_R_name[0]) - 'a' + 1;
      _chdrive(tdrive);
    }

    _chdir(pathbuff[i]);
    if(!Quiet)
      fprintf(stdout, "%s: Updating %s...\n", progname, ls_R_name);
    fprintf(ls_R, HDL);
    search(first_name);

    fclose(ls_R);
    if(!Quiet)
      fprintf(stdout, "%s: Updated %s.\n", progname, ls_R_name);
    _chdrive(cdrive);
  }

  if(!Quiet)
    fprintf(stdout, "%s: Done.\n", progname);
  
  for(i = 0; i < numtree; i++) {
    free(pathbuff[i]);
  }

  free (pathbuff);

  return 0;
}