home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
APPS
/
dm14.lzh
/
misc.c
< prev
next >
Wrap
Text File
|
1996-08-31
|
26KB
|
1,036 lines
/* miscellaneous commands */
/* file: misc.c */
/* functions in this file: commands(), getcwd(), tagfile(), copy(), */
/* lineinput(), delete(), changedir(), logdev(), makdir(), attr(), */
/* list(), waitkey(), move(), renfile(), infoscreen(), get_info(), */
/* getattr(), freedev() */
#include <stdio.h>
#include <modes.h>
#include <strings.h>
#include <errno.h>
#include <direct.h>
#include <sgstat.h>
#include <module.h>
#include "screen.h"
#include "diskmaster.h"
commands()
{
gotoxy(1,24);
flush_kbd();
cleol();
printf(" Attr Copy Rename Del Newdir Makdir mOve Tag/untag List Unarc Help Quit Shell");
foreground(RED);
gotoxy(2,24);
putc('A',stdout);
gotoxy(7,24);
putc('C',stdout);
gotoxy(12,24);
putc('R',stdout);
gotoxy(19,24);
putc('D',stdout);
gotoxy(23,24);
putc('N',stdout);
gotoxy(30,24);
putc('M',stdout);
gotoxy(38,24);
putc('O',stdout);
gotoxy(42,24);
putc('T',stdout);
gotoxy(52,24);
putc('L',stdout);
gotoxy(57,24);
putc('U',stdout);
gotoxy(63,24);
putc('H',stdout);
gotoxy(68,24);
putc('Q',stdout);
gotoxy(73,24);
putc('S',stdout);
gotoxy(1,24);
foreground(WHITE);
}
char *
getcwd(linebuf,len)
char *linebuf;
int len;
{
char *command = "pd >/pipe/getcwdpipe";
char *getcwdpipe, *endln;
int pipe_path;
/* create named pipe */
getcwdpipe = index(command,'/');
/* start up the command */
system(command);
/* pipe the output to string linebuf */
if((pipe_path = open(getcwdpipe,S_IREAD)) == -1) {
open_err(getcwdpipe,errno);
return(NULL);
}
*linebuf = '\0';
read(pipe_path,linebuf,len);
if((endln = index(linebuf,'\n')) < linebuf + len) {
*endln = '\0';
}
/* terminate */
close(pipe_path);
unlink(getcwdpipe);
return(linebuf);
}
tagfile(pos,tagged)
int pos;
int *tagged;
{
extern short *attrptr;
if(attrptr[pos] == TAG) {
attrptr[pos] &= 0xFF;
*tagged -= 1;
} else {
attrptr[pos] += TAG;
*tagged += 1;
}
}
int
copy(pos)
int pos;
{
extern char **nameptr;
extern short *attrptr;
char *copycmd;
char *copyparam, *malloc();
int ch;
int rewrite;
mod_exec *modloadp(), *modp;
gotoxy(1,24);
cleol();
if((attrptr[pos] & 0x80) == 0x80) {
printf("%c Can't copy directories",7);
sleep(5);
commands();
return(FAIL);
}
if ((modp = modloadp("copy",0,NULL)) == NULL) {
error("Can't load command","COPY",errno);
return(FAIL);
}
copyparam = malloc(256);
copycmd = malloc(256);
strcpy(copycmd,"copy ");
printf(" %cCOPY %s TO: ",7,nameptr[pos]);
lineinput(copyparam,80);
if(*copyparam == '\0') {
free(copycmd);
free(copyparam);
commands();
munlink(modp);
return(FAIL);
}
if(access(copyparam,S_IFDIR+S_IREAD+S_IWRITE) == 0) {
strcat(copyparam,"/");
strcat(copyparam,nameptr[pos]);
if(access(copyparam,S_IREAD+S_IWRITE) == 0) {
#ifdef MM1
CurOn(1);
#endif
gotoxy(1,24);
cleol();
printf(" %c%s EXISTS! Overwrite? (y/N)",7,copyparam);
do {
ch = toupper(getchar());
flush_kbd();
} while ((ch != 'Y') && (ch != 'N') && (ch != '\n'));
if(ch == 'Y') {
strcat(copycmd,"-r ");
} else {
free(copycmd);
free(copyparam);
commands();
munlink(modp);
#ifdef MM1
CurOff(1);
#endif
return(FAIL);
}
}
*(rindex(copyparam,'/')) = '\0';
rewrite = FAIL;
strcat(copycmd,"-w=");
strcat(copycmd,copyparam);
strcat(copycmd," ");
strcat(copycmd,nameptr[pos]);
} else if (access(copyparam,S_IREAD+S_IWRITE) != 0) {
rewrite = PASS;
strcat(copycmd,nameptr[pos]);
strcat(copycmd," ");
strcat(copycmd,copyparam);
} else {
gotoxy(1,24);
cleol();
printf(" %c%s EXISTS! Overwrite? (y/N)",7,copyparam);
do {
ch = toupper(getchar());
flush_kbd();
} while ((ch != 'Y') && (ch != 'N') && (ch != '\n'));
if(ch == 'Y') {
strcat(copycmd,"-r ");
strcat(copycmd,nameptr[pos]);
strcat(copycmd," ");
strcat(copycmd,copyparam);
if(index(copyparam,'/') != NULL) {
rewrite = FAIL;
}
} else {
free(copycmd);
free(copyparam);
commands();
munlink(modp);
#ifdef MM1
CurOff(1);
#endif
return(FAIL);
}
}
strcat(copycmd," >>>/nil");
system(copycmd);
free(copycmd);
free(copyparam);
#ifdef MM1
CurOff(1);
#endif
commands();
munlink(modp);
return(rewrite);
}
int
lineinput(copyparam,len)
char copyparam[];
int len;
{
int ch, i = 0;
#ifdef MM1
CurOn(1);
#endif
while (--len > 0 && (ch = getc(stdin)) != '\n' && ch != 0x1b) {
if (ch == '\b') {
if(--i >= 0) {
putc(ch,stdout);
putc(' ',stdout);
putc(ch,stdout);
len++;
continue;
} else {
len++;
continue;
}
}
copyparam[i++] = ch;
putc(ch,stdout);
}
if(ch == 0x1b) {
*copyparam = '\0';
return(FAIL);
}
copyparam[i] = '\0';
#ifdef MM1
CurOff(1);
#endif
return(PASS);
}
int
delete(pos)
int pos;
{
char *delcmd, *malloc();
char ch;
extern char **nameptr;
extern short *attrptr;
int dir = 0;
mod_exec *modloadp(), *modp;
if(pos == 0) {
gotoxy(1,24);
cleol();
printf(" %cCan't PARENT delete directory",7);
sleep(5);
commands();
return(FAIL);
}
delcmd = malloc(256);
if((attrptr[pos] & 0x80) == 0x80) {
if ((modp = modloadp("deldir",0,NULL)) == NULL) {
error("Can't load command","DELDIR",errno);
return(FAIL);
}
strcpy(delcmd,"deldir -q ");
dir = 1;
} else {
strcpy(delcmd,"del ");
}
gotoxy(1,24);
cleol();
printf(" %cDELETE %s ",7,(dir==0) ? "file" : "directory");
foreground(RED);
printf("%s",nameptr[pos]);
foreground(WHITE);
printf(" - ARE YOU SURE ? ");
#ifdef MM1
CurOn(1);
#endif
ch = getc(stdin);
flush_kbd();
if(toupper(ch) != 'Y') {
free(delcmd);
commands();
if (dir == 1) {
munlink(modp);
}
return(FAIL);
}
gotoxy(1,24);
cleol();
printf(" DELETING: %s",nameptr[pos]);
if(dir == 1) {
strcat(delcmd,nameptr[pos]);
strcat(delcmd," >>/nil");
system(delcmd);
} else {
unlink(nameptr[pos]);
}
free(delcmd);
sleep(1);
commands();
if (dir == 1) {
munlink(modp);
}
#ifdef MM1
CurOff(1);
#endif
return(PASS);
}
int
changedir(dirname,pos)
char *dirname;
int pos;
{
extern char **nameptr;
extern short *attrptr;
gotoxy(1,24);
cleol();
if(pos != -1) {
if((attrptr[pos] & 0x80) != 0x80) {
printf("%c %s is not a directory",7,nameptr[pos]);
sleep(5);
commands();
return(FAIL);
}
strcpy(dirname,nameptr[pos]);
if(strcmp(dirname,"==PARENT==") == 0) {
strcpy(dirname,"..");
}
}
chdir(dirname);
strcpy(dirname,".");
commands();
return(PASS);
}
int
logdev(dirname)
char *dirname;
{
char temp[32];
int success;
#ifdef MM1
CurOn(1);
#endif
gotoxy(1,24);
cleol();
printf("%c Change Device to: ",7);
success = lineinput(temp,32);
if(success == FAIL) {
commands();
return(FAIL);
}
if(access(temp,S_IFDIR+S_IREAD) != 0) {
open_err(temp,errno);
commands();
return(FAIL);
}
strcpy(dirname,temp);
commands();
#ifdef MM1
CurOff(1);
#endif
return(PASS);
}
#define DIRPERM S_IFDIR+S_IREAD+S_IWRITE+S_IEXEC+S_IOREAD+S_IOWRITE+S_IOEXEC
int
makedir()
{
char *dirpath, *malloc();
int x, success;
dirpath = malloc(256);
gotoxy(1,24);
cleol();
printf(" Directory to create: ");
success = lineinput(dirpath,54);
if((success == FAIL) || (*dirpath == '\0')) {
free(dirpath);
commands();
return(FAIL);
}
if(access(dirpath,S_IREAD) != 0) {
if(access(dirpath,S_IFDIR+S_IREAD) != 0) {
for(x=0;x<=strlen(dirpath);x++) {
*(dirpath+x) = toupper(*(dirpath+x));
}
gotoxy(1,24);
cleol();
printf(" Creating directory: ");
foreground(RED);
printf("%s",dirpath);
foreground(WHITE);
if(makdir(dirpath,S_IREAD|S_IWRITE,DIRPERM) != 0) {
creat_err(dirpath,errno);
free(dirpath);
commands();
return(FAIL);
} else {
free(dirpath);
sleep(1);
commands();
return(PASS);
}
}
}
commands();
free(dirpath);
return(FAIL);
}
int
attr(pos)
int pos;
{
extern char **nameptr;
extern char **fildesptr;
struct fildes fdes, *fdptr;
char *attrcmd, *malloc();
char attr[9];
char *newattr;
char *pipe_file = "dm_tmpXXXXXX";
char *pipeptr;
int pipe_path, success, path;
mod_exec *modloadp(), *modp;
if ((modp = modloadp("attr",0,NULL)) == NULL) {
error("Can't load command","ATTR",errno);
return(FAIL);
}
fdptr = &fdes;
attrcmd = malloc(80);
newattr = malloc(30);
mktemp(pipe_file);
strcpy(attrcmd,"attr ");
strcat(attrcmd,nameptr[pos]);
strcat(attrcmd," >/pipe/");
strcat(attrcmd,pipe_file);
system(attrcmd);
pipeptr = index(attrcmd,'/');
if((pipe_path = open(pipeptr,S_IREAD)) == -1) {
open_err(pipeptr,errno);
munlink(modp);
return(FAIL);
}
attr[0] = '\0';
read(pipe_path,attr,8);
attr[8] = '\0';
close(pipe_path);
unlink(pipeptr);
gotoxy(1,24);
cleol();
printf(" %cAttributes of %s are: %s. Change to: ",7,nameptr[pos],attr);
success = lineinput(newattr,30);
if((success == FAIL) || (*newattr == '\0')) {
free(attrcmd);
free(newattr);
commands();
munlink(modp);
return(FAIL);
}
if((index(newattr,'d') != NULL) || (index(newattr,'D') != NULL)) {
gotoxy(1,24);
cleol();
printf(" %cERROR: Can't add or remove directory attribute",7);
sleep(2);
free(attrcmd);
free(newattr);
commands();
munlink(modp);
return(FAIL);
}
strcpy(attrcmd,"attr ");
strcat(attrcmd,nameptr[pos]);
strcat(attrcmd," ");
strcat(attrcmd,newattr);
strcat(attrcmd," >>>/nil");
system(attrcmd);
if((path = open(nameptr[pos],S_IREAD)) == -1) {
if((path = open(nameptr[pos],S_IFDIR+S_IREAD)) == -1) {
;
}
}
if(path != -1) {
_gs_gfd(path,fdptr,sizeof(fdes));
_strass(fildesptr[pos],fdptr,sizeof(fdes));
close(path);
}
get_info(pos);
commands();
munlink(modp);
free(attrcmd);
free(newattr);
return(PASS);
}
int
list(pos)
int pos;
{
FILE *infile, *fopen();
char filename[32];
char *pipename = "dmtmpXXXXXX";
char *extensions = ".lzh.lha.arc.zoo.ar.zip.gz";
char pipepath[19];
char *ext, *match, *rindex(), *strstr();
int inchr, ch, x, count = 0, success;
char *buffer, *line;
extern char **nameptr;
extern struct sgbuf buff;
char *anykey = "Press any key...";
if(pos == HELP) {
strcpy(filename,"/dd/sys/dm.hlp");
} else {
strcpy(filename,nameptr[pos]);
}
ext = rindex(nameptr[pos],'.');
if (ext != NULL) {
for(x=0;x<=strlen(ext);x++) {
*(ext+x) = tolower(*(ext+x));
}
match = strstr(extensions,ext);
if (match != NULL) {
mktemp(pipename);
strcpy(pipepath,"/pipe/");
strcat(pipepath,pipename);
success = listarc(nameptr[pos],pipepath);
if (success == FAIL) return(FAIL);
strcpy(filename,pipepath);
}
}
if((infile = fopen(filename,"r")) == NULL) {
gotoxy(1,24);
cleol();
printf(" Can't open %s",filename);
sleep(2);
commands();
return(FAIL);
}
gotoxy(1,24);
cleol();
if(pos == HELP) {
printf(" Help is on the way...");
} else {
printf(" Listing file: %s",nameptr[pos]);
}
buffer = malloc(80);
line = buffer;
while((inchr = fgetc(infile)) != EOF) {
if(inchr == '\t') {
if((line-buffer) > (78 - (int)(buff.sg_tabsiz))) {
ungetc(inchr,infile);
inchr = '\n';
} else {
for(x=0;x<(int)(buff.sg_tabsiz);x++) {
*(line++) = ' ';
}
continue;
}
}
if(inchr == '\l') {
if(line == buffer) { /* ignore LF if it is after a CR */
continue;
} else {
inchr = '\n'; /* change it to CR if not */
}
}
if(inchr == '\f') { /* ignore FF char */
continue;
}
if(inchr == '\n') {
*(line++) = '\0';
gotoxy(2,count+3);
printf("%s",buffer);
line = buffer;
if(++count > MAXCNT -2) {
gotoxy(2,22);
printf("%s",anykey);
ch = waitkey();
if((ch == 'Q') || (ch == 3) || (ch == 0x1b)) {
fclose(infile);
clearwin(2,3,78,20);
commands();
return(PASS);
}
count = 0;
clearwin(2,3,78,20);
}
continue;
}
if((line - buffer) < 78) {
*(line++) = inchr;
} else {
ungetc(inchr,infile);
*(line++) = '\0';
gotoxy(2,count+3);
printf("%s",buffer);
if(++count > MAXCNT -2) {
gotoxy(2,22);
printf("%s",anykey);
ch = waitkey();
if((ch == 'Q') || (ch == 3) || (ch == 0x1b)){
fclose(infile);
clearwin(2,3,78,20);
commands();
return(PASS);
}
count = 0;
clearwin(2,3,78,20);
}
line = buffer;
}
}
gotoxy(2,22);
printf("%s",anykey);
ch = waitkey();
fclose(infile);
clearwin(2,3,78,20);
commands();
return(PASS);
}
int
waitkey()
{
int ch=0, lb=0, rb=0, mx, my;
extern int mouse;
#ifdef MM1
#include <mouse.h>
MSRET ms;
#endif
do {
if(_gs_rdy(STDIN) > 0) {
ch = toupper(getc(stdin));
flush_kbd();
} else {
if(!mouse) {
#ifdef MM1
_gs_mouse(STDOUT,&ms);
if (ms.pt_valid == 1) {
lb = ms.pt_cbsa;
rb = ms.pt_cbsc;
mx = ms.pt_acx/8;
my = ms.pt_acy/8;
#else
if(MsReady()) {
MsRdAbs(&lb, &rb, &mx, &my);
gotoxy(mx,my);
#endif
}
}
}
} while((ch == 0) && (!lb) && (!rb));
if(rb) return('Q');
if(lb) return(' ');
return(ch);
}
int
move(pos)
int pos;
{
extern char **nameptr;
extern short *attrptr;
int dpath, success, ch;
char *olddev;
char *newdev;
char *olddir;
char *newdir;
char *movcmd;
char *destpath;
char *malloc();
mod_exec *modloadp(), *modp;
if ((modp = modloadp("move",0,NULL)) == NULL) {
error("Can't load command","MOVE",errno);
return(FAIL);
}
olddev = malloc(32);
newdev = malloc(32);
olddir = malloc(256);
newdir = malloc(256);
movcmd = malloc(256);
destpath = malloc(256);
gotoxy(1,24);
cleol();
printf(" Move %s to: ",nameptr[pos]);
success = lineinput(newdir,80);
if((success == FAIL) || (*newdir == '\0')) {
free(olddev);
free(newdev);
free(olddir);
free(newdir);
free(movcmd);
munlink(modp);
return(FAIL);
}
getcwd(olddir,256);
dpath = open("@",S_IREAD);
_gs_devn(dpath,olddev);
close(dpath);
if(chdir(newdir) == FAIL) {
open_err(newdir,errno);
free(olddev);
free(newdev);
free(olddir);
free(newdir);
free(movcmd);
free(destpath);
munlink(modp);
return(FAIL);
}
dpath = open("@",S_IREAD);
_gs_devn(dpath,newdev);
close(dpath);
if(strcmp(olddev,newdev) != 0) {
gotoxy(1,24);
cleol();
printf(" %cCan't move to different device.",7);
sleep(5);
chdir(olddir);
commands();
free(olddev);
free(newdev);
free(olddir);
free(newdir);
free(movcmd);
free(destpath);
munlink(modp);
return(FAIL);
}
chdir(olddir);
strcpy(destpath,newdir);
strcat(destpath,"/");
strcat(destpath,nameptr[pos]);
if(access(destpath,S_IREAD+S_IWRITE) == 0) {
gotoxy(1,24);
cleol();
printf(" %cFile already exists at destination.",7);
sleep(5);
commands();
free(olddev);
free(newdev);
free(olddir);
free(newdir);
free(movcmd);
free(destpath);
munlink(modp);
return(FAIL);
}
strcpy(movcmd,"move ");
strcat(movcmd,nameptr[pos]);
strcat(movcmd," ");
strcat(movcmd,newdir);
strcat(movcmd," >>>/nil");
system(movcmd);
commands();
free(olddev);
free(newdev);
free(olddir);
free(newdir);
free(movcmd);
munlink(modp);
return(PASS);
}
int
renfile(pos)
int pos;
{
extern char **nameptr;
char *newname;
char *malloc();
int success;
gotoxy(1,24);
cleol();
newname = malloc(32);
printf(" %cRENAME %s TO: ",7,nameptr[pos]);
lineinput(newname,32);
if(*newname == '\0') {
free(newname);
commands();
return(FAIL);
}
if(index(newname,'/') == NULL) {
success = rename(nameptr[pos],newname);
if(success != 0) {
gotoxy(1,24);
cleol();
printf(" %cRename failed.",7);
if(errno == 218) {
printf(" File already exists!");
} else {
printf(" Error %d",errno);
}
sleep(1);
free(newname);
commands();
return(FAIL);
} else {
strcpy(nameptr[pos],newname);
free(newname);
commands();
return(PASS);
}
}
gotoxy(1,24);
cleol();
printf(" Can't rename %s to %s",nameptr[pos],newname);
sleep(1);
commands();
free(newname);
return(FAIL);
}
info_screen()
{
gotoxy(48,2);
putc(DTOP_TEE,stdout);
gotoxy(48,23);
putc(DBOT_TEE,stdout);
vert_doub(48,3,20);
gotoxy(56,3);
foreground(GREEN);
printf("File Information");
gotoxy(60,5);
printf("Filename");
gotoxy(59,7);
printf("Attributes");
gotoxy(57,10);
printf("Creation date");
gotoxy(57,12);
printf("Last modified");
foreground(WHITE);
}
union fsize {
char asize[4];
long lsize;
};
int
get_info(pos)
int pos;
{
extern char **nameptr;
extern char **fildesptr;
int scr_x, i;
struct fildes fdes, *desptr;
int path;
char attr[9];
char *spaces = " ";
char *getattr();
char *filename, *length, *malloc();
union fsize f;
strcpy(attr,"dsewrewr");
desptr = &fdes;
filename = malloc(32);
length = malloc(32);
if(strcmp(nameptr[pos],"==PARENT==") == 0) {
strcpy(filename,"..");
} else {
strcpy(filename,nameptr[pos]);
}
_strass(desptr,fildesptr[pos],sizeof(fdes));
scr_x = 48 + ((32 - strlen(filename))/2);
gotoxy(49,6);
printf(spaces);
gotoxy(scr_x,6);
printf("%s",filename);
gotoxy(60,8);
printf("%s",getattr(attr,fdes.fd_att));
gotoxy(49,9);
printf(spaces);
for(i=0;i<4;i++) f.asize[i] = fdes.fd_fsize[i];
sprintf(length,"Length: %ld bytes",f.lsize);
scr_x = 48 + ((32 - strlen(length))/2);
gotoxy(scr_x,9);
foreground(GREEN);
printf("Length:");
length = index(length,' ');
foreground(WHITE);
printf("%s",length);
gotoxy(60,11);
printf("%02d/%02d/%02d",fdes.fd_dcr[0],fdes.fd_dcr[1],fdes.fd_dcr[2]);
gotoxy(57,13);
printf("%02d/%02d/%02d %02d:%02d",fdes.fd_date[0],fdes.fd_date[1],fdes.fd_date[2],fdes.fd_date[3],fdes.fd_date[4]);
free(length);
free(filename);
return(PASS);
}
char *
getattr(attr,byte)
char *attr;
char byte;
{
int i, j, k;
for (i=0, j=128;i<8;i++,j/=2) {
k = byte & j;
if(!(k))
attr[i] = '-';
}
return(attr);
}
freedev()
{
int dpath, in, ppath;
char *inline, *dev, *freecmd, *pipepath, *malloc();
char *chrptr;
char pipename[10];
char *blanks = " ";
dev = malloc(32);
dpath = open("@",S_IREAD);
_gs_devn(dpath,dev);
close(dpath);
freecmd = malloc(32);
inline = malloc(80);
strcpy(pipename,"dm_XXXXXX");
strcpy(freecmd,"free /");
strcat(freecmd,dev);
strcat(freecmd," ");
strcat(freecmd,">>/nil >/pipe/");
strcat(freecmd,mktemp(pipename));
strcat(freecmd,"&");
system(freecmd);
freecmd[strlen(freecmd)-1] = '\0';
pipepath = rindex(freecmd,'>') + 1;
ppath = open(pipepath,S_IREAD);
if(ppath == -1) {
free(freecmd);
free(inline);
gotoxy(50,19);
printf(" Can't get size of /%s",dev);
free(dev);
return(0);
}
in = readln(ppath,inline,80);
in = readln(ppath,inline,80);
*(inline + in) = '\0';
chrptr = index(inline,'(');
if(chrptr == NULL) {
close(ppath);
unlink(pipepath);
free(freecmd);
free(inline);
gotoxy(50,19);
printf(" Can't get size of /%s",dev);
free(dev);
return(0);
}
*(chrptr) = '\0';
gotoxy(49,19);
printf("%s",blanks);
gotoxy(49+((31-strlen(inline))/2),19);
printf("%s",inline);
in = readln(ppath,inline,80);
*(inline + in) = '\0';
*(index(inline,',')) = '\0';
gotoxy(49,20);
printf("%s",blanks);
gotoxy(49+((31-strlen(inline))/2),20);
printf("%s",inline);
in = readln(ppath,inline,80);
*(inline + in) = '\0';
*(index(inline,' ')) = '\0';
gotoxy(49,21);
printf("%s",blanks);
gotoxy(49+((31-(strlen(inline)+11))/2),21);
printf("%s free bytes",inline);
close(ppath);
unlink(pipepath);
free(dev);
free(freecmd);
free(inline);
}
/* EOF misc.c */