uboot/tools/mingw_support.c
<<
>>
Prefs
   1/*
   2 * Copyright 2008 Extreme Engineering Solutions, Inc.
   3 *
   4 * mmap/munmap implementation derived from:
   5 * Clamav Native Windows Port : mmap win32 compatibility layer
   6 * Copyright (c) 2005-2006 Gianluigi Tiesi <sherpya@netfarm.it>
   7 * Parts by Kees Zeelenberg <kzlg@users.sourceforge.net> (LibGW32C)
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Library General Public
  11 * License as published by the Free Software Foundation; either
  12 * version 2 of the License, or (at your option) any later version.
  13 *
  14 * This library is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17 * Library General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU Library General Public
  20 * License along with this software; if not, write to the
  21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22 */
  23
  24#include "mingw_support.h"
  25#include <stdio.h>
  26#include <stdint.h>
  27#include <string.h>
  28#include <errno.h>
  29#include <assert.h>
  30#include <io.h>
  31
  32int fsync(int fd)
  33{
  34        return _commit(fd);
  35}
  36
  37void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
  38{
  39        void *map = NULL;
  40        HANDLE handle = INVALID_HANDLE_VALUE;
  41        DWORD cfm_flags = 0, mvf_flags = 0;
  42
  43        switch (prot) {
  44        case PROT_READ | PROT_WRITE:
  45                cfm_flags = PAGE_READWRITE;
  46                mvf_flags = FILE_MAP_ALL_ACCESS;
  47                break;
  48        case PROT_WRITE:
  49                cfm_flags = PAGE_READWRITE;
  50                mvf_flags = FILE_MAP_WRITE;
  51                break;
  52        case PROT_READ:
  53                cfm_flags = PAGE_READONLY;
  54                mvf_flags = FILE_MAP_READ;
  55                break;
  56        default:
  57                return MAP_FAILED;
  58        }
  59
  60        handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
  61                                cfm_flags, HIDWORD(len), LODWORD(len), NULL);
  62        if (!handle)
  63                return MAP_FAILED;
  64
  65        map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
  66                        LODWORD(offset), len);
  67        CloseHandle(handle);
  68
  69        if (!map)
  70                return MAP_FAILED;
  71
  72        return map;
  73}
  74
  75int munmap(void *addr, size_t len)
  76{
  77        if (!UnmapViewOfFile(addr))
  78                return -1;
  79
  80        return 0;
  81}
  82
  83/* Reentrant string tokenizer.  Generic version.
  84   Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc.
  85   This file is part of the GNU C Library.
  86
  87   This program is free software; you can redistribute it and/or modify
  88   it under the terms of the GNU General Public License as published by
  89   the Free Software Foundation; either version 2, or (at your option)
  90   any later version.
  91
  92   This program is distributed in the hope that it will be useful,
  93   but WITHOUT ANY WARRANTY; without even the implied warranty of
  94   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  95   GNU General Public License for more details.
  96
  97   You should have received a copy of the GNU General Public License along
  98   with this program; if not, write to the Free Software Foundation,
  99   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
 100
 101/* Parse S into tokens separated by characters in DELIM.
 102   If S is NULL, the saved pointer in SAVE_PTR is used as
 103   the next starting point.  For example:
 104        char s[] = "-abc-=-def";
 105        char *sp;
 106        x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def"
 107        x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL
 108        x = strtok_r(NULL, "=", &sp);   // x = NULL
 109                // s = "abc\0-def\0"
 110*/
 111char *strtok_r(char *s, const char *delim, char **save_ptr)
 112{
 113        char *token;
 114
 115        if (s == NULL)
 116                s = *save_ptr;
 117
 118        /* Scan leading delimiters.  */
 119        s += strspn(s, delim);
 120        if (*s == '\0') {
 121                *save_ptr = s;
 122                return NULL;
 123        }
 124
 125        /* Find the end of the token.  */
 126        token = s;
 127        s = strpbrk (token, delim);
 128        if (s == NULL) {
 129                /* This token finishes the string.  */
 130                *save_ptr = memchr(token, '\0', strlen(token));
 131        } else {
 132                /* Terminate the token and make *SAVE_PTR point past it.  */
 133                *s = '\0';
 134                *save_ptr = s + 1;
 135        }
 136        return token;
 137}
 138
 139/* getline.c -- Replacement for GNU C library function getline
 140
 141Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc.
 142
 143This program is free software; you can redistribute it and/or
 144modify it under the terms of the GNU General Public License as
 145published by the Free Software Foundation; either version 2 of the
 146License, or (at your option) any later version.
 147
 148This program is distributed in the hope that it will be useful, but
 149WITHOUT ANY WARRANTY; without even the implied warranty of
 150MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 151General Public License for more details.
 152
 153You should have received a copy of the GNU General Public License
 154along with this program; if not, write to the Free Software
 155Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
 156
 157/* Written by Jan Brittenson, bson@gnu.ai.mit.edu.  */
 158
 159/* Always add at least this many bytes when extending the buffer.  */
 160#define MIN_CHUNK 64
 161
 162/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
 163   + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from
 164   malloc (or NULL), pointing to *N characters of space.  It is realloc'd
 165   as necessary.  Return the number of characters read (not including the
 166   null terminator), or -1 on error or EOF.
 167   NOTE: There is another getstr() function declared in <curses.h>.  */
 168static int getstr(char **lineptr, size_t *n, FILE *stream,
 169                  char terminator, size_t offset)
 170{
 171        int nchars_avail;       /* Allocated but unused chars in *LINEPTR.  */
 172        char *read_pos;         /* Where we're reading into *LINEPTR. */
 173        int ret;
 174
 175        if (!lineptr || !n || !stream)
 176                return -1;
 177
 178        if (!*lineptr) {
 179                *n = MIN_CHUNK;
 180                *lineptr = malloc(*n);
 181                if (!*lineptr)
 182                        return -1;
 183        }
 184
 185        nchars_avail = *n - offset;
 186        read_pos = *lineptr + offset;
 187
 188        for (;;) {
 189                register int c = getc(stream);
 190
 191                /* We always want at least one char left in the buffer, since we
 192                   always (unless we get an error while reading the first char)
 193                   NUL-terminate the line buffer.  */
 194
 195                assert(*n - nchars_avail == read_pos - *lineptr);
 196                if (nchars_avail < 2) {
 197                        if (*n > MIN_CHUNK)
 198                                *n *= 2;
 199                        else
 200                                *n += MIN_CHUNK;
 201
 202                        nchars_avail = *n + *lineptr - read_pos;
 203                        *lineptr = realloc(*lineptr, *n);
 204                        if (!*lineptr)
 205                                return -1;
 206                        read_pos = *n - nchars_avail + *lineptr;
 207                        assert(*n - nchars_avail == read_pos - *lineptr);
 208                }
 209
 210                if (c == EOF || ferror (stream)) {
 211                        /* Return partial line, if any.  */
 212                        if (read_pos == *lineptr)
 213                                return -1;
 214                        else
 215                                break;
 216                }
 217
 218                *read_pos++ = c;
 219                nchars_avail--;
 220
 221                if (c == terminator)
 222                        /* Return the line.  */
 223                        break;
 224        }
 225
 226        /* Done - NUL terminate and return the number of chars read.  */
 227        *read_pos = '\0';
 228
 229        ret = read_pos - (*lineptr + offset);
 230        return ret;
 231}
 232
 233int getline (char **lineptr, size_t *n, FILE *stream)
 234{
 235        return getstr(lineptr, n, stream, '\n', 0);
 236}
 237