home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
bbs
/
may94
/
util
/
edit
/
jade.lha
/
Jade
/
src
/
unix_misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-04-19
|
15KB
|
585 lines
/* unix_misc.c -- Miscellaneous functions for Unix
Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
This file is part of Jade.
Jade 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.
Jade 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.
You should have received a copy of the GNU General Public License
along with Jade; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "jade.h"
#include "jade_protos.h"
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <pwd.h>
#include <netdb.h>
_PR int fileexists(u_char *);
_PR u_long filemodtime(u_char *);
_PR void sys_misc_init(void);
_PR bool samefiles(u_char *, u_char *);
_PR u_char * filepart(u_char *);
_PR VALUE geterrstring(void);
_PR void doconmsg(u_char *);
_PR u_char * squirrelfile(u_char *);
_PR VALUE valsquirrelfile(u_char *);
_PR u_long getsystime(void);
_PR int addfilepart(u_char *, const u_char *, int);
bool
samefiles(u_char *file1, u_char *file2)
{
bool rc = FALSE;
struct stat stat1, stat2;
if(!stat(file1, &stat1))
{
if(!stat(file2, &stat2))
{
if((stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino))
rc = TRUE;
}
}
else
rc = !strcmp(file1, file2);
return(rc);
}
u_char *
filepart(u_char *fname)
{
u_char *tmp = strrchr(fname, '/');
if(tmp)
return(tmp + 1);
return(fname);
}
VALUE
geterrstring(void)
{
return(valstrdup(ERRNO_STRING));
}
void
doconmsg(u_char *msg)
{
fputs(msg, stderr);
}
u_char *
squirrelfile(u_char *fileName)
{
FILE *fh = fopen(fileName, "r");
if(fh)
{
struct stat stat;
if(!fstat(fileno(fh), &stat))
{
u_char *mem = mystralloc(stat.st_size + 1);
if(mem)
{
fread(mem, 1, stat.st_size, fh);
mem[stat.st_size] = 0;
fclose(fh);
return(mem);
}
else
settitle(NoMemMsg);
}
fclose(fh);
}
return(FALSE);
}
VALUE
valsquirrelfile(u_char *fileName)
{
FILE *fh = fopen(fileName, "r");
if(fh)
{
struct stat stat;
if(!fstat(fileno(fh), &stat))
{
VALUE mem = valstralloc(stat.st_size + 1);
if(mem)
{
fread(VSTR(mem), 1, stat.st_size, fh);
VSTR(mem)[stat.st_size] = 0;
fclose(fh);
return(mem);
}
else
settitle(NoMemMsg);
}
fclose(fh);
}
return(cmd_signal(sym_file_error, list_2(geterrstring(), valstrdup(fileName))));
}
u_long
getsystime(void)
{
return(time(NULL));
}
int
addfilepart(u_char *buf, const u_char *part, int bufLen)
{
int bufend = strlen(buf);
int partlen = strlen(part);
if((bufend > 0) && (buf[bufend-1] != '/'))
{
if(++bufend >= bufLen)
return(FALSE);
buf[bufend-1] = '/';
buf[bufend] = 0;
}
if((bufend + partlen) >= bufLen)
return(FALSE);
strcpy(buf + bufend, part);
return(TRUE);
}
_PR VALUE cmd_delete_file(VALUE file);
DEFUN("delete-file", cmd_delete_file, subr_delete_file, (VALUE file), V_Subr1, DOC_delete_file) /*
::doc:delete_file::
(delete-file FILE-NAME)
Attempts to delete the file called FILE-NAME.
::end:: */
{
DECLARE1(file, STRINGP);
if(!unlink(VSTR(file)))
return(sym_t);
return(signalfileerror(file));
}
_PR VALUE cmd_rename_file(VALUE src, VALUE dst);
DEFUN("rename-file", cmd_rename_file, subr_rename_file, (VALUE src, VALUE dst), V_Subr2, DOC_rename_file) /*
::doc:rename_file::
(rename-file SRC DEST)
Tries to rename the file SRC as DEST, this doesn't work across filesystems, or
if a file DEST already exists.
::end:: */
{
DECLARE1(src, STRINGP);
DECLARE2(dst, STRINGP);
if(!rename(VSTR(src), VSTR(dst)))
return(sym_t);
return(signalfileerror(list_2(src, dst)));
}
_PR VALUE cmd_copy_file(VALUE src, VALUE dst);
DEFUN("copy-file", cmd_copy_file, subr_copy_file, (VALUE src, VALUE dst), V_Subr2, DOC_copy_file) /*
::doc:copy_file::
(copy-file SRC DEST)
Copies the file called SRC to the file DEST.
::end:: */
{
VALUE res = sym_t;
int srcf;
DECLARE1(src, STRINGP);
DECLARE2(dst, STRINGP);
srcf = open(VSTR(src), O_RDONLY);
if(srcf != -1)
{
int dstf = open(VSTR(dst), O_WRONLY | O_CREAT, 0666);
if(dstf != -1)
{
struct stat statb;
int rd;
if(fstat(srcf, &statb) == 0)
chmod(VSTR(dst), statb.st_mode);
do {
u_char buf[BUFSIZ];
int wr;
rd = read(srcf, buf, BUFSIZ);
if(rd < 0)
{
res = signalfileerror(src);
break;
}
wr = write(dstf, buf, rd);
if(wr != rd)
{
res = signalfileerror(dst);
break;
}
} while(rd != 0);
close(dstf);
}
else
res = signalfileerror(dst);
close(srcf);
}
else
res = signalfileerror(src);
return(res);
}
_PR VALUE cmd_file_readable_p(VALUE file);
DEFUN("file-readable-p", cmd_file_readable_p, subr_file_readable_p, (VALUE file), V_Subr1, DOC_file_readable_p) /*
::doc:file_readable_p::
(file-readable-p FILE)
Returns t if FILE available for reading from.
::end:: */
{
DECLARE1(file, STRINGP);
if(!access(VSTR(file), R_OK))
return(sym_t);
return(sym_nil);
}
_PR VALUE cmd_file_writeable_p(VALUE file);
DEFUN("file-writeable-p", cmd_file_writeable_p, subr_file_writeable_p, (VALUE file), V_Subr1, DOC_file_writeable_p) /*
::doc:file_writeable_p::
(file-writeable-p FILE)
Returns t if FILE available for writing to.
::end:: */
{
DECLARE1(file, STRINGP);
if(!access(VSTR(file), W_OK))
return(sym_t);
return(sym_nil);
}
_PR VALUE cmd_file_exists_p(VALUE file);
DEFUN("file-exists-p", cmd_file_exists_p, subr_file_exists_p, (VALUE file), V_Subr1, DOC_file_exists_p) /*
::doc:file_exists_p::
(file-exists-p FILE)
Returns t if FILE exists.
::end:: */
{
DECLARE1(file, STRINGP);
if(!access(VSTR(file), F_OK))
return(sym_t);
return(sym_nil);
}
int
fileexists(u_char *fileName)
{
if(!access(fileName, F_OK))
{
struct stat statb;
if(!stat(fileName, &statb) && !S_ISDIR(statb.st_mode))
return(TRUE);
}
return(FALSE);
}
_PR VALUE cmd_file_regular_p(VALUE file);
DEFUN("file-regular-p", cmd_file_regular_p, subr_file_regular_p, (VALUE file), V_Subr1, DOC_file_regular_p) /*
::doc:file_regular_p::
(file-regular-p FILE)
Returns t if FILE is a ``normal'' file, ie, not a directory, device, symbolic
link, etc...
::end:: */
{
struct stat statb;
DECLARE1(file, STRINGP);
if(!stat(VSTR(file), &statb))
{
if(S_ISREG(statb.st_mode))
return(sym_t);
}
return(sym_nil);
}
_PR VALUE cmd_file_directory_p(VALUE file);
DEFUN("file-directory-p", cmd_file_directory_p, subr_file_directory_p, (VALUE file), V_Subr1, DOC_file_directory_p) /*
::doc:file_directory_p::
(file-directory-p FILE)
Returns t if FILE is a directory.
::end:: */
{
struct stat statb;
DECLARE1(file, STRINGP);
if(!stat(VSTR(file), &statb))
{
if(S_ISDIR(statb.st_mode))
return(sym_t);
}
return(sym_nil);
}
_PR VALUE cmd_file_symlink_p(VALUE file);
DEFUN("file-symlink-p", cmd_file_symlink_p, subr_file_symlink_p, (VALUE file), V_Subr1, DOC_file_symlink_p) /*
::doc:file_symlink_p::
(file-symlink-p FILE)
Returns t if FILE is a symbolic link to another file.
::end:: */
{
struct stat statb;
DECLARE1(file, STRINGP);
if(!stat(VSTR(file), &statb))
{
if(S_ISLNK(statb.st_mode))
return(sym_t);
}
return(sym_nil);
}
_PR VALUE cmd_file_owner_p(VALUE file);
DEFUN("file-owner-p", cmd_file_owner_p, subr_file_owner_p, (VALUE file), V_Subr1, DOC_file_owner_p) /*
::doc:file_owner_p::
(file-owner-p FILE)
Returns t if the ownership (uid & gid) of file FILE (a string) is the same
as that of any files written by the editor.
::end:: */
{
struct stat statb;
DECLARE1(file, STRINGP);
if(!stat(VSTR(file), &statb))
{
if((statb.st_uid == geteuid()) && (statb.st_gid == getegid()))
return(sym_t);
}
return(sym_nil);
}
_PR VALUE cmd_file_n