-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
116 lines (94 loc) · 1.96 KB
/
Copy pathutils.c
File metadata and controls
116 lines (94 loc) · 1.96 KB
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
#include <utils.h>
#include <types.h>
#include <errno.h>
#include <segment.h>
void
copy_data (void *start, void *dest, int size)
{
DWord *p = start, *q = dest;
while (size > 0)
{
*q++ = *p++;
size -= 4;
}
}
/* Copia de espacio de usuario a espacio de kernel, devuelve 0 si ok y -1 si error*/
int
copy_from_user (void *start, void *dest, int size)
{
int error;
error = check_address(start, size);
if (error != 0) return error;
DWord *p = start, *q = dest;
while (size > 0)
{
*q++ = *p++;
size -= 4;
}
return 0;
}
int
check_address(void *buffer, int size)
{
unsigned int min_usr_address, max_usr_address, bytes_to_copy, max_bytes;
max_bytes = (NUM_PAG_CODE + NUM_PAG_DATA) * PAGE_SIZE;
min_usr_address = L_USER_START;
max_usr_address = L_USER_START + max_bytes;
bytes_to_copy = (unsigned int) size;
if ((unsigned int) buffer < min_usr_address
|| (unsigned int) buffer > max_usr_address || bytes_to_copy > max_bytes)
return -EFAULT;
return 0;
}
/* Copia de espacio de kernel a espacio de usuario, devuelve 0 si ok y -1 si error*/
int
copy_to_user (void *start, void *dest, int size)
{
int error;
if (size < 0)
return -EINVAL;
error = check_address(dest, size);
if (error != 0) return error;
DWord *p = start, *q = dest;
while (size > 0)
{
*q++ = *p++;
size -= 4;
}
return 0;
}
/*Strcmp implementation, thanks to:
* P.J. Plauger, The Standard C Library, 1992
*/
int
strcmp (const char *s1, const char *s2)
{
for (; *s1 == *s2; ++s1, ++s2)
if (*s1 == 0)
return 0;
return *(unsigned char *) s1 < *(unsigned char *) s2 ? -1 : 1;
}
int
strlen (const char *s1)
{
int i = 0;
for (; s1[i] != 0; i++);
return i;
}
void
itoa (int n, char *buffer)
{
int ndigits = n;
int i = 0;
while (ndigits > 0)
{
i++;
ndigits /= 10;
}
while (i > 0)
{
buffer[i - 1] = (n % 10) + '0';
n /= 10;
i--;
}
}