2 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * Like yylineno, this is the current open file pos.
27 struct dtc_file
*srcpos_file
;
29 static int dtc_open_one(struct dtc_file
*file
,
36 fullname
= xmalloc(strlen(search
) + strlen(fname
) + 2);
38 strcpy(fullname
, search
);
39 strcat(fullname
, "/");
40 strcat(fullname
, fname
);
42 fullname
= strdup(fname
);
45 file
->file
= fopen(fullname
, "r");
51 file
->name
= fullname
;
56 struct dtc_file
*dtc_open_file(const char *fname
,
57 const struct search_path
*search
)
59 static const struct search_path default_search
= { NULL
, NULL
, NULL
};
61 struct dtc_file
*file
;
64 file
= xmalloc(sizeof(struct dtc_file
));
66 slash
= strrchr(fname
, '/');
68 char *dir
= xmalloc(slash
- fname
+ 1);
70 memcpy(dir
, fname
, slash
- fname
);
71 dir
[slash
- fname
] = 0;
77 if (streq(fname
, "-")) {
83 if (fname
[0] == '/') {
84 file
->file
= fopen(fname
, "r");
88 file
->name
= strdup(fname
);
93 search
= &default_search
;
96 if (dtc_open_one(file
, search
->dir
, fname
))
102 search
= search
->next
;
106 die("Couldn't open \"%s\": %s\n", fname
, strerror(errno
));
109 void dtc_close_file(struct dtc_file
*file
)
111 if (fclose(file
->file
))
112 die("Error closing \"%s\": %s\n", file
->name
, strerror(errno
));