125
|
1 /*
|
|
2 * osdep.c : GeeXboX uShare OS independant helpers.
|
|
3 * Originally developped for the GeeXboX project.
|
|
4 * Copyright (C) 2005-2007 Benjamin Zores <ben@geexbox.org>
|
|
5 *
|
|
6 * This program is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * This program is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU Library General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License along
|
|
17 * with this program; if not, write to the Free Software Foundation,
|
|
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
19 */
|
|
20
|
|
21 #if (defined(__unix__) || defined(unix)) && !defined(USG)
|
|
22 #include <sys/param.h>
|
|
23 #endif
|
|
24
|
|
25 #include <stdio.h>
|
|
26 #include <stdlib.h>
|
|
27 #include <string.h>
|
|
28 #include <limits.h>
|
|
29
|
|
30 #include "osdep.h"
|
|
31
|
|
32 #if (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__))
|
|
33 char *
|
|
34 strndup (const char *s, size_t n)
|
|
35 {
|
|
36 size_t len;
|
|
37 char *sdup = NULL;
|
|
38
|
|
39 if (!s)
|
|
40 return NULL;
|
|
41
|
|
42 len = strlen (s);
|
|
43 len = n < len ? n : len;
|
|
44 sdup = (char *) malloc (len + 1);
|
|
45
|
|
46 if (sdup)
|
|
47 {
|
|
48 memcpy (sdup, s, len);
|
|
49 sdup[len] = '\0';
|
|
50 }
|
|
51
|
|
52 return sdup;
|
|
53 }
|
|
54
|
|
55 ssize_t
|
|
56 getline (char **lineptr, size_t *n, FILE *stream)
|
|
57 {
|
|
58 static char line[256];
|
|
59 char *ptr;
|
|
60 ssize_t len;
|
|
61
|
|
62 if (!lineptr || !n)
|
|
63 return -1;
|
|
64
|
|
65 if (ferror (stream))
|
|
66 return -1;
|
|
67
|
|
68 if (feof (stream))
|
|
69 return -1;
|
|
70
|
|
71 fgets (line, 256, stream);
|
|
72 ptr = strchr (line, '\n');
|
|
73
|
|
74 if (ptr)
|
|
75 *ptr = '\0';
|
|
76
|
|
77 len = strlen (line);
|
|
78 if ((len + 1) < 256)
|
|
79 {
|
|
80 ptr = realloc (*lineptr, 256);
|
|
81 if (!ptr)
|
|
82 return -1;
|
|
83
|
|
84 *lineptr = ptr;
|
|
85 *n = 256;
|
|
86 }
|
|
87 strcpy (*lineptr, line);
|
|
88
|
|
89 return len;
|
|
90 }
|
|
91 #endif /* (defined(BSD) || defined(__FreeBSD__) || defined(__APPLE__)) */
|