Author: mturk
Date: Fri Apr 29 15:45:50 2011
New Revision: 1097849

URL: http://svn.apache.org/viewvc?rev=1097849&view=rev
Log:
Add common posix wrappers

Added:
    commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c   (with 
props)
    commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c   (with 
props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/port.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in?rev=1097849&r1=1097848&r2=1097849&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.msc.in Fri Apr 29 
15:45:50 2011
@@ -75,12 +75,14 @@ ZLIB_SOURCES=\
 ASMSOURCES=
 
 WIN32_SOURCES=\
+       $(TOPDIR)\os\win32\dirent.c \
        $(TOPDIR)\os\win32\dso.c \
        $(TOPDIR)\os\win32\exec.c \
        $(TOPDIR)\os\win32\execmem.c \
        $(TOPDIR)\os\win32\init.c \
        $(TOPDIR)\os\win32\os.c \
        $(TOPDIR)\os\win32\platform.c \
+       $(TOPDIR)\os\win32\posix.c \
        $(TOPDIR)\os\win32\procmutex.c \
        $(TOPDIR)\os\win32\security.c \
        $(TOPDIR)\os\win32\semaphore.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/port.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/port.h?rev=1097849&r1=1097848&r2=1097849&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/port.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/port.h Fri Apr 29 
15:45:50 2011
@@ -18,6 +18,33 @@
 
 #include "acr/stdtypes.h"
 
+#if defined(WINDOWS)
+typedef struct dirent    struct_dirent;
+struct dirent {
+    acr_u64_t        d_ino;        /* inode number */
+    off_t            d_off;        /* offset to the next dirent */
+    unsigned short   d_reclen;     /* length of this record (name) */
+    mode_t           d_type;       /* attributes of a file */
+    wchar_t          d_name[1024]; /* extended from MAX_PATH
+                                    */
+};
+
+typedef struct DIR {
+    HANDLE           d_find;       /* Handle to FindFirstFile */
+    off_t            d_stat;       /* Status of search
+                                    *  0 -> Not started yet
+                                    * -1 -> EOF
+                                    * >0 -> o based index of next entry.
+                                    */
+
+    struct dirent    d_dir;
+    wchar_t          d_name[8192];
+    DWORD            d_star;       /* internal position where star is
+                                    * inserted during readdir
+                                    */
+} DIR;
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -53,6 +80,18 @@ unsigned int _bsd_arc4random(void);
 # define arc4random     _bsd_arc4random
 #endif
 
+#if defined(WINDOWS)
+
+DIR           *opendir(const wchar_t *path);
+int            closedir(DIR *dir);
+void           rewinddir(DIR *dir);
+off_t          telldir(DIR *dir);
+int            readdir_r(DIR *dir, struct dirent *entry, struct dirent 
**result);
+struct dirent *readdir(DIR *dir);
+void           seekdir(DIR *dir, off_t pos);
+
+#endif
+
 #ifdef __cplusplus
 }
 #endif

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h?rev=1097849&r1=1097848&r2=1097849&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_defs.h Fri Apr 
29 15:45:50 2011
@@ -19,6 +19,16 @@
 
 #include "acr/config.h"
 
+#define DT_UNKNOWN           0
+#define DT_FIFO              1
+#define DT_CHR               2
+#define DT_DIR               4
+#define DT_BLK               6
+#define DT_REG               8
+#define DT_LNK              10
+#define DT_SOCK             12
+#define DT_WHT              14
+
 #define ACR_I64_T_FMT           "I64d"
 #define ACR_U64_T_FMT           "I64u"
 #define ACR_X64_T_FMT           "I64x"

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h?rev=1097849&r1=1097848&r2=1097849&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h 
(original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/arch_opts.h Fri Apr 
29 15:45:50 2011
@@ -235,5 +235,8 @@ ACR_INLINE(void) MsecTimeToFileTime(LPFI
 
 HANDLE  AcrNullPipe(int flags, int fd);
 int     AcrPipePair(HANDLE *rd, HANDLE *wr, int flags, char *name);
+size_t  wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz);
+size_t  wcslcat(wchar_t *dst, const wchar_t *src, size_t siz);
+const wchar_t *basename_w(const wchar_t *path);
 
 #endif /* _ACR_ARCH_OPTS_H_ */

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c?rev=1097849&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c Fri Apr 29 
15:45:50 2011
@@ -0,0 +1,230 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/error.h"
+#include "acr/memory.h"
+#include "acr/iodefs.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+
+/*
+ * dirent.c
+ *
+ * Derived from DIRLIB.C by Matt J. Weinstein
+ * This note appears in the DIRLIB.H
+ * DIRLIB.H by M. J. Weinstein   Released to public domain 1-Jan-89
+ *
+ */
+
+static int ftype0(DWORD attrs)
+{
+    int type = DT_REG;
+
+    if (attrs & FILE_ATTRIBUTE_DIRECTORY)
+        type = DT_DIR;
+    else if (attrs & FILE_ATTRIBUTE_REPARSE_POINT)
+        type = DT_LNK;
+    else if (attrs & FILE_ATTRIBUTE_DEVICE)
+        type = DT_CHR;
+    return type;
+}
+
+DIR *opendir(const wchar_t *path)
+{
+    DIR     *dir;
+    DWORD    attrs;
+    DWORD    len;
+
+    if ((attrs = GetFileAttributesW(path)) == INVALID_FILE_ATTRIBUTES)
+        return 0;
+    if (!(attrs & FILE_ATTRIBUTE_DIRECTORY)) {
+        /* Not a directory */
+        ACR_SET_OS_ERROR(ACR_ENOTDIR);
+        return 0;
+    }
+    if ((dir = calloc(1, sizeof(DIR))) == 0) {
+        ACR_SET_OS_ERROR(ACR_ENOMEM);
+        return 0;
+    }
+    len = (DWORD)wcslcpy(dir->d_name, path, 8188);
+    if (len > 8188) {
+        AcrFree(dir);
+        ACR_SET_OS_ERROR(ACR_ERANGE);
+        return 0;
+    }
+    if (dir->d_name[len-2] != L'\\')
+        dir->d_name[len]    = L'\\';
+    dir->d_star = len;
+    dir->d_find = INVALID_HANDLE_VALUE;
+    dir->d_stat = 0;
+
+    return dir;
+}
+
+int closedir(DIR *dir)
+{
+    int rc = 0;
+    if (dir == 0) {
+        ACR_SET_OS_ERROR(ACR_EFAULT);
+        return -1;
+    }
+
+    if (dir->d_find != INVALID_HANDLE_VALUE) {
+        if (!FindClose(dir->d_find))
+            rc = ACR_GET_OS_ERROR();
+    }
+    AcrFree(dir);
+    if (rc != 0) {
+        ACR_SET_OS_ERROR(rc);
+        return -1;
+    }
+    else
+        return 0;
+}
+
+void rewinddir(DIR *dir)
+{
+    if (dir == 0) {
+        ACR_SET_OS_ERROR(ACR_EFAULT);
+        return;
+    }
+
+    if (dir->d_find != INVALID_HANDLE_VALUE)
+        FindClose(dir->d_find);
+
+    dir->d_find = INVALID_HANDLE_VALUE;
+    dir->d_stat = 0;
+}
+
+off_t telldir(DIR *dir)
+{
+    if (dir == 0) {
+        ACR_SET_OS_ERROR(ACR_EFAULT);
+        return -1;
+    }
+
+    return dir->d_stat;
+}
+
+int readdir_r(DIR *dir, struct dirent *entry, struct dirent **result)
+{
+    DWORD rc = 0;
+    WIN32_FIND_DATAW wfind;
+
+    if (dir == 0) {
+        return ACR_EFAULT;
+    }
+    if (dir->d_stat < 0) {
+        /* Already at the end
+         */
+        *result = 0;
+        return 0;
+    }
+    else if (dir->d_stat == 0) {
+        /* Start the search
+         */
+        dir->d_name[dir->d_star] = L'*';
+        dir->d_find = FindFirstFileW(dir->d_name, &wfind);
+        dir->d_name[dir->d_star] = 0;
+        if (dir->d_find == INVALID_HANDLE_VALUE) {
+            /* No files in the dir?
+             */
+            dir->d_stat = -1;
+        }
+        else
+            dir->d_stat =  1;
+    }
+    else {
+        /* Find the next entry
+         */
+        if (FindNextFileW(dir->d_find, &wfind)) {
+            /* Increment position counter
+             */
+            dir->d_stat++;
+        }
+        else {
+            rc = GetLastError();
+            if (rc == ERROR_NO_MORE_FILES)
+                rc = 0;
+            /* EOF or error
+             * Close the search handle
+             */
+            FindClose(dir->d_find);
+            dir->d_find = INVALID_HANDLE_VALUE;
+            dir->d_stat = -1;
+        }
+    }
+    if (dir->d_stat > 0) {
+        size_t  nlen;
+        LPCWSTR wname = wfind.cFileName;
+        /* Fill in the dirent structure
+         */
+        nlen = wcslcpy(entry->d_name, wname, 1024);
+        entry->d_ino    = 0;
+        entry->d_type   = ftype0(wfind.dwFileAttributes);
+        entry->d_off    = dir->d_stat;
+        entry->d_reclen = (unsigned short)(nlen - 1);
+        *result = entry;
+    }
+    else
+        *result = 0;
+    return rc;
+}
+
+static ACR_THREAD struct dirent *_readdir_entry = 0;
+struct dirent *readdir(DIR *dir)
+{
+    int rc;
+
+    if (dir == 0) {
+        ACR_SET_OS_ERROR(ACR_EFAULT);
+        return 0;
+    }
+
+    rc = readdir_r(dir, &dir->d_dir, &_readdir_entry);
+    ACR_SET_ERRNO_IF(rc);
+
+    return _readdir_entry;
+}
+
+void seekdir(DIR *dir, off_t pos)
+{
+    if (dir == 0) {
+        ACR_SET_OS_ERROR(ACR_EFAULT);
+        return;
+    }
+    if (pos < -1) {
+        ACR_SET_OS_ERROR(ACR_EINVAL);
+        return;
+    }
+    else if (pos == -1) {
+        /* Seek past end */
+        if (dir->d_find != INVALID_HANDLE_VALUE)
+            FindClose(dir->d_find);
+
+        dir->d_find = INVALID_HANDLE_VALUE;
+        dir->d_stat = -1;
+    }
+    else {
+        rewinddir(dir);
+
+        while (dir->d_stat < pos) {
+            if (readdir(dir) == 0)
+                break;
+        }
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/dirent.c
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c?rev=1097849&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c Fri Apr 29 
15:45:50 2011
@@ -0,0 +1,113 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/error.h"
+#include "acr/memory.h"
+#include "acr/iodefs.h"
+#include "acr/port.h"
+#include "arch_opts.h"
+
+#define IS_PSW(c) ((c) ==  L'/' || (c) ==  L'\\')
+#define NO_PSW(c) ((c) !=  L'/' && (c) !=  L'\\')
+
+const wchar_t *basename_w(const wchar_t *path)
+{
+    const wchar_t *endp, *startp;
+
+    /* Empty or 0 string gets treated as "." */
+    if (path == 0 || *path == '\0')
+        return L".";
+    /* Strip any trailing slashes */
+    endp = path + wcslen(path) - 1;
+    while (endp > path && IS_PSW(*endp))
+        endp--;
+
+    /* All slashes becomes "/" */
+    if (endp == path && IS_PSW(*endp))
+        return L"/";
+    /* Find the start of the base */
+    startp = endp;
+    while (startp > path && NO_PSW(*(startp - 1)))
+        startp--;
+
+    return startp;
+}
+
+/*
+ * Copy src to string dst of size siz.  At most siz-1 characters
+ * will be copied.  Always NUL terminates (unless siz == 0).
+ * Returns wcslen(src); if retval >= siz, truncation occurred.
+ */
+size_t
+wcslcpy(wchar_t *dst, const wchar_t *src, size_t siz)
+{
+    wchar_t *d = dst;
+    const wchar_t *s = src;
+    size_t n = siz;
+
+    /* Copy as many bytes as will fit */
+    if (n != 0) {
+        while (--n != 0) {
+            if ((*d++ = *s++) == L'\0')
+                break;
+        }
+    }
+
+    /* Not enough room in dst, add NUL and traverse rest of src */
+    if (n == 0) {
+        if (siz != 0)
+            *d = L'\0';      /* NUL-terminate dst */
+        while (*s++)
+            ;
+    }
+
+    return (s - src - 1);   /* count does not include NUL */
+}
+
+/*
+ * Appends src to string dst of size siz (unlike wcsncat, siz is the
+ * full size of dst, not space left).  At most siz-1 characters
+ * will be copied.  Always NUL terminates (unless siz == 0).
+ * Returns wcslen(initial dst) + wcslen(src); if retval >= siz,
+ * truncation occurred.
+ */
+size_t
+wcslcat(wchar_t *dst, const wchar_t *src, size_t siz)
+{
+    wchar_t *d = dst;
+    const wchar_t *s = src;
+    size_t n = siz;
+    size_t dlen;
+
+    /* Find the end of dst and adjust bytes left but don't go past end */
+    while (n-- != 0 && *d != L'\0')
+        d++;
+    dlen = d - dst;
+    n = siz - dlen;
+
+    if (n == 0)
+        return(dlen + wcslen(s));
+    while (*s != L'\0') {
+        if (n != 1) {
+            *d++ = *s;
+            n--;
+        }
+        s++;
+    }
+    *d = L'\0';
+
+    return(dlen + (s - src));   /* count does not include NUL */
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to