• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * compat.c - Tweaks for Windows compatibility
3  *
4  * Copyright (c) 2002 Richard Russon
5  * Copyright (c) 2002-2004 Anton Altaparmakov
6  *
7  * This program/include file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program/include file is distributed in the hope that it will be
13  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program (in the main directory of the NTFS-3G
19  * distribution in the file COPYING); if not, write to the Free Software
20  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "compat.h"
28 
29 #ifndef HAVE_FFS
30 /**
31  * ffs - Find the first set bit in an int
32  * @x:
33  *
34  * Description...
35  *
36  * Returns:
37  */
ffs(int x)38 int ffs(int x)
39 {
40 	int r = 1;
41 
42 	if (!x)
43 		return 0;
44 	if (!(x & 0xffff)) {
45 		x >>= 16;
46 		r += 16;
47 	}
48 	if (!(x & 0xff)) {
49 		x >>= 8;
50 		r += 8;
51 	}
52 	if (!(x & 0xf)) {
53 		x >>= 4;
54 		r += 4;
55 	}
56 	if (!(x & 3)) {
57 		x >>= 2;
58 		r += 2;
59 	}
60 	if (!(x & 1)) {
61 		x >>= 1;
62 		r += 1;
63 	}
64 	return r;
65 }
66 #endif /* HAVE_FFS */
67 
68 #ifndef HAVE_DAEMON
69 /* ************************************************************
70  *  From: src.opensolaris.org
71  *  src/lib/libresolv2/common/bsd/daemon.c
72  */
73 /*
74  * Copyright (c) 1997-2000 by Sun Microsystems, Inc.
75  * All rights reserved.
76  */
77 
78 #if defined(LIBC_SCCS) && !defined(lint)
79 static const char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
80 static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $";
81 #endif /* LIBC_SCCS and not lint */
82 
83 /*
84  * Copyright (c) 1990, 1993
85  *	The Regents of the University of California.  All rights reserved.
86  *
87  * Redistribution and use in source and binary forms, with or without
88  * modification, are permitted provided that the following conditions
89  * are met:
90  * 1. Redistributions of source code must retain the above copyright
91  *    notice, this list of conditions and the following disclaimer.
92  * 2. Redistributions in binary form must reproduce the above copyright
93  *    notice, this list of conditions and the following disclaimer in the
94  *    documentation and/or other materials provided with the distribution.
95  * 3. All advertising materials mentioning features or use of this software
96  *    must display the following acknowledgement:
97  *	This product includes software developed by the University of
98  *	California, Berkeley and its contributors.
99  * 4. Neither the name of the University nor the names of its contributors
100  *    may be used to endorse or promote products derived from this software
101  *    without specific prior written permission.
102  *
103  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
104  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
105  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
106  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
107  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
108  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
109  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
110  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
111  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
112  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
113  * SUCH DAMAGE.
114  */
115 
116 #ifdef HAVE_FCNTL_H
117 #include <fcntl.h>
118 #endif
119 #ifdef HAVE_UNISTD_H
120 #include <unistd.h>
121 #endif
122 
daemon(int nochdir,int noclose)123 int daemon(int nochdir, int noclose) {
124 	int fd;
125 
126 	switch (fork()) {
127 	case -1:
128 		return (-1);
129 	case 0:
130 		break;
131 	default:
132 		_exit(0);
133 	}
134 
135 	if (setsid() == -1)
136 		return (-1);
137 
138 	if (!nochdir)
139 		(void)chdir("/");
140 
141 	if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
142 		(void)dup2(fd, 0);
143 		(void)dup2(fd, 1);
144 		(void)dup2(fd, 2);
145 		if (fd > 2)
146 			(void)close (fd);
147 	}
148 	return (0);
149 }
150 /*
151  *  End: src/lib/libresolv2/common/bsd/daemon.c
152  *************************************************************/
153 #endif /* HAVE_DAEMON */
154 
155 #ifndef HAVE_STRSEP
156 /* ************************************************************
157  *  From: src.opensolaris.org
158  *  src/lib/libresolv2/common/bsd/strsep.c
159  */
160 /*
161  * Copyright (c) 1997, by Sun Microsystems, Inc.
162  * All rights reserved.
163  */
164 
165 #if defined(LIBC_SCCS) && !defined(lint)
166 static const char sccsid[] = "strsep.c	8.1 (Berkeley) 6/4/93";
167 static const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $";
168 #endif /* LIBC_SCCS and not lint */
169 
170 /*
171  * Copyright (c) 1990, 1993
172  *	The Regents of the University of California.  All rights reserved.
173  *
174  * Redistribution and use in source and binary forms, with or without
175  * modification, are permitted provided that the following conditions
176  * are met:
177  * 1. Redistributions of source code must retain the above copyright
178  *    notice, this list of conditions and the following disclaimer.
179  * 2. Redistributions in binary form must reproduce the above copyright
180  *    notice, this list of conditions and the following disclaimer in the
181  *    documentation and/or other materials provided with the distribution.
182  * 3. All advertising materials mentioning features or use of this software
183  *    must display the following acknowledgement:
184  *	This product includes software developed by the University of
185  *	California, Berkeley and its contributors.
186  * 4. Neither the name of the University nor the names of its contributors
187  *    may be used to endorse or promote products derived from this software
188  *    without specific prior written permission.
189  *
190  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
191  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
194  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
195  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
196  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
197  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
198  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
200  * SUCH DAMAGE.
201  */
202 
203 #ifdef HAVE_STRING_H
204 #include <string.h>
205 #endif
206 #ifdef HAVE_STDIO_H
207 #include <stdio.h>
208 #endif
209 
210 /*
211  * Get next token from string *stringp, where tokens are possibly-empty
212  * strings separated by characters from delim.
213  *
214  * Writes NULs into the string at *stringp to end tokens.
215  * delim need not remain constant from call to call.
216  * On return, *stringp points past the last NUL written (if there might
217  * be further tokens), or is NULL (if there are definitely no more tokens).
218  *
219  * If *stringp is NULL, strsep returns NULL.
220  */
strsep(char ** stringp,const char * delim)221 char *strsep(char **stringp, const char *delim) {
222 	char *s;
223 	const char *spanp;
224 	int c, sc;
225 	char *tok;
226 
227 	if ((s = *stringp) == NULL)
228 		return (NULL);
229 	for (tok = s;;) {
230 		c = *s++;
231 		spanp = delim;
232 		do {
233 			if ((sc = *spanp++) == c) {
234 				if (c == 0)
235 					s = NULL;
236 				else
237 					s[-1] = 0;
238 				*stringp = s;
239 				return (tok);
240 			}
241 		} while (sc != 0);
242 	}
243 	/* NOTREACHED */
244 }
245 
246 /*
247  *  End: src/lib/libresolv2/common/bsd/strsep.c
248  *************************************************************/
249 #endif /* HAVE_STRSEP */
250 
251