file make-gas.com was initially added on branch binutils-2_10-branch.
[binutils.git] / gprof / source.c
blob49ba57e61daa84d5182f9f7869e18ccd95804bb9
1 /*
2 * Keeps track of source files.
3 */
4 #include <sys/stat.h>
6 #include "gprof.h"
7 #include "libiberty.h"
8 #include "filenames.h"
9 #include "search_list.h"
10 #include "source.h"
12 #define EXT_ANNO "-ann" /* postfix of annotated files */
15 * Default option values:
17 bool create_annotation_files = FALSE;
19 Search_List src_search_list =
20 {0, 0};
21 Source_File *first_src_file = 0;
24 Source_File *
25 DEFUN (source_file_lookup_path, (path), const char *path)
27 Source_File *sf;
29 for (sf = first_src_file; sf; sf = sf->next)
31 if (FILENAME_CMP (path, sf->name) == 0)
33 break;
36 if (!sf)
38 /* create a new source file descriptor: */
40 sf = (Source_File *) xmalloc (sizeof (*sf));
41 memset (sf, 0, sizeof (*sf));
42 sf->name = xstrdup (path);
43 sf->next = first_src_file;
44 first_src_file = sf;
46 return sf;
50 Source_File *
51 DEFUN (source_file_lookup_name, (filename), const char *filename)
53 const char *fname;
54 Source_File *sf;
56 * The user cannot know exactly how a filename will be stored in
57 * the debugging info (e.g., ../include/foo.h
58 * vs. /usr/include/foo.h). So we simply compare the filename
59 * component of a path only:
61 for (sf = first_src_file; sf; sf = sf->next)
63 fname = strrchr (sf->name, '/');
64 if (fname)
66 ++fname;
68 else
70 fname = sf->name;
72 if (FILENAME_CMP (filename, fname) == 0)
74 break;
77 return sf;
81 FILE *
82 DEFUN (annotate_source, (sf, max_width, annote, arg),
83 Source_File * sf AND int max_width
84 AND void (*annote) PARAMS ((char *buf, int w, int l, void *arg))
85 AND void *arg)
87 static bool first_file = TRUE;
88 int i, line_num, nread;
89 bool new_line;
90 char buf[8192];
91 char fname[PATH_MAX];
92 char *annotation, *name_only;
93 FILE *ifp, *ofp;
94 Search_List_Elem *sle = src_search_list.head;
97 * Open input file. If open fails, walk along search-list until
98 * open succeeds or reaching end of list:
100 strcpy (fname, sf->name);
101 if (IS_ABSOLUTE_PATH (sf->name))
103 sle = 0; /* don't use search list for absolute paths */
105 name_only = 0;
106 while (TRUE)
108 DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
109 sf->name, fname));
110 ifp = fopen (fname, FOPEN_RB);
111 if (ifp)
113 break;
115 if (!sle && !name_only)
117 name_only = strrchr (sf->name, '/');
118 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
120 char *bslash = strrchr (sf->name, '\\');
121 if (bslash > name_only)
122 name_only = bslash;
123 if (name_only == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
124 name_only = (char *)sf->name + 1;
126 #endif
127 if (name_only)
129 /* try search-list again, but this time with name only: */
130 ++name_only;
131 sle = src_search_list.head;
134 if (sle)
136 strcpy (fname, sle->path);
137 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
138 /* d:foo is not the same thing as d:/foo! */
139 if (fname[strlen (fname) - 1] == ':')
140 strcat (fname, ".");
141 #endif
142 strcat (fname, "/");
143 if (name_only)
145 strcat (fname, name_only);
147 else
149 strcat (fname, sf->name);
151 sle = sle->next;
153 else
155 if (errno == ENOENT)
157 fprintf (stderr, _("%s: could not locate `%s'\n"),
158 whoami, sf->name);
160 else
162 perror (sf->name);
164 return 0;
168 ofp = stdout;
169 if (create_annotation_files)
171 /* try to create annotated source file: */
172 const char *filename;
174 /* create annotation files in the current working directory: */
175 filename = strrchr (sf->name, '/');
176 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
178 char *bslash = strrchr (sf->name, '\\');
179 if (bslash > filename)
180 filename = bslash;
181 if (filename == NULL && sf->name[0] != '\0' && sf->name[1] == ':')
182 filename = sf->name + 1;
184 #endif
185 if (filename)
187 ++filename;
189 else
191 filename = sf->name;
194 strcpy (fname, filename);
195 strcat (fname, EXT_ANNO);
196 #ifdef __MSDOS__
198 /* foo.cpp-ann can overwrite foo.cpp due to silent truncation of
199 file names on 8+3 filesystems. Their `stat' better be good... */
200 struct stat buf1, buf2;
202 if (stat (filename, &buf1) == 0
203 && stat (fname, &buf2) == 0
204 && buf1.st_ino == buf2.st_ino)
206 char *dot = strrchr (fname, '.');
208 if (dot)
209 *dot = '\0';
210 strcat (fname, ".ann");
213 #endif
214 ofp = fopen (fname, "w");
215 if (!ofp)
217 perror (fname);
218 return 0;
223 * Print file names if output goes to stdout and there are
224 * more than one source file:
226 if (ofp == stdout)
228 if (first_file)
230 first_file = FALSE;
232 else
234 fputc ('\n', ofp);
236 if (first_output)
238 first_output = FALSE;
240 else
242 fprintf (ofp, "\f\n");
244 fprintf (ofp, _("*** File %s:\n"), sf->name);
247 annotation = xmalloc (max_width + 1);
248 line_num = 1;
249 new_line = TRUE;
250 while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
252 for (i = 0; i < nread; ++i)
254 if (new_line)
256 (*annote) (annotation, max_width, line_num, arg);
257 fputs (annotation, ofp);
258 ++line_num;
259 new_line = FALSE;
261 new_line = (buf[i] == '\n');
262 fputc (buf[i], ofp);
265 free (annotation);
266 return ofp;