home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
internet
/
netlite
/
NET
/
c
/
ARC
next >
Wrap
Text File
|
1993-04-13
|
10KB
|
378 lines
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "werr.h"
#include "os.h"
#include "swis.h"
#include "global.h"
#include "cmdparse.h"
#include "iface.h"
#include "timer.h"
#include "netuser.h"
#include "tcp.h"
#include "smtp.h"
#include "misc.h"
#include "arc.h"
#define TICKSPERSEC (1000L / MSPTICK)
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[];
extern char nospace[];
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;
while (nticks-- > 0)
{
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)
{
werr(0,nospace);
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. */