/[path-sandbox]/trunk/libsbutil/get_sandbox_lib.c
Gentoo

Diff of /trunk/libsbutil/get_sandbox_lib.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/sandbox_utils.c Revision 281 trunk/libsbutil/sbutil.c Revision 309
1/* 1/*
2 * sbutil.c
3 *
4 * Util functions.
5 *
6 * Copyright 1999-2006 Gentoo Foundation
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 * Some parts might have Copyright:
23 *
2 * Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com> 24 * Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com>
3 * Distributed under the terms of the GNU General Public License, v2 or later
4 * Author: Brad House <brad@mainstreetsoftworks.com>
5 * Author: Martin Schlemmer <azarah@gentoo.org>
6 * 25 *
7 * $Header$ 26 * $Header$
8 *
9 */ 27 */
28
10 29
11#include <errno.h> 30#include <errno.h>
12#include <stdio.h> 31#include <stdio.h>
13#include <stdlib.h> 32#include <stdlib.h>
14#include <limits.h> 33#include <limits.h>
16#include <sys/stat.h> 35#include <sys/stat.h>
17#include <unistd.h> 36#include <unistd.h>
18#include <fcntl.h> 37#include <fcntl.h>
19#include <libgen.h> 38#include <libgen.h>
20 39
21#include "config.h"
22#include "localdecls.h"
23
24#include "sandbox.h" 40#include "sbutil.h"
25 41
26void get_sandbox_lib(char *path) 42void get_sandbox_lib(char *path)
27{ 43{
28#ifdef SB_HAVE_MULTILIB 44#ifdef SB_HAVE_MULTILIB
29 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME); 45 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
30#else 46#else
31 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME); 47 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
32# ifdef OUTSIDE_LIBSANDBOX
33 if (0 >= rc_file_exists(path)) { 48 if (!rc_file_exists(path)) {
34# else
35 if (0 >= exists(path)) {
36# endif
37 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME); 49 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
38 } 50 }
39#endif 51#endif
40} 52}
41
42#ifdef OUTSIDE_LIBSANDBOX
43 53
44void get_sandbox_rc(char *path) 54void get_sandbox_rc(char *path)
45{ 55{
46 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME); 56 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
47} 57}
108 return 0; 118 return 0;
109 119
110 return (st.st_size); 120 return (st.st_size);
111} 121}
112 122
113#endif /* OUTSIDE_LIBSANDBOX */
114
115bool is_env_on (const char *env) 123bool is_env_on (const char *env)
116{ 124{
117 if ((NULL != env) && (NULL != getenv(env)) && 125 if ((NULL != env) && (NULL != getenv(env)) &&
118 ((0 == strncasecmp(getenv(env), "1", 1)) || 126 ((0 == strncasecmp(getenv(env), "1", 1)) ||
119 (0 == strncasecmp(getenv(env), "true", 4)) || 127 (0 == strncasecmp(getenv(env), "true", 4)) ||
132 return TRUE; 140 return TRUE;
133 141
134 return FALSE; 142 return FALSE;
135} 143}
136 144
137#ifndef OUTSIDE_LIBSANDBOX
138
139int exists(const char *pathname)
140{
141 struct stat buf;
142 int retval;
143
144 if ((NULL == pathname) || (0 == strlen(pathname)))
145 return 0;
146
147 retval = lstat(pathname, &buf);
148 if (-1 != retval)
149 return 1;
150 /* Some or other error occurred */
151 if (ENOENT != errno)
152 return -1;
153
154 return 0;
155}
156
157char * gstrndup (const char *str, size_t size)
158{
159 char *new_str = NULL;
160 size_t len;
161
162 if (NULL == str)
163 return NULL;
164
165 /* Check lenght of str without breaching the size limit */
166 for (len = 0; (len < size) && ('\0' != str[len]); len++);
167
168 new_str = malloc (len + 1);
169 if (NULL == new_str)
170 return NULL;
171
172 /* Make sure our string is NULL terminated */
173 new_str[len] = '\0';
174
175 return (char *) memcpy (new_str, str, len);
176}
177
178char *
179gbasename (const char *path)
180{
181 char *new_path = NULL;
182
183 if (NULL == path)
184 return NULL;
185
186 /* Copied from glibc */
187 new_path = strrchr (path, '/');
188 return new_path ? new_path + 1 : (char *) path;
189}
190
191#endif /* !OUTSIDE_LIBSANDBOX */
192 145
193// vim:noexpandtab noai:cindent ai 146// vim:noexpandtab noai:cindent ai

Legend:
Removed from v.281  
changed lines
  Added in v.309

  ViewVC Help
Powered by ViewVC 1.1.20