os/ossrv/genericopenlibs/openenvcore/libc/src/opendir.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // 
    15 //
    16 
    17 #include <unistd.h>
    18 #include <errno.h>
    19 #include "sysreent.h"
    20 #include <dirent.h>
    21 #include <utf.h>
    22 #include <stddef.h>
    23 #include <string.h>
    24 #include <stdlib.h>
    25 #include <wchar.h>
    26 #include <sys/errno.h>
    27 #include "sysif.h"
    28 
    29 #define	MAXPATHLEN	260	/* E32STD.H: KMaxFullName + 4 to avoid data loss */
    30 
    31 extern "C" {
    32 /*
    33 Opens a directory.
    34 */
    35 EXPORT_C DIR* opendir(const char* _path)
    36 	{
    37 	wchar_t _wpath[MAXPATHLEN+1];
    38 	
    39 	if (mbstowcs(_wpath, _path, MAXPATHLEN) == (size_t)-1)
    40 		{
    41 		errno = EILSEQ;
    42 		return 0;
    43 		}
    44 	return _opendir_r(&errno, _wpath);
    45 	}
    46 
    47 /* 
    48 A wide-character version of opendir()
    49 */
    50 EXPORT_C WDIR* wopendir(const wchar_t* _wpath)
    51 	{
    52 	if (!_wpath)
    53 		{
    54 		errno = ENOENT;
    55 		return NULL;
    56 		}
    57 	return (WDIR*)_opendir_r(&errno, _wpath);
    58 	}
    59 
    60 EXPORT_C struct dirent* readdir(DIR* dp)
    61 	{
    62 	return _readdir_r(&errno, dp);
    63 	}
    64 
    65 EXPORT_C struct wdirent* wreaddir(WDIR* wdp)
    66 	{
    67 	if (!wdp)
    68 		{
    69 		errno = EBADF;
    70 		return NULL;
    71 		}
    72 	return _wreaddir_r(wdp);
    73 	}
    74 
    75 /*
    76 Sets the position (associated with the directory stream that dirp points to) 
    77 to the beginning of the directory. 
    78 Additionally, it causes the directory stream to refer to the current state of 
    79 the corresponding directory.
    80 */
    81 EXPORT_C void wrewinddir(WDIR* wdp)
    82 	{
    83 	_wrewinddir_r(wdp);
    84 	}
    85 
    86 EXPORT_C void rewinddir(DIR* dp)
    87 	{
    88 	WDIR* wdp = (__EPOC32_DIR*)dp;
    89 	wrewinddir(wdp);
    90 	}
    91 
    92 /*
    93 closes the directory stream that dirp refers to. 
    94 The memory associated with the directory stream is released. 
    95 When this function returns, the value of dirp no longer point to 
    96 an accessible object of type DIR
    97 */
    98 
    99 EXPORT_C int wclosedir(WDIR* wdp)
   100 	{
   101 	return _wclosedir_r(&errno,wdp);
   102 	}
   103 
   104 EXPORT_C int closedir(DIR* dp)
   105 	{
   106 	if (!dp)
   107 		{
   108 		errno = EBADF;
   109 		return -1;
   110 		}
   111 		
   112 	WDIR* wdp =  (__EPOC32_DIR*)dp;
   113 	return wclosedir(wdp);
   114 	}
   115 
   116 /*
   117 sets the position of the next readdir() operation on the directory 
   118 stream specified by dp to the position specified by index.
   119 */
   120 
   121 EXPORT_C void wseekdir(WDIR* wdp, off_t index)
   122 	{
   123 	if(!wdp)
   124 		{
   125 	 	errno = EFAULT ;
   126 		return ;	
   127 		}
   128 	wdp->iIndex = index;
   129 	}
   130 
   131 EXPORT_C void seekdir(DIR* dp, long index)
   132 	{
   133     WDIR* wdp = (__EPOC32_DIR*)dp;
   134 	wseekdir(wdp, index);
   135 	}
   136 
   137 
   138 EXPORT_C off_t wtelldir(const WDIR* wdp)
   139 	{
   140 	if(!wdp)
   141 		{
   142 	 	errno = EFAULT ;
   143 		return -1 ;	
   144 		}
   145 	if(wdp->iCount < 0 )
   146 		{
   147 		return -1;
   148 		}
   149 	return wdp->iIndex;
   150 	}
   151 
   152 /*
   153 Returns the current location associated with the directory stream dir.
   154 */
   155 EXPORT_C long telldir(DIR* dp)
   156 	{
   157 	WDIR* wdp =  (__EPOC32_DIR*)dp;
   158 	return (long)wtelldir(wdp);
   159 	}
   160 
   161 }// extern "C"