#include "config.h"
#include "system.h"
#include "cpplib.h"
static struct include_hash *redundant_include_p
PROTO ((cpp_reader *,
struct include_hash *,
struct file_name_list *));
static struct file_name_map *read_name_map PROTO ((cpp_reader *,
const char *));
static char *read_filename_string PROTO ((int, FILE *));
static char *remap_filename PROTO ((cpp_reader *, char *,
struct file_name_list *));
static long read_and_prescan PROTO ((cpp_reader *, cpp_buffer *,
int, size_t));
static struct file_name_list *actual_directory PROTO ((cpp_reader *, char *));
static void initialize_input_buffer PROTO ((cpp_reader *, int,
struct stat *));
#if 0
static void hack_vms_include_specification PROTO ((char *));
#endif
#ifdef VMS
#define INO_T_EQ(a, b) (!bcmp((char *) &(a), (char *) &(b), sizeof (a)))
#elif (defined _WIN32 && !defined CYGWIN && ! defined (_UWIN)) \
|| defined __MSDOS__
#define INO_T_EQ(a, b) 0
#else
#define INO_T_EQ(a, b) ((a) == (b))
#endif
void
merge_include_chains (opts)
struct cpp_options *opts;
{
struct file_name_list *prev, *cur, *other;
struct file_name_list *quote, *brack, *systm, *after;
struct file_name_list *qtail, *btail, *stail, *atail;
qtail = opts->pending->quote_tail;
btail = opts->pending->brack_tail;
stail = opts->pending->systm_tail;
atail = opts->pending->after_tail;
quote = opts->pending->quote_head;
brack = opts->pending->brack_head;
systm = opts->pending->systm_head;
after = opts->pending->after_head;
if (stail)
stail->next = after;
else
systm = after;
if (btail)
btail->next = systm;
else
brack = systm;
for (cur = quote, prev = NULL; cur; cur = cur->next)
{
for (other = quote; other != cur; other = other->next)
if (INO_T_EQ (cur->ino, other->ino)
&& cur->dev == other->dev)
{
if (opts->verbose)
cpp_notice ("ignoring duplicate directory `%s'\n", cur->name);
prev->next = cur->next;
free (cur->name);
free (cur);
cur = prev;
break;
}
prev = cur;
}
qtail = prev;
for (cur = brack; cur; cur = cur->next)
{
for (other = brack; other != cur; other = other->next)
if (INO_T_EQ (cur->ino, other->ino)
&& cur->dev == other->dev)
{
if (opts->verbose)
cpp_notice ("ignoring duplicate directory `%s'\n", cur->name);
prev->next = cur->next;
free (cur->name);
free (cur);
cur = prev;
break;
}
prev = cur;
}
if (quote)
{
if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
{
if (quote == qtail)
{
if (opts->verbose)
cpp_notice ("ignoring duplicate directory `%s'\n",
quote->name);
free (quote->name);
free (quote);
quote = brack;
}
else
{
cur = quote;
while (cur->next != qtail)
cur = cur->next;
cur->next = brack;
if (opts->verbose)
cpp_notice ("ignoring duplicate directory `%s'\n",
qtail->name);
free (qtail->name);
free (qtail);
}
}
else
qtail->next = brack;
}
else
quote = brack;
opts->quote_include = quote;
opts->bracket_include = brack;
}
struct include_hash *
include_hash (pfile, fname, add)
cpp_reader *pfile;
char *fname;
int add;
{
unsigned int hash = 0;
struct include_hash *l, *m;
char *f = fname;
while (*f)
hash += *f++;
l = pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE];
m = 0;
for (; l; m = l, l = l->next)
if (!strcmp (l->nshort, fname))
return l;
if (!add)
return 0;
l = (struct include_hash *) xmalloc (sizeof (struct include_hash));
l->next = NULL;
l->next_this_file = NULL;
l->foundhere = NULL;
l->buf = NULL;
l->limit = NULL;
if (m)
m->next = l;
else
pfile->all_include_files[hash % ALL_INCLUDE_HASHSIZE] = l;
return l;
}
static struct include_hash *
redundant_include_p (pfile, ihash, ilist)
cpp_reader *pfile;
struct include_hash *ihash;
struct file_name_list *ilist;
{
struct file_name_list *l;
struct include_hash *i;
if (! ihash->foundhere)
return 0;
for (i = ihash; i; i = i->next_this_file)
for (l = ilist; l; l = l->next)
if (i->foundhere == l)
return (i->control_macro
&& (i->control_macro[0] == '\0'
|| cpp_lookup (pfile, i->control_macro, -1, -1)))
? (struct include_hash *)-1 : i;
return 0;
}
static int
file_cleanup (pbuf, pfile)
cpp_buffer *pbuf;
cpp_reader *pfile;
{
if (pbuf->buf)
{
free (pbuf->buf);
pbuf->buf = 0;
}
if (pfile->system_include_depth)
pfile->system_include_depth--;
return 0;
}
int
find_include_file (pfile, fname, search_start, ihash, before)
cpp_reader *pfile;
char *fname;
struct file_name_list *search_start;
struct include_hash **ihash;
int *before;
{
struct file_name_list *l;
struct include_hash *ih, *jh;
int f, len;
char *name;
ih = include_hash (pfile, fname, 1);
jh = redundant_include_p (pfile, ih,
fname[0] == '/' ? ABSOLUTE_PATH : search_start);
if (jh != 0)
{
*before = 1;
*ihash = jh;
if (jh == (struct include_hash *)-1)
return -2;
else
return open (jh->name, O_RDONLY, 0666);
}
if (ih->foundhere)
{
jh = (struct include_hash *)xmalloc (sizeof (struct include_hash));
while (ih->next_this_file) ih = ih->next_this_file;
ih->next_this_file = jh;
jh = ih;
ih = ih->next_this_file;
ih->next = NULL;
ih->next_this_file = NULL;
ih->buf = NULL;
ih->limit = NULL;
}
*before = 0;
*ihash = ih;
ih->nshort = xstrdup (fname);
ih->control_macro = NULL;
if (fname[0] == '/')
{
ih->foundhere = ABSOLUTE_PATH;
ih->name = ih->nshort;
return open (ih->name, O_RDONLY, 0666);
}
len = strlen (fname);
name = xmalloc (len + pfile->max_include_len + 2 + INCLUDE_LEN_FUDGE);
for (l = search_start; l; l = l->next)
{
bcopy (l->name, name, l->nlen);
name[l->nlen] = '/';
strcpy (&name[l->nlen+1], fname);
simplify_pathname (name);
if (CPP_OPTIONS (pfile)->remap)
name = remap_filename (pfile, name, l);
f = open (name, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
#ifdef EACCES
if (f == -1 && errno == EACCES)
{
cpp_error(pfile, "included file `%s' exists but is not readable",
name);
return -1;
}
#endif
if (f >= 0)
{
ih->foundhere = l;
ih->name = xrealloc (name, strlen (name)+1);
return f;
}
}
if (jh)
{
jh->next_this_file = NULL;
free (ih);
}
free (name);
*ihash = (struct include_hash *)-1;
return -1;
}
struct file_name_map
{
struct file_name_map *map_next;
char *map_from;
char *map_to;
};
#define FILE_NAME_MAP_FILE "header.gcc"
static char *
read_filename_string (ch, f)
int ch;
FILE *f;
{
char *alloc, *set;
int len;
len = 20;
set = alloc = xmalloc (len + 1);
if (! is_space[ch])
{
*set++ = ch;
while ((ch = getc (f)) != EOF && ! is_space[ch])
{
if (set - alloc == len)
{
len *= 2;
alloc = xrealloc (alloc, len + 1);
set = alloc + len / 2;
}
*set++ = ch;
}
}
*set = '\0';
ungetc (ch, f);
return alloc;
}
struct file_name_map_list
{
struct file_name_map_list *map_list_next;
char *map_list_name;
struct file_name_map *map_list_map;
};
static struct file_name_map *
read_name_map (pfile, dirname)
cpp_reader *pfile;
const char *dirname;
{
register struct file_name_map_list *map_list_ptr;
char *name;
FILE *f;
for (map_list_ptr = CPP_OPTIONS (pfile)->map_list; map_list_ptr;
map_list_ptr = map_list_ptr->map_list_next)
if (! strcmp (map_list_ptr->map_list_name, dirname))
return map_list_ptr->map_list_map;
map_list_ptr = ((struct file_name_map_list *)
xmalloc (sizeof (struct file_name_map_list)));
map_list_ptr->map_list_name = xstrdup (dirname);
name = (char *) alloca (strlen (dirname) + strlen (FILE_NAME_MAP_FILE) + 2);
strcpy (name, dirname);
if (*dirname)
strcat (name, "/");
strcat (name, FILE_NAME_MAP_FILE);
f = fopen (name, "r");
if (!f)
map_list_ptr->map_list_map = (struct file_name_map *)-1;
else
{
int ch;
int dirlen = strlen (dirname);
while ((ch = getc (f)) != EOF)
{
char *from, *to;
struct file_name_map *ptr;
if (is_space[ch])
continue;
from = read_filename_string (ch, f);
while ((ch = getc (f)) != EOF && is_hor_space[ch])
;
to = read_filename_string (ch, f);
ptr = ((struct file_name_map *)
xmalloc (sizeof (struct file_name_map)));
ptr->map_from = from;
if (*to == '/')
ptr->map_to = to;
else
{
ptr->map_to = xmalloc (dirlen + strlen (to) + 2);
strcpy (ptr->map_to, dirname);
ptr->map_to[dirlen] = '/';
strcpy (ptr->map_to + dirlen + 1, to);
free (to);
}
ptr->map_next = map_list_ptr->map_list_map;
map_list_ptr->map_list_map = ptr;
while ((ch = getc (f)) != '\n')
if (ch == EOF)
break;
}
fclose (f);
}
map_list_ptr->map_list_next = CPP_OPTIONS (pfile)->map_list;
CPP_OPTIONS (pfile)->map_list = map_list_ptr;
return map_list_ptr->map_list_map;
}
static char *
remap_filename (pfile, name, loc)
cpp_reader *pfile;
char *name;
struct file_name_list *loc;
{
struct file_name_map *map;
const char *from, *p, *dir;
if (! loc->name_map)
loc->name_map = read_name_map (pfile,
loc->name
? loc->name : ".");
if (loc->name_map == (struct file_name_map *)-1)
return name;
from = name + strlen (loc->name) + 1;
for (map = loc->name_map; map; map = map->map_next)
if (!strcmp (map->map_from, from))
return map->map_to;
p = rindex (name, '/');
if (!p)
p = name;
if (loc && loc->name
&& strlen (loc->name) == (size_t) (p - name)
&& !strncmp (loc->name, name, p - name))
return name;
if (p == name)
{
dir = ".";
from = name;
}
else
{
char * newdir = (char *) alloca (p - name + 1);
bcopy (name, newdir, p - name);
newdir[p - name] = '\0';
dir = newdir;
from = p + 1;
}
for (map = read_name_map (pfile, dir); map; map = map->map_next)
if (! strcmp (map->map_from, name))
return map->map_to;
return name;
}
int
finclude (pfile, fd, ihash)
cpp_reader *pfile;
int fd;
struct include_hash *ihash;
{
struct stat st;
size_t st_size;
long length;
cpp_buffer *fp;
if (fstat (fd, &st) < 0)
goto perror_fail;
if (fcntl (fd, F_SETFL, 0) == -1)
goto perror_fail;
fp = CPP_BUFFER (pfile);
if (S_ISREG (st.st_mode))
{
st_size = (size_t) st.st_size;
if ((unsigned HOST_WIDEST_INT) st_size
!= (unsigned HOST_WIDEST_INT) st.st_size)
{
cpp_error (pfile, "file `%s' is too large", ihash->name);
goto fail;
}
}
else if (S_ISFIFO (st.st_mode) || S_ISSOCK (st.st_mode)
|| S_ISCHR (st.st_mode)
|| (st.st_mode == 0 && st.st_nlink == 0 && st.st_size == 0))
{
st_size = 4096;
}
else
{
cpp_error (pfile, "`%s' is not a file, pipe, or tty", ihash->name);
goto fail;
}
if (pfile->input_buffer == NULL)
initialize_input_buffer (pfile, fd, &st);
fp->ihash = ihash;
fp->nominal_fname = fp->fname = ihash->name;
length = read_and_prescan (pfile, fp, fd, st_size);
if (length < 0)
goto fail;
if (length == 0)
ihash->control_macro = "";
close (fd);
fp->rlimit = fp->alimit = fp->buf + length;
fp->cur = fp->buf;
if (ihash->foundhere != ABSOLUTE_PATH)
fp->system_header_p = ihash->foundhere->sysp;
fp->lineno = 1;
fp->colno = 1;
fp->line_base = fp->buf;
fp->cleanup = file_cleanup;
if (!CPP_OPTIONS (pfile)->ignore_srcdir)
fp->actual_dir = actual_directory (pfile, fp->fname);
pfile->input_stack_listing_current = 0;
return 1;
perror_fail:
cpp_error_from_errno (pfile, ihash->name);
fail:
cpp_pop_buffer (pfile);
close (fd);
return 0;
}
static struct file_name_list *
actual_directory (pfile, fname)
cpp_reader *pfile;
char *fname;
{
char *last_slash, *dir;
size_t dlen;
struct file_name_list *x;
dir = xstrdup (fname);
last_slash = rindex (dir, '/');
if (last_slash)
{
if (last_slash == dir)
{
dlen = 1;
last_slash[1] = '\0';
}
else
{
dlen = last_slash - dir;
*last_slash = '\0';
}
}
else
{
dir[0] = '.';
dir[1] = '\0';
dlen = 1;
}
if (dlen > pfile->max_include_len)
pfile->max_include_len = dlen;
for (x = pfile->actual_dirs; x; x = x->alloc)
if (!strcmp (x->name, dir))
{
free (dir);
return x;
}
x = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
x->name = dir;
x->nlen = dlen;
x->next = CPP_OPTIONS (pfile)->quote_include;
x->alloc = pfile->actual_dirs;
x->sysp = CPP_BUFFER (pfile)->system_header_p;
x->name_map = NULL;
pfile->actual_dirs = x;
return x;
}
static void
find_position (start, limit, linep, colp)
U_CHAR *start;
U_CHAR *limit;
unsigned long *linep;
unsigned long *colp;
{
unsigned long line = *linep, col = 0;
while (start < limit)
{
U_CHAR ch = *start++;
if (ch == '\n' || ch == '\r')
line++, col = 1;
else
col++;
}
*linep = line, *colp = col;
}
#define SPECCASE_EMPTY 0
#define SPECCASE_NUL 1
#define SPECCASE_CR 2
#define SPECCASE_BACKSLASH 3
#define SPECCASE_QUESTION 4
static long
read_and_prescan (pfile, fp, desc, len)
cpp_reader *pfile;
cpp_buffer *fp;
int desc;
size_t len;
{
U_CHAR *buf = (U_CHAR *) xmalloc (len);
U_CHAR *ip, *op, *line_base;
U_CHAR *ibase;
U_CHAR *speccase = pfile->input_speccase;
unsigned long line;
unsigned int deferred_newlines;
int count;
size_t offset;
offset = 0;
op = buf;
line_base = buf;
line = 1;
ibase = pfile->input_buffer + 2;
deferred_newlines = 0;
for (;;)
{
read_next:
count = read (desc, pfile->input_buffer + 2, pfile->input_buffer_len);
if (count < 0)
goto error;
else if (count == 0)
break;
offset += count;
ip = ibase;
ibase = pfile->input_buffer + 2;
ibase[count] = ibase[count+1] = '\0';
if (offset > len)
{
size_t delta_op;
size_t delta_line_base;
len *= 2;
if (offset > len)
goto too_big;
delta_op = op - buf;
delta_line_base = line_base - buf;
buf = (U_CHAR *) xrealloc (buf, len);
op = buf + delta_op;
line_base = buf + delta_line_base;
}
for (;;)
{
unsigned int span = 0;
if (deferred_newlines)
{
while (speccase[ip[span]] == SPECCASE_EMPTY
&& ip[span] != '\n'
&& ip[span] != '\t'
&& ip[span] != ' ')
span++;
memcpy (op, ip, span);
op += span;
ip += span;
if (*ip == '\n' || *ip == '\t'
|| *ip == ' ' || *ip == ' ')
while (deferred_newlines)
deferred_newlines--, *op++ = '\r';
span = 0;
}
while (speccase[ip[span]] == SPECCASE_EMPTY) span++;
memcpy (op, ip, span);
op += span;
ip += span;
switch (speccase[*ip++])
{
case SPECCASE_NUL:
ibase[-1] = op[-1];
goto read_next;
case SPECCASE_CR:
if (*ip == '\n')
ip++;
else if (*ip == '\0')
{
*--ibase = '\r';
goto read_next;
}
else if (ip[-2] == '\n')
continue;
*op++ = '\n';
break;
case SPECCASE_BACKSLASH:
backslash:
{
if (*ip == '\0')
{
*--ibase = '\\';
goto read_next;
}
else if (*ip == '\n')
{
ip++;
if (*ip == '\r') ip++;
if (*ip == '\n' || *ip == '\t' || *ip == ' ')
*op++ = '\r';
else if (op[-1] == '\t' || op[-1] == ' '
|| op[-1] == '\r' || op[-1] == '\n')
*op++ = '\r';
else
deferred_newlines++;
line++;
line_base = op;
}
else if (*ip == '\r')
{
ip++;
if (*ip == '\n') ip++;
else if (*ip == '\0')
{
*--ibase = '\r';
*--ibase = '\\';
goto read_next;
}
else if (*ip == '\r' || *ip == '\t' || *ip == ' ')
*op++ = '\r';
else
deferred_newlines++;
line++;
line_base = op;
}
else
*op++ = '\\';
}
break;
case SPECCASE_QUESTION:
{
unsigned int d;
d = ip[0];
if (d == '\0')
{
*--ibase = '?';
goto read_next;
}
if (d != '?')
{
*op++ = '?';
break;
}
d = ip[1];
if (d == '\0')
{
*--ibase = '?';
*--ibase = '?';
goto read_next;
}
if (!trigraph_table[d])
{
*op++ = '?';
break;
}
if (CPP_OPTIONS (pfile)->warn_trigraphs)
{
unsigned long col;
find_position (line_base, op, &line, &col);
line_base = op - col;
cpp_warning_with_line (pfile, line, col,
"trigraph ??%c encountered", d);
}
if (CPP_OPTIONS (pfile)->trigraphs)
{
if (trigraph_table[d] == '\\')
goto backslash;
else
*op++ = trigraph_table[d];
}
else
{
*op++ = '?';
*op++ = '?';
*op++ = d;
}
ip += 2;
}
}
}
}
if (offset == 0)
return 0;
if (ibase == pfile->input_buffer)
{
if (*ibase == '?')
{
*op++ = '?';
*op++ = '?';
}
else
*op++ = '\r';
}
else if (ibase == pfile->input_buffer + 1)
{
if (*ibase == '\r')
*op++ = '\n';
else
*op++ = *ibase;
}
if (op[-1] != '\n')
{
unsigned long col;
find_position (line_base, op, &line, &col);
cpp_warning_with_line (pfile, line, col, "no newline at end of file\n");
if (offset + 1 > len)
{
len += 1;
if (offset + 1 > len)
goto too_big;
buf = (U_CHAR *) xrealloc (buf, len);
op = buf + offset;
}
*op++ = '\n';
}
fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
return op - buf;
too_big:
cpp_error (pfile, "file is too large (>%lu bytes)\n", (unsigned long)offset);
free (buf);
return -1;
error:
cpp_error_from_errno (pfile, fp->fname);
free (buf);
return -1;
}
static void
initialize_input_buffer (pfile, fd, st)
cpp_reader *pfile;
int fd;
struct stat *st;
{
long pipe_buf;
U_CHAR *tmp;
tmp = xmalloc (1 << CHAR_BIT);
memset (tmp, SPECCASE_EMPTY, 1 << CHAR_BIT);
tmp['\0'] = SPECCASE_NUL;
tmp['\r'] = SPECCASE_CR;
tmp['\\'] = SPECCASE_BACKSLASH;
if (CPP_OPTIONS (pfile)->trigraphs || CPP_OPTIONS (pfile)->warn_trigraphs)
tmp['?'] = SPECCASE_QUESTION;
pfile->input_speccase = tmp;
pipe_buf = -1;
if (! S_ISREG (st->st_mode))
{
#ifdef _PC_PIPE_BUF
pipe_buf = fpathconf (fd, _PC_PIPE_BUF);
#endif
if (pipe_buf == -1)
{
#ifdef PIPE_BUF
pipe_buf = PIPE_BUF;
#else
pipe_buf = 8192;
#endif
}
}
if (pipe_buf < 8192)
pipe_buf = 8192;
tmp = xmalloc (pipe_buf + 2 + 2);
pfile->input_buffer = tmp;
pfile->input_buffer_len = pipe_buf;
}
void
deps_output (pfile, string, spacer)
cpp_reader *pfile;
char *string;
int spacer;
{
int size;
int cr = 0;
if (!*string)
return;
size = strlen (string);
#ifndef MAX_OUTPUT_COLUMNS
#define MAX_OUTPUT_COLUMNS 72
#endif
if (pfile->deps_column > 0
&& (pfile->deps_column + size) > MAX_OUTPUT_COLUMNS)
{
cr = 5;
pfile->deps_column = 0;
}
if (pfile->deps_size + size + cr + 8 > pfile->deps_allocated_size)
{
pfile->deps_allocated_size = (pfile->deps_size + size + 50) * 2;
pfile->deps_buffer = (char *) xrealloc (pfile->deps_buffer,
pfile->deps_allocated_size);
}
if (cr)
{
bcopy (" \\\n ", &pfile->deps_buffer[pfile->deps_size], 5);
pfile->deps_size += 5;
}
if (spacer == ' ' && pfile->deps_column > 0)
pfile->deps_buffer[pfile->deps_size++] = ' ';
bcopy (string, &pfile->deps_buffer[pfile->deps_size], size);
pfile->deps_size += size;
pfile->deps_column += size;
if (spacer == ':')
pfile->deps_buffer[pfile->deps_size++] = ':';
pfile->deps_buffer[pfile->deps_size] = 0;
}
void
simplify_pathname (path)
char *path;
{
char *from, *to;
char *base;
int absolute = 0;
#if defined (HAVE_DOS_BASED_FILE_SYSTEM)
for (from = path; *from; from++)
if (*from == '\\') *from = '/';
if (ISALPHA (path[0]) && path[1] == ':')
from = to = &path[2];
else
from = to = path;
#else
from = to = path;
#endif
if (*from == '/')
{
absolute = 1;
to++;
from++;
if (*from == '/')
{
if (*++from == '/')
while (*++from == '/');
else
to++;
}
}
base = to;
for (;;)
{
while (*from == '/')
from++;
if (from[0] == '.' && from[1] == '/')
from += 2;
else if (from[0] == '.' && from[1] == '\0')
goto done;
else if (from[0] == '.' && from[1] == '.' && from[2] == '/')
{
if (base == to)
{
if (absolute)
from += 3;
else
{
*to++ = *from++;
*to++ = *from++;
*to++ = *from++;
base = to;
}
}
else
{
to -= 2;
while (to > base && *to != '/') to--;
if (*to == '/')
to++;
from += 3;
}
}
else if (from[0] == '.' && from[1] == '.' && from[2] == '\0')
{
if (base == to)
{
if (!absolute)
{
*to++ = *from++;
*to++ = *from++;
}
}
else
{
to -= 2;
while (to > base && *to != '/') to--;
if (*to == '/')
to++;
}
goto done;
}
else
while ((*to++ = *from++) != '/')
{
if (!to[-1])
{
to--;
goto done;
}
}
}
done:
if (to[0] == '/' && (!absolute || to > path+1))
to--;
if (to == path)
*to++ = '.';
*to = '\0';
return;
}
#if 0
static void
hack_vms_include_specification (fullname)
char *fullname;
{
register char *basename, *unixname, *local_ptr, *first_slash;
int f, check_filename_before_returning, must_revert;
char Local[512];
check_filename_before_returning = 0;
must_revert = 0;
first_slash = index (fullname, '/');
if (first_slash == 0)
return 0;
if (index (fullname, ':') == 0)
{
if (first_slash == fullname)
{
first_slash = index (fullname+1, '/');
if (first_slash)
*first_slash = ':';
for (basename = fullname; *basename != 0; basename++)
*basename = *(basename+1);
}
else if ((first_slash[-1] != '.')
&& (first_slash[-1] != ':')
&& (first_slash[-1] != ']'))
{
*first_slash = ':';
}
else if ((first_slash[1] == '[')
&& (first_slash[-1] == '.'))
fullname += 2;
}
basename = base_name (fullname);
local_ptr = Local;
if ((basename[-1] == ':')
|| (basename[-1] == ']')
|| (basename[-1] == '>'))
unixname = basename;
else
unixname = fullname;
if (*unixname == '/')
unixname++;
if (((basename - fullname) > 1)
&& ( (basename[-1] == ']')
|| (basename[-1] == '>')))
{
if (basename[-2] != '.')
{
basename -= 1;
unixname--;
}
else
{
basename -= 2;
unixname--;
}
}
else
{
if ((unixname != fullname)
&& (basename[-1] != ':'))
*local_ptr++ = ':';
*local_ptr++ = '[';
}
if (unixname == fullname)
{
must_revert = 1;
if ((first_slash != 0)
&& (*unixname != '/')
&& (*unixname != '.'))
*local_ptr++ = '.';
}
if (index (unixname, '/') == 0)
{
if (local_ptr[-1] == '[')
{
strcpy (local_ptr, "000000]");
local_ptr += strlen (local_ptr);
check_filename_before_returning = 1;
}
}
else
{
while (index (unixname, '/') != 0)
{
if ((unixname[0] == '.') && (unixname[1] == '/'))
{
if ( ((unixname == fullname)
&& (fullname+2 != basename))
|| ((basename[-1] == ':')
&& (unixname-1 == basename)))
*local_ptr++ = '.';
unixname += 2;
continue;
}
if ( local_ptr[-1] != '.'
&& local_ptr[-1] != '['
&& local_ptr[-1] != '<')
*local_ptr++ = '.';
if ( (unixname[0] == '.')
&& (unixname[1] == '.')
&& (unixname[2] == '/'))
{
if ((local_ptr[-1] == '.')
&& (local_ptr[-2] == '['))
local_ptr--;
*local_ptr++ = '-';
unixname += 3;
continue;
}
while (*unixname != '/')
*local_ptr++= *unixname++;
unixname++;
}
if (local_ptr[-1] == '.')
local_ptr--;
if (local_ptr[-1] == '[')
local_ptr--;
else
*local_ptr++ = ']';
}
while (*unixname)
*local_ptr++ = *unixname++;
*local_ptr = 0;
strcpy ((must_revert==1)?fullname:basename, Local);
if (check_filename_before_returning)
{
f = open (fullname, O_RDONLY, 0666);
if (f >= 0)
{
close (f);
return 1;
}
basename = index (fullname, '[');
local_ptr = index (fullname, ']') + 1;
strcpy (basename, local_ptr);
}
return 1;
}
#endif