DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/dos/dos_network2.h
00001 
00002 /*
00003 DOS     Network Routines.
00004 
00005 
00006 Log:
00007 July 11 2005            cyberwalker             Created.
00008 July 23 2005            cyberwalker
00009         Removed code from dos.cpp to this file.
00010 August 8 2005           cyberwalker
00011                 Make all functions  to avoid modifying makefile.
00012 
00013 */
00014 
00015 #if defined(WIN32) && !defined(__MINGW32__)
00016 
00017 #include <windows.h>
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <time.h>
00023 #include <errno.h>
00024 //#include <dirent.h>
00025 
00026 #include "config.h"
00027 #include "dosbox.h"
00028 #include "dos_inc.h"
00029 
00030 #include <io.h>
00031 #include <fcntl.h>
00032 #include <share.h>
00033 
00034 extern Bit16u   NetworkHandleList[127]; /*in dos_files.cpp*/
00035 
00036  bool   Network_IsNetworkResource(char * filename)
00037 {
00038         if(strlen(filename)>1)
00039                         return (filename[0]=='\\' && filename[1]=='\\');
00040         else    return false;
00041 }//bool Network_IsNetworkFile(Bit16u entry)
00042 
00043 
00044  bool   Network_IsActiveResource(Bit16u entry)
00045 {
00046         Bit32u          handle=RealHandle(entry);
00047         return  (NetworkHandleList[entry]==handle);
00048 }//bool Network_IsNetworkFile(Bit16u entry)
00049 
00050 
00051  bool   Network_OpenFile(char * filename,Bit8u flags,Bit16u * entry)
00052 {
00053         int oflag=_O_BINARY;
00054         int shflag=0;
00055 
00056         //bit0-bit2:access mode
00057         //000:read only 001:write only 010:read/write
00058         switch(flags & 0x3)
00059         {
00060         case 0:
00061                 oflag|=_O_RDONLY;
00062                 break;
00063         case 1:
00064                 oflag|=_O_WRONLY;
00065                 break;
00066         case 2:
00067                 oflag|=_O_RDWR;
00068                 break;
00069         }
00070 
00071         //bit4-bit6:share mode
00072         //000:compatibility mode 001:deny all 010:deny write 011:deny read 100:deny none
00073         switch((flags>>4) & 0x4)
00074         {
00075         case 0:
00076                 shflag|=_SH_DENYNO;
00077                 break;
00078         case 1:
00079                 shflag|=_SH_DENYRW;
00080                 break;
00081         case 2:
00082                 shflag|=_SH_DENYWR;
00083                 break;
00084         case 3:
00085                 shflag|=_SH_DENYRD;
00086                 break;
00087         case 4:
00088                 shflag|=_SH_DENYNO;
00089                 break;
00090         }
00091 
00092         int     handle=_sopen(filename,oflag,shflag,_S_IREAD|_S_IWRITE);
00093 
00094         if(handle!=-1)
00095         {
00096                 DOS_PSP         psp(dos.psp());
00097                 *entry = psp.FindFreeFileEntry();
00098                 psp.SetFileHandle(*entry,handle);
00099 
00100                 NetworkHandleList[*entry]=handle;
00101 
00102                 return  true;
00103         }
00104         else    dos.errorcode=(Bit16u)_doserrno;
00105 
00106         return false;
00107 }//bool Network_OpenFile(char * filename,Bit8u flags,Bit16u * entry)
00108 
00109 #ifndef CMAKE_BUILD // TODO there must be a better way to fix this problem
00110 extern "C"
00111 #endif
00112 int _nhandle;
00113 
00114  bool   Network_CloseFile(Bit16u entry)
00115 {
00116                 Bit32u handle=RealHandle(entry);
00117                 int _Expr_val=!!((handle >= 0 && (unsigned)handle < (unsigned)_nhandle));
00118                 //_ASSERT_EXPR( ( _Expr_val ), _CRT_WIDE(#(handle >= 0 && (unsigned)handle < (unsigned)_nhandle)) );
00119                 if (!(handle > 0) || ( !( _Expr_val ))) {
00120                         _doserrno = 0L;
00121                         errno = EBADF;
00122                         dos.errorcode=(Bit16u)_doserrno;
00123                         return false;
00124                 }
00125 
00126                 if(close(handle)==0)
00127                 {
00128                         NetworkHandleList[entry]=0;
00129 
00130                         DOS_PSP         psp(dos.psp());
00131                         psp.SetFileHandle(entry,0xff);
00132 
00133                         return  true;
00134                 }
00135                 else
00136                 {
00137                         dos.errorcode=(Bit16u)_doserrno;
00138                         return  false;
00139                 }
00140 }//bool Network_CloseFile(Bit16u entry)
00141 
00142  bool Network_ReadFile(Bit16u entry,Bit8u * data,Bit16u * amount)
00143 {
00144         Bit32u handle=RealHandle(entry);
00145         Bit16u toread=*amount;
00146 
00147         toread=_read(handle,data,toread);
00148         if (toread>=0) 
00149                         *amount=toread;
00150         else    
00151         {
00152                 *amount=0;
00153                 dos.errorcode=(Bit16u)_doserrno;
00154         }
00155 
00156         return  (toread>=0);
00157 }//bool Network_ReadFile(Bit16u entry,Bit8u * data,Bit16u * amount)
00158 
00159 
00160  bool   Network_WriteFile(Bit16u entry,Bit8u * data,Bit16u * amount)
00161 {
00162                 Bit16u towrite=*amount;
00163                 Bit32u handle=RealHandle(entry);
00164 
00165                 towrite=_write(handle,data,towrite);
00166                 if(towrite!=-1)
00167                         *amount=towrite;
00168                 else 
00169                 {
00170                         dos.errorcode=(Bit16u)_doserrno;
00171                         *amount=0;
00172                 }
00173                 return (towrite!=-1);
00174 }//bool Network_WriteFile(Bit16u entry,Bit8u * data,Bit16u * amount)
00175 
00176 
00177 
00178 #endif // defined(WIN32) && !defined(__MINGW32__)