=================================================================== RCS file: /home/cvs/OpenXM_contrib2/asir2000/builtin/file.c,v retrieving revision 1.28 retrieving revision 1.37 diff -u -p -r1.28 -r1.37 --- OpenXM_contrib2/asir2000/builtin/file.c 2013/02/15 07:05:49 1.28 +++ OpenXM_contrib2/asir2000/builtin/file.c 2017/09/02 04:04:41 1.37 @@ -45,20 +45,21 @@ * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE, * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE. * - * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.27 2009/03/13 04:45:15 ohara Exp $ + * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.36 2017/06/12 06:22:50 noro Exp $ */ #include "ca.h" #include "parse.h" #include "ox.h" #include "base.h" -#if !defined(VISUAL) +#include +#if !defined(VISUAL) && !defined(__MINGW32__) #include "unistd.h" #endif #if defined(PARI) #include "genpari.h" #endif -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) #include #include #include @@ -68,6 +69,8 @@ #define R_OK 0x04 #endif +void Psprintf(NODE,STRING *); + void Pget_rootdir(); void Paccess(),Premove_file(); void Pbsave_enc(), Pbload_enc(); @@ -169,7 +172,7 @@ void Popen_file(NODE arg,Q *rp) asir_assert(ARG1(arg),O_STR,"open_file"); fp = fopen(name,BDY((STRING)ARG1(arg))); } else - fp = fopen(name,"r"); + fp = fopen(name,"rb"); } if ( !fp ) { sprintf(errbuf,"open_file : cannot open \"%s\"",name); @@ -239,6 +242,12 @@ void Pget_line(NODE arg,STRING *rp) fsetpos(fp,&head); str = (char *)MALLOC_ATOMIC(j+1); fgets(str,j+1,fp); + for ( i = 0; i < j; i++ ) + if ( str[i] == '\r' ) { + str[i] = '\n'; + str[i+1] = 0; + break; + } MKSTR(*rp,str); } else error("get_line : invalid argument"); @@ -506,24 +515,34 @@ void Pbload_cmo(NODE arg,Obj *rp) static struct oSTRING rootdir; -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) +static void chop_delim(char *s) +{ + int n=strlen(s); + if(n>1 && s[n-1]=='\\') { + s[n-1]=0; + } +} + void get_rootdir(char *name,int len) { LONG ret; HKEY hOpenKey; DWORD Type; - char *slash; + char drive[_MAX_DRIVE], dir0[_MAX_DIR], dir[_MAX_DIR]; + char fullname[_MAX_PATH]; if ( rootdir.body ) { - strcpy(name,rootdir.body); + strcpy_s(name,len,rootdir.body); return; } - - if ( access("UseCurrentDir",0) >= 0 ) { - GetCurrentDirectory(BUFSIZ,name); - slash = strrchr(name,'\\'); - if ( slash ) - *slash = 0; + if ( GetModuleFileName(NULL,fullname,_MAX_PATH) ) { + _splitpath(fullname,drive,dir0,NULL,NULL); + chop_delim(dir0); + _splitpath(dir0,NULL,dir,NULL,NULL); + chop_delim(dir); + strcpy_s(name,len,drive); + strcat_s(name,len,dir); return; } name[0] = 0; @@ -536,10 +555,11 @@ void get_rootdir(char *name,int len) RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len); RegCloseKey(hOpenKey); } else { - GetCurrentDirectory(len,name); - slash = strrchr(name,'\\'); - if ( slash ) - *slash = 0; + GetCurrentDirectory(_MAX_PATH,fullname); + _splitpath(fullname,drive,dir,NULL,NULL); + chop_delim(dir); + strcpy_s(name,len,drive); + strcat_s(name,len,dir); } } @@ -589,7 +609,7 @@ void Pgetpid(Q *rp) { int id; -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) id = GetCurrentProcessId(); #else id = getpid(); @@ -699,7 +719,7 @@ void Paccess(NODE arg,Q *rp) *rp = 0; } -#if defined(VISUAL) +#if defined(VISUAL) || defined(__MINGW32__) int process_id() { return GetCurrentProcessId();