Portability fixes.
[binutils.git] / gprof / source.c
blob4901a04b0379c228583b5ca279e8f00eb8c6b164
1 /*
2 * Keeps track of source files.
3 */
4 #include "gprof.h"
5 #include "libiberty.h"
6 #include "search_list.h"
7 #include "source.h"
9 #define EXT_ANNO "-ann" /* postfix of annotated files */
12 * Default option values:
14 bool create_annotation_files = FALSE;
16 Search_List src_search_list =
17 {0, 0};
18 Source_File *first_src_file = 0;
21 Source_File *
22 DEFUN (source_file_lookup_path, (path), const char *path)
24 Source_File *sf;
26 for (sf = first_src_file; sf; sf = sf->next)
28 if (strcmp (path, sf->name) == 0)
30 break;
33 if (!sf)
35 /* create a new source file descriptor: */
37 sf = (Source_File *) xmalloc (sizeof (*sf));
38 memset (sf, 0, sizeof (*sf));
39 sf->name = xstrdup (path);
40 sf->next = first_src_file;
41 first_src_file = sf;
43 return sf;
47 Source_File *
48 DEFUN (source_file_lookup_name, (filename), const char *filename)
50 const char *fname;
51 Source_File *sf;
53 * The user cannot know exactly how a filename will be stored in
54 * the debugging info (e.g., ../include/foo.h
55 * vs. /usr/include/foo.h). So we simply compare the filename
56 * component of a path only:
58 for (sf = first_src_file; sf; sf = sf->next)
60 fname = strrchr (sf->name, '/');
61 if (fname)
63 ++fname;
65 else
67 fname = sf->name;
69 if (strcmp (filename, fname) == 0)
71 break;
74 return sf;
78 FILE *
79 DEFUN (annotate_source, (sf, max_width, annote, arg),
80 Source_File * sf AND int max_width
81 AND void (*annote) PARAMS ((char *buf, int w, int l, void *arg))
82 AND void *arg)
84 static bool first_file = TRUE;
85 int i, line_num, nread;
86 bool new_line;
87 char buf[8192];
88 char fname[PATH_MAX];
89 char *annotation, *name_only;
90 FILE *ifp, *ofp;
91 Search_List_Elem *sle = src_search_list.head;
94 * Open input file. If open fails, walk along search-list until
95 * open succeeds or reaching end of list:
97 strcpy (fname, sf->name);
98 if (sf->name[0] == '/')
100 sle = 0; /* don't use search list for absolute paths */
102 name_only = 0;
103 while (TRUE)
105 DBG (SRCDEBUG, printf ("[annotate_source]: looking for %s, trying %s\n",
106 sf->name, fname));
107 ifp = fopen (fname, FOPEN_RB);
108 if (ifp)
110 break;
112 if (!sle && !name_only)
114 name_only = strrchr (sf->name, '/');
115 if (name_only)
117 /* try search-list again, but this time with name only: */
118 ++name_only;
119 sle = src_search_list.head;
122 if (sle)
124 strcpy (fname, sle->path);
125 strcat (fname, "/");
126 if (name_only)
128 strcat (fname, name_only);
130 else
132 strcat (fname, sf->name);
134 sle = sle->next;
136 else
138 if (errno == ENOENT)
140 fprintf (stderr, _("%s: could not locate `%s'\n"),
141 whoami, sf->name);
143 else
145 perror (sf->name);
147 return 0;
151 ofp = stdout;
152 if (create_annotation_files)
154 /* try to create annotated source file: */
155 const char *filename;
157 /* create annotation files in the current working directory: */
158 filename = strrchr (sf->name, '/');
159 if (filename)
161 ++filename;
163 else
165 filename = sf->name;
168 strcpy (fname, filename);
169 strcat (fname, EXT_ANNO);
170 ofp = fopen (fname, "w");
171 if (!ofp)
173 perror (fname);
174 return 0;
179 * Print file names if output goes to stdout and there are
180 * more than one source file:
182 if (ofp == stdout)
184 if (first_file)
186 first_file = FALSE;
188 else
190 fputc ('\n', ofp);
192 if (first_output)
194 first_output = FALSE;
196 else
198 fprintf (ofp, "\f\n");
200 fprintf (ofp, _("*** File %s:\n"), sf->name);
203 annotation = xmalloc (max_width + 1);
204 line_num = 1;
205 new_line = TRUE;
206 while ((nread = fread (buf, 1, sizeof (buf), ifp)) > 0)
208 for (i = 0; i < nread; ++i)
210 if (new_line)
212 (*annote) (annotation, max_width, line_num, arg);
213 fputs (annotation, ofp);
214 ++line_num;
215 new_line = FALSE;
217 new_line = (buf[i] == '\n');
218 fputc (buf[i], ofp);
221 free (annotation);
222 return ofp;