Fixed a crash caused by yadif deinterlacer on Windows XP
[vlc/solaris.git] / src / text / filesystem.c
blobf3eb1d7f4c05c68bdaa4f0af08090db5dd22c6eb
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>
33 #include <vlc_rand.h>
35 #include <assert.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
45 /**
46 * Opens a FILE pointer.
47 * @param filename file path, using UTF-8 encoding
48 * @param mode fopen file open mode
49 * @return NULL on error, an open FILE pointer on success.
51 FILE *vlc_fopen (const char *filename, const char *mode)
53 int rwflags = 0, oflags = 0;
55 for (const char *ptr = mode; *ptr; ptr++)
57 switch (*ptr)
59 case 'r':
60 rwflags = O_RDONLY;
61 break;
63 case 'a':
64 rwflags = O_WRONLY;
65 oflags |= O_CREAT | O_APPEND;
66 break;
68 case 'w':
69 rwflags = O_WRONLY;
70 oflags |= O_CREAT | O_TRUNC;
71 break;
73 case 'x':
74 oflags |= O_EXCL;
75 break;
77 case '+':
78 rwflags = O_RDWR;
79 break;
81 #ifdef O_BINARY
82 case 'b':
83 oflags = (oflags & ~O_TEXT) | O_BINARY;
84 break;
86 case 't':
87 oflags = (oflags & ~O_BINARY) | O_TEXT;
88 break;
89 #endif
93 int fd = vlc_open (filename, rwflags | oflags, 0666);
94 if (fd == -1)
95 return NULL;
97 FILE *stream = fdopen (fd, mode);
98 if (stream == NULL)
99 close (fd);
101 return stream;
105 static int dummy_select( const char *str )
107 (void)str;
108 return 1;
112 * Does the same as vlc_scandir(), but takes an open directory pointer
113 * instead of a directory path.
115 int vlc_loaddir( DIR *dir, char ***namelist,
116 int (*select)( const char * ),
117 int (*compar)( const char **, const char ** ) )
119 assert (dir);
121 if (select == NULL)
122 select = dummy_select;
124 char **tab = NULL;
125 unsigned num = 0;
127 rewinddir (dir);
129 for (unsigned size = 0;;)
131 errno = 0;
132 char *entry = vlc_readdir (dir);
133 if (entry == NULL)
135 if (errno)
136 goto error;
137 break;
140 if (!select (entry))
142 free (entry);
143 continue;
146 if (num >= size)
148 size = size ? (2 * size) : 16;
149 char **newtab = realloc (tab, sizeof (*tab) * (size));
151 if (unlikely(newtab == NULL))
153 free (entry);
154 goto error;
156 tab = newtab;
159 tab[num++] = entry;
162 if (compar != NULL)
163 qsort (tab, num, sizeof (*tab),
164 (int (*)( const void *, const void *))compar);
165 *namelist = tab;
166 return num;
168 error:
169 for (unsigned i = 0; i < num; i++)
170 free (tab[i]);
171 free (tab);
172 return -1;
176 * Selects file entries from a directory, as GNU C scandir().
178 * @param dirname UTF-8 diretory path
179 * @param pointer [OUT] pointer set, on successful completion, to the address
180 * of a table of UTF-8 filenames. All filenames must be freed with free().
181 * The table itself must be freed with free() as well.
183 * @return How many file names were selected (possibly 0),
184 * or -1 in case of error.
186 int vlc_scandir( const char *dirname, char ***namelist,
187 int (*select)( const char * ),
188 int (*compar)( const char **, const char ** ) )
190 DIR *dir = vlc_opendir (dirname);
191 int val = -1;
193 if (dir != NULL)
195 val = vlc_loaddir (dir, namelist, select, compar);
196 closedir (dir);
198 return val;
201 int vlc_mkstemp( char *template )
203 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
204 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
206 /* */
207 assert( template );
209 /* Check template validity */
210 const size_t i_length = strlen( template );
211 char *psz_rand = &template[i_length-6];
213 if( i_length < 6 || strcmp( psz_rand, "XXXXXX" ) )
215 errno = EINVAL;
216 return -1;
219 /* */
220 for( int i = 0; i < 256; i++ )
222 /* Create a pseudo random file name */
223 uint8_t pi_rand[6];
225 vlc_rand_bytes( pi_rand, sizeof(pi_rand) );
226 for( int j = 0; j < 6; j++ )
227 psz_rand[j] = digits[pi_rand[j] % i_digits];
229 /* */
230 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
231 if( fd >= 0 )
232 return fd;
233 if( errno != EEXIST )
234 return -1;
237 errno = EEXIST;
238 return -1;