bsd-cygwin_util.c revision 240075
1/*
2 * Copyright (c) 2000, 2001, 2011 Corinna Vinschen <vinschen@redhat.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * Created: Sat Sep 02 12:17:00 2000 cv
25 *
26 * This file contains functions for forcing opened file descriptors to
27 * binary mode on Windows systems.
28 */
29
30#include "includes.h"
31
32#ifdef HAVE_CYGWIN
33
34#if defined(open) && open == binary_open
35# undef open
36#endif
37
38#include <sys/types.h>
39
40#include <fcntl.h>
41#include <stdlib.h>
42#include <unistd.h>
43#include <windows.h>
44
45#include "xmalloc.h"
46
47int
48binary_open(const char *filename, int flags, ...)
49{
50	va_list ap;
51	mode_t mode;
52
53	va_start(ap, flags);
54	mode = va_arg(ap, mode_t);
55	va_end(ap);
56	return (open(filename, flags | O_BINARY, mode));
57}
58
59int
60check_ntsec(const char *filename)
61{
62	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
63}
64
65#define NL(x) x, (sizeof (x) - 1)
66#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
67
68static struct wenv {
69	const char *name;
70	size_t namelen;
71} wenv_arr[] = {
72	{ NL("ALLUSERSPROFILE=") },
73	{ NL("COMPUTERNAME=") },
74	{ NL("COMSPEC=") },
75	{ NL("CYGWIN=") },
76	{ NL("OS=") },
77	{ NL("PATH=") },
78	{ NL("PATHEXT=") },
79	{ NL("PROGRAMFILES=") },
80	{ NL("SYSTEMDRIVE=") },
81	{ NL("SYSTEMROOT=") },
82	{ NL("WINDIR=") }
83};
84
85char **
86fetch_windows_environment(void)
87{
88	char **e, **p;
89	unsigned int i, idx = 0;
90
91	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
92	for (e = environ; *e != NULL; ++e) {
93		for (i = 0; i < WENV_SIZ; ++i) {
94			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
95				p[idx++] = *e;
96		}
97	}
98	p[idx] = NULL;
99	return p;
100}
101
102void
103free_windows_environment(char **p)
104{
105	xfree(p);
106}
107
108#endif /* HAVE_CYGWIN */
109