Home | History | Annotate | Download | only in openbsd-compat
      1 /*
      2  * Copyright (c) 2000, 2001, 2011 Corinna Vinschen <vinschen (at) 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 
     47 int
     48 binary_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 
     59 int
     60 check_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 
     68 static 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("SYSTEMDRIVE=") },
     80 	{ NL("SYSTEMROOT=") },
     81 	{ NL("WINDIR=") }
     82 };
     83 
     84 char **
     85 fetch_windows_environment(void)
     86 {
     87 	char **e, **p;
     88 	unsigned int i, idx = 0;
     89 
     90 	p = xcalloc(WENV_SIZ + 1, sizeof(char *));
     91 	for (e = environ; *e != NULL; ++e) {
     92 		for (i = 0; i < WENV_SIZ; ++i) {
     93 			if (!strncmp(*e, wenv_arr[i].name, wenv_arr[i].namelen))
     94 				p[idx++] = *e;
     95 		}
     96 	}
     97 	p[idx] = NULL;
     98 	return p;
     99 }
    100 
    101 void
    102 free_windows_environment(char **p)
    103 {
    104 	xfree(p);
    105 }
    106 
    107 #endif /* HAVE_CYGWIN */
    108