/* Rename is in Standard C. */ void osdep_rename( word w_from, word w_to ) { if (string_length(w_from) > FILENAME_MAX) globals[ G_RESULT ] = fixnum(-1); else { char fnbuf[ FILENAME_MAX+1 ]; strcpy( fnbuf, string2asciiz( w_from ) ); globals[ G_RESULT ] = fixnum( rename( fnbuf, string2asciiz(w_to) ) ); } }
/* system() is in ANSI/ISO C. */ void osdep_system( word w_cmd ) { #ifdef __MWERKS__ /* system() is broken in CodeWarrior 6, at least: once called with a command, it sticks with that command though it allows the arguments to be changed. Examining the code for the function (included in the mwerks libs), the cause is obvious: it uses strcat on the string returned from getenv("COMSPEC"). Gag! We might be able to hack around by preserving COMSPEC around calls to system, but who knows what else it clobbers. So reimplement system() here. */ char *cmd = string2asciiz( w_cmd ); char *comspec = getenv( "COMSPEC" ); STARTUPINFO si; PROCESS_INFORMATION pi; char command[1024]; char *p; int n; int size; if (comspec == NULL || strlen(comspec) + strlen(cmd) + sizeof(" /C ") + 1 > sizeof(command)) { globals[ G_RESULT ] = fixnum(1); return; } strcpy( command, comspec ); strcat( command, " /C " ); strcat( command, cmd ); memset( &si, 0, sizeof( si ) ); si.cb = sizeof(si); if (CreateProcess( NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) == 0) { globals[ G_RESULT ] = fixnum(1); return; } WaitForSingleObject(pi.hProcess, ~0L); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); globals[ G_RESULT ] = fixnum(0); #else char *cmd = string2asciiz( w_cmd ); globals[ G_RESULT ] = fixnum(system( cmd )); #endif /* __MWERKS__ */ }
/* Standard C does not have a procedure to get the modification time of a file, but if stat() exists we can use it. If not, return a vector containing midnight, January 1, 1970 always. This is consistent with the result returned by osdep_access(), below. */ void osdep_mtime( word w_fn, word w_buf ) { int r = 0; struct tm *tm; #ifdef HAVE_STAT struct stat s; const char *fn = string2asciiz( w_fn ); r = stat( fn, &s ); #else struct { time_t st_mtime; } s; s.st_mtime = 0; #endif if (r != 0) { globals[ G_RESULT ] = fixnum(-1); return; } tm = localtime( &s.st_mtime ); vector_set( w_buf, 0, fixnum( tm->tm_year + 1900 ) ); vector_set( w_buf, 1, fixnum( tm->tm_mon + 1 ) ); vector_set( w_buf, 2, fixnum( tm->tm_mday ) ); vector_set( w_buf, 3, fixnum( tm->tm_hour ) ); vector_set( w_buf, 4, fixnum( tm->tm_min ) ); vector_set( w_buf, 5, fixnum( tm->tm_sec ) ); globals[ G_RESULT ] = fixnum( 0 ); }
/* remove() is in Standard C */ void osdep_unlinkfile( word w_fn ) { char *fn = string2asciiz( w_fn ); if (fn == 0) { globals[ G_RESULT ] = fixnum( -1 ); return; } globals[ G_RESULT ] = remove( fn ) ? fixnum(-1) : fixnum(0); }
void osdep_openfile( word w_fn, word w_flags, word w_mode ) { char *fn = string2asciiz( w_fn ); int i, flags = nativeint( w_flags ); char newflags[5]; char *p = newflags; int mode = 0; FILE *fp; #ifdef USE_STDIO check_standard_filedes(); #endif /* This is a real thin pipe for the semantics ... */ if (flags & 0x01) { *p++ = 'r'; mode |= MODE_READ; } if (flags & 0x02) { *p++ = 'w'; mode |= MODE_WRITE; } if (flags & 0x04) *p++ = '+'; if (flags & 0x20) { *p++ = 'b'; mode |= MODE_BINARY; } *p = '\0'; if (!(mode & MODE_BINARY)) mode |= MODE_TEXT; if (fn == 0) { globals[ G_RESULT ] = fixnum( -1 ); return; } fp = fopen( fn, newflags ); if (fp == NULL) { globals[ G_RESULT ] = fixnum( -1 ); return; } /* Now register the file and return the table index. */ for ( i=0 ; i < num_fds && fdarray[i].fp != 0 ; i++ ) ; if (i == num_fds) { int n = max(2*num_fds,5); struct finfo *narray = (struct finfo*)must_malloc( sizeof(struct finfo)*n ); if (fdarray != 0) memcpy( narray, fdarray, sizeof(struct finfo)*num_fds ); for ( i=num_fds ; i < n ; i++ ) { narray[i].fp = 0; narray[i].mode = 0; } i = num_fds; num_fds = n; if (fdarray != 0) free( fdarray ); fdarray = narray; } fdarray[i].fp = fp; fdarray[i].mode = mode; globals[ G_RESULT ] = fixnum(i); }
/* Standard C does not have a procedure to check whether a file exists. We use stat() if we have it; many systems do. If not, try to open the file in read mode to find out if it exists; this is usually OK (not always). The mode is ignored. */ void osdep_access( word w_fn, word w_bits ) { #ifdef HAVE_STAT struct stat s; globals[ G_RESULT ]= fixnum(stat(string2asciiz(w_fn), &s )); #else FILE *fp; const char *fn = string2asciiz( w_fn ); if ((fp = fopen( fn, "r" )) != 0) { fclose( fp ); globals[ G_RESULT ] = fixnum(0); } else globals[ G_RESULT ] = fixnum(-1); #endif }
void primitive_dumpheap( word w_fn, word w_proc ) { char *fn; fn = string2asciiz( w_fn ); /* heap file name */ globals[ G_STARTUP ] = w_proc; /* startup procedure */ if (fn == 0 || dump_heap_image_to_file( fn ) == -1) globals[ G_RESULT ] = FALSE_CONST; else globals[ G_RESULT ] = TRUE_CONST; }
void osdep_chdir( word w_cmd ) { char *path = string2asciiz( w_cmd ); globals[ G_RESULT ] = fixnum(chdir(path)); }
/* system() is in Standard C */ void osdep_system( word w_cmd ) { char *cmd = string2asciiz( w_cmd ); globals[ G_RESULT ] = fixnum(system( cmd )); }