home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
new
/
util
/
edit
/
jade
/
src
/
unix_misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-15
|
18KB
|
741 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 <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <pwd.h>
#include <netdb.h>
#ifdef HAVE_STRERROR
# include <errno.h>
#else
extern int sys_nerr, errno;
extern char *sys_errlist[];
#endif
#ifdef ENVIRON_UNDEFINED
extern char **environ;
#endif
_PR bool file_exists(u_char *);
_PR u_long file_mod_time(u_char *);
_PR void sys_misc_init(void);
_PR bool same_files(u_char *, u_char *);
_PR u_char * file_part(u_char *);
_PR VALUE lookup_errno(void);
_PR void doconmsg(u_char *);
_PR VALUE read_file(u_char *);
_PR u_long sys_time(void);
_PR int add_file_part(u_char *, const u_char *, int);
_PR VALUE sys_expand_file_name(VALUE);
_PR VALUE sys_fully_qualify_file_name(VALUE);
bool
same_files(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 *
file_part(u_char *fname)
{
u_char *tmp = strrchr(fname, '/');
if(tmp)
return(tmp + 1);
return(fname);
}
VALUE
lookup_errno(void)
{
#ifdef HAVE_STRERROR
return(string_dup(strerror(errno)));
#else
if(errno >= sys_nerr)
return(string_dup(sys_errlist[errno]));
else
return(MKSTR("<error>"));
#endif
}
void
doconmsg(u_char *msg)
{
fputs(msg, stderr);
}
VALUE
read_file(u_char *fileName)
{
FILE *fh = fopen(fileName, "r");
if(fh)
{
struct stat stat;
if(!fstat(fileno(fh), &stat))
{
VALUE mem = make_string(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
mem_error();
}
fclose(fh);
}
return(cmd_signal(sym_file_error,
list_2(lookup_errno(), string_dup(fileName))));
}
u_long
sys_time(void)
{
return(time(NULL));
}
int
add_file_part(u_char *buf, const u_char *part, int bufLen)
{
int bufend = strlen(buf);
int partlen = strlen(part);
if((bufend > 0) && (buf[bufend-1] != '/') && (*part != '/'))
{
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_INT("delete-file", cmd_delete_file, subr_delete_file, (VALUE file), V_Subr1, DOC_delete_file, "fDelete 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(signal_file_error(file));
}
_PR VALUE cmd_rename_file(VALUE src, VALUE dst);
DEFUN_INT("rename-file", cmd_rename_file, subr_rename_file, (VALUE src, VALUE dst), V_Subr2, DOC_rename_file, "fRename file:\nFRename file `%s' as:") /*
::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(signal_file_error(list_2(src, dst)));
}
_PR VALUE cmd_copy_file(VALUE src, VALUE dst);
DEFUN_INT("copy-file", cmd_copy_file, subr_copy_file, (VALUE src, VALUE dst), V_Subr2, DOC_copy_file, "fCopy file:\nFCopy file `%s' to:") /*
::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 | O_TRUNC, 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 = signal_file_error(src);
break;
}
wr = write(dstf, buf, rd);
if(wr != rd)
{
res = signal_file_error(dst);
break;
}
} while(rd != 0);
close(dstf);
}
else
res = signal_file_error(dst);
close(srcf);
}
else
res = signal_file_error(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_writable_p(VALUE file);
DEFUN("file-writable-p", cmd_file_writable_p, subr_file_writable_p, (VALUE file), V_Subr1, DOC_file_writeable_p) /*
::doc:file_writeable_p::
file-writable-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);
}
bool
file_exists(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);