2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
gbx_subr_file.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
|
|
|
|
#include "gbx_subr.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gb_list.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
#include "gbx_archive.h"
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gbx_local.h"
|
|
|
|
#include "gbx_regexp.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_c_file.h"
|
2014-05-21 10:35:34 +02:00
|
|
|
#include "gbx_math.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
typedef
|
2012-01-15 09:54:57 +01:00
|
|
|
struct _stream {
|
|
|
|
struct _stream *next;
|
|
|
|
CSTREAM *stream;
|
|
|
|
}
|
|
|
|
CSTREAM_NODE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static void *_default_in = NULL;
|
|
|
|
static void *_default_out = NULL;
|
|
|
|
static void *_default_err = NULL;
|
|
|
|
|
|
|
|
static GB_ARRAY _result;
|
|
|
|
static char *_pattern;
|
|
|
|
static int _len_pattern;
|
|
|
|
static int _ignore;
|
|
|
|
|
|
|
|
static STREAM *_stream;
|
|
|
|
|
|
|
|
|
|
|
|
static void push_stream(void **list, CSTREAM *stream)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CSTREAM_NODE *slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&slot, sizeof(CSTREAM_NODE));
|
2012-01-15 09:54:57 +01:00
|
|
|
slot->stream = stream;
|
2013-03-30 14:51:10 +01:00
|
|
|
//OBJECT_REF(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
slot->next = *list;
|
|
|
|
*list = slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CSTREAM *pop_stream(void **list)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CSTREAM *stream;
|
|
|
|
CSTREAM_NODE *slot;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!*list)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = ((CSTREAM_NODE *)*list)->stream;
|
|
|
|
slot = *list;
|
|
|
|
*list = slot->next;
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&slot);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static STREAM *get_default(intptr_t val)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
|
|
|
|
|
|
|
switch(val)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if (_default_in)
|
|
|
|
stream = CSTREAM_stream(((CSTREAM_NODE *)_default_in)->stream);
|
|
|
|
else
|
|
|
|
stream = CSTREAM_stream(CFILE_in);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (_default_out)
|
|
|
|
stream = CSTREAM_stream(((CSTREAM_NODE *)_default_out)->stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = CSTREAM_stream(CFILE_out);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (_default_err)
|
|
|
|
stream = CSTREAM_stream(((CSTREAM_NODE *)_default_err)->stream);
|
|
|
|
else
|
|
|
|
stream = CSTREAM_stream(CFILE_err);
|
|
|
|
break;
|
2010-05-28 09:47:49 +02:00
|
|
|
default:
|
|
|
|
stream = NULL;
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-05-15 15:17:18 +02:00
|
|
|
if (!stream)
|
|
|
|
THROW(E_CLOSED);
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
#define _get_stream(_value, _can_default) \
|
2010-06-05 01:48:53 +02:00
|
|
|
STREAM *stream; \
|
|
|
|
\
|
2010-11-03 21:28:54 +01:00
|
|
|
VARIANT_undo(_value); \
|
|
|
|
\
|
2010-06-05 01:48:53 +02:00
|
|
|
if ((_can_default) && TYPE_is_integer((_value)->type) && (_value)->_integer.value >= 0 && (_value)->_integer.value <= 2) \
|
|
|
|
stream = get_default((intptr_t)(_value)->_integer.value); \
|
|
|
|
else \
|
|
|
|
{ \
|
2010-06-21 20:26:38 +02:00
|
|
|
if (TYPE_is_object((_value)->type) && (_value)->_object.object && OBJECT_class((_value)->_object.object)->is_stream) \
|
2010-06-05 01:48:53 +02:00
|
|
|
stream = CSTREAM_stream((_value)->_object.object); \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
if (VALUE_is_null(_value)) \
|
|
|
|
THROW(E_NULL); \
|
|
|
|
\
|
|
|
|
VALUE_conv_object((_value), (TYPE)CLASS_Stream); \
|
|
|
|
stream = NULL; \
|
|
|
|
} \
|
2013-07-06 21:00:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define get_stream(_value, _can_default) \
|
|
|
|
({ \
|
|
|
|
_get_stream(_value, _can_default); \
|
2010-06-05 01:48:53 +02:00
|
|
|
\
|
|
|
|
if (STREAM_is_closed(stream)) \
|
|
|
|
THROW(E_CLOSED); \
|
|
|
|
\
|
|
|
|
stream; \
|
|
|
|
})
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
#define get_stream_for_writing(_value, _can_default) \
|
|
|
|
({ \
|
|
|
|
_get_stream(_value, _can_default); \
|
|
|
|
\
|
|
|
|
if (STREAM_is_closed_for_writing(stream)) \
|
|
|
|
THROW(E_CLOSED); \
|
|
|
|
\
|
|
|
|
stream; \
|
|
|
|
})
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static char *get_path(VALUE *param)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
char *name;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_get_string_len(param, &name, &len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
return STRING_conv_file_name(name, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_open(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CFILE *file;
|
|
|
|
STREAM stream;
|
|
|
|
int mode;
|
2009-09-17 22:58:27 +02:00
|
|
|
void *addr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_check_integer(&PARAM[1]);
|
|
|
|
mode = PARAM[1]._integer.value;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (code & 0x3F)
|
2009-09-17 22:58:27 +02:00
|
|
|
{
|
2010-05-19 14:43:57 +02:00
|
|
|
if (TYPE_is_pointer(PARAM->type))
|
|
|
|
addr = (void *)PARAM->_pointer.value;
|
2009-09-17 22:58:27 +02:00
|
|
|
else
|
2012-01-15 09:54:57 +01:00
|
|
|
THROW(E_TYPE, "Pointer", TYPE_get_name(PARAM->type));
|
2009-09-17 22:58:27 +02:00
|
|
|
|
|
|
|
STREAM_open(&stream, (char *)addr, mode | ST_MEMORY);
|
|
|
|
}
|
2013-11-04 16:17:01 +01:00
|
|
|
else if (mode & ST_STRING)
|
|
|
|
{
|
2013-11-04 21:12:24 +01:00
|
|
|
char *str;
|
|
|
|
|
|
|
|
STREAM_open(&stream, NULL, mode);
|
|
|
|
|
|
|
|
if (!VALUE_is_null(PARAM))
|
|
|
|
{
|
|
|
|
str = SUBR_get_string(PARAM);
|
|
|
|
|
|
|
|
if (mode & ST_WRITE)
|
|
|
|
{
|
|
|
|
stream.string.buffer = STRING_new(str, STRING_length(str));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream.string.buffer = str;
|
|
|
|
STRING_ref(str);
|
|
|
|
}
|
|
|
|
stream.string.size = STRING_length(str);
|
|
|
|
}
|
2013-11-04 16:17:01 +01:00
|
|
|
}
|
2009-09-17 22:58:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
STREAM_open(&stream, get_path(PARAM), mode);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
file = CFILE_create(&stream, mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
OBJECT_put(RETURN, file);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_close(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-11-04 16:17:01 +01:00
|
|
|
STREAM *stream;
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-11-04 16:17:01 +01:00
|
|
|
stream = get_stream(PARAM, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-11-04 16:17:01 +01:00
|
|
|
if (stream->type == &STREAM_string)
|
|
|
|
{
|
2013-11-04 19:16:26 +01:00
|
|
|
char *buffer = stream->string.buffer;
|
|
|
|
|
2013-11-04 16:17:01 +01:00
|
|
|
RETURN->type = T_STRING;
|
2013-11-04 19:16:26 +01:00
|
|
|
RETURN->_string.addr = buffer;
|
2013-11-04 16:17:01 +01:00
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = stream->string.size;
|
|
|
|
|
2013-11-04 19:16:26 +01:00
|
|
|
STRING_ref(buffer);
|
2013-11-04 16:17:01 +01:00
|
|
|
STREAM_close(stream);
|
2013-11-04 19:16:26 +01:00
|
|
|
STRING_free_later(buffer);
|
|
|
|
|
|
|
|
//fprintf(stderr, "buffer ref = %d\n", STRING_from_ptr(buffer)->ref);
|
|
|
|
|
2013-11-04 16:17:01 +01:00
|
|
|
SUBR_LEAVE();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STREAM_close(stream);
|
|
|
|
SUBR_LEAVE_VOID();
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_flush(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_flush(get_stream(PARAM, TRUE));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*static void print_it(char *addr, long len)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_write(_stream, addr, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
}*/
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_print(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int i;
|
|
|
|
char *addr;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM < 1)
|
|
|
|
THROW(E_NEPARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
_stream = get_stream_for_writing(PARAM, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
//PRINT_init(print_it, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
for (i = 1; i < NPARAM; i++)
|
|
|
|
{
|
|
|
|
PARAM++;
|
|
|
|
//PRINT_value(PARAM);
|
|
|
|
VALUE_to_string(PARAM, &addr, &len);
|
|
|
|
if (len == 1 && *addr == '\n')
|
|
|
|
STREAM_write_eol(_stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_write(_stream, addr, len);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_linput(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
|
|
|
char *addr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = get_stream(&SP[-1], TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
addr = STREAM_line_input(stream, NULL);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
SP--;
|
2012-02-27 00:07:14 +01:00
|
|
|
if (!TYPE_is_integer(SP->type))
|
|
|
|
RELEASE_OBJECT(SP);
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SP->type = T_STRING;
|
|
|
|
SP->_string.addr = addr;
|
|
|
|
SP->_string.start = 0;
|
|
|
|
SP->_string.len = STRING_length(addr);
|
2010-06-05 01:48:53 +02:00
|
|
|
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_input(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
static STREAM *stream = NULL;
|
|
|
|
char *addr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (NPARAM == 1)
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = get_stream(PARAM, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (stream)
|
2012-01-15 09:54:57 +01:00
|
|
|
addr = STREAM_input(stream);
|
2012-03-13 03:39:10 +01:00
|
|
|
else
|
|
|
|
addr = NULL;
|
|
|
|
|
|
|
|
if (NPARAM == 1)
|
|
|
|
{
|
|
|
|
SP--;
|
|
|
|
if (!TYPE_is_integer(SP->type))
|
|
|
|
RELEASE_OBJECT(SP);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (addr)
|
|
|
|
{
|
2012-03-16 22:41:38 +01:00
|
|
|
//VALUE_from_string(SP, addr, STRING_length(addr));
|
|
|
|
SP->type = T_STRING;
|
|
|
|
SP->_string.addr = addr;
|
|
|
|
SP->_string.start = 0;
|
|
|
|
SP->_string.len = STRING_length(addr);
|
2012-03-13 03:39:10 +01:00
|
|
|
}
|
2012-03-16 22:41:38 +01:00
|
|
|
else
|
|
|
|
SP->type = T_NULL;
|
2012-03-13 03:39:10 +01:00
|
|
|
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_eof(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-06-05 01:48:53 +02:00
|
|
|
STREAM *stream;
|
|
|
|
bool eof;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (NPARAM == 1)
|
|
|
|
{
|
|
|
|
stream = get_stream(PARAM, FALSE);
|
|
|
|
eof = STREAM_eof(stream);
|
|
|
|
RELEASE_OBJECT(PARAM);
|
|
|
|
SP--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream = get_default(0);
|
|
|
|
eof = STREAM_eof(stream);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
SP->type = T_BOOLEAN;
|
|
|
|
SP->_boolean.value = (-eof);
|
|
|
|
SP++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_lof(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM == 1)
|
|
|
|
stream = get_stream(PARAM, FALSE);
|
|
|
|
else
|
|
|
|
stream = get_default(0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->type = T_LONG;
|
|
|
|
STREAM_lof(stream, &(RETURN->_long.value));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_seek(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
|
|
|
int64_t pos;
|
|
|
|
int64_t len;
|
|
|
|
int whence = SEEK_SET;
|
|
|
|
|
|
|
|
SUBR_ENTER();
|
|
|
|
|
|
|
|
stream = get_stream(PARAM, FALSE);
|
|
|
|
|
|
|
|
if (NPARAM >= 2)
|
|
|
|
{
|
|
|
|
VALUE_conv(&PARAM[1], T_LONG);
|
|
|
|
pos = PARAM[1]._long.value;
|
|
|
|
|
|
|
|
if (NPARAM == 3)
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(&PARAM[2]);
|
|
|
|
whence = PARAM[2]._integer.value;
|
|
|
|
if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END)
|
|
|
|
THROW(E_ARG);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pos < 0)
|
|
|
|
{
|
|
|
|
STREAM_lof(stream, &len);
|
|
|
|
pos += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STREAM_seek(stream, pos, (int)whence);
|
|
|
|
RETURN->type = T_VOID;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RETURN->type = T_LONG;
|
|
|
|
RETURN->_long.value = STREAM_tell(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_read(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
|
|
|
int len = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = get_stream(PARAM, TRUE);
|
2009-09-20 19:32:12 +02:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (code & 0x3F)
|
2009-09-20 19:32:12 +02:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
2009-09-20 19:32:12 +02:00
|
|
|
len = PARAM[1]._integer.value;
|
2010-12-28 22:58:42 +01:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
|
|
|
RETURN->type = T_NULL;
|
|
|
|
}
|
|
|
|
else if (len > 0)
|
|
|
|
{
|
|
|
|
STRING_new_temp_value(RETURN, NULL, len);
|
|
|
|
STREAM_read(stream, RETURN->_string.addr, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = (-len);
|
|
|
|
|
|
|
|
RETURN->type = T_STRING;
|
|
|
|
RETURN->_string.addr = STRING_new(NULL, len);
|
|
|
|
RETURN->_string.start = 0;
|
|
|
|
RETURN->_string.len = len;
|
|
|
|
|
|
|
|
STREAM_read_max(stream, RETURN->_string.addr, len);
|
|
|
|
|
|
|
|
if (STREAM_eff_read == 0)
|
|
|
|
{
|
|
|
|
RETURN->type = T_NULL;
|
|
|
|
STRING_free(&RETURN->_string.addr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (STREAM_eff_read < len)
|
|
|
|
{
|
2011-09-07 14:38:29 +02:00
|
|
|
RETURN->_string.addr = STRING_extend(RETURN->_string.addr, STREAM_eff_read);
|
2010-12-28 22:58:42 +01:00
|
|
|
RETURN->_string.len = STREAM_eff_read;
|
|
|
|
}
|
2011-09-07 14:38:29 +02:00
|
|
|
STRING_extend_end(RETURN->_string.addr);
|
2010-12-28 22:58:42 +01:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 19:32:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
TYPE type = SUBR_get_type(&PARAM[1]);
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_read_type(stream, type, RETURN);
|
2009-09-20 19:32:12 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-09-20 19:32:12 +02:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_write(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM *stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(3);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2013-07-06 21:00:41 +02:00
|
|
|
stream = get_stream_for_writing(PARAM, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (code & 0x3F)
|
2009-09-20 19:32:12 +02:00
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
char *str;
|
|
|
|
int len;
|
|
|
|
int lenw;
|
|
|
|
|
2011-09-03 03:55:51 +02:00
|
|
|
VALUE_conv_integer(&PARAM[2]);
|
|
|
|
lenw = PARAM[2]._integer.value;
|
|
|
|
|
2011-09-03 03:45:52 +02:00
|
|
|
if (TYPE_is_pointer(PARAM[1].type))
|
|
|
|
{
|
2011-09-03 03:55:51 +02:00
|
|
|
if (lenw < 0)
|
|
|
|
lenw = 0;
|
|
|
|
len = lenw;
|
2011-09-03 03:45:52 +02:00
|
|
|
str = (char *)PARAM[1]._pointer.value;
|
2011-09-03 03:55:51 +02:00
|
|
|
fprintf(stderr, "%p\n", str);
|
2011-09-03 03:45:52 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SUBR_get_string_len(&PARAM[1], &str, &len);
|
2011-09-03 03:55:51 +02:00
|
|
|
if (lenw < 0)
|
|
|
|
lenw = len;
|
2011-09-03 03:45:52 +02:00
|
|
|
}
|
2010-12-28 22:58:42 +01:00
|
|
|
|
|
|
|
if (lenw > 0)
|
|
|
|
{
|
|
|
|
STREAM_write(stream, str, Min(len, lenw));
|
|
|
|
if (lenw > len)
|
|
|
|
STREAM_write_zeros(stream, lenw - len);
|
|
|
|
}
|
2009-09-20 19:32:12 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-28 22:58:42 +01:00
|
|
|
TYPE type;
|
2009-09-20 19:32:12 +02:00
|
|
|
type = SUBR_get_type(&PARAM[2]);
|
|
|
|
VALUE_conv(&PARAM[1], type);
|
2010-12-28 22:58:42 +01:00
|
|
|
STREAM_write_type(stream, type, &PARAM[1]);
|
2009-09-20 19:32:12 +02:00
|
|
|
}
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_stat(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
const char *path;
|
|
|
|
CSTAT *cstat;
|
|
|
|
FILE_STAT info;
|
|
|
|
bool follow = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (NPARAM == 2)
|
2009-09-10 21:30:41 +02:00
|
|
|
follow = SUBR_get_boolean(&PARAM[1]);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
FILE_stat(path, &info, follow);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
cstat = OBJECT_new(CLASS_Stat, NULL, NULL);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF_KEEP(cstat);
|
2012-01-15 09:54:57 +01:00
|
|
|
cstat->info = info;
|
|
|
|
cstat->path = STRING_new_zero(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
RETURN->_object.class = CLASS_Stat;
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->_object.object = cstat;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-17 12:57:55 +02:00
|
|
|
void SUBR_exist(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
bool exist;
|
|
|
|
const char *path;
|
|
|
|
bool follow = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2011-07-17 13:11:22 +02:00
|
|
|
|
|
|
|
if (!NPARAM)
|
|
|
|
{
|
|
|
|
NPARAM++;
|
|
|
|
PARAM--;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-07-17 12:57:55 +02:00
|
|
|
if (NPARAM == 2)
|
|
|
|
follow = SUBR_get_boolean(&PARAM[1]);
|
|
|
|
|
|
|
|
exist = FILE_exist_follow(path, follow);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->type = T_BOOLEAN;
|
|
|
|
RETURN->_integer.value = exist ? -1 : 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_dir(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
GB_ARRAY array;
|
|
|
|
const char *path;
|
|
|
|
char *pattern;
|
|
|
|
int len_pattern;
|
|
|
|
char *str;
|
|
|
|
int attr = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM >= 2)
|
|
|
|
{
|
|
|
|
pattern = SUBR_get_string(&PARAM[1]);
|
|
|
|
if (NPARAM == 3)
|
|
|
|
attr = SUBR_get_integer(&PARAM[2]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pattern = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
FILE_dir_first(path, pattern, attr);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
GB_ArrayNew(&array, T_STRING, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
while (!FILE_dir_next(&pattern, &len_pattern))
|
|
|
|
{
|
|
|
|
if (!LOCAL_is_UTF8)
|
|
|
|
{
|
|
|
|
if (STRING_conv(&str, pattern, len_pattern, LOCAL_encoding, SC_UTF8, FALSE))
|
|
|
|
str = STRING_new(pattern, len_pattern);
|
|
|
|
else
|
|
|
|
STRING_ref(str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str = STRING_new(pattern, len_pattern);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
*((char **)GB_ArrayAdd(array)) = str;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->_object.class = OBJECT_class(array);
|
|
|
|
RETURN->_object.object = array;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void found_file(const char *path)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
char *str;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path += _ignore;
|
|
|
|
len = strlen(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (_pattern && !REGEXP_match(_pattern, _len_pattern, path, len))
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (!LOCAL_is_UTF8)
|
|
|
|
{
|
|
|
|
if (STRING_conv(&str, path, len, LOCAL_encoding, SC_UTF8, FALSE))
|
|
|
|
str = STRING_new(path, len);
|
|
|
|
else
|
|
|
|
STRING_ref(str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str = STRING_new(path, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
*((char **)GB_ArrayAdd(_result)) = str;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_rdir(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
const char *path;
|
|
|
|
int attr = 0;
|
2010-03-25 03:00:55 +01:00
|
|
|
bool follow = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM >= 2)
|
|
|
|
{
|
|
|
|
SUBR_get_string_len(&PARAM[1], &_pattern, &_len_pattern);
|
|
|
|
if (NPARAM >= 3)
|
2010-03-25 03:00:55 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
attr = SUBR_get_integer(&PARAM[2]);
|
2010-03-25 03:00:55 +01:00
|
|
|
if (NPARAM == 4)
|
|
|
|
follow = SUBR_get_boolean(&PARAM[3]);
|
|
|
|
}
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_pattern = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
GB_ArrayNew(&_result, T_STRING, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!path || *path == 0)
|
|
|
|
path = ".";
|
2012-01-15 09:54:57 +01:00
|
|
|
_ignore = strlen(path);
|
|
|
|
if (_ignore > 0 && path[_ignore - 1] != '/')
|
|
|
|
_ignore++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
FILE_recursive_dir(path, found_file, NULL, attr, follow);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->_object.class = OBJECT_class(_result);
|
|
|
|
RETURN->_object.object = _result;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
void SUBR_kill(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
switch(code & 0xFF)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
FILE_unlink(get_path(PARAM));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
FILE_mkdir(get_path(PARAM));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
FILE_rmdir(get_path(PARAM));
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
default:
|
|
|
|
THROW_ILLEGAL();
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-11 02:27:18 +02:00
|
|
|
void SUBR_mkdir(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-04-11 02:27:18 +02:00
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
|
|
|
switch (code & 0xFF)
|
|
|
|
{
|
|
|
|
case 0: // Deprecated Mkdir
|
|
|
|
SUBR_kill(1);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 1: // Even
|
|
|
|
VALUE_conv(PARAM, T_LONG);
|
|
|
|
PARAM->type = GB_T_BOOLEAN;
|
|
|
|
PARAM->_boolean.value = (PARAM->_long.value & 1) == 0 ? -1 : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // Odd
|
|
|
|
VALUE_conv(PARAM, T_LONG);
|
|
|
|
PARAM->type = GB_T_BOOLEAN;
|
|
|
|
PARAM->_boolean.value = (PARAM->_long.value & 1) != 0 ? -1 : 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
THROW_ILLEGAL();
|
|
|
|
}
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2014-05-21 10:35:34 +02:00
|
|
|
void SUBR_rmdir(ushort code)
|
2012-01-15 09:54:57 +01:00
|
|
|
{
|
2014-05-21 10:35:34 +02:00
|
|
|
SUBR_ENTER();
|
|
|
|
int min, max;
|
|
|
|
|
|
|
|
switch (code & 0x3F)
|
|
|
|
{
|
|
|
|
case 0: // Deprecated RmDir
|
|
|
|
SUBR_kill(2);
|
|
|
|
return;
|
|
|
|
|
|
|
|
default: // Rand
|
|
|
|
|
|
|
|
if (NPARAM == 1)
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(PARAM);
|
|
|
|
min = 0;
|
|
|
|
max = PARAM->_integer.value;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(PARAM);
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
min = PARAM->_integer.value;
|
|
|
|
max = PARAM[1]._integer.value;
|
|
|
|
if (max < min)
|
|
|
|
{
|
|
|
|
int temp = max;
|
|
|
|
max = min;
|
|
|
|
min = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RETURN->type = T_INTEGER;
|
|
|
|
RETURN->_integer.value = min + (int)(rnd() * (max + 1 - min));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
void SUBR_move(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
char *path;
|
|
|
|
char *auth;
|
|
|
|
FILE_STAT info;
|
|
|
|
|
|
|
|
SUBR_ENTER_PARAM(2);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(&PARAM[0]);
|
|
|
|
|
|
|
|
switch (code & 0xFF)
|
|
|
|
{
|
|
|
|
case 0: // Move
|
|
|
|
|
|
|
|
FILE_rename(path, get_path(&PARAM[1]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: // Copy
|
|
|
|
|
|
|
|
FILE_copy(path, get_path(&PARAM[1]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2: // Link
|
|
|
|
|
|
|
|
/* Parameters are NOT inverted ANYMORE! */
|
|
|
|
FILE_link(path, get_path(&PARAM[1]));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3: // Chmod
|
|
|
|
|
|
|
|
auth = SUBR_get_string(&PARAM[1]);
|
|
|
|
FILE_stat(path, &info, TRUE);
|
|
|
|
FILE_chmod(path, FILE_mode_from_string(info.mode, auth));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4: // Chown
|
|
|
|
|
|
|
|
auth = SUBR_get_string(&PARAM[1]);
|
|
|
|
FILE_chown(path, auth);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5: // Chgrp
|
|
|
|
|
|
|
|
auth = SUBR_get_string(&PARAM[1]);
|
|
|
|
FILE_chgrp(path, auth);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
THROW_ILLEGAL();
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-31 03:33:01 +01:00
|
|
|
void SUBR_link(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-31 03:33:01 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
|
|
|
|
|
|
|
switch (code & 0xFF)
|
|
|
|
{
|
2012-04-11 02:27:18 +02:00
|
|
|
case 0: // Deprecated Link
|
2012-01-31 03:33:01 +01:00
|
|
|
SUBR_move(2);
|
|
|
|
return;
|
|
|
|
|
2012-04-11 02:27:18 +02:00
|
|
|
case 1: // IsNan
|
2012-01-31 03:33:01 +01:00
|
|
|
VALUE_conv(PARAM, T_FLOAT);
|
|
|
|
PARAM->type = GB_T_BOOLEAN;
|
|
|
|
PARAM->_boolean.value = isnan(PARAM->_float.value) ? -1 : 0;
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-04-11 02:27:18 +02:00
|
|
|
case 2: // IsInf
|
2012-01-31 03:33:01 +01:00
|
|
|
VALUE_conv(PARAM, T_FLOAT);
|
|
|
|
PARAM->type = GB_T_INTEGER;
|
|
|
|
PARAM->_integer.value = isinf(PARAM->_float.value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
THROW_ILLEGAL();
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_temp(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
char *temp;
|
|
|
|
int len;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM == 0)
|
|
|
|
temp = FILE_make_temp(&len, NULL);
|
|
|
|
else
|
|
|
|
temp = FILE_make_temp(&len, SUBR_get_string(PARAM));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
STRING_new_temp_value(RETURN, temp, len);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_isdir(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
bool isdir;
|
|
|
|
const char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
path = get_path(PARAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
isdir = FILE_is_dir(path);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->type = T_BOOLEAN;
|
|
|
|
RETURN->_integer.value = isdir ? -1 : 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_access(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
int access;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (NPARAM == 1)
|
|
|
|
access = R_OK;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
VALUE_conv_integer(&PARAM[1]);
|
|
|
|
access = PARAM[1]._integer.value;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->type = T_BOOLEAN;
|
|
|
|
RETURN->_integer.value = FILE_access(get_path(PARAM), access) ? -1 : 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_lock(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-03-09 18:51:52 +01:00
|
|
|
code &= 0x1F;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2014-03-09 18:51:52 +01:00
|
|
|
if (code == 1)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2014-03-09 18:51:52 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_close(get_stream(PARAM, FALSE));
|
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-09 18:51:52 +01:00
|
|
|
SUBR_ENTER_PARAM((code == 2) ? 2 : 1);
|
2007-12-30 17:41:49 +01:00
|
|
|
STREAM stream;
|
|
|
|
CFILE *file;
|
|
|
|
const char *path = get_path(PARAM);
|
2014-03-09 18:51:52 +01:00
|
|
|
double wait, timer;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (FILE_is_relative(path))
|
|
|
|
THROW(E_BADPATH);
|
|
|
|
|
2014-03-09 18:51:52 +01:00
|
|
|
if (code == 2)
|
|
|
|
{
|
|
|
|
DATE_timer(&wait, FALSE);
|
|
|
|
wait += SUBR_get_float(&PARAM[1]);
|
|
|
|
}
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
STREAM_open(&stream, path, ST_WRITE | ST_CREATE | ST_DIRECT);
|
2014-03-09 18:51:52 +01:00
|
|
|
|
|
|
|
for(;;)
|
2014-03-02 21:18:06 +01:00
|
|
|
{
|
2014-03-09 18:51:52 +01:00
|
|
|
if (!STREAM_lock(&stream))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (code == 2)
|
|
|
|
{
|
|
|
|
DATE_timer(&timer, FALSE);
|
|
|
|
if (timer < wait)
|
|
|
|
{
|
|
|
|
usleep(10000);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-02 21:18:06 +01:00
|
|
|
STREAM_close(&stream);
|
|
|
|
THROW(E_LOCK);
|
|
|
|
}
|
2014-03-09 18:51:52 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
file = CFILE_create(&stream, ST_WRITE | ST_CREATE | ST_DIRECT);
|
|
|
|
OBJECT_put(RETURN, file);
|
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
void SUBR_inp_out(ushort code)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CSTREAM *stream;
|
|
|
|
void **where;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
switch(code & 0x1F)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
case 0: where = &_default_in; break;
|
|
|
|
case 1: where = &_default_out; break;
|
|
|
|
default: where = &_default_err; break;
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
if (VALUE_is_null(PARAM))
|
|
|
|
{
|
|
|
|
stream = pop_stream(where);
|
|
|
|
if (stream)
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(stream);
|
2012-01-15 09:54:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
VALUE_conv_object(PARAM, (TYPE)CLASS_Stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
stream = PARAM->_object.object;
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_REF(stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
push_stream(where, stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE_VOID();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void free_list(void **list)
|
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
CSTREAM *stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
stream = pop_stream(list);
|
|
|
|
if (!stream)
|
|
|
|
return;
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(stream);
|
2012-01-15 09:54:57 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_exit_inp_out(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
free_list((void **)&_default_in);
|
|
|
|
free_list((void **)&_default_out);
|
|
|
|
free_list((void **)&_default_err);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_dfree(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_ENTER_PARAM(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
RETURN->type = T_LONG;
|
|
|
|
RETURN->_long.value = FILE_free(get_path(PARAM));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
void SUBR_debug(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2012-01-15 09:54:57 +01:00
|
|
|
const int NPARAM = 0;
|
|
|
|
STREAM *stream = get_default(2);
|
|
|
|
const char *s = DEBUG_get_current_position();
|
|
|
|
|
|
|
|
STREAM_write(stream, (void *)s, strlen(s));
|
|
|
|
STREAM_write(stream, ": ", 2);
|
|
|
|
|
|
|
|
SUBR_LEAVE();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2010-01-20 15:08:22 +01:00
|
|
|
|