home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include "os.h"
- #include "swis.h"
- #include "global.h"
- #include "cmdparse.h"
- #include "iface.h"
- #include "icmp.h"
- #include "timer.h"
- #include "netuser.h"
- #include "tcp.h"
- #include "smtp.h"
- #include "misc.h"
- #include "arc.h"
-
- #define TICKSPERSEC (1000L / MSPTICK)
- #define TICKLIMIT (2 * TICKSPERSEC)
-
- static int Star(char *, char *);
- static int wildmat(char *, char *);
- static int create_dir(char *, char *, char *);
- static int create_ls(char *, char *, char *);
- static int isdir(char *);
-
- extern struct cmds attab[];
-
- void time_adj(char *itime, int adj)
- {
- BOOL carry = FALSE;
- char btime[8];
- int loop, temp;
-
- /* memcpy(&rtime[3], &itime[0], 5); */
- *((int *)&btime[0]) = adj * 100;
- if (adj < 0)
- *((int *)&btime[4]) = -1;
- else
- *((int *)&btime[4]) = 0;
-
- for (loop = 0; loop < 5; loop++)
- {
- temp = itime[loop] + btime[loop] + carry;
- itime[loop] = temp & 0xFF;
- carry = ((temp & 0x100) != 0);
- }
- /* memcpy(&itime[0], &ntime[3], 5); */
- }
-
- void check_time(void)
- {
- static int clockstart = 0;
- static clock_t clkval = 0;
- clock_t nticks;
- clock_t clkadj;
-
- if (!clockstart)
- {
- clockstart = 1;
- clkval = clock();
- return;
- }
-
- clkadj = clock();
- nticks = clkadj - clkval;
- clkval = clkadj;
- clkadj = nticks;
- nticks /= 5;
- clkval -= (clkadj - (nticks * 5));
-
- if (nticks > TICKLIMIT)
- nticks = TICKLIMIT;
-
- while (nticks-- > 0)
- {
- icmpclk();
- tick();
- iss();
- }
- }
-
- void filedir (char *name, int times, char *ret_str)
- {
- static int count;
- os_regset regs;
- char Buffer[21];
-
- if (times == 0) count = 0;
-
- /*
- * Make sure that the NULL is there incase we don't find anything
- */
- ret_str[0] = '\0';
-
- regs.r[0] = 9;
- regs.r[1] = (int)name;
- regs.r[2] = (int)Buffer;
- regs.r[3] = 1;
- regs.r[4] = count;
- regs.r[5] = 20;
- regs.r[6] = (int)"*";
-
- if (os_swix(OS_GBPB, ®s) != (os_error *)NULL)
- return;
-
- if (regs.r[4] <= 0) return;
-
- count = regs.r[4];
-
- strcpy(ret_str, Buffer);
- }
-
- char *dir(char *path, int full)
- {
- char *data;
- char *name;
- char *s;
-
- if ((data = malloc(80 * 80)) == NULL)
- {
- cwprintf(NULL, "Out of memory in ARC\r\n");
- return(NULL);
- }
-
- *data = '\0';
- name = "*";
-
- /* Root directory is a special case */
- if (path == NULLCHAR || *path == '\0')
- {
- path = "$";
- }
- else
- {
- if (strpbrk(path, "*?[]") != NULLCHAR || !isdir(path))
- {
- s = strrchr(path, '.');
- name = s + 1;
- *s = '\0';
- }
- }
-
- if (full)
- create_dir(path, name, data);
- else
- create_ls(path, name, data);
-
- return(data);
- }
-
- static int create_dir(char *path, char *pattern, char *data)
- {
- os_gbpbstr filedir;
- os_regset regs;
- char Buffer[61];
- int File_Type;
- int Length;
- char Text_Type[11];
- char Time5[10];
- char Date_Buffer[31];
- char Size_Buffer[21];
- char Text_Buffer[81];
- char Permissions[5];
-
- filedir.action = 11;
- filedir.file_handle = (int)path;
- filedir.data_addr = Buffer;
- filedir.number = 1;
- filedir.seq_point = 0;
- filedir.buf_len = 60;
- filedir.wild_fld = "*";
-
- if (os_gbpb(&filedir) != (os_error *)NULL)
- return(0);
-
- while (filedir.seq_point > 0)
- {
- if (wildmat(Buffer + 29, pattern))
- {
- if (Buffer[16] == 1)
- {
- memcpy(&File_Type, Buffer + 1, sizeof(int));
- File_Type &= 0x0FFF;
-
- regs.r[0] = 18;
- regs.r[2] = File_Type;
-
- if (os_swix(OS_FSControl, ®s) != (os_error *)NULL)
- return(0);
-
- memcpy(Text_Type + 0, ®s.r[2], 4);
- memcpy(Text_Type + 4, ®s.r[3], 4);
- Text_Type[8] = '\0';
-
- *Permissions = '\0';
- if (Buffer[12] & 0x08) strcat(Permissions, "L");
- if (Buffer[12] & 0x02) strcat(Permissions, "W");
- if (Buffer[12] & 0x01) strcat(Permissions, "R");
- }
- else
- {
- strcpy(Text_Type, "Directory");
- strcpy(Permissions, "D");
- if (Buffer[12] & 0x08) strcat(Permissions, "L");
- }
-
- memcpy(Time5 + 0, Buffer + 4, 4);
- memcpy(Time5 + 4, Buffer + 0, 1);
- memcpy(&Length, Buffer + 8, 4);
-
- regs.r[0] = (int)Time5;
- regs.r[1] = (int)Date_Buffer;
- regs.r[2] = 30;
- regs.r[3] = (int)"%24:%MI:%SE %DY-%M3-%CE%YR";
-
- if (os_swix(OS_ConvertDateAndTime, ®s) != (os_error *)NULL)
- return(0);
-
- regs.r[0] = Length;
- regs.r[1] = (int)Size_Buffer;
- regs.r[2] = 20;
-
- if (os_swix(OS_ConvertFixedFileSize, ®s) != (os_error *)NULL)
- return(0);
-
- sprintf(Text_Buffer, "%-11s%-4s%-9s %s %s\n",
- Buffer + 29, Permissions, Text_Type,
- Date_Buffer, Size_Buffer);
- strcat(data, Text_Buffer);
- }
-
- if (os_gbpb(&filedir) != (os_error *)NULL)
- return(0);
- }
-
- return(1);
- }
-
- static int create_ls(char *path, char *pattern, char *data)
- {
- os_gbpbstr filedir;
- char Buffer[61];
-
- filedir.action = 9;
- filedir.file_handle = (int)path;
- filedir.data_addr = Buffer;
- filedir.number = 1;
- filedir.seq_point = 0;
- filedir.buf_len = 60;
- filedir.wild_fld = "*";
-
- if (os_gbpb(&filedir) != (os_error *)NULL)
- return(0);
-
- while (filedir.seq_point > 0)
- {
- if (wildmat(Buffer, pattern))
- {
- strcat(data, Buffer);
- strcat(data, "\n");
- }
-
- if (os_gbpb(&filedir) != (os_error *)NULL)
- return(0);
- }
-
- return(1);
- }
-
- static int isdir(char *name)
- {
- os_regset regs;
-
- regs.r[0] = 13;
- regs.r[1] = (int)"*";
- regs.r[4] = (int)name;
-
- if (os_swix(OS_File, ®s) != (os_error *)NULL)
- return(0);
-
- if (regs.r[0] == 2)
- return(1);
-
- return(0);
- }
-
- static int Star(register char *s, register char *p)
- {
- while (wildmat(s, p) == 0)
- if (*++s == '\0')
- return(0);
- return(1);
- }
-
- static int wildmat(register char *s, register char *p)
- {
- register int last;
- register int matched;
- register int reverse;
- char name[21];
- char *t;
-
- for (t = name; (*t = tolower(*s)) != '\0'; t++, s++);
- s = name;
-
- for ( ; *p; s++, p++)
- switch (*p) {
- case '\\':
- /* Literal match with following character; fall through. */
- p++;
- default:
- if (*s != *p)
- return(0);
- continue;
- case '?':
- /* Match anything. */
- if (*s == '\0')
- return(0);
- continue;
- case '*':
- /* Trailing star matches everything. */
- return(*++p ? Star(s, p) : 1);
- case '[':
- /* [^....] means inverse character class. */
- reverse = p[1] == '^';
- if (reverse)
- p++;
- for (last = 0400, matched = 0; *++p && *p != ']'; last = *p)
- /* This next line requires a good C compiler. */
- if (*p == '-' ? *s <= *++p && *s >= last : *s == *p)
- matched = 1;
- if (matched == reverse)
- return(0);
- continue;
- }
-
- /* For "tar" use, matches that end at a slash also work. --hoptoad!gnu */
- return(*s == '\0' || *s == '/');
- }
-
- int mkdir(char *path)
- {
- os_regset regs;
-
- regs.r[0] = 8;
- regs.r[1] = (int)path;
- regs.r[4] = 0;
-
- if (os_swix(OS_File, ®s) != (os_error *)NULL)
- return(-1);
-
- return(0);
- }
-
- int rmdir(char *path)
- {
- os_regset regs;
-
- regs.r[0] = 6;
- regs.r[1] = (int)path;
-
- if (os_swix(OS_File, ®s) != (os_error *)NULL)
- return(-1);
-
- return(0);
- }
-
- int access(char *name, int modes)
- {
- os_regset regs;
- int Attributes;
- char full_name[81];
-
- regs.r[0] = (int)name;
- regs.r[1] = (int)full_name;
- regs.r[2] = 80;
-
- if (os_swix(OS_GSTrans, ®s) != (os_error *)NULL)
- return(-1);
-
- regs.r[0] = 13;
- regs.r[1] = (int)"";
- regs.r[4] = (int)full_name;
-
- if (os_swix(OS_File, ®s) != (os_error *)NULL)
- return(-1);
-
- if (regs.r[0] == 0)
- return(-1);
-
- Attributes = regs.r[5] & 0xFF;
-
- switch (modes)
- {
- case 0: return(0);
- case 2: return((Attributes & 0x02) ? 0 : -1);
- case 4: return((Attributes & 0x01) ? 0 : -1);
- case 6: return(((Attributes & 0x01) && (Attributes & 0x02)) ? 0 : -1);
- }
-
- return(-1); /* !!! */
- }
-
- /* Some useful routins for ensureing the presence of a path
- when creating a file.
- */
-
- static os_error* file_create(char *f, int t, int len)
- {
- os_filestr file;
-
- file.action = (t==0x1000 || t==0x2000)?8:11;
- file.name = f;
- file.loadaddr = (t==0x1000 || t==0x2000)?0:t;
- file.execaddr = 0;
- file.start = 0;
- file.end = (t==0x1000 || t==0x2000)?0:len;
- return os_file(&file);
- }
-
- static os_error *file_objtype(char *f, int *t)
- {
- os_error *e;
- os_filestr file;
-
- file.action = 20;
- file.name = f;
- file.loadaddr = 0;
- file.execaddr = 0;
- file.start = 0;
- file.end = 0;
- e = os_file(&file);
-
- if (e==NULL)
- *t = file.action;
-
- return e;
- }
-
- int check_path(char *path)
- {
- int t;
- os_error *e;
-
- if (e = file_objtype(path, &t), e!=NULL)
- return (int)e;
- return t;
- }
-
- os_error* file_settype(char *f, int t)
- {
- os_filestr file;
-
- file.action = 18;
- file.name = f;
- file.loadaddr = t;
- return os_file(&file);
- }
-
- os_error *create_path(char *path, int size, int type)
- {
- os_error *e = NULL;
- char buf[256];
- char *p = buf;
- char *s = path;
- int t;
-
- os_error file_in_path =
- { 0, "File in specified path" };
-
- os_error obj_is_dir =
- { 0, "Specified file is a directory" };
-
- while (*s>' ')
- {
- while (*s!='.' && *s>' ')
- *p++ = *s++;
-
- *p = '\0';
-
- if (e = file_objtype(buf, &t), e!=NULL)
- return e;
-
- if (t==0)
- {
- if (e = file_create(buf, (*s>' ')?0x1000:type, (*s>' ')?0:size), e!=NULL)
- return e;
- }
- else if (*s>' ' && t==1)
- return &file_in_path;
- else if (*s<=' ' && t==2)
- return &obj_is_dir;
-
- if (*s=='.')
- *p++ = *s++;
- }
-
- if (e==NULL)
- file_settype(path, type);
-
- return e;
- }
-
- int atoft(char *s)
- {
- os_regset r;
-
- r.r[0] = 31;
- r.r[1] = (int)s;
- if (os_swix(XOS_FSControl, &r))
- return -1;
- return r.r[2];
- }
-
- char *fttoa(int n)
- {
- static char s[9];
- char *p;
-
- os_regset r;
-
- r.r[0] = 18;
- r.r[2] = n;
- if (n<0 || os_swix(XOS_FSControl, &r))
- return "Unknown";
-
- *(int *)s = r.r[2];
- *(int *)(s+4) = r.r[3];
- s[8] = '\0';
-
- if (p = strchr(s, ' '), p!=NULL)
- *p = '\0';
-
- return s;
- }
-
- char *fttola(int n)
- {
- char *p;
- char *s = fttoa(n);
- for (p=s; *p; p++)
- *p = tolower(*p);
- return s;
- }
-