1987da915Sopenharmony_ci/** 2987da915Sopenharmony_ci * compat.c - Tweaks for Windows compatibility 3987da915Sopenharmony_ci * 4987da915Sopenharmony_ci * Copyright (c) 2002 Richard Russon 5987da915Sopenharmony_ci * Copyright (c) 2002-2004 Anton Altaparmakov 6987da915Sopenharmony_ci * 7987da915Sopenharmony_ci * This program/include file is free software; you can redistribute it and/or 8987da915Sopenharmony_ci * modify it under the terms of the GNU General Public License as published 9987da915Sopenharmony_ci * by the Free Software Foundation; either version 2 of the License, or 10987da915Sopenharmony_ci * (at your option) any later version. 11987da915Sopenharmony_ci * 12987da915Sopenharmony_ci * This program/include file is distributed in the hope that it will be 13987da915Sopenharmony_ci * useful, but WITHOUT ANY WARRANTY; without even the implied warranty 14987da915Sopenharmony_ci * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15987da915Sopenharmony_ci * GNU General Public License for more details. 16987da915Sopenharmony_ci * 17987da915Sopenharmony_ci * You should have received a copy of the GNU General Public License 18987da915Sopenharmony_ci * along with this program (in the main directory of the NTFS-3G 19987da915Sopenharmony_ci * distribution in the file COPYING); if not, write to the Free Software 20987da915Sopenharmony_ci * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21987da915Sopenharmony_ci */ 22987da915Sopenharmony_ci 23987da915Sopenharmony_ci#ifdef HAVE_CONFIG_H 24987da915Sopenharmony_ci#include "config.h" 25987da915Sopenharmony_ci#endif 26987da915Sopenharmony_ci 27987da915Sopenharmony_ci#include "compat.h" 28987da915Sopenharmony_ci 29987da915Sopenharmony_ci#ifndef HAVE_FFS 30987da915Sopenharmony_ci/** 31987da915Sopenharmony_ci * ffs - Find the first set bit in an int 32987da915Sopenharmony_ci * @x: 33987da915Sopenharmony_ci * 34987da915Sopenharmony_ci * Description... 35987da915Sopenharmony_ci * 36987da915Sopenharmony_ci * Returns: 37987da915Sopenharmony_ci */ 38987da915Sopenharmony_ciint ffs(int x) 39987da915Sopenharmony_ci{ 40987da915Sopenharmony_ci int r = 1; 41987da915Sopenharmony_ci 42987da915Sopenharmony_ci if (!x) 43987da915Sopenharmony_ci return 0; 44987da915Sopenharmony_ci if (!(x & 0xffff)) { 45987da915Sopenharmony_ci x >>= 16; 46987da915Sopenharmony_ci r += 16; 47987da915Sopenharmony_ci } 48987da915Sopenharmony_ci if (!(x & 0xff)) { 49987da915Sopenharmony_ci x >>= 8; 50987da915Sopenharmony_ci r += 8; 51987da915Sopenharmony_ci } 52987da915Sopenharmony_ci if (!(x & 0xf)) { 53987da915Sopenharmony_ci x >>= 4; 54987da915Sopenharmony_ci r += 4; 55987da915Sopenharmony_ci } 56987da915Sopenharmony_ci if (!(x & 3)) { 57987da915Sopenharmony_ci x >>= 2; 58987da915Sopenharmony_ci r += 2; 59987da915Sopenharmony_ci } 60987da915Sopenharmony_ci if (!(x & 1)) { 61987da915Sopenharmony_ci x >>= 1; 62987da915Sopenharmony_ci r += 1; 63987da915Sopenharmony_ci } 64987da915Sopenharmony_ci return r; 65987da915Sopenharmony_ci} 66987da915Sopenharmony_ci#endif /* HAVE_FFS */ 67987da915Sopenharmony_ci 68987da915Sopenharmony_ci#ifndef HAVE_DAEMON 69987da915Sopenharmony_ci/* ************************************************************ 70987da915Sopenharmony_ci * From: src.opensolaris.org 71987da915Sopenharmony_ci * src/lib/libresolv2/common/bsd/daemon.c 72987da915Sopenharmony_ci */ 73987da915Sopenharmony_ci/* 74987da915Sopenharmony_ci * Copyright (c) 1997-2000 by Sun Microsystems, Inc. 75987da915Sopenharmony_ci * All rights reserved. 76987da915Sopenharmony_ci */ 77987da915Sopenharmony_ci 78987da915Sopenharmony_ci#if defined(LIBC_SCCS) && !defined(lint) 79987da915Sopenharmony_cistatic const char sccsid[] = "@(#)daemon.c 8.1 (Berkeley) 6/4/93"; 80987da915Sopenharmony_cistatic const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $"; 81987da915Sopenharmony_ci#endif /* LIBC_SCCS and not lint */ 82987da915Sopenharmony_ci 83987da915Sopenharmony_ci/* 84987da915Sopenharmony_ci * Copyright (c) 1990, 1993 85987da915Sopenharmony_ci * The Regents of the University of California. All rights reserved. 86987da915Sopenharmony_ci * 87987da915Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 88987da915Sopenharmony_ci * modification, are permitted provided that the following conditions 89987da915Sopenharmony_ci * are met: 90987da915Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 91987da915Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 92987da915Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 93987da915Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 94987da915Sopenharmony_ci * documentation and/or other materials provided with the distribution. 95987da915Sopenharmony_ci * 3. All advertising materials mentioning features or use of this software 96987da915Sopenharmony_ci * must display the following acknowledgement: 97987da915Sopenharmony_ci * This product includes software developed by the University of 98987da915Sopenharmony_ci * California, Berkeley and its contributors. 99987da915Sopenharmony_ci * 4. Neither the name of the University nor the names of its contributors 100987da915Sopenharmony_ci * may be used to endorse or promote products derived from this software 101987da915Sopenharmony_ci * without specific prior written permission. 102987da915Sopenharmony_ci * 103987da915Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 104987da915Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 105987da915Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 106987da915Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 107987da915Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 108987da915Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 109987da915Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 110987da915Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 111987da915Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 112987da915Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 113987da915Sopenharmony_ci * SUCH DAMAGE. 114987da915Sopenharmony_ci */ 115987da915Sopenharmony_ci 116987da915Sopenharmony_ci#ifdef HAVE_FCNTL_H 117987da915Sopenharmony_ci#include <fcntl.h> 118987da915Sopenharmony_ci#endif 119987da915Sopenharmony_ci#ifdef HAVE_UNISTD_H 120987da915Sopenharmony_ci#include <unistd.h> 121987da915Sopenharmony_ci#endif 122987da915Sopenharmony_ci 123987da915Sopenharmony_ciint daemon(int nochdir, int noclose) { 124987da915Sopenharmony_ci int fd; 125987da915Sopenharmony_ci 126987da915Sopenharmony_ci switch (fork()) { 127987da915Sopenharmony_ci case -1: 128987da915Sopenharmony_ci return (-1); 129987da915Sopenharmony_ci case 0: 130987da915Sopenharmony_ci break; 131987da915Sopenharmony_ci default: 132987da915Sopenharmony_ci _exit(0); 133987da915Sopenharmony_ci } 134987da915Sopenharmony_ci 135987da915Sopenharmony_ci if (setsid() == -1) 136987da915Sopenharmony_ci return (-1); 137987da915Sopenharmony_ci 138987da915Sopenharmony_ci if (!nochdir) 139987da915Sopenharmony_ci (void)chdir("/"); 140987da915Sopenharmony_ci 141987da915Sopenharmony_ci if (!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1) { 142987da915Sopenharmony_ci (void)dup2(fd, 0); 143987da915Sopenharmony_ci (void)dup2(fd, 1); 144987da915Sopenharmony_ci (void)dup2(fd, 2); 145987da915Sopenharmony_ci if (fd > 2) 146987da915Sopenharmony_ci (void)close (fd); 147987da915Sopenharmony_ci } 148987da915Sopenharmony_ci return (0); 149987da915Sopenharmony_ci} 150987da915Sopenharmony_ci/* 151987da915Sopenharmony_ci * End: src/lib/libresolv2/common/bsd/daemon.c 152987da915Sopenharmony_ci *************************************************************/ 153987da915Sopenharmony_ci#endif /* HAVE_DAEMON */ 154987da915Sopenharmony_ci 155987da915Sopenharmony_ci#ifndef HAVE_STRSEP 156987da915Sopenharmony_ci/* ************************************************************ 157987da915Sopenharmony_ci * From: src.opensolaris.org 158987da915Sopenharmony_ci * src/lib/libresolv2/common/bsd/strsep.c 159987da915Sopenharmony_ci */ 160987da915Sopenharmony_ci/* 161987da915Sopenharmony_ci * Copyright (c) 1997, by Sun Microsystems, Inc. 162987da915Sopenharmony_ci * All rights reserved. 163987da915Sopenharmony_ci */ 164987da915Sopenharmony_ci 165987da915Sopenharmony_ci#if defined(LIBC_SCCS) && !defined(lint) 166987da915Sopenharmony_cistatic const char sccsid[] = "strsep.c 8.1 (Berkeley) 6/4/93"; 167987da915Sopenharmony_cistatic const char rcsid[] = "$Id: compat.c,v 1.1.1.1.2.1 2008-08-16 15:17:44 jpandre Exp $"; 168987da915Sopenharmony_ci#endif /* LIBC_SCCS and not lint */ 169987da915Sopenharmony_ci 170987da915Sopenharmony_ci/* 171987da915Sopenharmony_ci * Copyright (c) 1990, 1993 172987da915Sopenharmony_ci * The Regents of the University of California. All rights reserved. 173987da915Sopenharmony_ci * 174987da915Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 175987da915Sopenharmony_ci * modification, are permitted provided that the following conditions 176987da915Sopenharmony_ci * are met: 177987da915Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 178987da915Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 179987da915Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 180987da915Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 181987da915Sopenharmony_ci * documentation and/or other materials provided with the distribution. 182987da915Sopenharmony_ci * 3. All advertising materials mentioning features or use of this software 183987da915Sopenharmony_ci * must display the following acknowledgement: 184987da915Sopenharmony_ci * This product includes software developed by the University of 185987da915Sopenharmony_ci * California, Berkeley and its contributors. 186987da915Sopenharmony_ci * 4. Neither the name of the University nor the names of its contributors 187987da915Sopenharmony_ci * may be used to endorse or promote products derived from this software 188987da915Sopenharmony_ci * without specific prior written permission. 189987da915Sopenharmony_ci * 190987da915Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 191987da915Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 192987da915Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 193987da915Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 194987da915Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 195987da915Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 196987da915Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 197987da915Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 198987da915Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 199987da915Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 200987da915Sopenharmony_ci * SUCH DAMAGE. 201987da915Sopenharmony_ci */ 202987da915Sopenharmony_ci 203987da915Sopenharmony_ci#ifdef HAVE_STRING_H 204987da915Sopenharmony_ci#include <string.h> 205987da915Sopenharmony_ci#endif 206987da915Sopenharmony_ci#ifdef HAVE_STDIO_H 207987da915Sopenharmony_ci#include <stdio.h> 208987da915Sopenharmony_ci#endif 209987da915Sopenharmony_ci 210987da915Sopenharmony_ci/* 211987da915Sopenharmony_ci * Get next token from string *stringp, where tokens are possibly-empty 212987da915Sopenharmony_ci * strings separated by characters from delim. 213987da915Sopenharmony_ci * 214987da915Sopenharmony_ci * Writes NULs into the string at *stringp to end tokens. 215987da915Sopenharmony_ci * delim need not remain constant from call to call. 216987da915Sopenharmony_ci * On return, *stringp points past the last NUL written (if there might 217987da915Sopenharmony_ci * be further tokens), or is NULL (if there are definitely no more tokens). 218987da915Sopenharmony_ci * 219987da915Sopenharmony_ci * If *stringp is NULL, strsep returns NULL. 220987da915Sopenharmony_ci */ 221987da915Sopenharmony_cichar *strsep(char **stringp, const char *delim) { 222987da915Sopenharmony_ci char *s; 223987da915Sopenharmony_ci const char *spanp; 224987da915Sopenharmony_ci int c, sc; 225987da915Sopenharmony_ci char *tok; 226987da915Sopenharmony_ci 227987da915Sopenharmony_ci if ((s = *stringp) == NULL) 228987da915Sopenharmony_ci return (NULL); 229987da915Sopenharmony_ci for (tok = s;;) { 230987da915Sopenharmony_ci c = *s++; 231987da915Sopenharmony_ci spanp = delim; 232987da915Sopenharmony_ci do { 233987da915Sopenharmony_ci if ((sc = *spanp++) == c) { 234987da915Sopenharmony_ci if (c == 0) 235987da915Sopenharmony_ci s = NULL; 236987da915Sopenharmony_ci else 237987da915Sopenharmony_ci s[-1] = 0; 238987da915Sopenharmony_ci *stringp = s; 239987da915Sopenharmony_ci return (tok); 240987da915Sopenharmony_ci } 241987da915Sopenharmony_ci } while (sc != 0); 242987da915Sopenharmony_ci } 243987da915Sopenharmony_ci /* NOTREACHED */ 244987da915Sopenharmony_ci} 245987da915Sopenharmony_ci 246987da915Sopenharmony_ci/* 247987da915Sopenharmony_ci * End: src/lib/libresolv2/common/bsd/strsep.c 248987da915Sopenharmony_ci *************************************************************/ 249987da915Sopenharmony_ci#endif /* HAVE_STRSEP */ 250987da915Sopenharmony_ci 251