home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
PC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-16
|
3KB
|
139 lines
/*
pc.c : Copyright Paul Healy, EI9GL, 1992.
Derived from bm.
Copyright 1986 Bdale Garbee, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
This file contains MSDOS specific functions
911215 : Added this header
920809 : Added option for command line editing with CED
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <signal.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dos.h>
#include <dir.h>
#include <conio.h>
#include "pc.h"
#include "rc.h"
void
setvideo(char *s)
{
#ifdef __TURBOC__
if (strncmp("bios", s, 4) == 0)
directvideo = 0;
else
directvideo = 1;
#endif
}
char *
getline(char *line, int size)
{
char *opt = getrc(editline);
if ( (opt == NULL) || (tolower(*opt) == 'n'))
return fgets(line, size, stdin);
else {
char *p;
line[0] = size;
line[1] = 0; /* INT 21h, AH=0Ah, DS:DX[1] = length of previous line */
p = cgets(line); /* that can be returned - just for safety */
putchar('\n'); /* emulate fgets */
return p;
}
}
static int
numfiles(char *name)
{
struct ffblk sbuf;
int n = 0;
if (findfirst(name, &sbuf, FA_HIDDEN | FA_SYSTEM | FA_DIREC ) == 0) {
n++;
while (findnext(&sbuf) == 0)
n++;
}
return n;
}
/* directory search utility for DOS */
void
filedir(char *name, int times, char *ret_str)
{
static struct ffblk sbuf;
if (times == 0) {
if (findfirst(name, &sbuf, FA_HIDDEN | FA_SYSTEM | FA_DIREC ))
sbuf.ff_name[0] = '\0'; /* make eof/directory mark */
}
else
if (findnext(&sbuf))
sbuf.ff_name[0] = '\0'; /* make eof/directory mark */
strcpy(ret_str, sbuf.ff_name);
strlwr(ret_str);
}
static int
getdir(char *name, String buf[], int max)
{
int i = 0;
filedir(name, 0, buf[i]);
while ((buf[i][0] != '\0') && (i<max) ) {
i++;
filedir(name, 1, buf[i]);
}
if (i)
qsort(buf, i, sizeof(String), strcmp);
return i;
}
void
initdir(Directory *d)
{
d->buf = NULL;
}
char *
readdir(Directory *d, char *name)
{
if (d->buf == NULL) { /* get directory listing to buffer */
int maxfound;
if ( (maxfound = numfiles(name)) == NULL)
return NULL; /* empty directory */
if ( (d->buf = (String *) malloc(sizeof(String) * maxfound) ) == NULL) {
fprintf(stderr, "readdir: no room for directory listing\n");
return NULL;
}
d->num = getdir(name, d->buf, maxfound);
if (d->num != maxfound)
fprintf(stderr, "readdir: warning, directory changed during listing\n");
d->ptr = 0;
}
else {
d->ptr = d->ptr + 1;
if (d->ptr == d->num) {
free(d->buf);
d->buf = NULL;
}
}
return d->ptr<d->num ? d->buf[d->ptr] : NULL;
}