198937Sdes/*
2251135Sdes * Copyright (c) 2000, 2001, 2011, 2013 Corinna Vinschen <vinschen@redhat.com>
398937Sdes *
498937Sdes * Redistribution and use in source and binary forms, with or without
598937Sdes * modification, are permitted provided that the following conditions
698937Sdes * are met:
798937Sdes * 1. Redistributions of source code must retain the above copyright
898937Sdes *    notice, this list of conditions and the following disclaimer.
998937Sdes * 2. Redistributions in binary form must reproduce the above copyright
1098937Sdes *    notice, this list of conditions and the following disclaimer in the
1198937Sdes *    documentation and/or other materials provided with the distribution.
1298937Sdes *
1398937Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1498937Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1598937Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1698937Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1798937Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1898937Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1998937Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2098937Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2198937Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2298937Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2398937Sdes *
2498937Sdes * Created: Sat Sep 02 12:17:00 2000 cv
2598937Sdes *
2698937Sdes * This file contains functions for forcing opened file descriptors to
2798937Sdes * binary mode on Windows systems.
2898937Sdes */
2998937Sdes
30251135Sdes#define NO_BINARY_OPEN	/* Avoid redefining open to binary_open for this file */
3198937Sdes#include "includes.h"
3298937Sdes
3398937Sdes#ifdef HAVE_CYGWIN
3498937Sdes
35162852Sdes#include <sys/types.h>
3698937Sdes#include <fcntl.h>
37251135Sdes#include <string.h>
38162852Sdes#include <unistd.h>
39162852Sdes
40146998Sdes#include "xmalloc.h"
4198937Sdes
42124208Sdesint
43124208Sdesbinary_open(const char *filename, int flags, ...)
4498937Sdes{
4598937Sdes	va_list ap;
4698937Sdes	mode_t mode;
4798937Sdes
4898937Sdes	va_start(ap, flags);
4998937Sdes	mode = va_arg(ap, mode_t);
5098937Sdes	va_end(ap);
51124208Sdes	return (open(filename, flags | O_BINARY, mode));
5298937Sdes}
5398937Sdes
54124208Sdesint
55124208Sdescheck_ntsec(const char *filename)
5698937Sdes{
57181111Sdes	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
5898937Sdes}
5998937Sdes
60146998Sdes#define NL(x) x, (sizeof (x) - 1)
61146998Sdes#define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
62146998Sdes
63146998Sdesstatic struct wenv {
64146998Sdes	const char *name;
65146998Sdes	size_t namelen;
66146998Sdes} wenv_arr[] = {
67146998Sdes	{ NL("ALLUSERSPROFILE=") },
68146998Sdes	{ NL("COMPUTERNAME=") },
69146998Sdes	{ NL("COMSPEC=") },
70147001Sdes	{ NL("CYGWIN=") },
71146998Sdes	{ NL("OS=") },
72146998Sdes	{ NL("PATH=") },
73146998Sdes	{ NL("PATHEXT=") },
74247485Sdes	{ NL("PROGRAMFILES=") },
75146998Sdes	{ NL("SYSTEMDRIVE=") },
76146998Sdes	{ NL("SYSTEMROOT=") },
77147001Sdes	{ NL("WINDIR=") }
78146998Sdes};
79146998Sdes
80146998Sdeschar **
81146998Sdesfetch_windows_environment(void)
82146998Sdes{
83146998Sdes	char **e, **p;
84162852Sdes	unsigned int i, idx = 0;
85146998Sdes
86162852Sdes	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
87146998Sdes	for (e = environ; *e != NULL; ++e) {
88146998Sdes		for (i = 0; i < WENV_SIZ; ++i) {
89146998Sdes			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
90146998Sdes				p[idx++] = *e;
91146998Sdes		}
92146998Sdes	}
93146998Sdes	p[idx] = NULL;
94146998Sdes	return p;
95146998Sdes}
96146998Sdes
97146998Sdesvoid
98146998Sdesfree_windows_environment(char **p)
99146998Sdes{
100263970Sdes	free(p);
101146998Sdes}
102146998Sdes
10398937Sdes#endif /* HAVE_CYGWIN */
104