}
/* Change the default include directory to be the current source file's
- directory, instead of the current working directory. If DOT is non-zero,
- set to "." instead. */
+ directory. */
static void
tic54x_set_default_include (void)
{
- char *dir, *tmp = NULL;
- const char *curfile;
unsigned lineno;
-
- curfile = as_where (&lineno);
- dir = xstrdup (curfile);
- tmp = strrchr (dir, '/');
+ const char *curfile = as_where (&lineno);
+ const char *tmp = strrchr (curfile, '/');
if (tmp != NULL)
{
- int len;
-
- *tmp = '\0';
- len = strlen (dir);
- if (include_dir_count == 0)
- {
- include_dirs = XNEWVEC (const char *, 1);
- include_dir_count = 1;
- }
- include_dirs[0] = dir;
+ size_t len = tmp - curfile;
if (len > include_dir_maxlen)
include_dir_maxlen = len;
+ include_dirs[0] = notes_memdup (curfile, len, len + 1);
}
- else if (include_dirs != NULL)
+ else
include_dirs[0] = ".";
}
{
char *filename;
char *path;
- int len, i;
+ int len;
bfd *abfd, *mbfd;
ILLEGAL_WITHIN_STRUCT ();
demand_empty_rest_of_line ();
tic54x_set_default_include ();
- path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5);
-
- for (i = 0; i < include_dir_count; i++)
- {
- FILE *try;
-
- strcpy (path, include_dirs[i]);
- strcat (path, "/");
- strcat (path, filename);
- if ((try = fopen (path, "r")) != NULL)
- {
- fclose (try);
- break;
- }
- }
-
- if (i >= include_dir_count)
- {
- free (path);
- path = filename;
- }
+ path = notes_alloc (len + include_dir_maxlen + 2);
+ FILE *try = search_and_open (filename, path);
+ if (try)
+ fclose (try);
- /* FIXME: if path is found, malloc'd storage is not freed. Of course, this
- happens all over the place, and since the assembler doesn't usually keep
- running for a very long time, it really doesn't matter. */
register_dependency (path);
/* Expand all archive entries to temporary files and include them. */
#include "dw2gencfi.h"
#include "codeview.h"
#include "wchar.h"
+#include "filenames.h"
#include <limits.h>
const char **include_dirs;
/* How many are in the table. */
-int include_dir_count;
+size_t include_dir_count;
/* Length of longest in table. */
-int include_dir_maxlen = 1;
+size_t include_dir_maxlen;
#ifndef WORKING_DOT_WORD
struct broken_word *broken_words;
}
}
+/* Open FILENAME, first trying the unadorned file name, then if that
+ fails and the file name is not an absolute path, attempt to open
+ the file in current -I include paths. PATH is a preallocated
+ buffer which will be set to the file opened, or FILENAME if no file
+ is found. */
+
+FILE *
+search_and_open (const char *filename, char *path)
+{
+ FILE *f = fopen (filename, FOPEN_RB);
+ if (f == NULL && !IS_ABSOLUTE_PATH (filename))
+ {
+ for (size_t i = 0; i < include_dir_count; i++)
+ {
+ sprintf (path, "%s/%s", include_dirs[i], filename);
+ f = fopen (path, FOPEN_RB);
+ if (f != NULL)
+ return f;
+ }
+ }
+ strcpy (path, filename);
+ return f;
+}
+
/* .incbin -- include a file verbatim at the current location. */
void
demand_empty_rest_of_line ();
- /* Try opening absolute path first, then try include dirs. */
- binfile = fopen (filename, FOPEN_RB);
- if (binfile == NULL)
- {
- int i;
-
- path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5);
-
- for (i = 0; i < include_dir_count; i++)
- {
- sprintf (path, "%s/%s", include_dirs[i], filename);
-
- binfile = fopen (path, FOPEN_RB);
- if (binfile != NULL)
- break;
- }
+ path = XNEWVEC (char, len + include_dir_maxlen + 2);
+ binfile = search_and_open (filename, path);
- if (binfile == NULL)
- as_bad (_("file not found: %s"), filename);
- }
+ if (binfile == NULL)
+ as_bad (_("file not found: %s"), filename);
else
- path = xstrdup (filename);
-
- if (binfile)
{
long file_len;
struct stat filestat;
}
demand_empty_rest_of_line ();
- path = notes_alloc ((size_t) i + include_dir_maxlen + 5);
- for (i = 0; i < include_dir_count; i++)
- {
- strcpy (path, include_dirs[i]);
- strcat (path, "/");
- strcat (path, filename);
- if (0 != (try_file = fopen (path, FOPEN_RT)))
- {
- fclose (try_file);
- goto gotit;
- }
- }
+ path = notes_alloc (i + include_dir_maxlen + 2);
+ try_file = search_and_open (filename, path);
+ if (try_file)
+ fclose (try_file);
- notes_free (path);
- path = filename;
- gotit:
register_dependency (path);
input_scrub_insert_file (path);
}
void
-add_include_dir (char *path)
+init_include_dir (void)
{
- int i;
-
- if (include_dir_count == 0)
- {
- include_dirs = XNEWVEC (const char *, 2);
- include_dirs[0] = "."; /* Current dir. */
- include_dir_count = 2;
- }
- else
- {
- include_dir_count++;
- include_dirs = XRESIZEVEC (const char *, include_dirs,
- include_dir_count);
- }
+ include_dirs = XNEWVEC (const char *, 1);
+ include_dirs[0] = "."; /* Current dir. */
+ include_dir_count = 1;
+ include_dir_maxlen = 1;
+}
+void
+add_include_dir (char *path)
+{
+ include_dir_count++;
+ include_dirs = XRESIZEVEC (const char *, include_dirs, include_dir_count);
include_dirs[include_dir_count - 1] = path; /* New one. */
- i = strlen (path);
+ size_t i = strlen (path);
if (i > include_dir_maxlen)
include_dir_maxlen = i;
}
/* Table of -I directories. */
extern const char **include_dirs;
-extern int include_dir_count;
-extern int include_dir_maxlen;
+extern size_t include_dir_count;
+extern size_t include_dir_maxlen;
/* The offset in the absolute section. */
extern addressT abs_section_offset;
extern void s_mri_sect (char *);
extern char *mri_comment_field (char *);
extern void mri_comment_end (char *, int);
-extern void add_include_dir (char *path);
+extern void init_include_dir (void);
+extern void add_include_dir (char *);
+extern FILE *search_and_open (const char *, char *);
extern void cons (int nbytes);
extern void demand_empty_rest_of_line (void);
extern void emit_expr (expressionS *exp, unsigned int nbytes);