~ubuntu-branches/ubuntu/wily/mpv/wily

« back to all changes in this revision

Viewing changes to osdep/io.c

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2013-10-16 12:38:59 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20131016123859-wj70wr6n3mzimx3e
Tags: 0.2.0-1
* New upstream release
* Install sample configuration files as examples
* Enable Lua scripting support
* Remove copyright for talloc (not used anymore)
* Update installed docs list
* Update 01_spelling.patch
* Enable VAAPI support

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
#include <io.h>
59
59
#include <fcntl.h>
60
60
 
 
61
#ifdef HAVE_PTHREADS
 
62
#include <pthread.h>
 
63
#endif
 
64
 
 
65
#include "mpvcore/mp_talloc.h"
 
66
 
61
67
//http://git.libav.org/?p=libav.git;a=blob;f=cmdutils.c;h=ade3f10ce2fc030e32e375a85fbd06c26d43a433#l161
62
68
 
63
69
static char** win32_argv_utf8;
255
261
    return res;
256
262
}
257
263
 
 
264
static char **utf8_environ;
 
265
static void *utf8_environ_ctx;
 
266
 
 
267
static void free_env(void)
 
268
{
 
269
    talloc_free(utf8_environ_ctx);
 
270
    utf8_environ_ctx = NULL;
 
271
    utf8_environ = NULL;
 
272
}
 
273
 
 
274
// Note: UNIX getenv() returns static strings, and we try to do the same. Since
 
275
// using putenv() is not multithreading safe, we don't expect env vars to change
 
276
// at runtime, and converting/allocating them in advance is ok.
 
277
static void init_getenv(void)
 
278
{
 
279
    if (utf8_environ_ctx)
 
280
        return;
 
281
    wchar_t *wenv = GetEnvironmentStringsW();
 
282
    if (!wenv)
 
283
        return;
 
284
    utf8_environ_ctx = talloc_new(NULL);
 
285
    int num_env = 0;
 
286
    while (1) {
 
287
        size_t len = wcslen(wenv);
 
288
        if (!len)
 
289
            break;
 
290
        char *s = mp_to_utf8(utf8_environ_ctx, wenv);
 
291
        MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, s);
 
292
        wenv += len + 1;
 
293
    }
 
294
    MP_TARRAY_APPEND(utf8_environ_ctx, utf8_environ, num_env, NULL);
 
295
    // Avoid showing up in leak detectors etc.
 
296
    atexit(free_env);
 
297
}
 
298
 
 
299
char *mp_getenv(const char *name)
 
300
{
 
301
#ifdef HAVE_PTHREADS
 
302
    static pthread_once_t once_init_getenv = PTHREAD_ONCE_INIT;
 
303
    pthread_once(&once_init_getenv, init_getenv);
 
304
#else
 
305
    init_getenv();
 
306
#endif
 
307
    // Copied from musl, http://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
 
308
    // Copyright © 2005-2013 Rich Felker, standard MIT license
 
309
    int i;
 
310
    size_t l = strlen(name);
 
311
    if (!utf8_environ || !*name || strchr(name, '=')) return NULL;
 
312
    for (i=0; utf8_environ[i] && (strncmp(name, utf8_environ[i], l)
 
313
            || utf8_environ[i][l] != '='); i++) {}
 
314
    if (utf8_environ[i]) return utf8_environ[i] + l+1;
 
315
    return NULL;
 
316
}
 
317
 
258
318
#endif // __MINGW32__