59c935e3d0cac15ce543a04d91335a8669fd3b11
[platform/upstream/busybox.git] / libbb / libc5.c
1 /* vi: set sw=4 ts=4: */
2
3
4 #include <features.h>
5 #include <string.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <paths.h>
9 #include <unistd.h>
10
11
12 #if ! defined __dietlibc__ &&  __GNU_LIBRARY__ < 5
13
14 /*
15  * Some systems already have updwtmp().  Some don't...  This is
16  * the updwtmp() implementation from uClibc, Copyright 2002 by
17  * Erik Andersen <andersee@debian.org> 
18  */
19 extern void updwtmp(const char *wtmp_file, const struct utmp *lutmp)
20 {
21         int fd;
22
23         fd = open(wtmp_file, O_APPEND | O_WRONLY, 0);
24         if (fd >= 0) {
25                 if (lockf(fd, F_LOCK, 0)==0) {
26                         write(fd, (const char *) lutmp, sizeof(struct utmp));
27                         lockf(fd, F_ULOCK, 0);
28                         close(fd);
29                 }
30         }
31 }
32
33 /* Copyright (C) 1991 Free Software Foundation, Inc.
34 This file is part of the GNU C Library.
35
36 The GNU C Library is free software; you can redistribute it and/or
37 modify it under the terms of the GNU Library General Public License as
38 published by the Free Software Foundation; either version 2 of the
39 License, or (at your option) any later version.
40
41 The GNU C Library is distributed in the hope that it will be useful,
42 but WITHOUT ANY WARRANTY; without even the implied warranty of
43 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
44 Library General Public License for more details.
45
46 You should have received a copy of the GNU Library General Public
47 License along with the GNU C Library; see the file COPYING.LIB.  If
48 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
49 Cambridge, MA 02139, USA.  */
50
51 /*
52  * Modified by Manuel Novoa III     Mar 1, 2001
53  *
54  * Converted original strtok.c code of strtok to __strtok_r.
55  * Cleaned up logic and reduced code size.
56  */
57
58
59 char *strtok_r(char *s, const char *delim, char **save_ptr)
60 {
61         char *token;
62
63         token = 0;                                      /* Initialize to no token. */
64
65         if (s == 0) {                           /* If not first time called... */
66                 s = *save_ptr;                  /* restart from where we left off. */
67         }
68         
69         if (s != 0) {                           /* If not finished... */
70                 *save_ptr = 0;
71
72                 s += strspn(s, delim);  /* Skip past any leading delimiters. */
73                 if (*s != '\0') {               /* We have a token. */
74                         token = s;
75                         *save_ptr = strpbrk(token, delim); /* Find token's end. */
76                         if (*save_ptr != 0) {
77                                 /* Terminate the token and make SAVE_PTR point past it.  */
78                                 *(*save_ptr)++ = '\0';
79                         }
80                 }
81         }
82
83         return token;
84 }
85
86 /* Basically getdelim() with the delimiter hard wired to '\n' */
87 ssize_t getline(char **linebuf, size_t *n, FILE *file)
88 {
89       return (getdelim (linebuf, n, '\n', file));
90 }
91
92
93 /*
94  * daemon implementation for uClibc
95  *
96  * Copyright (c) 1991, 1993
97  *      The Regents of the University of California.  All rights reserved.
98  *
99  * Modified for uClibc by Erik Andersen <andersee@debian.org>
100  *
101  * The uClibc Library is free software; you can redistribute it and/or
102  * modify it under the terms of the GNU Library General Public License as
103  * published by the Free Software Foundation; either version 2 of the
104  * License, or (at your option) any later version.
105  * 
106  * The GNU C Library is distributed in the hope that it will be useful,
107  * but WITHOUT ANY WARRANTY; without even the implied warranty of
108  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
109  * Library General Public License for more details.
110  * 
111  * You should have received a copy of the GNU Library General Public
112  * License along with the GNU C Library; see the file COPYING.LIB.  If not,
113  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
114  * Boston, MA 02111-1307, USA. 
115  *
116  * Original copyright notice is retained at the end of this file.
117  */
118
119 int daemon( int nochdir, int noclose )
120 {
121     int fd;
122
123     switch (fork()) {
124         case -1:
125             return(-1);
126         case 0:
127             break;
128         default:
129             _exit(0);
130     }
131
132     if (setsid() == -1)
133         return(-1);
134
135     if (!nochdir)
136         chdir("/");
137
138     if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
139         dup2(fd, STDIN_FILENO);
140         dup2(fd, STDOUT_FILENO);
141         dup2(fd, STDERR_FILENO);
142         if (fd > 2)
143             close(fd);
144     }
145     return(0);
146 }
147
148
149 /*-
150  * Copyright (c) 1990, 1993
151  *      The Regents of the University of California.  All rights reserved.
152  *
153  * Redistribution and use in source and binary forms, with or without
154  * modification, are permitted provided that the following conditions
155  * are met:
156  * 1. Redistributions of source code must retain the above copyright
157  *    notice, this list of conditions and the following disclaimer.
158  * 2. Redistributions in binary form must reproduce the above copyright
159  *    notice, this list of conditions and the following disclaimer in the
160  *    documentation and/or other materials provided with the distribution.
161  *
162  * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 
163  *              ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 
164  *
165  * 4. Neither the name of the University nor the names of its contributors
166  *    may be used to endorse or promote products derived from this software
167  *    without specific prior written permission.
168  *
169  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
170  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
172  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
173  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
174  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
175  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
176  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
177  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
178  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
179  * SUCH DAMAGE.
180  */
181
182
183 #endif  
184