summaryrefslogtreecommitdiffstats
path: root/lib/filenames.c
blob: fc154a749cc96419482e8feee59604201ccd7944 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/* filenames.c -- file & directory name manipulations
   Copyright (C) 1986, 1995, 1996 Free Software Foundation, Inc.

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

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <config.h>
#include "system.h"
#include "strxtra.h"
#include "filenames.h"
#include "misc.h"
#include "error.h"

extern char *xgetcwd __P((void));

/* root_name returns the base name of the file with any leading
 * directory information or trailing suffix stripped off. Examples:
 *
 *   /usr/include/stdio.h   ->   stdio
 *   fred                   ->   fred
 *   barney.c               ->   barney
 *   bill/bob               ->   bob
 *   /                      ->   < null string >
 */
char const *
root_name (char const *path)
{
  static char file_name_buffer[BUFSIZ];
  char const *root;
  char const *dot;

  root = strrchr (path, '/');
  if (root == NULL)
    root = path;
  else
    root++;

  dot = strrchr (root, '.');
  if (dot == NULL)
    strcpy (file_name_buffer, root);
  else
    {
      strncpy (file_name_buffer, root, dot - root);
      file_name_buffer[dot - root] = '\0';
    }
  return file_name_buffer;
}

/* suff_name returns the suffix (including the dot) or a null string
 * if there is no suffix. Examples:
 *
 *   /usr/include/stdio.h   ->   .h
 *   fred                   ->   < null string >
 *   barney.c               ->   .c
 *   bill/bob               ->   < null string >
 *   /                      ->   < null string >
 */
char const *
suff_name (char const *path)
{
  char const *dot;

  dot = strrchr (path, '.');
  if (dot == NULL)
    return "";
  return dot;
}

int
can_crunch (char const *path1, char const *path2)
{
  char const *slash1;
  char const *slash2;

  slash1 = strrchr (path1, '/');
  slash2 = strrchr (path2, '/');

  if (slash1 == NULL && slash2 == NULL)
    return strequ (suff_name (path1), suff_name (path2));
  if ((slash1 - path1) != (slash2 - path2))
    return 0;
  if (!strnequ (path1, path2, slash1 - path1))
    return 0;
  return strequ (suff_name (slash1), suff_name (slash2));
}

/* look_up adds ../s to the beginning of a file name until it finds
 * the one that really exists. Returns NULL if it gets all the way
 * to / and never finds it.
 *
 * If the file name starts with /, just return it as is.
 *
 * This routine is used to locate the ID database file.
 */
char const *
look_up (char const *arg)
{
  static char file_name_buffer[BUFSIZ];
  char *buf = file_name_buffer;
  char *id_path = 0;
  struct stat rootb;
  struct stat statb;

  if (arg == 0)
    {
      id_path = getenv ("IDPATH");
      if (id_path)
	{
	  id_path = strdup (id_path);
	  arg = strtok (id_path, ":");
	  /* FIXME: handle multiple ID file names */
	}
    }
  if (arg == 0)
    arg = ID_FILE_NAME;

  /* if we got absolute name, just use it. */
  if (arg[0] == '/')
    return arg;
  /* if the name we were give exists, don't bother searching */
  if (stat (arg, &statb) == 0)
    return arg;
  /* search up the tree until we find a directory where this
	 * relative file name is visible.
	 * (or we run out of tree to search by hitting root).
	 */

  if (stat ("/", &rootb) != 0)
    return NULL;
  do
    {
      strcpy (buf, "../");
      buf += 3;
      strcpy (buf, arg);
      if (stat (file_name_buffer, &statb) == 0)
	return file_name_buffer;
      *buf = '\0';
      if (stat (file_name_buffer, &statb) != 0)
	return NULL;
    }
  while (!((statb.st_ino == rootb.st_ino) ||
	   (statb.st_dev == rootb.st_dev)));
  return NULL;
}

/* define special name components */

static char slash[] = "/";
static char dot[] = ".";
static char dotdot[] = "..";

/* nextc points to the next character to look at in the string or is
 * null if the end of string was reached.
 *
 * namep points to buffer that holds the components.
 */
static char const *nextc = NULL;
static char *namep;

/* lexname - Return next name component. Uses global variables initialized
 * by canonize_file_name to figure out what it is scanning.
 */
static char const *
lexname (void)
{
  char c;
  char const *d;

  if (nextc)
    {
      c = *nextc++;
      if (c == '\0')
	{
	  nextc = NULL;
	  return NULL;
	}
      if (c == '/')
	{
	  return &slash[0];
	}
      if (c == '.')
	{
	  if ((*nextc == '/') || (*nextc == '\0'))
	    return &dot[0];
	  if (*nextc == '.' && (*(nextc + 1) == '/' || *(nextc + 1) == '\0'))
	    {
	      ++nextc;
	      return &dotdot[0];
	    }
	}
      d = namep;
      *namep++ = c;
      while ((c = *nextc) != '/')
	{
	  *namep++ = c;
	  if (c == '\0')
	    {
	      nextc = NULL;
	      return d;
	    }
	  ++nextc;
	}
      *namep++ = '\0';
      return d;
    }
  else
    {
      return NULL;
    }
}

/* canonize_file_name - Put a file name in cannonical form. Looks for all the
 * whacky wonderful things a demented *ni* programmer might put
 * in a file name and reduces the name to cannonical form.
 */
void
canonize_file_name (char *n)
{
  char const *components[1024];
  char const **cap = &components[0];
  char const **cad;
  char const *cp;
  char namebuf[2048];
  char const *s;

  /* initialize scanner */
  nextc = n;
  namep = &namebuf[0];

  /* break the file name into individual components */
  while ((cp = lexname ()))
    {
      *cap++ = cp;
    }

  /* If name is empty, leave it that way */
  if (cap == &components[0])
    return;

  /* flag end of component list */
  *cap = NULL;

  /* remove all trailing slashes and dots */
  while ((--cap != &components[0]) &&
	 ((*cap == &slash[0]) || (*cap == &dot[0])))
    *cap = NULL;

  /* squeeze out all . / component sequences */
  cap = &components[0];
  cad = cap;
  while (*cap)
    {
      if ((*cap == &dot[0]) && (*(cap + 1) == &slash[0]))
	{
	  cap += 2;
	}
      else
	{
	  *cad++ = *cap++;
	}
    }
  *cad++ = NULL;

  /* find multiple // and use last slash as root, except on apollo which
    * apparently actually uses // in real file names (don't ask me why).
    */
#ifndef apollo
  s = NULL;
  cap = &components[0];
  cad = cap;
  while (*cap)
    {
      if ((s == &slash[0]) && (*cap == &slash[0]))
	{
	  cad = &components[0];
	}
      s = *cap++;
      *cad++ = s;
    }
  *cad = NULL;
#endif

  /* if this is absolute name get rid of any /.. at beginning */
  if ((components[0] == &slash[0]) && (components[1] == &dotdot[0]))
    {
      cap = &components[1];
      cad = cap;
      while (*cap == &dotdot[0])
	{
	  ++cap;
	  if (*cap == NULL)
	    break;
	  if (*cap == &slash[0])
	    ++cap;
	}
      while (*cap)
	*cad++ = *cap++;
      *cad = NULL;
    }

  /* squeeze out any name/.. sequences (but leave leading ../..) */
  cap = &components[0];
  cad = cap;
  while (*cap)
    {
      if ((*cap == &dotdot[0]) &&
	  ((cad - 2) >= &components[0]) &&
	  ((*(cad - 2)) != &dotdot[0]))
	{
	  cad -= 2;
	  ++cap;
	  if (*cap)
	    ++cap;
	}
      else
	{
	  *cad++ = *cap++;
	}
    }
  /* squeezing out a trailing /.. can leave unsightly trailing /s */
  if ((cad >= &components[2]) && ((*(cad - 1)) == &slash[0]))
    --cad;
  *cad = NULL;
  /* if it was just name/.. it now becomes . */
  if (components[0] == NULL)
    {
      components[0] = &dot[0];
      components[1] = NULL;
    }

  /* re-assemble components */
  cap = &components[0];
  while ((s = *cap++))
    {
      while (*s)
	*n++ = *s++;
    }
  *n++ = '\0';
}

FILE *
open_source_FILE (char *file_name)
{
  FILE *source_FILE;

  source_FILE = fopen (file_name, "r");
  if (source_FILE == NULL)
    error (0, errno, _("can't open `%s'"), file_name);
  return source_FILE;
}

void
close_source_FILE (FILE *fp)
{
  fclose (fp);
}