xref: /freebsd/crypto/openssh/openbsd-compat/bsd-cygwin_util.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*
2  * Copyright (c) 2000, 2001, 2011, 2013 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 #define NO_BINARY_OPEN	/* Avoid redefining open to binary_open for this file */
31 #include "includes.h"
32 
33 #ifdef HAVE_CYGWIN
34 
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "xmalloc.h"
41 
42 int
43 binary_open(const char *filename, int flags, ...)
44 {
45 	va_list ap;
46 	mode_t mode;
47 
48 	va_start(ap, flags);
49 	mode = va_arg(ap, mode_t);
50 	va_end(ap);
51 	return (open(filename, flags | O_BINARY, mode));
52 }
53 
54 int
55 check_ntsec(const char *filename)
56 {
57 	return (pathconf(filename, _PC_POSIX_PERMISSIONS));
58 }
59 
60 #define NL(x) x, (sizeof (x) - 1)
61 #define WENV_SIZ (sizeof (wenv_arr) / sizeof (wenv_arr[0]))
62 
63 static struct wenv {
64 	const char *name;
65 	size_t namelen;
66 } wenv_arr[] = {
67 	{ NL("ALLUSERSPROFILE=") },
68 	{ NL("COMPUTERNAME=") },
69 	{ NL("COMSPEC=") },
70 	{ NL("CYGWIN=") },
71 	{ NL("OS=") },
72 	{ NL("PATH=") },
73 	{ NL("PATHEXT=") },
74 	{ NL("PROGRAMFILES=") },
75 	{ NL("SYSTEMDRIVE=") },
76 	{ NL("SYSTEMROOT=") },
77 	{ NL("WINDIR=") }
78 };
79 
80 char **
81 fetch_windows_environment(void)
82 {
83 	char **e, **p;
84 	unsigned int i, idx = 0;
85 
86 	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
87 	for (e = environ; *e != NULL; ++e) {
88 		for (i = 0; i < WENV_SIZ; ++i) {
89 			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
90 				p[idx++] = *e;
91 		}
92 	}
93 	p[idx] = NULL;
94 	return p;
95 }
96 
97 void
98 free_windows_environment(char **p)
99 {
100 	free(p);
101 }
102 
103 #endif /* HAVE_CYGWIN */
104