-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfiles.c
213 lines (171 loc) · 3.87 KB
/
files.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "c2ada.h"
#include "hostinfo.h"
#ifdef HAS_MMAP
#include <sys/mman.h>
#else
#include <unistd.h>
#include <malloc.h>
#endif
#undef NULL
#define NULL 0
/* This module manages the correspondence between file pathnames and
* an internal numbering of unique files, as represented by the
* type <file_id_t>.
* It implements the type <file_pos_t> which encodes a file number
* and line number.
*
* Finally, the routines <sizeof_file>, <map_file>, and <unmap_file>
* confusingly deal with Unix file descriptors, and don't interact
* with the other routines in this module.
*/
/* <fnames> lists the unique pathnames in <file_id_t> order. <findex>
* is the next available free <file_id_t>.
*/
static char* fnames[MAX_UNIQ_FNAMES];
static file_id_t findex;
#define FILE_ORD(x) ((x) >> LINE_NUMBER_BITS)
file_id_t pos_file(file_pos_t pos)
{
return FILE_ORD(pos);
}
line_nt pos_line(file_pos_t pos)
{
return line_number(pos);
}
static file_pos_t file_line_pos(file_id_t f, int line)
{
return (((long)f) << LINE_NUMBER_BITS) | line;
}
int num_files()
{
return findex;
}
char* file_name_from_ord(file_id_t ord)
{
return fnames[ord];
}
char* file_name(file_pos_t pos)
{
return fnames[FILE_ORD(pos)];
}
file_pos_t add_file(char* path)
{
file_pos_t i;
file_id_t n;
if((i = find_file(path)) != -1)
{
return i;
}
assert(findex < MAX_UNIQ_FNAMES);
n = findex++;
fnames[n] = path;
return file_line_pos(n, 1);
}
file_pos_t set_file_pos(char* path, int line)
{
file_pos_t pos;
pos = add_file(path);
return file_line_pos(pos_file(pos), line);
}
/* return 1 if strings not equal, 0 if equal */
int compare_path(char* s1, char* s2)
{
char c1, c2;
/* compare paths but treat // the same as / */
while(1)
{
c1 = *s1++;
c2 = *s2++;
if(c1 != c2)
return 1;
if(c1 == '\0')
return (c2 != 0);
if(c2 == '\0')
return 1;
if(c1 == '/')
while(*s1 == '/')
s1++;
if(c2 == '/')
while(*s2 == '/')
s2++;
}
}
file_pos_t find_file(char* path)
/*
* Search for <path> in the fnames list;
* if found, return pos corresponding to file's first line;
* otherwise return -1.
*/
{
file_id_t i;
for(i = 0; i < findex; i++)
{
if(!compare_path(fnames[i], path))
{
return file_line_pos(i, 1);
}
}
return -1;
}
size_t sizeof_file(int fd)
{
struct stat stat_buf;
if(fstat(fd, &stat_buf) == -1)
{
return -1;
}
return stat_buf.st_size;
}
#ifdef HAS_MMAP /* Use Unix mmap() and munmap() */
void* map_file(int fd, size_t fsize)
{
return mmap(0, fsize, PROT_READ, MAP_PRIVATE, fd, 0);
}
int unmap_file(void* addr, size_t len)
{
return munmap(addr, (int)len);
}
#else /* This system doesn't support mmap() and munmap() */
void* map_file(fd, fsize) int fd;
size_t fsize;
{
char* addr;
int len;
addr = (char*)malloc(fsize);
assert(addr != NULL);
len = read(fd, addr, (unsigned)fsize);
assert(len == (int)fsize);
return addr;
}
int unmap_file(addr, len) void* addr;
size_t len;
{
free(addr);
}
#endif
/* convenience functions for source-specific warning messages */
void error_at(file_pos_t pos, char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vreport(Report_error, file_name(pos), line_number(pos), fmt, ap);
va_end(ap);
}
void warning_at(file_pos_t pos, char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vreport(Report_warning, file_name(pos), line_number(pos), fmt, ap);
va_end(ap);
}
void inform_at(file_pos_t pos, char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vreport(Report_inform, file_name(pos), line_number(pos), fmt, ap);
}