revert between 56095 -> 55830 in arch
[AROS.git] / compiler / purify / src / io.c
blobab64f34bdf91f66d0b194c6a052401ae444ae73d
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <stdio.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <dirent.h>
11 #include "funcs.h"
12 #include "hash.h"
13 #include "error.h"
15 size_t Purify_fread (void *ptr, size_t size, size_t nmemb, FILE *stream)
17 if (!Purify_CheckMemoryAccess (ptr, size*nmemb, PURIFY_MemAccess_Write))
19 if (Purify_Error == NotOwnMemory)
21 Purify_Error = IllPointer;
22 Purify_PrintError ("fread (addr=%p, size=%ld, num=%ld, file=%p)"
23 , ptr, size, nmemb, stream);
25 else
27 Purify_PrintAccessError ("fread()", ptr, size);
31 return fread (ptr, size, nmemb, stream);
34 size_t Purify_read (int fd, void *ptr, size_t size)
36 if (!Purify_CheckMemoryAccess (ptr, size, PURIFY_MemAccess_Write))
38 if (Purify_Error == NotOwnMemory)
40 Purify_Error = IllPointer;
41 Purify_PrintError ("read (fd=%d, addr=%p, size=%ld)"
42 , fd, ptr, size);
44 else
46 Purify_PrintAccessError ("read()", ptr, size);
50 return read (fd, ptr, size);
53 char * Purify_fgets (char * ptr, int size, FILE * stream)
55 if (!Purify_CheckMemoryAccess (ptr, size, PURIFY_MemAccess_Write))
57 if (Purify_Error == NotOwnMemory)
59 Purify_Error = IllPointer;
60 Purify_PrintError ("fgets (ptr=%p, size=%d, stream=%p)"
61 , ptr, size, stream);
63 else
65 Purify_PrintAccessError ("fgets()", ptr, size);
69 return fgets (ptr, size, stream);
72 char * Purify_getcwd (char * buf, size_t size)
74 char * ret;
76 if (buf)
78 if (!Purify_CheckMemoryAccess (buf, size, PURIFY_MemAccess_Write))
80 if (Purify_Error == NotOwnMemory)
82 Purify_Error = IllPointer;
83 Purify_PrintError ("getcwd (buf=%p, size=%ld)"
84 , buf, size);
86 else
88 Purify_PrintAccessError ("getcwd()", buf, size);
92 ret = getcwd (buf, size);
94 else
96 char * ptr;
98 ptr = getcwd (NULL, size);
100 if (!ptr)
101 return NULL;
103 if (!size)
105 ret = Purify_strdup (ptr);
107 if (!ret)
108 return NULL;
110 else
112 MemHash * hash;
114 ret = Purify_malloc (size);
116 if (!ret)
117 return NULL;
119 strcpy (ret, ptr);
121 hash = Purify_FindMemory (ret);
122 Purify_SetMemoryFlags (hash, 0, strlen (ptr) + 1,
123 PURIFY_MemFlag_Readable|PURIFY_MemFlag_Writable
127 free (ptr);
130 return ret;
133 int Purify_stat (char * path, struct stat * st)
135 int len;
137 if (!Purify_CheckMemoryAccess (path, 1, PURIFY_MemAccess_Read))
138 Purify_PrintAccessError ("stat (path, ...)", path, 1);
140 len = strlen (path)+1;
142 if (!Purify_CheckMemoryAccess (path, len, PURIFY_MemAccess_Read))
143 Purify_PrintAccessError ("stat (path, ...)", path, len);
145 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
146 Purify_PrintAccessError ("stat (..., stat)", st, sizeof (struct stat));
148 return stat (path, st);
151 int Purify_lstat (char * path, struct stat * st)
153 int len;
155 if (!Purify_CheckMemoryAccess (path, 1, PURIFY_MemAccess_Read))
156 Purify_PrintAccessError ("lstat (path, ...)", path, 1);
158 len = strlen (path)+1;
160 if (!Purify_CheckMemoryAccess (path, len, PURIFY_MemAccess_Read))
161 Purify_PrintAccessError ("lstat (path, ...)", path, len);
163 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
164 Purify_PrintAccessError ("lstat (..., stat)", st, sizeof (struct stat));
166 return lstat (path, st);
169 int Purify_fstat (int fd, struct stat * st)
171 if (!Purify_CheckMemoryAccess (st, sizeof (struct stat), PURIFY_MemAccess_Write))
172 Purify_PrintAccessError ("fstat (..., stat)", st, sizeof (struct stat));
174 return fstat (fd, st);
177 struct dirent * Purify_readdir (DIR * dir)
179 static struct dirent * last_de = NULL;
180 struct dirent * de;
182 de = readdir (dir);
184 if (de != last_de)
186 MemHash * hash;
188 if (last_de)
189 Purify_RemMemory (last_de);
191 hash = Purify_AddMemory (de, sizeof (struct dirent),
192 PURIFY_MemFlag_Writable
193 | PURIFY_MemFlag_Readable
194 , PURIFY_MemType_Data
197 hash->data = "dirent";
199 last_de = de;
202 return de;