1 /*****************************************************************************
2 * filesystem.c: Common file system helpers
3 *****************************************************************************
4 * Copyright (C) 2005-2006 the VideoLAN team
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
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
31 #include <vlc_common.h>
39 #include <sys/types.h>
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;
56 /* Assume binary mode by default (i.e. no translation) */
59 for (const char *ptr = mode; *ptr; ptr++)
69 oflags |= O_CREAT | O_APPEND;
74 oflags |= O_CREAT | O_TRUNC;
83 oflags = (oflags & ~O_TEXT) | O_BINARY;
87 oflags = (oflags & ~O_BINARY) | O_TEXT;
93 int fd = vlc_open (filename, rwflags | oflags, 0666);
97 FILE *stream = fdopen (fd, mode);
105 static int dummy_select( const char *str )
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 ** ) )
122 select = dummy_select;
129 for (unsigned size = 0;;)
132 char *entry = vlc_readdir (dir);
148 size = size ? (2 * size) : 16;
149 char **newtab = realloc (tab, sizeof (*tab) * (size));
151 if (unlikely(newtab == NULL))
163 qsort (tab, num, sizeof (*tab),
164 (int (*)( const void *, const void *))compar);
169 for (unsigned i = 0; i < num; i++)
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);
195 val = vlc_loaddir (dir, namelist, select, compar);
201 int vlc_mkstemp( char *template )
203 static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
204 static const int i_digits = sizeof(digits)/sizeof(*digits) - 1;
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" ) )
220 for( int i = 0; i < 256; i++ )
222 /* Create a pseudo random file name */
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];
230 int fd = vlc_open( template, O_CREAT | O_EXCL | O_RDWR, 0600 );
233 if( errno != EEXIST )