vfs: check userland buffers before reading them.
[haiku.git] / src / bin / rc / rdef.cpp
blob3c2765eef4032e96c6f22cd29e5e0db593060a17
1 /*
2 * Copyright (c) 2003 Matthijs Hollemans
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "rdef.h"
29 #include "private.h"
31 ptr_list_t include_dirs;
32 ptr_list_t input_files;
34 uint32 flags = 0;
36 status_t rdef_err;
37 int32 rdef_err_line;
38 char rdef_err_file[B_PATH_NAME_LENGTH];
39 char rdef_err_msg[1024];
42 void
43 free_ptr_list(ptr_list_t &list)
45 for (ptr_iter_t i = list.begin(); i != list.end(); ++i) {
46 free(*i);
49 list.clear();
53 int32
54 rdef_get_version()
56 return 2;
60 status_t
61 rdef_add_include_dir(const char *dir, bool toEndOfList)
63 clear_error();
65 char *path = (char *)malloc(strlen(dir) + 2);
66 if (path == NULL) {
67 rdef_err = B_NO_MEMORY;
68 return B_NO_MEMORY;
71 strcpy(path, dir);
72 strcat(path, "/");
74 if (toEndOfList)
75 include_dirs.push_back(path);
76 else
77 include_dirs.push_front(path);
79 return B_OK;
83 status_t
84 rdef_remove_include_dir(const char *dir)
86 size_t length = strlen(dir);
87 bool noSlash = false;
88 if (dir[length - 1] != '/')
89 noSlash = true;
91 for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
92 char *path = (char *)*i;
94 if (!strncmp(dir, path, length)
95 && path[length + (noSlash ? 1 : 0)] == '\0') {
96 // we found the entry in the list, let's remove it
97 include_dirs.erase(i);
98 free(path);
99 return B_OK;
103 return B_ENTRY_NOT_FOUND;
107 void
108 rdef_free_include_dirs()
110 free_ptr_list(include_dirs);
114 status_t
115 rdef_add_input_file(const char *file)
117 clear_error();
119 char *temp = strdup(file);
120 if (temp == NULL) {
121 rdef_err = B_NO_MEMORY;
122 return B_NO_MEMORY;
125 input_files.push_back(temp);
126 return B_OK;
130 void
131 rdef_free_input_files()
133 free_ptr_list(input_files);
137 void
138 rdef_set_flags(uint32 flags_)
140 flags = flags_;
144 void
145 rdef_clear_flags()
147 flags = 0;
151 void
152 clear_error()
154 rdef_err = B_OK;
155 rdef_err_line = 0;
156 rdef_err_file[0] = '\0';
157 rdef_err_msg[0] = '\0';
161 bool
162 open_file_from_include_dir(const char *filename, char *outname)
164 for (ptr_iter_t i = include_dirs.begin(); i != include_dirs.end(); ++i) {
165 char tmpname[B_PATH_NAME_LENGTH];
166 strcpy(tmpname, (char *)*i);
167 strcat(tmpname, filename);
169 if (access(tmpname, R_OK) == 0) {
170 strcpy(outname, tmpname);
171 return true;
175 return false;