Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Win32 implementation of open() and sopen().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. #include "variety.h"
  33. #include "widechar.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <stdarg.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #include <io.h>
  40. #include <fcntl.h>
  41. #include <sys/stat.h>
  42. #include <share.h>
  43. #include "liballoc.h"
  44. #include "iomode.h"
  45. #include "fileacc.h"
  46. #include "openmode.h"
  47. #include "rtdata.h"
  48. #include "seterrno.h"
  49.  
  50. extern unsigned __NFiles;
  51. extern char *__appcwd;
  52. extern int __appcwdlen;
  53.  
  54. #if (defined(__WINDOWS__) || defined(__NT__))
  55.  
  56. typedef struct
  57. {   DWORD    attr;
  58.     DWORD    flags;
  59.     DWORD    cr_time;
  60.     DWORD    cr_date;
  61.     DWORD    acc_time;
  62.     DWORD    acc_date;
  63.     DWORD    mod_time;
  64.     DWORD    mod_date;
  65.     DWORD    size;
  66.     DWORD    size_high;
  67. } FILEINFO;
  68.  
  69. int _stdcall get_fileinfo(const char *name,FILEINFO* pinfo);
  70.  
  71.  
  72. typedef struct
  73. {
  74.   char     *name;
  75.   unsigned int offset;
  76. }__file_handle;
  77.  
  78.  
  79. static char* getfullpath(const char* path)
  80. {
  81.     int prev_is_slash=0;
  82.     int len=0, depth=0, i;
  83.     char* buff;
  84.     char c;
  85.    
  86.     if(*path == '/')
  87.     {
  88.       buff = (char*)lib_malloc(strlen(path)+1);
  89.       buff[0] = '\0';
  90.       len=0;
  91.     }  
  92.     else
  93.     {
  94.       len= __appcwdlen;
  95.       buff = (char*)lib_malloc(len+strlen(path)+1);
  96.       strncpy(buff, __appcwd, __appcwdlen);
  97.      
  98.       prev_is_slash = 1;
  99.       buff[len] = 0;
  100.       for(i=0; buff[i]; i++)
  101.         if(buff[i] == '/' && i < len-1) depth++;
  102.     }
  103.    
  104.     while(c=*path++)
  105.     {
  106.       switch (c)
  107.       {
  108.      
  109.         case '.':
  110.           if((*path == '.')&&
  111.              (*path+1)== '/')
  112.           { if(!depth)
  113.             {  free(buff);
  114.                return 0;
  115.             };
  116.             buff[len-1] = 0;
  117.             len = strrchr(buff, '/') + 1 - buff;
  118.             buff[len] = 0;
  119.             depth--;
  120.             path +=2;
  121.             prev_is_slash = 1;
  122.             continue;
  123.           }
  124.           if(*path == '/')
  125.           {
  126.             path++;
  127.             prev_is_slash = 1;
  128.             continue;
  129.           }
  130.           buff[len++] = c;
  131.           continue;
  132.      
  133.         case '/':
  134.           prev_is_slash = 1;
  135.           buff[len++] = c;
  136.           continue;
  137.          
  138.         default:
  139.           prev_is_slash = 0;
  140.           buff[len++] = c;
  141.           continue;
  142.         };
  143.     };
  144.     buff[len]= '\0';
  145.     return buff;
  146. };
  147.  
  148.  
  149. size_t FileSize(FILE *fp)
  150. {
  151.   int hdl;
  152.   __file_handle *fh;
  153.   FILEINFO info;
  154.  
  155.   hdl = fileno( fp );
  156.  // __handle_check( hdl, -1 );
  157.  
  158.   fh = (__file_handle*) __getOSHandle(hdl);
  159.  
  160.    
  161.   get_fileinfo(fh->name,&info);
  162.  
  163.   return info.size;
  164.  
  165. }
  166.  
  167. int access(const char *path, int mode)
  168. { size_t retval;
  169.   FILEINFO info;
  170.   char *p;
  171.  
  172.   p = getfullpath(path);
  173.   retval=get_fileinfo(p,&info);
  174.   free(p);
  175.  
  176.   return retval;
  177.  
  178. }
  179.  
  180.  
  181. static HANDLE __createFileHandle(const CHAR_TYPE *name)
  182. {
  183.   FILEINFO info;
  184.   __file_handle *handle;
  185.   char *path;
  186.  
  187.   path = getfullpath(name);
  188.  
  189.   if(get_fileinfo(path,&info))
  190.   {
  191. //    printf("failed getfileinfo %s\n\r", path);  
  192.     lib_free(path);
  193.     return (HANDLE)-1;
  194.   };
  195.  
  196.   if ( !(handle=(__file_handle*)lib_malloc(sizeof( __file_handle) )))
  197.   { lib_free(path);
  198.     return (HANDLE)-1;
  199.   };
  200.  
  201.   handle->name = path;
  202.   handle->offset = 0;  
  203.  
  204.   return (HANDLE)handle;
  205. };    
  206.  
  207.  
  208.  
  209. static int __F_NAME(_sopen,__wsopen)( const CHAR_TYPE *name, int mode, int share, va_list args )
  210. {
  211.     HANDLE              handle;
  212.     int                 hid, rwmode;
  213.     unsigned            iomode_flags;
  214.  
  215.     // First try to get the required slot.
  216.     // No point in creating a file only to not use it.  JBS 99/10/26
  217.     hid = __allocPOSIXHandle( DUMMY_HANDLE );
  218.     if( hid == -1 )
  219.      {
  220.         return( -1 );
  221.     }
  222.  
  223.     rwmode = mode;
  224.  
  225.  
  226.     /*** Open the file ***/
  227.        
  228.     handle = __createFileHandle( name);
  229.    
  230.     if( handle==(HANDLE)-1 )
  231.     {
  232.  
  233.       printf("handle = -1 \n\r");
  234.        
  235.       if( mode&O_CREAT )
  236.       {
  237. //             handle = CreateFileA( name, desired_access,
  238. //                                   share_mode, NULL, create_disp,
  239. //                                    fileattr, NULL );
  240.       }
  241.       if( handle == (HANDLE)-1 )
  242.       {
  243.          __freePOSIXHandle( hid );
  244.          return( -1 ); //error
  245.       }
  246.     }
  247.  
  248. // Now use the slot we got.
  249.     __setOSHandle( hid, handle );   // JBS 99/11/01
  250.  
  251.     iomode_flags = 0;
  252.  
  253.  
  254.     if( rwmode == O_RDWR )       iomode_flags |= _READ | _WRITE;
  255.     else if( rwmode == O_RDONLY) iomode_flags |= _READ;
  256.     else if( rwmode == O_WRONLY) iomode_flags |= _WRITE;
  257.     if( mode & O_APPEND )        iomode_flags |= _APPEND;
  258.     if( mode & (O_BINARY|O_TEXT) ) {
  259.         if( mode & O_BINARY )    iomode_flags |= _BINARY;
  260.     } else {
  261.         if( _RWD_fmode == O_BINARY ) iomode_flags |= _BINARY;
  262.     }
  263.     __SetIOMode( hid, iomode_flags );
  264.     return( hid );
  265. }
  266.  
  267. #elif
  268. static int __F_NAME(_sopen,__wsopen)( const CHAR_TYPE *name, int mode, int share, va_list args )
  269. {
  270.     DWORD               create_disp, exists_disp;
  271.     DWORD               perm, fileattr;
  272.     DWORD               desired_access, share_mode;
  273.     SECURITY_ATTRIBUTES security;
  274.     HANDLE              handle;
  275.     int                 hid, rwmode;
  276.     unsigned            iomode_flags;
  277.  
  278.     // First try to get the required slot.
  279.     // No point in creating a file only to not use it.  JBS 99/10/26
  280.     hid = __allocPOSIXHandle( DUMMY_HANDLE );
  281.     if( hid == -1 ) {
  282.         return( -1 );
  283.     }
  284.  
  285.     rwmode = mode & OPENMODE_ACCESS_MASK;
  286.     __GetNTAccessAttr( rwmode, &desired_access, &perm );
  287.     __GetNTShareAttr( share|rwmode, &share_mode );
  288.     fileattr = FILE_ATTRIBUTE_NORMAL;
  289.  
  290.     security.nLength = sizeof( SECURITY_ATTRIBUTES );
  291.     security.lpSecurityDescriptor = NULL;
  292.     security.bInheritHandle = mode&O_NOINHERIT ? FALSE : TRUE;
  293.  
  294. #ifdef DEFAULT_WINDOWING
  295. #ifdef __WIDECHAR__
  296.     if( _WindowsNewWindow != 0 && !_wcsicmp( name, L"con" ) )
  297. #else
  298.     if( _WindowsNewWindow != 0 && !stricmp( name, "con" ) )
  299. #endif
  300.     {
  301.         handle = (HANDLE) __NTGetFakeHandle();
  302.  
  303.         // Now use the slot we got.
  304.         __setOSHandle( hid, handle );   // JBS 99/11/01
  305.         _WindowsNewWindow( NULL, hid, -1 );
  306.  
  307.         iomode_flags = _ISTTY;
  308.     } else {
  309. #endif
  310.         if( mode & O_CREAT ) {
  311.             perm = va_arg( args, int );
  312.                 va_end( args );
  313.                 perm &= ~_RWD_umaskval;             /* 05-jan-95 */
  314.             if( ( perm & S_IREAD ) && !( perm & S_IWRITE ) ) {
  315.                 fileattr = FILE_ATTRIBUTE_READONLY;
  316.             }
  317.             if( mode & O_EXCL ) {
  318.                 create_disp = CREATE_NEW;
  319.                 exists_disp = CREATE_NEW;
  320.             } else if( mode & O_TRUNC ) {
  321.                 create_disp = CREATE_ALWAYS;
  322.                 exists_disp = CREATE_NEW;
  323.             } else {
  324.                 create_disp = OPEN_ALWAYS;
  325.                 exists_disp = OPEN_EXISTING;
  326.             }
  327.         } else if( mode & O_TRUNC ) {
  328.             exists_disp = TRUNCATE_EXISTING;
  329.         } else {
  330.             exists_disp = OPEN_EXISTING;
  331.         }
  332.  
  333.         /*** Open the file ***/
  334.         #ifdef __WIDECHAR__
  335.             handle = __lib_CreateFileW( name, desired_access, share_mode,
  336.                                         &security, exists_disp, fileattr,
  337.                                         NULL );
  338.         #else
  339.             handle = CreateFileA( name, desired_access, share_mode,
  340.                                   &security, exists_disp, fileattr, NULL );
  341.         #endif
  342.         if( handle==(HANDLE)-1 ) {
  343.             if( mode&O_CREAT ) {
  344.                 #ifdef __WIDECHAR__
  345.                     handle = __lib_CreateFileW( name, desired_access,
  346.                                                 share_mode, NULL, create_disp,
  347.                                                 fileattr, NULL );
  348.                 #else
  349.                     handle = CreateFileA( name, desired_access,
  350.                                           share_mode, NULL, create_disp,
  351.                                           fileattr, NULL );
  352.                 #endif
  353.             }
  354.             if( handle == (HANDLE)-1 ) {
  355.                 __freePOSIXHandle( hid );
  356.                 return( __set_errno_nt() );
  357.             }
  358.         }
  359.  
  360.         // Now use the slot we got.
  361.         __setOSHandle( hid, handle );   // JBS 99/11/01
  362.  
  363.         iomode_flags = 0;
  364.  
  365.         if( isatty(hid) ) {
  366.             iomode_flags = _ISTTY;
  367.         }
  368. #ifdef DEFAULT_WINDOWING
  369.     }
  370. #endif
  371.  
  372.     if( rwmode == O_RDWR )       iomode_flags |= _READ | _WRITE;
  373.     else if( rwmode == O_RDONLY) iomode_flags |= _READ;
  374.     else if( rwmode == O_WRONLY) iomode_flags |= _WRITE;
  375.     if( mode & O_APPEND )        iomode_flags |= _APPEND;
  376.     if( mode & (O_BINARY|O_TEXT) ) {
  377.         if( mode & O_BINARY )    iomode_flags |= _BINARY;
  378.     } else {
  379.         if( _RWD_fmode == O_BINARY ) iomode_flags |= _BINARY;
  380.     }
  381.     __SetIOMode( hid, iomode_flags );
  382.     return( hid );
  383. }
  384. #endif
  385.  
  386. _WCRTLINK int __F_NAME(open,_wopen)( const CHAR_TYPE *name, int mode, ... )
  387. {
  388.     int         permission;
  389.     va_list     args;
  390.  
  391.     va_start( args, mode );
  392.     permission = va_arg( args, int );
  393.     va_end( args );
  394.     return( __F_NAME(sopen,_wsopen)( name, mode, SH_COMPAT, permission ) );
  395. }
  396.  
  397.  
  398. _WCRTLINK int __F_NAME(sopen,_wsopen)( const CHAR_TYPE *name, int mode, int shflag, ... )
  399. {
  400.     va_list             args;
  401.  
  402.     va_start( args, shflag );
  403.     return( __F_NAME(_sopen,__wsopen)( name, mode, shflag, args ) );
  404. }
  405.