2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbx_c_file.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-12-31 03:39:20 +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
|
|
|
|
2011-12-31 03:39:20 +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
|
|
|
|
2011-12-31 03:39:20 +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
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_C_FILE_C
|
|
|
|
|
|
|
|
#include "gbx_info.h"
|
|
|
|
|
|
|
|
#ifndef GBX_INFO
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <sys/stat.h>
|
2018-08-29 14:29:54 +02:00
|
|
|
#include <sys/ioctl.h>
|
2016-04-10 15:10:25 +02:00
|
|
|
#include <termios.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_list.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
|
|
|
|
#include "gbx_api.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
#include "gbx_class.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_project.h"
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_date.h"
|
2015-01-02 04:44:30 +01:00
|
|
|
#include "gbx_watch.h"
|
2016-04-10 15:10:25 +02:00
|
|
|
#include "gbx_signal.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gbx_c_file.h"
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
#define STREAM_FD STREAM_handle(THE_STREAM)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-07-10 23:49:11 +02:00
|
|
|
CFILE *CFILE_in, *CFILE_out, *CFILE_err;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
DECLARE_EVENT(EVENT_Read);
|
|
|
|
DECLARE_EVENT(EVENT_Write);
|
2016-05-23 00:03:16 +02:00
|
|
|
DECLARE_EVENT(EVENT_Resize);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
static GB_FUNCTION _read_func;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
static char _buffer[16];
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
static bool _term_init = FALSE;
|
|
|
|
static ushort _term_width = 0;
|
|
|
|
static ushort _term_height = 0;
|
|
|
|
static SIGNAL_CALLBACK *_SIGWINCH_callback = NULL;
|
|
|
|
static GB_FUNCTION _term_resize_func;
|
|
|
|
|
|
|
|
|
2017-02-26 00:10:45 +01:00
|
|
|
static void callback_read(int fd, int type, CSTREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
if (!STREAM_read_ahead(CSTREAM_TO_STREAM(stream)))
|
2017-02-26 00:10:45 +01:00
|
|
|
GB_Raise(stream, EVENT_Read, 0);
|
2015-01-02 04:44:30 +01:00
|
|
|
else
|
|
|
|
WATCH_little_sleep();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-26 00:10:45 +01:00
|
|
|
static void callback_write(int fd, int type, CSTREAM *stream)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-02-26 00:10:45 +01:00
|
|
|
GB_Raise(stream, EVENT_Write, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
static void cb_term_resize(int signum, intptr_t data)
|
|
|
|
{
|
|
|
|
_term_width = _term_height = 0;
|
2016-05-23 00:03:16 +02:00
|
|
|
if (CFILE_in)
|
|
|
|
GB_Raise(CFILE_in, EVENT_Resize, 0);
|
2016-04-10 15:10:25 +02:00
|
|
|
}
|
|
|
|
|
2017-02-26 00:10:45 +01:00
|
|
|
static void watch_stream(CSTREAM *_object, int mode, bool on)
|
|
|
|
{
|
|
|
|
STREAM *stream = &THIS_STREAM->stream;
|
|
|
|
int fd = STREAM_handle(stream);
|
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
if (mode & GB_ST_READ)
|
2017-02-26 00:10:45 +01:00
|
|
|
GB_Watch(fd, GB_WATCH_READ, (void *)(on ? callback_read : NULL), (intptr_t)THIS);
|
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
if (mode & GB_ST_WRITE)
|
2017-02-26 00:10:45 +01:00
|
|
|
GB_Watch(fd, GB_WATCH_WRITE, (void *)(on ? callback_write : NULL), (intptr_t)THIS);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
CFILE *CFILE_create(STREAM *stream, int mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-02-26 00:10:45 +01:00
|
|
|
CFILE *file = OBJECT_new(CLASS_File, NULL, NULL);
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF_KEEP(file);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if (stream)
|
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
*CSTREAM_TO_STREAM(file) = *stream;
|
2017-02-26 00:10:45 +01:00
|
|
|
//file->watch_fd = -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
if (mode & GB_ST_WATCH)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
2017-02-26 00:10:45 +01:00
|
|
|
watch_stream(&file->ob, mode, TRUE);
|
2010-11-16 02:49:18 +01:00
|
|
|
OBJECT_attach((OBJECT *)file, OP ? (OBJECT *)OP : (OBJECT *)CP, "File");
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
return file;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-07-10 23:49:11 +02:00
|
|
|
static CFILE *create_default_stream(FILE *file, int mode)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
STREAM stream;
|
2017-01-11 17:57:44 +01:00
|
|
|
bool tty = isatty(fileno(file));
|
2010-11-16 02:49:18 +01:00
|
|
|
|
|
|
|
CLEAR(&stream);
|
|
|
|
stream.type = &STREAM_buffer;
|
2017-01-11 17:57:44 +01:00
|
|
|
stream.common.available_now = !tty;
|
|
|
|
stream.common.no_read_ahead = tty;
|
2009-02-04 23:51:20 +01:00
|
|
|
stream.common.standard = TRUE;
|
2010-11-16 02:49:18 +01:00
|
|
|
stream.buffer.file = file;
|
2010-04-07 20:39:45 +02:00
|
|
|
STREAM_check_blocking(&stream);
|
2019-12-21 17:03:42 +01:00
|
|
|
return (CFILE *)OBJECT_REF(CFILE_create(&stream, mode));
|
2008-07-10 23:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFILE_init(void)
|
|
|
|
{
|
2020-11-09 17:38:28 +01:00
|
|
|
CFILE_in = create_default_stream(stdin, GB_ST_READ);
|
|
|
|
CFILE_out = create_default_stream(stdout, GB_ST_WRITE);
|
|
|
|
CFILE_err = create_default_stream(stderr, GB_ST_WRITE);
|
2008-07-10 23:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFILE_exit(void)
|
|
|
|
{
|
2013-03-30 14:51:10 +01:00
|
|
|
OBJECT_UNREF(CFILE_in);
|
|
|
|
OBJECT_UNREF(CFILE_out);
|
|
|
|
OBJECT_UNREF(CFILE_err);
|
2016-04-10 15:10:25 +02:00
|
|
|
|
|
|
|
if (_term_init)
|
|
|
|
SIGNAL_unregister(SIGWINCH, _SIGWINCH_callback);
|
2008-07-10 23:49:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFILE_init_watch(void)
|
|
|
|
{
|
2016-05-23 00:03:16 +02:00
|
|
|
bool has_term_func = GB_GetFunction(&_term_resize_func, PROJECT_class, "Application_Resize", "", "") == 0;
|
|
|
|
bool has_read_func = GB_GetFunction(&_read_func, PROJECT_class, "Application_Read", "", "") == 0;
|
|
|
|
|
|
|
|
if (has_term_func || has_read_func)
|
|
|
|
OBJECT_attach((OBJECT *)CFILE_in, (OBJECT *)PROJECT_class, "Application");
|
|
|
|
|
|
|
|
if (has_read_func)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
|
|
|
//fprintf(stderr, "watch stdin\n");
|
2017-02-26 00:10:45 +01:00
|
|
|
//CFILE_in->watch_fd = STDIN_FILENO;
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_Watch(STDIN_FILENO, GB_WATCH_READ, (void *)callback_read, (intptr_t)CFILE_in);
|
|
|
|
}
|
2008-07-10 23:49:11 +02:00
|
|
|
}
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD_VOID(File_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-02-04 23:51:20 +01:00
|
|
|
STREAM_close(&THIS->ob.stream);
|
2009-02-02 14:04:04 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(File_In)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnObject(CFILE_in);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(File_Out)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnObject(CFILE_out);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(File_Err)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnObject(CFILE_err);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD_VOID(Stat_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
STRING_unref(&THIS_STAT->path);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Type)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnInt(THIS_STAT->info.type);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_ReturnString(THIS_STAT->path);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Link)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (THIS_STAT->info.type == GB_STAT_LINK)
|
|
|
|
GB_ReturnNewZeroString(FILE_readlink(THIS_STAT->path));
|
|
|
|
else
|
2011-10-24 21:33:41 +02:00
|
|
|
GB_ReturnVoidString();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Mode)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnInt(THIS_STAT->info.mode);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Hidden)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnBoolean(THIS_STAT->info.hidden);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Size)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnLong(THIS_STAT->info.size);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_LastAccess)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
VALUE date;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
DATE_from_time(THIS_STAT->info.atime, 0, &date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnDate((GB_DATE *)&date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_LastChange)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
VALUE date;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
DATE_from_time(THIS_STAT->info.ctime, 0, &date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnDate((GB_DATE *)&date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Time)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
VALUE date;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
DATE_from_time(THIS_STAT->info.mtime, 0, &date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnDate((GB_DATE *)&date);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
static char *get_file_user(CFILE *_object)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
struct passwd *pwd;
|
2012-10-02 10:01:25 +02:00
|
|
|
uid_t uid = (uid_t)THIS_STAT->info.uid;
|
2010-11-16 02:49:18 +01:00
|
|
|
|
|
|
|
if (uid == 0)
|
|
|
|
return "root";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pwd = getpwuid(uid);
|
|
|
|
if (!pwd)
|
|
|
|
{
|
|
|
|
snprintf(_buffer, sizeof(_buffer), "%d", (int)uid);
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return pwd->pw_name;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_User)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnNewZeroString(get_file_user(THIS));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
static char *get_file_group(CFILE *_object)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
struct group *grp;
|
2012-10-02 10:01:25 +02:00
|
|
|
gid_t gid = (gid_t)THIS_STAT->info.gid;
|
2010-11-16 02:49:18 +01:00
|
|
|
|
|
|
|
if (gid == 0)
|
|
|
|
return "root";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grp = getgrgid(gid);
|
|
|
|
if (!grp)
|
|
|
|
{
|
|
|
|
snprintf(_buffer, sizeof(_buffer), "%d", (int)gid);
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return grp->gr_name;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Group)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnNewZeroString(get_file_group(THIS));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_SetUID)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnBoolean(THIS_STAT->info.mode & S_ISUID);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_SetGID)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnBoolean(THIS_STAT->info.mode & S_ISGID);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stat_Sticky)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnBoolean(THIS_STAT->info.mode & S_ISVTX);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2012-01-15 09:54:57 +01:00
|
|
|
|
|
|
|
BEGIN_PROPERTY(Stat_Auth)
|
|
|
|
|
|
|
|
char *auth = FILE_mode_to_string(THIS_STAT->info.mode);
|
|
|
|
|
|
|
|
GB_ReturnNewString(auth, FILE_buffer_length());
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#if 0
|
|
|
|
BEGIN_METHOD(CFILE_access, GB_INTEGER who; GB_INTEGER access)
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
bool ret;
|
|
|
|
int access = VARGOPT(access, GB_STAT_READ);
|
|
|
|
int who = VARG(who);
|
|
|
|
int mode = THIS_STAT->info.mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if ((access & GB_STAT_READ) == 0)
|
|
|
|
mode &= ~(S_IRUSR | S_IRGRP | S_IROTH);
|
|
|
|
if ((access & GB_STAT_WRITE) == 0)
|
|
|
|
mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
|
|
|
|
if ((access & GB_STAT_EXEC) == 0)
|
|
|
|
mode &= ~(S_IXUSR | S_IXGRP | S_IXOTH);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
switch(who)
|
|
|
|
{
|
|
|
|
case GB_STAT_USER: ret = mode & S_IRWXU; break;
|
|
|
|
case GB_STAT_GROUP: ret = mode & S_IRWXG; break;
|
|
|
|
case GB_STAT_OTHER: ret = mode & S_IRWXO; break;
|
|
|
|
default: ret = FALSE; break;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnBoolean(ret);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static void return_perm(CSTAT *_object, int rf, int wf, int xf)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
char perm[4];
|
|
|
|
char *p;
|
|
|
|
int mode = THIS_STAT->info.mode;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
p = perm;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if (mode & rf) *p++ = 'r';
|
|
|
|
if (mode & wf) *p++ = 'w';
|
|
|
|
if (mode & xf) *p++ = 'x';
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
*p = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnNewZeroString(perm);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CFILE_perm_user)
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
return_perm(THIS_STAT, S_IRUSR, S_IWUSR, S_IXUSR);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CFILE_perm_group)
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
return_perm(THIS_STAT, S_IRGRP, S_IWGRP, S_IXGRP);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CFILE_perm_other)
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
return_perm(THIS_STAT, S_IROTH, S_IWOTH, S_IXOTH);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_METHOD(CFILE_perm_get, GB_STRING user)
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
char *who;
|
|
|
|
char *user = GB_ToZeroString(ARG(user));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
who = get_file_user(THIS);
|
|
|
|
if (strcmp(user, who) == 0)
|
|
|
|
{
|
2011-01-04 03:35:48 +01:00
|
|
|
return_perm(THIS_STAT, S_IRUSR, S_IWUSR, S_IXUSR);
|
2010-11-16 02:49:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
who = get_file_group(THIS);
|
|
|
|
if (strlen(user) > 2 && user[0] == '*' && user[1] == '.' && strcmp(&user[2], who) == 0)
|
|
|
|
{
|
2011-01-04 03:35:48 +01:00
|
|
|
return_perm(THIS_STAT, S_IRGRP, S_IWGRP, S_IXGRP);
|
2010-11-16 02:49:18 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-01-04 03:35:48 +01:00
|
|
|
return_perm(THIS_STAT, S_IROTH, S_IWOTH, S_IXOTH);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
/*---- File path functions --------------------------------------------------*/
|
|
|
|
|
|
|
|
static char *_dir;
|
|
|
|
static char *_basename;
|
|
|
|
static char *_ext;
|
|
|
|
|
|
|
|
static void split_path(char *path)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = rindex(path, '/');
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
if (p == path)
|
|
|
|
{
|
|
|
|
_dir = "/";
|
|
|
|
_basename = path + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
_dir = path;
|
|
|
|
_basename = p + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dir = "";
|
|
|
|
_basename = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = rindex(_basename, '.');
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
_ext = p + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_ext = "";
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void return_path(void)
|
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
char *tmp = NULL;
|
2010-12-09 04:15:42 +01:00
|
|
|
int len = strlen(_dir);
|
2010-12-27 15:28:59 +01:00
|
|
|
char *test;
|
2010-12-09 04:15:42 +01:00
|
|
|
|
2010-12-27 15:28:59 +01:00
|
|
|
if (len)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
2011-09-07 14:38:29 +02:00
|
|
|
tmp = STRING_add(tmp, _dir, len);
|
2010-12-27 15:28:59 +01:00
|
|
|
test = _basename ? _basename : _ext;
|
|
|
|
if (tmp[len - 1] != '/' && *test != '/')
|
2011-09-07 14:38:29 +02:00
|
|
|
tmp = STRING_add_char(tmp, '/');
|
2010-11-16 02:49:18 +01:00
|
|
|
}
|
2010-12-27 15:28:59 +01:00
|
|
|
|
2013-01-06 17:13:31 +01:00
|
|
|
if (_basename && *_basename)
|
2011-09-07 14:38:29 +02:00
|
|
|
tmp = STRING_add(tmp, _basename, 0);
|
2010-12-27 15:28:59 +01:00
|
|
|
|
|
|
|
if (*_ext)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
2010-12-27 15:28:59 +01:00
|
|
|
if (*_ext != '.')
|
2011-09-07 14:38:29 +02:00
|
|
|
tmp = STRING_add_char(tmp, '.');
|
2010-12-27 15:28:59 +01:00
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
tmp = STRING_add(tmp, _ext, 0);
|
2010-11-16 02:49:18 +01:00
|
|
|
}
|
|
|
|
|
2011-09-07 14:38:29 +02:00
|
|
|
STRING_extend_end(tmp);
|
2010-11-16 02:49:18 +01:00
|
|
|
|
|
|
|
GB_ReturnString(tmp);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_Dir, GB_STRING path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
|
|
|
GB_ReturnNewZeroString(_dir);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_SetDir, GB_STRING path; GB_STRING new_dir)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
|
|
|
_dir = GB_ToZeroString(ARG(new_dir));
|
|
|
|
return_path();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_Name, GB_STRING path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
//if (LENGTH(path) && STRING(path)[LENGTH(path) - 1] == '/')
|
|
|
|
// LENGTH(path)--;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
path = GB_ToZeroString(ARG(path));
|
|
|
|
GB_ReturnNewZeroString(FILE_get_name(path));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_SetName, GB_STRING path; GB_STRING new_name)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
char *path;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if (LENGTH(path) && STRING(path)[LENGTH(path) - 1] == '/')
|
|
|
|
LENGTH(path)--;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
path = GB_ToZeroString(ARG(path));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(path);
|
2010-12-27 15:28:59 +01:00
|
|
|
_basename = GB_ToZeroString(ARG(new_name));
|
|
|
|
_ext = "";
|
2010-11-16 02:49:18 +01:00
|
|
|
return_path();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_Ext, GB_STRING path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_ReturnNewZeroString(_ext);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_SetExt, GB_STRING path; GB_STRING new_ext)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
|
|
|
_ext = GB_ToZeroString(ARG(new_ext));
|
|
|
|
return_path();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_BaseName, GB_STRING path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_ReturnNewZeroString(_basename);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_SetBaseName, GB_STRING path; GB_STRING new_basename)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
split_path(GB_ToZeroString(ARG(path)));
|
|
|
|
_basename = GB_ToZeroString(ARG(new_basename));
|
|
|
|
return_path();
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
static void error_CFILE_load_save(STREAM *stream)
|
2010-06-18 02:14:53 +02:00
|
|
|
{
|
2017-09-28 02:05:43 +02:00
|
|
|
STREAM_close(stream);
|
2010-06-18 02:14:53 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_Load, GB_STRING path)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
STREAM stream;
|
|
|
|
int64_t len;
|
|
|
|
int rlen;
|
2017-09-28 02:05:43 +02:00
|
|
|
char *str = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
STREAM_open(&stream, STRING_conv_file_name(STRING(path), LENGTH(path)), GB_ST_READ);
|
2017-09-28 02:05:43 +02:00
|
|
|
|
|
|
|
ON_ERROR_1(error_CFILE_load_save, &stream)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
|
|
|
STREAM_lof(&stream, &len);
|
|
|
|
if (len >> 31)
|
|
|
|
THROW(E_MEMORY);
|
2010-11-06 04:27:58 +01:00
|
|
|
|
|
|
|
if (len == 0)
|
|
|
|
{
|
2017-09-28 02:05:43 +02:00
|
|
|
char buffer[256];
|
2010-11-06 04:27:58 +01:00
|
|
|
|
|
|
|
str = NULL;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
len = STREAM_read_max(&stream, buffer, sizeof(buffer));
|
2011-09-07 14:38:29 +02:00
|
|
|
if (len) str = STRING_add(str, buffer, len);
|
2010-11-06 04:27:58 +01:00
|
|
|
if (len < sizeof(buffer))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rlen = len;
|
|
|
|
|
2013-06-29 18:11:32 +02:00
|
|
|
str = STRING_new(NULL, rlen);
|
|
|
|
rlen = STREAM_read_max(&stream, str, rlen);
|
|
|
|
str = STRING_extend(str, rlen);
|
2010-11-06 04:27:58 +01:00
|
|
|
}
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
STREAM_close(&stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-09-28 02:05:43 +02:00
|
|
|
STRING_free_later(str);
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnString(str);
|
|
|
|
}
|
|
|
|
END_ERROR
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(File_Save, GB_STRING path; GB_STRING data)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
STREAM stream;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
STREAM_open(&stream, STRING_conv_file_name(STRING(path), LENGTH(path)), GB_ST_CREATE);
|
2017-09-28 02:05:43 +02:00
|
|
|
|
|
|
|
ON_ERROR_1(error_CFILE_load_save, &stream)
|
2010-11-16 02:49:18 +01:00
|
|
|
{
|
|
|
|
STREAM_write(&stream, STRING(data), LENGTH(data));
|
|
|
|
STREAM_close(&stream);
|
|
|
|
}
|
|
|
|
END_ERROR
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
BEGIN_METHOD(File_IsRelative, GB_STRING path)
|
|
|
|
|
|
|
|
char *path = STRING(path);
|
|
|
|
int len = LENGTH(path);
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
{
|
|
|
|
GB_ReturnBoolean(FALSE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnBoolean(FILE_is_relative(path));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2015-10-17 03:59:12 +02:00
|
|
|
BEGIN_METHOD(File_IsHidden, GB_STRING path)
|
|
|
|
|
|
|
|
char *path = STRING(path);
|
|
|
|
int len = LENGTH(path);
|
|
|
|
int i;
|
2015-10-17 20:40:29 +02:00
|
|
|
char c;
|
2015-10-17 03:59:12 +02:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
{
|
2015-10-17 20:40:29 +02:00
|
|
|
if (path[i] != '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i > 0 && path[i - 1] != '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (i == (len - 1))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
c = path[i + 1];
|
|
|
|
if (c == '/')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (c == '.')
|
2015-10-17 03:59:12 +02:00
|
|
|
{
|
2015-10-17 20:40:29 +02:00
|
|
|
if (i == (len - 2))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (path[i + 2] == '/')
|
|
|
|
continue;
|
2015-10-17 03:59:12 +02:00
|
|
|
}
|
2015-10-17 20:40:29 +02:00
|
|
|
|
|
|
|
GB_ReturnBoolean(TRUE);
|
|
|
|
return;
|
2015-10-17 03:59:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnBoolean(FALSE);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2019-02-28 02:57:25 +01:00
|
|
|
BEGIN_METHOD(File_RealPath, GB_STRING path)
|
|
|
|
|
|
|
|
char *path = STRING_conv_file_name(STRING(path), LENGTH(path));
|
|
|
|
char *rpath = NULL;
|
|
|
|
|
|
|
|
if (!FILE_is_relative(path))
|
|
|
|
{
|
|
|
|
rpath = realpath(path, NULL);
|
|
|
|
path = rpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
GB_ReturnNewZeroString(path);
|
|
|
|
if (rpath) free(rpath);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
//---------------------------------------------------------------------------
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_Handle)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2016-03-26 14:09:20 +01:00
|
|
|
GB_ReturnInteger(STREAM_FD);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_ByteOrder)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
bool endian = EXEC_big_endian;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if (READ_PROPERTY)
|
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
if (THE_STREAM->common.swap)
|
2010-11-16 02:49:18 +01:00
|
|
|
endian = !endian;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_ReturnInteger(endian ? 1 : 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool val = VPROP(GB_INTEGER);
|
2019-12-21 17:03:42 +01:00
|
|
|
THE_STREAM->common.swap = endian ^ val;
|
2010-11-16 02:49:18 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_EndOfLine)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2019-12-21 17:03:42 +01:00
|
|
|
GB_ReturnInteger(THE_STREAM->common.eol);
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int eol = VPROP(GB_INTEGER);
|
|
|
|
|
|
|
|
if (eol >= 0 && eol <= 2)
|
2019-12-21 17:03:42 +01:00
|
|
|
THE_STREAM->common.eol = eol;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD_VOID(Stream_Close)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_close(THE_STREAM);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_EndOfFile)
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
GB_ReturnBoolean(THE_STREAM->common.eof);
|
2010-11-16 02:49:18 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
END_PROPERTY
|
2008-10-29 19:26:56 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_Blocking)
|
2008-10-29 19:26:56 +01:00
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2019-12-21 17:03:42 +01:00
|
|
|
GB_ReturnBoolean(STREAM_is_blocking(THE_STREAM));
|
2008-10-29 19:26:56 +01:00
|
|
|
else
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_blocking(THE_STREAM, VPROP(GB_BOOLEAN));
|
2008-10-29 19:26:56 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
2009-06-07 01:31:48 +02:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_PROPERTY(Stream_Tag)
|
2009-06-07 01:31:48 +02:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB_ReturnVariant(&THIS_STREAM->tag);
|
|
|
|
else
|
|
|
|
GB_StoreVariant(PROP(GB_VARIANT), POINTER(&(THIS_STREAM->tag)));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD_VOID(Stream_free)
|
2010-11-16 02:49:18 +01:00
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_close(THE_STREAM);
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_StoreVariant(NULL, POINTER(&(THIS_STREAM->tag)));
|
2009-06-07 01:31:48 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2019-05-20 16:14:17 +02:00
|
|
|
BEGIN_PROPERTY(Stream_Eof)
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
GB_ReturnBoolean(STREAM_eof(THE_STREAM));
|
2019-05-20 16:14:17 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2019-12-18 06:29:33 +01:00
|
|
|
BEGIN_PROPERTY(Stream_NullTerminatedString)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2019-12-21 17:03:42 +01:00
|
|
|
GB_ReturnBoolean(THE_STREAM->common.null_terminated);
|
2019-12-18 06:29:33 +01:00
|
|
|
else
|
2019-12-21 17:03:42 +01:00
|
|
|
THE_STREAM->common.null_terminated = VPROP(GB_BOOLEAN);
|
2019-12-18 06:29:33 +01:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
BEGIN_METHOD(Stream_ReadLine, GB_STRING escape)
|
|
|
|
|
|
|
|
char *escape;
|
2012-03-08 02:41:23 +01:00
|
|
|
char *str;
|
2011-02-16 23:01:35 +01:00
|
|
|
|
2012-04-23 18:02:35 +02:00
|
|
|
if (MISSING(escape))
|
2011-02-16 23:01:35 +01:00
|
|
|
escape = NULL;
|
2012-04-23 18:02:35 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
escape = GB_ToZeroString(ARG(escape));
|
|
|
|
if (!*escape)
|
|
|
|
escape = NULL;
|
|
|
|
}
|
2011-02-16 23:01:35 +01:00
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
str = STREAM_line_input(THE_STREAM, escape);
|
2012-03-08 02:41:23 +01:00
|
|
|
STRING_free_later(str);
|
|
|
|
GB_ReturnString(str);
|
2011-02-16 23:01:35 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
BEGIN_METHOD_VOID(Stream_Begin)
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_begin(THE_STREAM);
|
2011-04-04 02:40:32 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Stream_End)
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_end(THE_STREAM);
|
2011-04-04 02:40:32 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(Stream_Cancel)
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
STREAM_cancel(THE_STREAM);
|
2011-04-04 02:40:32 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2016-03-26 14:09:20 +01:00
|
|
|
BEGIN_PROPERTY(Stream_IsTerm)
|
|
|
|
|
|
|
|
GB_ReturnBoolean(isatty(STREAM_FD));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2017-02-26 00:10:45 +01:00
|
|
|
BEGIN_METHOD(Stream_Watch, GB_INTEGER mode; GB_BOOLEAN on)
|
|
|
|
|
|
|
|
int mode = VARG(mode);
|
|
|
|
|
2020-11-09 17:38:28 +01:00
|
|
|
if (mode != GB_ST_READ && mode != GB_ST_WRITE)
|
2017-02-26 00:10:45 +01:00
|
|
|
{
|
2020-11-09 17:38:28 +01:00
|
|
|
GB_Error(GB_ERR_ARG);
|
2017-02-26 00:10:45 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch_stream(THIS_STREAM, mode, VARG(on));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2011-05-27 20:46:56 +02:00
|
|
|
BEGIN_METHOD_VOID(StreamLines_next)
|
|
|
|
|
2012-03-08 02:41:23 +01:00
|
|
|
char *str;
|
|
|
|
|
2019-12-21 17:03:42 +01:00
|
|
|
if (STREAM_eof(THE_STREAM))
|
2011-05-27 20:46:56 +02:00
|
|
|
GB_StopEnum();
|
|
|
|
else
|
2012-03-08 02:41:23 +01:00
|
|
|
{
|
2019-12-21 17:03:42 +01:00
|
|
|
str = STREAM_line_input(THE_STREAM, NULL);
|
2012-03-08 02:41:23 +01:00
|
|
|
STRING_free_later(str);
|
|
|
|
GB_ReturnString(str);
|
|
|
|
}
|
2011-05-27 20:46:56 +02:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2016-05-23 00:03:16 +02:00
|
|
|
static void init_term_size(void *_object)
|
|
|
|
{
|
|
|
|
struct winsize winSize;
|
|
|
|
|
|
|
|
if (_term_width == 0 || _term_height == 0)
|
|
|
|
{
|
|
|
|
if (ioctl(STREAM_FD, TIOCGWINSZ, (char *)&winSize))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
|
|
|
_term_width = winSize.ws_col;
|
|
|
|
_term_height = winSize.ws_row;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_term_init)
|
|
|
|
{
|
|
|
|
_SIGWINCH_callback = SIGNAL_register(SIGWINCH, cb_term_resize, 0);
|
|
|
|
_term_init = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-26 14:09:20 +01:00
|
|
|
BEGIN_PROPERTY(StreamTerm_Name)
|
|
|
|
|
|
|
|
GB_ReturnNewZeroString(ttyname(STREAM_FD));
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_METHOD(StreamTerm_Resize, GB_INTEGER width; GB_INTEGER height)
|
|
|
|
|
|
|
|
struct winsize winSize = { 0 };
|
|
|
|
|
|
|
|
winSize.ws_row = (unsigned short)Max(1, Min(65535, VARG(height)));
|
|
|
|
winSize.ws_col = (unsigned short)Max(1, Min(65535, VARG(width)));
|
|
|
|
|
|
|
|
if (ioctl(STREAM_FD, TIOCSWINSZ, (char *)&winSize))
|
|
|
|
THROW_SYSTEM(errno, NULL);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2016-04-03 12:54:21 +02:00
|
|
|
static void handle_term_property(void *_object, void *_param, bool iflag, int flag)
|
|
|
|
{
|
2016-03-27 10:54:02 +02:00
|
|
|
struct termios ttmode;
|
|
|
|
|
|
|
|
if (tcgetattr(STREAM_FD, &ttmode))
|
|
|
|
THROW_SYSTEM(errno, "");
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
2016-04-03 12:54:21 +02:00
|
|
|
GB_ReturnBoolean(((iflag ? ttmode.c_iflag : ttmode.c_lflag) & flag) == flag);
|
2016-03-27 10:54:02 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (VPROP(GB_BOOLEAN))
|
2016-04-03 12:54:21 +02:00
|
|
|
{
|
|
|
|
if (iflag)
|
|
|
|
ttmode.c_iflag |= flag;
|
|
|
|
else
|
|
|
|
ttmode.c_lflag |= flag;
|
|
|
|
}
|
2016-03-27 10:54:02 +02:00
|
|
|
else
|
2016-04-03 12:54:21 +02:00
|
|
|
{
|
|
|
|
if (iflag)
|
|
|
|
ttmode.c_iflag &= ~flag;
|
|
|
|
else
|
|
|
|
ttmode.c_lflag &= ~flag;
|
|
|
|
}
|
2016-03-27 10:54:02 +02:00
|
|
|
|
|
|
|
if (tcsetattr(STREAM_FD, TCSANOW, &ttmode))
|
|
|
|
THROW_SYSTEM(errno, "");
|
|
|
|
}
|
2016-04-03 12:54:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(StreamTerm_Echo)
|
|
|
|
|
|
|
|
handle_term_property(_object, _param, FALSE, ECHO);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(StreamTerm_FlowControl)
|
|
|
|
|
|
|
|
handle_term_property(_object, _param, TRUE, IXON | IXOFF);
|
2016-03-27 10:54:02 +02:00
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
BEGIN_PROPERTY(StreamTerm_Width)
|
|
|
|
|
|
|
|
init_term_size(_object);
|
|
|
|
GB_ReturnInteger(_term_width);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(StreamTerm_Height)
|
|
|
|
|
|
|
|
init_term_size(_object);
|
|
|
|
GB_ReturnInteger(_term_height);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2017-02-26 00:10:45 +01:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC StreamLinesDesc[] =
|
2011-05-27 20:46:56 +02:00
|
|
|
{
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DECLARE_VIRTUAL(".Stream.Lines"),
|
2011-05-27 20:46:56 +02:00
|
|
|
|
|
|
|
GB_METHOD("_next", "s", StreamLines_next, NULL),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC StreamTermDesc[] =
|
2016-03-26 14:09:20 +01:00
|
|
|
{
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DECLARE_VIRTUAL(".Stream.Term"),
|
2016-03-26 14:09:20 +01:00
|
|
|
|
|
|
|
GB_PROPERTY_READ("Name", "s", StreamTerm_Name),
|
2016-03-27 10:54:02 +02:00
|
|
|
GB_PROPERTY("Echo", "b", StreamTerm_Echo),
|
2016-04-03 12:54:21 +02:00
|
|
|
GB_PROPERTY("FlowControl", "b", StreamTerm_FlowControl),
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_PROPERTY_READ("Width", "i", StreamTerm_Width),
|
|
|
|
GB_PROPERTY_READ("W", "i", StreamTerm_Width),
|
|
|
|
GB_PROPERTY_READ("Height", "i", StreamTerm_Height),
|
|
|
|
GB_PROPERTY_READ("H", "i", StreamTerm_Height),
|
2016-03-26 14:09:20 +01:00
|
|
|
GB_METHOD("Resize", NULL, StreamTerm_Resize, "(Width)i(Height)i"),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC StreamDesc[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_DECLARE("Stream", sizeof(CSTREAM)),
|
|
|
|
GB_NOT_CREATABLE(),
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_METHOD("_free", NULL, Stream_free, NULL),
|
2010-11-16 02:49:18 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_PROPERTY("ByteOrder", "i", Stream_ByteOrder),
|
|
|
|
GB_PROPERTY_READ("Handle", "i", Stream_Handle),
|
|
|
|
GB_PROPERTY("EndOfLine", "i", Stream_EndOfLine),
|
|
|
|
GB_METHOD("Close", NULL, Stream_Close, NULL),
|
|
|
|
GB_PROPERTY_READ("EndOfFile", "b", Stream_EndOfFile),
|
2019-12-18 06:29:33 +01:00
|
|
|
GB_PROPERTY("NullTerminatedString", "b", Stream_NullTerminatedString),
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_PROPERTY("Blocking", "b", Stream_Blocking),
|
|
|
|
GB_PROPERTY("Tag", "v", Stream_Tag),
|
|
|
|
GB_METHOD("ReadLine", "s", Stream_ReadLine, "[(Escape)s]"),
|
2016-03-26 14:09:20 +01:00
|
|
|
GB_PROPERTY_READ("IsTerm", "b", Stream_IsTerm),
|
2011-04-04 02:40:32 +02:00
|
|
|
|
2011-08-21 23:46:20 +02:00
|
|
|
GB_PROPERTY_SELF("Lines", ".Stream.Lines"),
|
2016-03-26 14:09:20 +01:00
|
|
|
GB_PROPERTY_SELF("Term", ".Stream.Term"),
|
2011-05-27 20:46:56 +02:00
|
|
|
|
2011-04-04 02:40:32 +02:00
|
|
|
GB_METHOD("Begin", NULL, Stream_Begin, NULL),
|
|
|
|
GB_METHOD("Send", NULL, Stream_End, NULL),
|
|
|
|
GB_METHOD("Drop", NULL, Stream_Cancel, NULL),
|
2017-02-26 00:10:45 +01:00
|
|
|
|
|
|
|
GB_METHOD("Watch", NULL, Stream_Watch, "(Mode)i(Watch)b"),
|
2019-05-20 16:14:17 +02:00
|
|
|
|
|
|
|
GB_PROPERTY_READ("Eof", "b", Stream_Eof),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC StatPermDesc[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DECLARE_VIRTUAL(".Stat.Perm"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_METHOD("_get", "s", CFILE_perm_get, "(UserOrGroup)s"),
|
|
|
|
GB_PROPERTY_READ("User", "s", CFILE_perm_user),
|
|
|
|
GB_PROPERTY_READ("Group", "s", CFILE_perm_group),
|
|
|
|
GB_PROPERTY_READ("Other", "s", CFILE_perm_other),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC StatDesc[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_DECLARE("Stat", sizeof(CSTAT)),
|
|
|
|
GB_NOT_CREATABLE(),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_METHOD("_free", NULL, Stat_free, NULL),
|
|
|
|
|
|
|
|
GB_PROPERTY_READ("Type", "i", Stat_Type),
|
|
|
|
GB_PROPERTY_READ("Mode", "i", Stat_Mode),
|
|
|
|
GB_PROPERTY_READ("Hidden", "b", Stat_Hidden),
|
|
|
|
GB_PROPERTY_READ("Size", "l", Stat_Size),
|
|
|
|
GB_PROPERTY_READ("Time", "d", Stat_Time),
|
|
|
|
GB_PROPERTY_READ("LastAccess", "d", Stat_LastAccess),
|
|
|
|
GB_PROPERTY_READ("LastModified", "d", Stat_Time),
|
|
|
|
GB_PROPERTY_READ("LastChange", "d", Stat_LastChange),
|
|
|
|
GB_PROPERTY_READ("User", "s", Stat_User),
|
|
|
|
GB_PROPERTY_READ("Group", "s", Stat_Group),
|
2011-08-22 01:50:51 +02:00
|
|
|
GB_PROPERTY_SELF("Perm", ".Stat.Perm"),
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_PROPERTY_READ("SetGID", "b", Stat_SetGID),
|
|
|
|
GB_PROPERTY_READ("SetUID", "b", Stat_SetUID),
|
|
|
|
GB_PROPERTY_READ("Sticky", "b", Stat_Sticky),
|
|
|
|
GB_PROPERTY_READ("Path", "s", Stat_Path),
|
|
|
|
GB_PROPERTY_READ("Link", "s", Stat_Link),
|
2012-01-15 09:54:57 +01:00
|
|
|
GB_PROPERTY_READ("Auth", "s", Stat_Auth),
|
2010-11-16 02:49:18 +01:00
|
|
|
|
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-04-10 15:10:25 +02:00
|
|
|
GB_DESC FileDesc[] =
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_DECLARE("File", sizeof(CFILE)),
|
|
|
|
GB_INHERITS("Stream"),
|
|
|
|
GB_NOT_CREATABLE(),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_METHOD("_free", NULL, File_free, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
// GB_STATIC_PROPERTY_READ("Separator", "s", CFILE_separator),
|
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_STATIC_PROPERTY_READ("In", "File", File_In),
|
|
|
|
GB_STATIC_PROPERTY_READ("Out", "File", File_Out),
|
|
|
|
GB_STATIC_PROPERTY_READ("Err", "File", File_Err),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_STATIC_METHOD("Dir", "s", File_Dir, "(Path)s"),
|
|
|
|
GB_STATIC_METHOD("Name", "s", File_Name, "(Path)s"),
|
|
|
|
GB_STATIC_METHOD("Ext", "s", File_Ext, "(Path)s"),
|
|
|
|
GB_STATIC_METHOD("BaseName", "s", File_BaseName, "(Path)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_STATIC_METHOD("SetDir", "s", File_SetDir, "(Path)s(NewDir)s"),
|
|
|
|
GB_STATIC_METHOD("SetName", "s", File_SetName, "(Path)s(NewName)s"),
|
|
|
|
GB_STATIC_METHOD("SetExt", "s", File_SetExt, "(Path)s(NewExt)s"),
|
|
|
|
GB_STATIC_METHOD("SetBaseName", "s", File_SetBaseName, "(Path)s(NewBaseName)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2015-09-13 01:21:44 +02:00
|
|
|
GB_STATIC_METHOD("IsRelative", "b", File_IsRelative, "(Path)s"),
|
2015-10-17 03:59:12 +02:00
|
|
|
GB_STATIC_METHOD("IsHidden", "b", File_IsHidden, "(Path)s"),
|
2015-09-13 01:21:44 +02:00
|
|
|
|
2011-02-16 23:01:35 +01:00
|
|
|
GB_STATIC_METHOD("Load", "s", File_Load, "(FileName)s"),
|
|
|
|
GB_STATIC_METHOD("Save", NULL, File_Save, "(FileName)s(Data)s"),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2019-02-28 02:57:25 +01:00
|
|
|
GB_STATIC_METHOD("RealPath", "s", File_RealPath, "(Path)s"),
|
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_EVENT("Read", NULL, NULL, &EVENT_Read),
|
|
|
|
GB_EVENT("Write", NULL, NULL, &EVENT_Write),
|
2016-05-23 00:03:16 +02:00
|
|
|
GB_EVENT("Resize", NULL, NULL, &EVENT_Resize),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-11-16 02:49:18 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|