demux: adaptive: handle obsolete http header line folding
[vlc.git] / src / text / filesystem.c
blob9917e30f918255bdc215f878ec93c7c99e706358
1 /*****************************************************************************
2 * filesystem.c: Common file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VLC authors and VideoLAN
5 * Copyright © 2005-2008 Rémi Denis-Courmont
7 * Authors: Rémi Denis-Courmont <rem # videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_fs.h>
34 #include <assert.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <sys/types.h>
39 #include <fcntl.h>
40 #include <unistd.h>
42 /**
43 * Opens a FILE pointer.
44 * @param filename file path, using UTF-8 encoding
45 * @param mode fopen file open mode
46 * @return NULL on error, an open FILE pointer on success.
48 FILE *vlc_fopen (const char *filename, const char *mode)
50 int rwflags = 0, oflags = 0;
52 for (const char *ptr = mode; *ptr; ptr++)
54 switch (*ptr)
56 case 'r':
57 rwflags = O_RDONLY;
58 break;
60 case 'a':
61 rwflags = O_WRONLY;
62 oflags |= O_CREAT | O_APPEND;
63 break;
65 case 'w':
66 rwflags = O_WRONLY;
67 oflags |= O_CREAT | O_TRUNC;
68 break;
70 case 'x':
71 oflags |= O_EXCL;
72 break;
74 case '+':
75 rwflags = O_RDWR;
76 break;
78 #ifdef O_BINARY
79 case 'b':
80 oflags = (oflags & ~O_TEXT) | O_BINARY;
81 break;
83 case 't':
84 oflags = (oflags & ~O_BINARY) | O_TEXT;
85 break;
86 #endif
90 int fd = vlc_open (filename, rwflags | oflags, 0666);
91 if (fd == -1)
92 return NULL;
94 FILE *stream = fdopen (fd, mode);
95 if (stream == NULL)
96 vlc_close (fd);
98 return stream;
102 static int dummy_select( const char *str )
104 (void)str;
105 return 1;
109 * Does the same as vlc_scandir(), but takes an open directory pointer
110 * instead of a directory path.
112 int vlc_loaddir( DIR *dir, char ***namelist,
113 int (*select)( const char * ),
114 int (*compar)( const char **, const char ** ) )
116 assert (dir);
118 if (select == NULL)
119 select = dummy_select;
121 char **tab = NULL;
122 unsigned num = 0;
124 rewinddir (dir);
126 for (unsigned size = 0;;)
128 errno = 0;
129 const char *entry = vlc_readdir (dir);
130 if (entry == NULL)
132 if (errno)
133 goto error;
134 break;
137 if (!select (entry))
138 continue;
140 if (num >= size)
142 size = size ? (2 * size) : 16;
143 char **newtab = realloc (tab, sizeof (*tab) * (size));
145 if (unlikely(newtab == NULL))
146 goto error;
147 tab = newtab;
150 tab[num] = strdup(entry);
151 if (likely(tab[num] != NULL))
152 num++;
155 if (compar != NULL && num > 0)
156 qsort (tab, num, sizeof (*tab),
157 (int (*)( const void *, const void *))compar);
158 *namelist = tab;
159 return num;
161 error:
162 for (unsigned i = 0; i < num; i++)
163 free (tab[i]);
164 free (tab);
165 return -1;
169 * Selects file entries from a directory, as GNU C scandir().
171 * @param dirname UTF-8 diretory path
172 * @param pointer [OUT] pointer set, on successful completion, to the address
173 * of a table of UTF-8 filenames. All filenames must be freed with free().
174 * The table itself must be freed with free() as well.
176 * @return How many file names were selected (possibly 0),
177 * or -1 in case of error.
179 int vlc_scandir( const char *dirname, char ***namelist,
180 int (*select)( const char * ),
181 int (*compar)( const char **, const char ** ) )
183 DIR *dir = vlc_opendir (dirname);
184 int val = -1;
186 if (dir != NULL)
188 val = vlc_loaddir (dir, namelist, select, compar);
189 closedir (dir);
191 return val;
194 #if defined (_WIN32) || defined (__OS2__)
195 # include <vlc_rand.h>
197 int vlc_mkstemp( char *template )
199 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
200 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
202 /* */
203 assert( template );
205 /* Check template validity */
206 const size_t i_length = strlen( template );
207 char *psz_rand = &template[i_length-6];
209 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
211 errno = EINVAL;
212 return -1;
215 /* */
216 for( int i = 0; i < 256; i++ )
218 /* Create a pseudo random file name */
219 uint8_t pi_rand[6];
221 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
222 for( int j = 0; j < 6; j++ )
223 psz_rand[j] = digits[pi_rand[j] % i_digits];
225 /* */
226 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
227 if( fd >= 0 )
228 return fd;
229 if( errno != EEXIST )
230 return -1;
233 errno = EEXIST;
234 return -1;
236 #endif