Updated PCI IDs to latest snapshot.
[tangerine.git] / compiler / purify / src / io.c
blobb670a97e00c812c4625741645c50211bce4fef4a
1 #include <stdio.h>
2 #include <sys/stat.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <dirent.h>
6 #include "funcs.h"
7 #include "hash.h"
8 #include "error.h"
10 size_t Purify_fread (void *ptr, size_t size, size_t nmemb, FILE *stream)
12 if (!Purify_CheckMemoryAccess (ptr, size*nmemb, PURIFY_MemAccess_Write))
14 if (Purify_Error == NotOwnMemory)
16 Purify_Error = IllPointer;
17 Purify_PrintError ("fread (addr=%p, size=%ld, num=%ld, file=%p)"
18 , ptr, size, nmemb, stream);
20 else
22 Purify_PrintAccessError ("fread()", ptr, size);
26 return fread (ptr, size, nmemb, stream);
29 size_t Purify_read (int fd, void *ptr, size_t size)
31 if (!Purify_CheckMemoryAccess (ptr, size, PURIFY_MemAccess_Write))
33 if (Purify_Error == NotOwnMemory)
35 Purify_Error = IllPointer;
36 Purify_PrintError ("read (fd=%d, addr=%p, size=%ld)"
37 , fd, ptr, size);
39 else
41 Purify_PrintAccessError ("read()", ptr, size);
45 return read (fd, ptr, size);
48 char * Purify_fgets (char * ptr, int size, FILE * stream)
50 if (!Purify_CheckMemoryAccess (ptr, size, PURIFY_MemAccess_Write))
52 if (Purify_Error == NotOwnMemory)
54 Purify_Error = IllPointer;
55 Purify_PrintError ("fgets (ptr=%p, size=%d, stream=%p)"
56 , ptr, size, stream);
58 else
60 Purify_PrintAccessError ("fgets()", ptr, size);
64 return fgets (ptr, size, stream);
67 char * Purify_getcwd (char * buf, size_t size)
69 char * ret;
71 if (buf)
73 if (!Purify_CheckMemoryAccess (buf, size, PURIFY_MemAccess_Write))
75 if (Purify_Error == NotOwnMemory)
77 Purify_Error = IllPointer;
78 Purify_PrintError ("getcwd (buf=%p, size=%ld)"
79 , buf, size);
81 else
83 Purify_PrintAccessError ("getcwd()", buf, size);
87 ret = getcwd (buf, size);
89 else
91 char * ptr;
93 ptr = getcwd (NULL, size);
95 if (!ptr)
96 return NULL;
98 if (!size)
100 ret = Purify_strdup (ptr);
102 if (!ret)
103 return NULL;
105 else
107 MemHash * hash;
109 ret = Purify_malloc (size);
111 if (!ret)
112 return NULL;
114 strcpy (ret, ptr);
116 hash = Purify_FindMemory (ret);
117 Purify_SetMemoryFlags (hash, 0, strlen (ptr) + 1,
118 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
122 free (ptr);
125 return ret;
128 int Purify_stat (char * path, struct stat * st)
130 int len;
132 if (!Purify_CheckMemoryAccess (path, 1, PURIFY_MemAccess_Read))
133 Purify_PrintAccessError ("stat (path, ...)", path, 1);
135 len = strlen (path)+1;
137 if (!Purify_CheckMemoryAccess (path, len, PURIFY_MemAccess_Read))
138 Purify_PrintAccessError ("stat (path, ...)", path, len);
140 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
141 Purify_PrintAccessError ("stat (..., stat)", st, sizeof (struct stat));
143 return stat (path, st);
146 int Purify_lstat (char * path, struct stat * st)
148 int len;
150 if (!Purify_CheckMemoryAccess (path, 1, PURIFY_MemAccess_Read))
151 Purify_PrintAccessError ("lstat (path, ...)", path, 1);
153 len = strlen (path)+1;
155 if (!Purify_CheckMemoryAccess (path, len, PURIFY_MemAccess_Read))
156 Purify_PrintAccessError ("lstat (path, ...)", path, len);
158 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
159 Purify_PrintAccessError ("lstat (..., stat)", st, sizeof (struct stat));
161 return lstat (path, st);
164 int Purify_fstat (int fd, struct stat * st)
166 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
167 Purify_PrintAccessError ("fstat (..., stat)", st, sizeof (struct stat));
169 return fstat (fd, st);
172 struct dirent * Purify_readdir (DIR * dir)
174 static struct dirent * last_de = NULL;
175 struct dirent * de;
177 de = readdir (dir);
179 if (de != last_de)
181 MemHash * hash;
183 if (last_de)
184 Purify_RemMemory (last_de);
186 hash = Purify_AddMemory (de, sizeof (struct dirent),
187 PURIFY_MemFlag_Writable
188 | PURIFY_MemFlag_Readable
189 , PURIFY_MemType_Data
192 hash->data = "dirent";
194 last_de = de;
197 return de;