Category : Files from Magazines
Archive   : BYTEBRO.ZIP
Filename : BROWSE1.C

 
Output of file : BROWSE1.C contained in archive : BYTEBRO.ZIP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define BUFSIZE 4096
#define BUFSIZEL 4096l

#define STR_SIZE 1000

#define TRUE 1
#define FALSE 0

#define BELL 7
#define BS 8
#define TAB 9
#define LINEFEED 10
#define FORMFEED 12
#define CR 13
#define BACKTAB 15
#define ESC 27
#define SPACE 32

#define HOMEKEY 71
#define ENDKEY 79
#define UPKEY 72
#define DOWNKEY 80
#define PGUPKEY 73
#define PGDNKEY 81
#define LEFTKEY 75
#define INSKEY 82
#define RIGHTKEY 77
#define DELKEY 83
#define CTRLLEFTKEY 115
#define CTRLRIGHTKEY 116
#define F1 59
#define F2 60
#define F3 61
#define F4 62
#define F5 63
#define F6 64
#define F7 65
#define F8 66
#define F9 67
#define F10 68

/************************************/

int kbdstring(char buff[], int max_chars);
void getkey(void);

int fgetrecord(unsigned char buff[], int max_chars);
int fgetline(unsigned char buff[], int max_chars);
int fgetbyte(void);

int fprevrecord(void);
int fprevline(void);
int fprevbyte(void);

void gohome(void);
void find_text(void);
void showfile(void);
void fill_window(void);
void show_line_num(void);
void display_line(void);
void display_hex(void);
void adjust_line_number(int amount);

int get_video_mode(void);
void save_cursor(struct csavetype *csave);
void restore_cursor(struct csavetype *csave);
void scroll_up(unsigned char numlines);
void scroll_dn(unsigned char numlines);

int search(unsigned char *pat, int plen, unsigned char *str, int slen);
void search_setup(unsigned char *pat, int plen, int icase);
void gettext(int top, int left, int bottom, int right, int *destin);
void puttext(int top, int left, int bottom, int right, int *source);
void movetext(int top, int left, int bottom, int right, int destleft, int desttop);
void beep(void);
void textattr(short fg, long bg);
/************************************/

char *fileprompt = "Filename? ";
char *inv_arg = "Invalid command line arguments.";
char *notfound = "File not found.";
char *emptyfile = "File is empty.";
char *noaccess = "File in use. (Try later)";
char *presskey = "Press a key to continue.";

#define HELP_LINES 17
char *help[HELP_LINES] =
{
" File Browser 1.4 (c) 1992 Barry Nance, BYTE Magazine's Software Corner ",
" converted to Microsoft C 8.0, Jason Olasky 1993 ",
" ",
" ",
" Press: ",
" ESC, X, or Q.....Quit W.........Toggle line wrap ",
" T or HOME........Top of file H.........Toggle Hex/ASCII ",
" B or END.........Bottom of file D.........Shell to DOS ",
" N................Next file P.........Previous file ",
" PgUp or PgDn.....Up/Down a screenful ",
" ÄÙ or Spacebar..Next screenful ",
" \x1a.............Up/down/right/left ",
" F................Find text (any case) ",
" S................Search (exact case) ",
" A................Find/search again ",
" ",
" "
};

unsigned char string[STR_SIZE];
char string1[101];

char search_arg[81];
int search_flag;
int ignore_case;
int spos;

long line_number;
long linenum_save;

unsigned char CharCode;
unsigned char ScanCode;

int xesc = FALSE;
int eof_flag = FALSE;
int start_of_file = TRUE;
int line_end_char = 0;
int hex_mode = FALSE;
int wrap_mode = FALSE;

int start_save;
int eof_save;
int fbufblock_save;
int fbufndx_save;

unsigned top, left, bottom, right, screen_width, screen_height;
int left_relative_pos;

struct ftime1 {
unsigned ft_tsec: 5;
unsigned ft_min: 6;
unsigned ft_hour: 5;
unsigned ft_day: 5;
unsigned ft_month: 4;
unsigned ft_year: 7;
};

struct ftime2 {
unsigned int time;
unsigned int date;
};

union {
struct ftime1 ftimestruct1;
struct ftime2 ftimestruct2;
} ftime;

struct _find_t findblock;

struct _rccoord xypos;

char filename[80];
char drive[5];
char path[81];
char name[10];
char ext[5];

int count;
int flag;
int file_num;
int current_row;
int file_is_empty;

unsigned screen_rcds;
unsigned screen_lines_hold;
unsigned screen_lines;
unsigned lines_read;
unsigned rcds_read;
unsigned lines_displayed;

unsigned i, j, k;

int fh;
long filesize;
long curr_filepos;
long temp_long;
unsigned num_blocks;
int fbufndx;
int fbufbytes;
unsigned fbufblock;

char *filelist_ptr;
char *list_ptr;
char *file_buff_ptr;
int *screen_save_ptr;
int *dos_screen_save_ptr;
int *line_save;

short low_inten = 14; /* Yellow */
short high_inten = 15; /* Bright white */
short fg25 = 0; /* Black */
long bkcolor = 1L; /* Blue */
long bk25 = 7L; /* White */

struct csavetype
{
unsigned int curloc;
unsigned int curmode;
};

struct csavetype cursor_data;
struct csavetype my_cpos;
struct csavetype dos_cpos;

union REGS regs;

#define CSIZE 256 /* character set size */
static int skip[CSIZE]; /* increment to rightmost occurance of ch */
static int cmap[CSIZE]; /* upper to lowercase character map */

struct _videoconfig vc;
char linebuf[120];

/************************************/

void main(int argc, char *argv[])
{
_getvideoconfig(&vc);

file_buff_ptr = malloc(BUFSIZE+10);
if (file_buff_ptr == NULL)
{
sprintf(linebuf,"Insufficient memory.\n");
_outtext(linebuf);
return;
}
memset(file_buff_ptr, 0, BUFSIZE+10);

screen_save_ptr = malloc(4000);
if (screen_save_ptr == NULL)
{
free(file_buff_ptr);
sprintf(linebuf,"Insufficient memory.\n");
_outtext(linebuf);
return;
}

dos_screen_save_ptr = malloc(4000);
if (dos_screen_save_ptr == NULL)
{
free(file_buff_ptr);
free(screen_save_ptr);
sprintf(linebuf,"Insufficient memory.\n");
_outtext(linebuf);
return;
}

line_save = malloc(160);
if (line_save == NULL)
{
free(file_buff_ptr);
free(screen_save_ptr);
free(dos_screen_save_ptr);
sprintf(linebuf,"Insufficient memory.\n");
_outtext(linebuf);
return;

}

filelist_ptr = calloc(1001, 13);
if (filelist_ptr == NULL)
{
free(file_buff_ptr);
free(screen_save_ptr);
free(dos_screen_save_ptr);
free(line_save);
sprintf(linebuf,"Insufficient memory.\n");
_outtext(linebuf);
return;
}
list_ptr = filelist_ptr;
count = 0;

textattr(low_inten,bkcolor);
gettext(1, 1, 25, 80, screen_save_ptr);
save_cursor(&cursor_data);
_clearscreen(_GCLEARSCREEN);
top = 1; left = 1; bottom = 24; right = 80;

if (argc == 1)
{
_settextwindow(25,1, 25, 80);
textattr(fg25,bk25);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);
_settextposition(25, 1);
_outtext(fileprompt);
_settextposition(25, 11);
kbdstring(filename, 70);
goto p30;
}

if (argc == 2)
{
strcpy(filename, argv[1]);
goto p30;
}

if (argc == 6)
{
top = atoi(argv[2]);
left = atoi(argv[3]);
bottom = atoi(argv[4]);
right = atoi(argv[5]);
if (top == 0 || left == 0 || bottom == 0 || right == 0)
goto p20;
strcpy(filename, argv[1]);
goto p30;
}

p20:
_settextposition(25, 1);
textattr(fg25,bk25);
_outtext(inv_arg);
gohome();

p30:
if (strlen(filename) == 0)
gohome();

if ( (flag = _dos_findfirst(filename, _A_NORMAL, &findblock)) != 0 )
{
sprintf(linebuf,"\n%s\n", notfound);
_outtext(linebuf);
free(file_buff_ptr);
free(screen_save_ptr);
free(dos_screen_save_ptr);
free(filelist_ptr);
exit(1);
}

screen_width = right - left + 1;
screen_height = bottom - top + 1;

_splitpath(filename, drive, path, name, ext);

while (flag == 0)
{
strcpy(list_ptr, findblock.name);
list_ptr += 13;
count++;
if (count >= 1000)
break;
flag = _dos_findnext(&findblock);
}
list_ptr = filelist_ptr;
file_num = 1;

open_a_file:

_makepath(filename, drive, path, list_ptr, NULL);
fh = _open(filename, O_RDONLY | O_BINARY );

if (fh == -1)
{
beep();
_settextposition(25,1);
textattr(fg25,bk25);
if (errno == ENOENT)
_outtext(notfound);
else
_outtext(noaccess);
gohome();
}

_dos_getftime(fh, &ftime.ftimestruct2.date, &ftime.ftimestruct2.time);
filesize = _filelength(fh);

temp_long = (filesize + (BUFSIZEL - 1l)) / BUFSIZEL;
num_blocks = (unsigned) temp_long;

start_of_file = TRUE;
eof_flag = FALSE;
fbufblock = 0;
fbufndx = 0;
_lseek(fh, 0l, SEEK_SET);
fbufbytes = _read(fh, file_buff_ptr, BUFSIZE);

if (fbufbytes == 0)
file_is_empty = TRUE;
else
file_is_empty = FALSE;

if (strchr(file_buff_ptr, LINEFEED) != NULL)
line_end_char = LINEFEED;
else
line_end_char = CR;

textattr(fg25,bk25);
_settextwindow(25, 1, 25, 80);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);
_settextposition(25, 1);
sprintf(linebuf,
"Line: [ ] %9ld bytes %2d/%2d/%2d %2d:%2.2d:%2.2d (F1=help) ",
filesize,
(int) ftime.ftimestruct1.ft_month,
(int) ftime.ftimestruct1.ft_day,
(int) ftime.ftimestruct1.ft_year + 80,
(int) ftime.ftimestruct1.ft_hour,
(int) ftime.ftimestruct1.ft_min,
(int) ftime.ftimestruct1.ft_tsec);
_outtext(linebuf);
_settextposition(25, 14);
strcpy(string, list_ptr);
while (strlen(string) < 12)
strcat(string, " ");
_outtext(string);

_settextposition(25, 80);

CharCode = 0;
ScanCode = HOMEKEY;
showfile();

get_key:
getkey();

if ( CharCode == 'X'
|| CharCode == 'x'
|| CharCode == 'Q'
|| CharCode == 'q'
|| CharCode == ESC)
{
close(fh);
xesc = TRUE;
gohome();
}

if (CharCode == 'T' || CharCode == 't')
{
CharCode = 0;
ScanCode = HOMEKEY;
}
else
if (CharCode == 'B' || CharCode == 'b')
{
CharCode = 0;
ScanCode = ENDKEY;
}

if (CharCode == 'H' || CharCode == 'h')
{
lines_read = 0;
rcds_read = 0;
wrap_mode = FALSE;
if (hex_mode)
{
hex_mode = FALSE;
line_number = -1L;
while (rcds_read < ((bottom - top + 1) / 2))
if (fprevrecord() == -1)
break;
}
else
{
while (rcds_read < screen_rcds)
if (fprevrecord() == -1)
break;
hex_mode = TRUE;
}
CharCode = 255;
showfile();
goto get_key;
}

if (CharCode == 'W' || CharCode == 'w')
{
if (hex_mode)
goto get_key;
rcds_read = 0;
lines_read = 0;
while (rcds_read < screen_rcds)
if (fprevrecord() == -1)
break;
if (wrap_mode)
wrap_mode = FALSE;
else
wrap_mode = TRUE;
CharCode = 255;
line_number = -1L;
showfile();
goto get_key;
}

if ( CharCode == '?' || (CharCode == 0 && ScanCode == F1) )
{
gettext(1, 1, 25, 80, dos_screen_save_ptr);
save_cursor(&dos_cpos);
textattr(low_inten,bkcolor);
_settextwindow(top, left, bottom, right);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);
for (k=0; k {
if (k+4 >= bottom) break;
_settextposition(k+3, 2);
_outtext(help[k]);
}
_settextposition(25, 80);
getkey();
puttext(1, 1, 25, 80, dos_screen_save_ptr);
restore_cursor(&dos_cpos);
goto get_key;
}

if (CharCode == 'D' || CharCode == 'd')
{
gettext(1, 1, 25, 80, dos_screen_save_ptr);
save_cursor(&dos_cpos);
puttext(1, 1, 25, 80, screen_save_ptr);
restore_cursor(&cursor_data);
textattr(high_inten,bkcolor);
sprintf(linebuf,"\n");
_outtext(linebuf);
sprintf(linebuf,
" ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» \n");
_outtext(linebuf);
sprintf(linebuf,
" º Type EXIT to resume file browsing. º \n");
_outtext(linebuf);
sprintf(linebuf,
" ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ \n");
_outtext(linebuf);
textattr(low_inten,bkcolor);
system("command.com");
puttext(1, 1, 25, 80, dos_screen_save_ptr);
restore_cursor(&dos_cpos);
goto get_key;
}

if (CharCode == 'N' || CharCode == 'n')
{
if (file_num < count)
{
close(fh);
list_ptr += 13;
file_num++;
goto open_a_file;
}
goto get_key;
}

if (CharCode == 'P' || CharCode == 'p')
{
if (file_num > 1)
{
close(fh);
list_ptr -= 13;
file_num--;
goto open_a_file;
}
goto get_key;
}

if (CharCode == 0 || CharCode == 0xE0)
if ( ScanCode == HOMEKEY
|| ScanCode == ENDKEY
|| ScanCode == PGUPKEY
|| ScanCode == PGDNKEY
|| ScanCode == UPKEY
|| ScanCode == DOWNKEY
|| ScanCode == LEFTKEY
|| ScanCode == RIGHTKEY)
{
showfile();
goto get_key;
}

if ( CharCode == ' ' || CharCode == CR)
{
showfile();
goto get_key;
}

if ( CharCode == 'F' || CharCode == 'f'
|| CharCode == 'S' || CharCode == 's'
|| CharCode == 'A' || CharCode == 'a')
{
find_text();
goto get_key;
}

goto get_key;
}

/************************************/

void find_text(void)
{
gettext(25, 1, 25, 80, line_save);
save_cursor(&my_cpos);
_settextwindow(25, 1, 25, 80);
textattr(fg25,bk25);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);
if ( strlen(search_arg) < 1
|| (CharCode != 'A' && CharCode != 'a') )
{
_settextposition(25, 1);
if (CharCode == 'S' || CharCode == 's')
{
ignore_case = FALSE;
_outtext("Find(case-sen): ");
_settextposition(25, 17);
}
else
{
ignore_case = TRUE;
_outtext("Find: ");
_settextposition(25, 7);
}
kbdstring(search_arg, 50);
if (strlen(search_arg) < 1)
{
puttext(25, 1, 25, 80, line_save);
restore_cursor(&my_cpos);
return;
}
}
fbufblock_save = fbufblock;
fbufndx_save = fbufndx;
start_save = start_of_file;
eof_save = eof_flag;
linenum_save = line_number;
search_setup(search_arg, strlen(search_arg), ignore_case);
search_flag = 1;
if (line_number == 1L
&& (CharCode != 'A' || CharCode != 'a'))
{
start_of_file = TRUE;
eof_flag = FALSE;
fbufblock = 0;
fbufndx = 0;
lseek(fh, 0l, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
}
lines_read = 0;
_settextwindow(25,1,25,80);
_clearscreen(_GWINDOW);
_settextwindow(1,1,25,80);
_settextposition(25,1);
_outtext("Looking... ");
while (1)
{
if (fgetrecord(string, STR_SIZE) == -1)
{
_settextposition(25, 1);
_outtext("Not found. ");
_settextposition(80, 25);
getkey();
break;
}
if ( (spos = search(search_arg, strlen(search_arg),
string, strlen(string))) != -1)
{
line_number = -1L;
search_flag = 2;
if (!wrap_mode && !hex_mode)
if (spos > left_relative_pos + screen_width)
{
left_relative_pos = spos - (screen_width / 2);
left_relative_pos = max(0, spos);
}
else
{
left_relative_pos = 0;
}
break;
}
}
puttext(25, 1, 25, 80, line_save);
restore_cursor(&my_cpos);
if (search_flag == 2)
{
showfile();
}
else
{
line_number = linenum_save;
start_of_file = start_save;
eof_flag = eof_save;
fbufblock = fbufblock_save;
fbufndx = fbufndx_save;
lseek(fh, ((long) fbufblock) * BUFSIZEL, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
}
search_flag = 0;
}

/************************************/

void adjust_line_number(int amount)
{
if (line_number == -1L)
return;

line_number += amount;
if (line_number < 1L)
line_number = 1L;
}

/************************************/

void showfile(void)
{
if (search_flag == 2)
{
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines / 2)
if (fprevline() == -1)
break;
fill_window();
show_line_num();
return;
}

if (ScanCode == HOMEKEY)
{
left_relative_pos = 0;
line_number = 1L;
start_of_file = TRUE;
eof_flag = FALSE;
fbufblock = 0;
fbufndx = 0;
if (filesize == 0l)
{
beep();
_settextposition(top, left);
textattr(high_inten,bkcolor);
_outtext(emptyfile);
return;
}
else
{
lseek(fh, 0l, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
fill_window();
show_line_num();
}
return;
}

if (file_is_empty)
return;

if (ScanCode == PGUPKEY)
{
if (line_number == 1L)
return;
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines)
if (fprevline() == -1)
{
lines_read = 0;
while (lines_read < screen_lines)
if (fgetline(string, STR_SIZE) == -1)
break;
return;
}
lines_read = 0;
rcds_read = 0;
while (lines_read < bottom - top + 1)
if (fprevline() == -1)
{
line_number = 1L;
break;
}
adjust_line_number(rcds_read * -1);
fill_window();
show_line_num();
return;
}

if (ScanCode == PGDNKEY)
{
if (eof_flag)
return;
rcds_read = 0;
screen_lines_hold = screen_lines;
fill_window();
adjust_line_number(screen_lines_hold);
show_line_num();
return;
}

if (ScanCode == UPKEY)
{
if (line_number == 1L)
return;
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines)
if (fprevline() == -1)
{
lines_read = 0;
while (lines_read < screen_lines)
if (fgetline(string, STR_SIZE) != -1)
break;
return;
}
if (fprevline() != -1)
{
fgetline(string, STR_SIZE);
scroll_dn(1);
textattr(low_inten,bkcolor);
current_row = top;
if (hex_mode)
display_hex();
else
display_line();
adjust_line_number(-1);
}
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines - 1)
if (fgetline(string, STR_SIZE) == -1)
break;
show_line_num();
return;
}

if (ScanCode == DOWNKEY)
{
if (eof_flag)
return;
rcds_read = 0;
lines_read = 0;
if (fgetline(string, STR_SIZE) != -1)
{
scroll_up(1);
textattr(low_inten,bkcolor);
current_row = bottom;
if (hex_mode)
display_hex();
else
display_line();
adjust_line_number(1);
show_line_num();
}
return;
}

if (ScanCode == LEFTKEY)
{
if (left_relative_pos == 0)
return;
if (hex_mode || wrap_mode)
return;
movetext(top+1, left+1, bottom-1, right-6,
top+1, left+6);
left_relative_pos -= 5;
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines)
if (fprevline() == -1)
break;
fill_window();
show_line_num();
return;
}

if (ScanCode == RIGHTKEY)
{
if (hex_mode || wrap_mode)
return;
if (left_relative_pos >= STR_SIZE - screen_width - 1)
return;
left_relative_pos += 5;
rcds_read = 0;
lines_read = 0;
while (lines_read < screen_lines)
if (fprevline() == -1)
break;
fill_window();
show_line_num();
return;
}

if (CharCode == CR || CharCode == 255 || CharCode == ' ')
{
if (eof_flag)
return;
if (CharCode == CR || CharCode == 255)
left_relative_pos = 0;
screen_lines_hold = screen_lines;
rcds_read = 0;
lines_read = 0;
fill_window();
if (CharCode != 255)
adjust_line_number(screen_lines_hold);
show_line_num();
return;
}

if (ScanCode == ENDKEY)
{
left_relative_pos = 0;
line_number = -1L;
start_of_file = FALSE;
fbufblock = num_blocks - 1;
lseek(fh, ((long) fbufblock) * BUFSIZEL, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
fbufndx = fbufbytes - 1;
rcds_read = 0;
lines_read = 0;
while (lines_read < bottom - top - 2)
if (fprevline() == -1)
break;
fill_window();
show_line_num();
return;
}
}

/************************************/

void fill_window(void)
{
textattr(low_inten,bkcolor);
_settextwindow(top, left, bottom, right);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);

current_row = top;
screen_rcds = 0;
screen_lines= 0;

while (screen_lines < bottom - top + 1)
{
if (fgetrecord(string, STR_SIZE) == -1)
break;
screen_rcds++;
textattr(low_inten,bkcolor);
if ( search_flag == 2
&& (search(search_arg, strlen(search_arg),
string, strlen(string)) != -1) )
textattr(high_inten,bkcolor);
if (hex_mode)
display_hex();
else
display_line();
screen_lines += lines_displayed;
}
}

/************************************/

void display_line(void)
{
int w, index;
char s[81];

w = 0;
lines_displayed = 0;
index = left_relative_pos;

do {
_settextwindow(current_row, left, current_row, right);
_clearscreen(_GWINDOW);
_settextwindow(1, 1, 25, 80);
_settextposition(current_row, left);
strncpy(s, &string[index], screen_width);
s[screen_width] = '\0';
_outtext(s);
current_row++;
lines_displayed++;
if (strlen(&string[index]) < screen_width + 1)
return;
index += screen_width;
w += screen_width;
}
while (wrap_mode && current_row < bottom);
}

/************************************/

void show_line_num(void)
{
if (hex_mode || line_number == -1L)
strcpy(string1, " ");
else
sprintf(string1, "%5ld", line_number);
textattr(fg25,bk25);
_settextposition(25, 7);
_outtext(string1);
_settextposition(80, 25);
}

/************************************/

void display_hex(void)
{
int i, column_shift;
unsigned char ch;

column_shift = 10;
if ( search_flag == 2
&& (search(search_arg, strlen(search_arg),
string, strlen(string)) != -1) )
textattr(high_inten,bkcolor);
else
textattr(low_inten,bkcolor);

_settextposition(current_row, left+2);
sprintf(linebuf,"%6.6lX ", curr_filepos);
_outtext(linebuf);
lines_displayed = 1;

for (i=0; i<16; i++)
{
if (curr_filepos + i == filesize)
break;
ch = string[i];
_settextposition(current_row, left+column_shift+(i * 3));
sprintf(linebuf,"%2.2X ", (int) ch);
_outtext(linebuf);
if (i == 7)
{
sprintf(linebuf,"- ");
_outtext(linebuf);
column_shift = 12;
}
_settextposition(current_row, left+61+i);
if ( ch == 0 || ch == BELL
|| ch == BS || ch == TAB
|| ch == LINEFEED || ch == CR )
ch = '.';
sprintf(linebuf,"%c", ch);
_outtext(linebuf);
}

current_row++;
textattr(low_inten,bkcolor);
}

/************************************/

int fgetbyte(void)
{
eof_flag = FALSE;
start_of_file = FALSE;
fgetbyte01:
if (fbufblock == (num_blocks - 1) && fbufndx == fbufbytes)
return -1;

if (fbufndx < fbufbytes)
return (int) file_buff_ptr[fbufndx++];

fbufblock++;
fbufndx = 0;
lseek(fh, ((long) fbufblock) * BUFSIZEL, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
goto fgetbyte01;
}

/************************************/

int fgetline(unsigned char buff[], int max_chars)
{
int ch, i;

if (hex_mode || !wrap_mode)
return fgetrecord(buff, max_chars);

memset(buff, 0, max_chars);
i = 0;

while (i < screen_width)
{
if ( (ch = fgetbyte()) == -1)
{
if (i == 0)
{
eof_flag = TRUE;
return -1;
}
break;
}
if (ch == line_end_char || ch == 0)
break;
if (ch == CR || ch == LINEFEED || ch == BS)
continue;
if (ch == TAB)
{
do
buff[i++] = SPACE;
while ( (i % 4) != 0 );
continue;
}
if (ch == BELL)
ch = SPACE;
buff[i++] = (unsigned char) ch;
}

lines_read++;
return 0;
}

/************************************/

int fgetrecord(unsigned char buff[], int max_chars)
{
int i, c, rc;

memset(buff, 0, max_chars);
if (hex_mode)
{
curr_filepos = ( ((long) fbufblock) * BUFSIZEL) + fbufndx;
for (i=0; i<16; i++)
{
if ( (c = fgetbyte()) == -1 )
if (i == 0 || search_flag != 0)
{
eof_flag = TRUE;
rc = -1;
goto fgs_exit;
}
else
{
lines_read++;
rc = 0;
goto fgs_exit;
}
buff[i] = (unsigned char) c;
}
lines_read++;
rc = 0;
goto fgs_exit;
}

i = 0;
fgs1: if (i == max_chars - 1)
{
if (wrap_mode)
{
c = (i + screen_width-1) / screen_width;
if (c == 0) c = 1;
lines_read += c;
}
else
{
lines_read++;
}
rc = 0;
goto fgs_exit;
}

if ( (c = fgetbyte()) == -1 )
{
if (i == 0)
{
eof_flag = TRUE;
rc = -1;
goto fgs_exit;
}
if (wrap_mode)
{
c = (i + screen_width-1) / screen_width;
if (c == 0) c = 1;
lines_read += c;
}
else
lines_read++;
rc = 0;
goto fgs_exit;
}

if (c == line_end_char || c == 0)
{
if (wrap_mode)
{
c = (i + screen_width-1) / screen_width;
if (c == 0) c = 1;
lines_read += c;
}
else
lines_read++;
rc = 0;
goto fgs_exit;
}

if (c == CR || c == LINEFEED)
goto fgs1;
if (c == BS && i == 0)
goto fgs1;

if (c == TAB)
{
do
buff[i++] = SPACE;
while ( (i % 4) != 0 );
goto fgs1;
}

if (c == BELL)
c = SPACE;

buff[i++] = (unsigned char) c;
goto fgs1;


fgs_exit:
if (rc != -1)
rcds_read++;

return rc;
}

/************************************/

int fprevbyte(void)
{
eof_flag = FALSE;
start_of_file = FALSE;
sprev01:
if (fbufblock == 0 && fbufndx <= 0)
return -1;

if (fbufndx > 0)
return (int) file_buff_ptr[--fbufndx];

fbufblock--;
lseek(fh, ((long) fbufblock) * BUFSIZEL, SEEK_SET);
fbufbytes = read(fh, file_buff_ptr, BUFSIZE);
fbufndx = fbufbytes;
goto sprev01;
}

/************************************/

int fprevline(void)
{
int ch, i, j;

if (hex_mode || !wrap_mode)
return fprevrecord();

if ( (ch = fprevbyte()) == -1)
{
start_of_file = TRUE;
return -1;
}

i = 1;
if (ch == line_end_char || ch == 0)
ch = fprevbyte();

while (ch != line_end_char && ch != 0 && ch != -1)
{
ch = fprevbyte();
if (ch == CR || ch == LINEFEED || ch == BS)
continue;
i++;
}

j = 0;
while (i > screen_width || j % (screen_width + 1) != 0)
{
ch = fgetbyte();
i--;
j++;
}

lines_read++;
return 0;
}

/************************************/

int fprevrecord(void)
{
int ch, slen, rc;

if (hex_mode)
{
if (fprevbyte() == -1)
{
start_of_file = TRUE;
rc = -1;
goto g1l_exit;
}
while ( (fbufndx % 16) != 0 )
{
if (fprevbyte() == -1)
break;
}
lines_read++;
rc = 0;
goto g1l_exit;
}

if ( (ch = fprevbyte()) == -1)
{
start_of_file = TRUE;
rc = -1;
goto g1l_exit;
}

slen = 1;
if (ch == line_end_char || ch == 0)
ch = fprevbyte();

while (ch != line_end_char && ch != 0 && ch != -1)
{
slen++;
ch = fprevbyte();
}

if (ch != -1)
{
slen--;
ch = fgetbyte();
}

if (wrap_mode)
{
ch = (slen + screen_width-1) / screen_width;
if (ch == 0) ch = 1;
lines_read += ch;
}
else
lines_read++;

rc = 0;

g1l_exit:
if (rc != -1)
rcds_read++;

return rc;
}

/************************************/

void gohome(void)
{
if (xesc) goto restore_the_screen;

textattr(high_inten,bkcolor);
_settextposition(25, 1);
_outtext(presskey);
getkey();

restore_the_screen:
puttext(1, 1, 25, 80, screen_save_ptr);
restore_cursor(&cursor_data);

free(file_buff_ptr);
free(screen_save_ptr);
free(dos_screen_save_ptr);
free(filelist_ptr);

exit(0);
}

/************************************/


void save_cursor(struct csavetype *csave)

{

unsigned int curloc,curmode;

__asm
{
mov ah, 3;
mov bh, 0;
int 10h;
mov curloc, dx;
mov curmode, cx;
}
csave->curloc=curloc;
csave->curmode=curmode;
}


void restore_cursor(struct csavetype *csave)

{

unsigned int curloc, curmode;
curloc=csave->curloc;
curmode=csave->curmode;

__asm
{
mov dx, curloc;
mov ah, 2;
mov bh, 0;
int 10h;
mov cx, curmode;
mov ah, 1;
int 10h;
}
}

int kbdstring(char buff[], int max_chars)
{
int ft, i, j, insert_mode, ctype, res;
unsigned char row, col, trow, tcol;
unsigned int cblock;

ft = TRUE;
i = j = insert_mode = 0;
if (get_video_mode() == 7)
cblock = 0x000D;
else
cblock = 0x0007;

__asm
{
mov ah, 3;
mov bh, 0;
int 10h;
mov ctype, cx;
}
xypos=_gettextposition();
col = xypos.col;
row = xypos.row;

sprintf(linebuf,"%-*s", max_chars-1, buff);
_outtext(linebuf);
_settextposition(row, col);

ks1: getkey();
xypos=_gettextposition();
tcol = xypos.col;
trow = xypos.row;

if (CharCode == ESC)
{
buff[0] = '\0';
res = 0;
goto kbdstring_exit;
}

if (CharCode == 0)
{
if (ScanCode == INSKEY)
{
if (insert_mode)
{
insert_mode = FALSE;
__asm
{
mov cx, ctype;
mov ah,1;
int 10h;
}
}
else
{
insert_mode = TRUE;
__asm
{
mov cx, cblock
mov ah, 1
int 10h;
}
}
}
else
if (ScanCode == HOMEKEY)
{
i = 0;
_settextposition(row, col);
}
else
if (ScanCode == ENDKEY)
{
i = strlen(buff);
_settextposition(row, col+strlen(buff));
}
else
if (ScanCode == DELKEY)
{
for (j = i; j < strlen(buff); j++)
buff[j] = buff[j+1];
_settextposition(row, col);
sprintf(linebuf,"%-*s", max_chars-1, buff);
_outtext(linebuf);
_settextposition(trow, tcol);
}
else
if (ScanCode == RIGHTKEY)
{
if (i < strlen(buff))
{
i++;
_settextposition(trow, tcol+1);
}
}
else
if (ScanCode == LEFTKEY)
{
if (i > 0)
{
i--;
_settextposition(trow, tcol-1);
}
}
}

if (CharCode == 0)
goto ks1;

if (CharCode == BS)
{
if (i > 0)
{
i--;
for (j = i; j < strlen(buff); j++)
buff[j] = buff[j+1];
_settextposition(row, col);
sprintf(linebuf,"%-*s", max_chars-1, buff);
_outtext(linebuf);
_settextposition(trow, tcol-1);

}
}

if (CharCode == CR)
{
res = 0;
goto kbdstring_exit;
}

if (CharCode < 32)
goto ks1;

if (i == max_chars-1)
goto ks1;

if (ft)
{
ft = FALSE;
if (i == 0 && !insert_mode)
{
memset(buff, 0, max_chars);
_settextposition(row, col);
sprintf(linebuf,"%-*s", max_chars-1, buff);
_outtext(linebuf);
_settextposition(row, col);
}
}

if (insert_mode)
{
for (j = strlen(buff)-1; j >= i; j--)
if (j < max_chars-2)
buff[j+1] = buff[j];
buff[i++] = CharCode;
__asm
{
mov cx, ctype;
mov ah, 1;
int 10h;
}
_settextposition(row, col);
sprintf(linebuf,"%-*s", max_chars-1, buff);
_outtext(linebuf);
_settextposition(trow, ++tcol);
__asm
{
mov cx, cblock;
mov ah, 1;
int 10h;
}
}
else
{
buff[i++] = CharCode;
sprintf(linebuf,"%c", CharCode);
_outtext(linebuf);
}

goto ks1;



kbdstring_exit:
__asm
{
mov cx,ctype;
mov ah, 1;
int 10h;
}
return res;
}

void getkey(void)
{
int k;

k = _bios_keybrd(_KEYBRD_READ);
CharCode = k & 0x00FF;
ScanCode = (k & 0xFF00) >> 8;
}

int get_video_mode(void)
{
regs.x.ax = 0x0f00;
int86(0x10, ®s, ®s);
regs.h.ah = 0;
return regs.x.ax;
}

void scroll_up(unsigned char numlines)
{
regs.h.ah = 6;
regs.h.al = numlines;
regs.h.ch = (unsigned char) top-1;
regs.h.cl = (unsigned char) left-1;
regs.h.dh = (unsigned char) bottom-1;
regs.h.dl = (unsigned char) right-1;
regs.h.bh = (unsigned char) 0x17; /* yellow on blue */
int86(0x10, ®s, ®s);
}

void scroll_dn(unsigned char numlines)
{
regs.h.ah = 7;
regs.h.al = numlines;
regs.h.ch = (unsigned char) top-1;
regs.h.cl = (unsigned char) left-1;
regs.h.dh = (unsigned char) bottom-1;
regs.h.dl = (unsigned char) right-1;
regs.h.bh = (unsigned char) 0x17; /* yellow on blue */
int86(0x10, ®s, ®s);
}

/************************************/

void search_setup(pat,plen,icase)
unsigned char *pat; /* pattern */
int plen; /* pattern length */
int icase; /* ignore case */

{
register unsigned char *p;
register int i;

/* initialize the skip array and character map */
for (i = 0; i < CSIZE; ++i)
{
skip[i] = plen;
cmap[i] = i;
}

/* map upper to lowercase on case insensitive searches */
if (icase)
for (i = 'A'; i <= 'Z'; ++i)
cmap[i] = tolower(i);

/* compute the skip values for characters in the pattern */
for (i = plen-1, p = pat; i > 0; --i, ++p)
skip[cmap[*p]] = i;
}


int search(pat,plen,str,slen)
unsigned char *pat; /* pattern */
int plen; /* pattern length */
unsigned char *str; /* string */
int slen; /* string length */
{
register unsigned char *pp,*pe,*sp,*se,*sb;

/* exit immediately if the pattern is longer than the string */
if (plen > slen)
return -1;

/* compute the pattern and string ends */
pe = pat + plen;
se = str + slen;

/* compute the starting point for the first match attempt */
sb = str + plen - 1;

/* match the pattern against the string */
do {
pp = pe - 1;
sp = sb;
while (pp >= pat && cmap[*sp] == cmap[*pp])
{
--sp;
--pp;
}
if (pp < pat)
return (sp - str + 1);
}
while ((sb += skip[cmap[*sb]]) < se);

/* no match found */
return -1;
}

#define VID_START 0XB8000000 /* START OF TEXT MEM COLOR */

void gettext(int top, int left, int bottom, int right, int *destin)
{
int row, col;
int far *screen;

screen = (int far *) VID_START;
for (row = top-1; row < bottom; row++)
{
for (col = left-1; col < right; col++)
{
*(destin+row*80+col) = *(screen+row*80+col);
}
}
}

void puttext(int top, int left, int bottom, int right, int *source)
{
int row, col;
int far *screen;

screen = (int far *) VID_START;
for (row = top-1; row < bottom; row++)
{
for (col = left-1; col < right; col++)
{
*(screen+row*80+col) = *(source+row*80+col);
}
}
}



void movetext(int top, int left, int bottom, int right, int desttop, int destleft)
{
int row, col;
int far *screen;

screen = (int far *) VID_START;
for (row = top-1; row < bottom; row++)
{
for (col = left-1; col < right; col++)
{
*(screen+(desttop-1)*80+destleft-1+col) = *(screen+row*80+col);
}
}
}
void textattr(short fg, long bg)
{
_settextcolor(fg);
_setbkcolor(bg);
}


/* Use intrinsic versions of _outp and _inp */
/* #pragma intrinsic( _outp, _inp ) */

void beep(void)
{
int control, frequency=440;

/* Prepare timer by sending 10111100 to port 43. */
_outp( 0x43, 0xb6 );
/* Divide input frequency by timer ticks per second
and write (byte by byte) to timer*/

frequency = (unsigned)(1193180L / frequency);
_outp( 0x42, (char)frequency );
_outp( 0x42, (char)(frequency >> 8) );
/* Save speaker control byte. */
control = _inp( 0x61 );
/* Turn on the speaker (with bits 0 and 1). */
_outp( 0x61, control | 0x3 );
/* wait 2 clock ticks = 110 msec*/
__asm
{
mov ah,0
int 1Ah
add dx,2
mov bx,dx
repeat:
int 1Ah
cmp dx,bx
jne repeat
}
/* turn off the speaker */
_outp( 0x61, control);

}



  3 Responses to “Category : Files from Magazines
Archive   : BYTEBRO.ZIP
Filename : BROWSE1.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/