1 /*****************************************************************
2 * SQUID - a library of functions for biological sequence analysis
3 * Copyright (C) 1992-2002 Washington University School of Medicine
5 * This source code is freely distributed under the terms of the
6 * GNU General Public License. See the files COPYRIGHT and LICENSE
8 *****************************************************************/
12 * SRE, Wed Jun 19 11:19:22 1996
14 * File operation utilities, dealing with pathnames, directories,
15 * and environment variables.
17 * The goal is to have these be platform-independent but they
18 * currently are UNIX-specific: i.e. this file is currently POSIX compliant
19 * but it is NOT ANSI C compliant. (The sole offender is getenv().)
21 * RCS $Id: file.c 217 2011-03-19 10:27:10Z andreas $ (Original squid RCS Id: file.c,v 1.8 2002/03/07 03:18:02 eddy Exp)
32 * VMS: #define DIRSLASH ']'
33 * MacOS: #define DIRSLASH ':'
34 * DOS: #define DIRSLASH '\\'
36 * The code assumes that '.' is used for file name extensions,
39 #define DIRSLASH '/' /* UNIX directory paths have /foo/bar */
43 /* Function: FileDirname()
45 * Purpose: Returns the path from a filename:
46 * "/foo/bar/baz" -> "/foo/bar"
50 * i.e. the string will be non-NULL; it will
51 * contain the string up to but not including the
52 * last '/' character; returns "." if
53 * there are no '/' characters, and returns "/"
54 * if the last slash is the first character.
55 * Modeled on Tcl's "file dirname" command.
57 * Args: file - name of file "/foo/bar/baz".
59 * Return: ptr to malloc'ed string "/foo/bar".
62 FileDirname(char *file)
68 lastslash = strrchr(file, DIRSLASH);
69 len = (lastslash == NULL) ? 0 : (int) (lastslash - file);
70 dirname = (char *) MallocOrDie (sizeof(char) * (len+2));
71 if (len > 0) strncpy(dirname, file, len);
72 else if (*file != DIRSLASH) { *dirname = '.'; len = 1; }
73 else { *dirname = DIRSLASH; len = 1; }
79 /* Function: FileTail()
81 * Purpose: Return everything after the DIRSLASH:
82 * "/foo/bar/baz.1" -> "baz.1"
86 * If noextension is TRUE, removes a trailing ".foo" extension
89 * Args: file - name of file "/foo/bar/baz.1"
90 * noextension - TRUE to also remove extensions
92 * Return: ptr to malloc'ed string "baz.1"
95 FileTail(char *file, int noextension)
100 /* remove directory prefix */
101 lastslash = strrchr(file, DIRSLASH);
102 tail = (char *) MallocOrDie (sizeof(char) * (strlen(file)+1));
103 if (lastslash == NULL) strcpy(tail, file);
104 else strcpy(tail, lastslash+1);
105 /* remove trailing suffix */
107 if ((lastdot = strrchr(tail, '.')) != NULL)
115 /* Function: FileSameDirectory()
116 * Date: SRE, Wed Mar 6 20:03:23 2002 [St. Louis]
118 * Purpose: Given a path to one file, and the
119 * name of another file in the same directory,
120 * concat the path from file1 onto file2, and
121 * return the result. Caller must free the ptr
124 * Written for SSI - SSI indices contain filenames
125 * without paths, and we will need to convert that
128 * Args: file1 - a path to a file, e.g. "/foo/bar/baz.1"
129 * file2 - a simple filename, e.g. "quux.2"
131 * Returns: path to file2: e.g. "/foo/bar/quux.2"
132 * Returns NULL if file2 already has a path, and the result
133 * would be a different place.
136 FileSameDirectory(char *file1, char *file2)
143 path = FileDirname(file1);
144 tail = FileTail(file2, FALSE);
145 if (strcmp(file2, tail) != 0) seems_ok = 0; /* ut-oh, file2 *had* a path */
146 result = FileConcat(path, tail);
147 if (! seems_ok && strcmp(result, file2) != 0) {
148 free(result); result = NULL;
155 /* Function: FileConcat()
157 * Purpose: Concatenate a directory path and a file name,
158 * returning a pointer to a malloc'ed string with the
159 * full filename. This isn't just a string concat,
160 * because we're careful about the dir slash.
163 FileConcat(char *dir, char *file)
167 full = (char *) MallocOrDie (sizeof(char) * (strlen(dir)+strlen(file)+2));
168 if (*file == DIRSLASH) strcpy(full, file); /* file = "/foo", ignore directory. */
169 else sprintf(full, "%s%c%s", dir, DIRSLASH, file);
174 /* Function: FileAddSuffix()
175 * Date: SRE, Wed Aug 1 11:19:33 2001 [Pasadena]
177 * Purpose: Add a suffix to a filename, return a malloc'ed
178 * string containing the new filename.sfx name.
180 * FileAddSuffix("genbank", "ssi")
181 * returns "genbank.ssi".
184 FileAddSuffix(char *filename, char *sfx)
187 new = MallocOrDie(strlen(filename) + strlen(sfx) + 2);
188 sprintf(new, "%s.%s", filename, sfx);
192 /* Function: EnvFileOpen()
193 * Date: Sun Feb 12 10:55:29 1995
195 * Purpose: Open a file, given a file name and an environment
196 * variable that contains a directory path. Files
197 * are opened read-only. Does not look at current directory
198 * unless "." is explicitly in the path specified by env.
201 * fp = EnvFileOpen("BLOSUM45", "BLASTMAT", NULL);
203 * fp = EnvFileOpen("swiss", "BLASTDB", NULL);
205 * Environment variables may contain a colon-delimited
206 * list of more than one path; e.g.
207 * setenv BLASTDB /nfs/databases/foo:/nfs/databases/bar
209 * Sometimes a group of files may be found in
210 * one directory; for instance, an index file with a
211 * database. The caller can EnvFileOpen() the main
212 * file, and ask to get the name of the
213 * directory back in ret_dir, so it can construct
214 * the other auxiliary file names and fopen() them. (If it called
215 * EnvFileOpen(), it might get confused by
216 * file name clashes and open files in different
219 * Args: fname - name of file to open
220 * env - name of environment variable containing path
221 * ret_dir - if non-NULL, RETURN: name of dir that was used.
223 * Return: FILE * to open file, or NULL on failure -- same as fopen()
224 * Caller must free ret_dir if it passed a non-NULL address.
227 EnvFileOpen(char *fname, char *env, char **ret_dir)
231 char *s; /* ptr to indiv element in env list */
232 char full[1024]; /* constructed file name */
234 if (env == NULL) return NULL;
235 if ((path = Strdup(getenv(env))) == NULL) return NULL;
238 s = strtok(path, ":");
241 if (((int) strlen(fname) + (int) strlen(s) + 2) > 1024)
242 { free(path); return NULL; }
243 sprintf(full, "%s%c%s", s, DIRSLASH, fname);
244 if ((fp = fopen(full, "r")) != NULL) break;
245 s = strtok(NULL, ":");
248 /* Return the path we used, if caller wants it
250 if (ret_dir != NULL) *ret_dir = Strdup(s);
257 /* Function: FileExists()
259 * Purpose: Return TRUE if filename exists.
260 * Testing fopen() is the only possible platform-independent test
264 FileExists(char *filename)
267 if ((fp = fopen(filename, "r"))) { fclose(fp); return TRUE; }