/* Return the expansion of FILENAME. */ char * expand_filename (char *filename, char *input_name) { int i; if (filename) { filename = full_pathname (filename); if (IS_ABSOLUTE (filename) || (*filename == '.' && (IS_SLASH (filename[1]) || (filename[1] == '.' && IS_SLASH (filename[2]))))) return filename; } else { filename = filename_non_directory (input_name); if (!*filename) { free (filename); filename = xstrdup ("noname.texi"); } for (i = strlen (filename) - 1; i; i--) if (filename[i] == '.') break; if (!i) i = strlen (filename); if (i + 6 > (strlen (filename))) filename = xrealloc (filename, i + 6); strcpy (filename + i, html ? ".html" : ".info"); return filename; } if (IS_ABSOLUTE (input_name)) { /* Make it so that relative names work. */ char *result; i = strlen (input_name) - 1; result = xmalloc (1 + strlen (input_name) + strlen (filename)); strcpy (result, input_name); while (!IS_SLASH (result[i]) && i) i--; if (IS_SLASH (result[i])) i++; strcpy (&result[i], filename); free (filename); return result; } return filename; }
FILE *search_path::open_file(const char *name, char **pathp) { assert(name != 0); if (IS_ABSOLUTE(name) || *dirs == '\0') { FILE *fp = fopen(name, "r"); if (fp) { if (pathp) *pathp = strsave(name); return fp; } else return 0; } unsigned namelen = strlen(name); char *p = dirs; for (;;) { char *end = strchr(p, PATH_SEP_CHAR); if (!end) end = strchr(p, '\0'); int need_slash = end > p && strchr(DIR_SEPS, end[-1]) == 0; char *origpath = new char[(end - p) + need_slash + namelen + 1]; memcpy(origpath, p, end - p); if (need_slash) origpath[end - p] = '/'; strcpy(origpath + (end - p) + need_slash, name); #if 0 fprintf(stderr, "origpath `%s'\n", origpath); #endif char *path = relocate(origpath); a_delete origpath; #if 0 fprintf(stderr, "trying `%s'\n", path); #endif FILE *fp = fopen(path, "r"); if (fp) { if (pathp) *pathp = path; else a_delete path; return fp; } a_delete path; if (*end == '\0') break; p = end + 1; } return 0; }
/* Return the full pathname for FILENAME by searching along PATH. When found, return the stat () info for FILENAME in FINFO. If PATH is NULL, only the current directory is searched. If the file could not be found, return a NULL pointer. */ char * get_file_info_in_path (char *filename, char *path, struct stat *finfo) { char *dir; int result, index = 0; if (path == NULL) path = "."; /* Handle absolute pathnames. */ if (IS_ABSOLUTE (filename) || (*filename == '.' && (IS_SLASH (filename[1]) || (filename[1] == '.' && IS_SLASH (filename[2]))))) { if (stat (filename, finfo) == 0) return xstrdup (filename); else return NULL; } while ((dir = extract_colon_unit (path, &index))) { char *fullpath; if (!*dir) { free (dir); dir = xstrdup ("."); } fullpath = xmalloc (2 + strlen (dir) + strlen (filename)); sprintf (fullpath, "%s/%s", dir, filename); free (dir); result = stat (fullpath, finfo); if (result == 0) return fullpath; else free (fullpath); } return NULL; }
const char *index_search_item::munge_filename(const char *filename) { if (IS_ABSOLUTE(filename)) return filename; const char *cwd = pool; int need_slash = (cwd[0] != 0 && strchr(DIR_SEPS, strchr(cwd, '\0')[-1]) == 0); int len = strlen(cwd) + strlen(filename) + need_slash + 1; if (len > filename_buflen) { a_delete filename_buffer; filename_buflen = len; filename_buffer = new char[len]; } strcpy(filename_buffer, cwd); if (need_slash) strcat(filename_buffer, "/"); strcat(filename_buffer, filename); return filename_buffer; }
/* * Emit a set of symbols. * type - 0: have symbol tell whether it is local, extern or global * 1: assume all symbols in set to be global * 2: assume all symbols in set to be extern */ static void emitSymbolSet(set *s, int type) { symbol *sym; initList *list; unsigned sectionNr = 0; for (sym = setFirstItem(s); sym; sym = setNextItem(s)) { #if 0 fprintf (stdout, "; name %s, rname %s, level %d, block %d, key %d, local %d, ival %p, static %d, cdef %d, used %d\n", sym->name, sym->rname, sym->level, sym->block, sym->key, sym->islocal, sym->ival, IS_STATIC(sym->etype), sym->cdef, sym->used); #endif if (sym->etype && SPEC_ABSA(sym->etype) && IS_CONFIG_ADDRESS(SPEC_ADDR(sym->etype)) && sym->ival) { // handle config words pic14_assignConfigWordValue(SPEC_ADDR(sym->etype), (int)list2int(sym->ival)); pic14_stringInSet(sym->rname, &emitted, 1); continue; } if (sym->isstrlit) { // special case: string literals emitInitVal(ivalBuf, sym, sym->type, NULL); continue; } if (type != 0 || sym->cdef || (!IS_STATIC(sym->etype) && IS_GLOBAL(sym))) { // bail out for ___fsadd and friends if (sym->cdef && !sym->used) continue; /* export or import non-static globals */ if (!pic14_stringInSet(sym->rname, &emitted, 0)) { if (type == 2 || IS_EXTERN(sym->etype) || sym->cdef) { /* do not add to emitted set, it might occur again! */ //if (!sym->used) continue; // declare symbol emitIfNew (extBuf, &emitted, "\textern\t%s\n", sym->rname); } else { // declare symbol emitIfNew (gloBuf, &emitted, "\tglobal\t%s\n", sym->rname); if (!sym->ival && !IS_FUNC(sym->type)) { // also define symbol if (IS_ABSOLUTE(sym->etype)) { // absolute location? //dbuf_printf (gloDefBuf, "UD_%s_%u\tudata\t0x%04X\n", moduleName, sectionNr++, SPEC_ADDR(sym->etype)); // deferred to pic14_constructAbsMap } else { dbuf_printf (gloDefBuf, "UD_%s_%u\tudata\n", moduleName, sectionNr++); dbuf_printf (gloDefBuf, "%s\tres\t%d\n\n", sym->rname, getSize(sym->type)); } } // if } // if pic14_stringInSet(sym->rname, &emitted, 1); } // if } // if list = sym->ival; //if (list) showInitList(list, 0); if (list) { resolveIvalSym( list, sym->type ); emitInitVal(ivalBuf, sym, sym->type, sym->ival); dbuf_printf (ivalBuf, "\n"); } } // for sym }
char * info_find_fullpath (char *partial) { int initial_character; char *temp; filesys_error_number = 0; maybe_initialize_infopath (); if (partial && (initial_character = *partial)) { char *expansion; expansion = lookup_info_filename (partial); if (expansion) return expansion; /* If we have the full path to this file, we still may have to add various extensions to it. I guess we have to stat this file after all. */ if (IS_ABSOLUTE (partial)) temp = info_absolute_file (partial); else if (initial_character == '~') { expansion = tilde_expand_word (partial); if (IS_ABSOLUTE (expansion)) { temp = info_absolute_file (expansion); free (expansion); } else temp = expansion; } else if (initial_character == '.' && (IS_SLASH (partial[1]) || (partial[1] == '.' && IS_SLASH (partial[2])))) { if (local_temp_filename_size < 1024) local_temp_filename = xrealloc (local_temp_filename, (local_temp_filename_size = 1024)); #if defined (HAVE_GETCWD) if (!getcwd (local_temp_filename, local_temp_filename_size)) #else /* !HAVE_GETCWD */ if (!getwd (local_temp_filename)) #endif /* !HAVE_GETCWD */ { filesys_error_number = errno; return partial; } strcat (local_temp_filename, "/"); strcat (local_temp_filename, partial); temp = info_absolute_file (local_temp_filename); /* try extensions */ if (!temp) partial = local_temp_filename; } else temp = info_file_in_path (partial, infopath); if (temp) { remember_info_filename (partial, temp); if (strlen (temp) > (unsigned int) local_temp_filename_size) local_temp_filename = xrealloc (local_temp_filename, (local_temp_filename_size = (50 + strlen (temp)))); strcpy (local_temp_filename, temp); free (temp); return local_temp_filename; } } return partial; }
FILE *search_path::open_file_cautious(const char *name, char **pathp, const char *mode) { if (!mode) mode = "r"; bool reading = (strchr(mode, 'r') != 0); if (name == 0 || strcmp(name, "-") == 0) { if (pathp) *pathp = strsave(reading ? "stdin" : "stdout"); return (reading ? stdin : stdout); } if (!reading || IS_ABSOLUTE(name) || *dirs == '\0') { FILE *fp = fopen(name, mode); if (fp) { if (pathp) *pathp = strsave(name); return fp; } else return 0; } unsigned namelen = strlen(name); char *p = dirs; for (;;) { char *end = strchr(p, PATH_SEP_CHAR); if (!end) end = strchr(p, '\0'); int need_slash = end > p && strchr(DIR_SEPS, end[-1]) == 0; char *origpath = new char[(end - p) + need_slash + namelen + 1]; memcpy(origpath, p, end - p); if (need_slash) origpath[end - p] = '/'; strcpy(origpath + (end - p) + need_slash, name); #if 0 fprintf(stderr, "origpath `%s'\n", origpath); #endif char *path = relocate(origpath); a_delete origpath; #if 0 fprintf(stderr, "trying `%s'\n", path); #endif FILE *fp = fopen(path, mode); if (fp) { if (pathp) *pathp = path; else a_delete path; return fp; } int err = errno; a_delete path; if (err != ENOENT) { errno = err; return 0; } if (*end == '\0') break; p = end + 1; } errno = ENOENT; return 0; }
/* Return the full path to FILENAME. */ static char * full_pathname (char *filename) { int initial_character; char *result; /* No filename given? */ if (!filename || !*filename) return xstrdup (""); /* Already absolute? */ if (IS_ABSOLUTE (filename) || (*filename == '.' && (IS_SLASH (filename[1]) || (filename[1] == '.' && IS_SLASH (filename[2]))))) return xstrdup (filename); initial_character = *filename; if (initial_character != '~') { char *localdir = xmalloc (1025); #ifdef HAVE_GETCWD if (!getcwd (localdir, 1024)) #else if (!getwd (localdir)) #endif { fprintf (stderr, _("%s: getwd: %s, %s\n"), progname, filename, localdir); xexit (1); } strcat (localdir, "/"); strcat (localdir, filename); result = xstrdup (localdir); free (localdir); } else { /* Does anybody know why WIN32 doesn't want to support $HOME? If the reason is they don't have getpwnam, they should only disable the else clause below. */ #ifndef WIN32 if (IS_SLASH (filename[1])) { /* Return the concatenation of the environment variable HOME and the rest of the string. */ char *temp_home; temp_home = (char *) getenv ("HOME"); result = xmalloc (strlen (&filename[1]) + 1 + temp_home ? strlen (temp_home) : 0); *result = 0; if (temp_home) strcpy (result, temp_home); strcat (result, &filename[1]); } else { struct passwd *user_entry; int i, c; char *username = xmalloc (257); for (i = 1; (c = filename[i]); i++) { if (IS_SLASH (c)) break; else username[i - 1] = c; } if (c) username[i - 1] = 0; user_entry = getpwnam (username); if (!user_entry) return xstrdup (filename); result = xmalloc (1 + strlen (user_entry->pw_dir) + strlen (&filename[i])); strcpy (result, user_entry->pw_dir); strcat (result, &filename[i]); } #endif /* not WIN32 */ } return result; }