Category : Files from Magazines
Archive   : BYTE0389.ZIP
Filename : ZMAKEF.C

 
Output of file : ZMAKEF.C contained in archive : BYTE0389.ZIP

/*
** This program allows you to create ZSAM key and data files.
** In other words, it creates the file and writes the initial
** header information. The files are empty and ready to use.
**
** For KEY files, you'll need:
** a) The file name. It is created in the current directory.
** b) The file type. "K" means it's a key file with an associated
** data file. "O" means it's keyonly (gungily supported in
** ZSAM). A keyonly file has no associated data file, and
** it meant to be used as a sorting vehicle.
** c) The keylength (up to 64).
** For DATA files, you'll need:
** a) The file name.
** b) The file type. "S" is a simple file (one record per
** key). "C" is a complex file (multiple records per key).
** c) The record length. Minimum (what? there's a minimum?)
** is 3 bytes.
*/
#include
#include
#include

/*
** ZSAM Globals
*/

#define nzkfiles 5 /* Number of key files */
#define nzdfiles 5 /* Number of data files */

/*
** ZSAM structure definitions.
*/

typedef unsigned char byte;

typedef struct { /* Key file header */
char kftype; /* Key file type, "K" or "O" */
unsigned int rootsc; /* Root sector */
unsigned int nokeysl; /* Number of keys, lo */
byte nokeysh; /* Number of keys, hi */
unsigned int kavsec; /* Key avail list head */
unsigned int nxkysc; /* Next free sector */
byte keylen; /* Key length */
byte maxks; /* Maximum keys per sector (node) */
unsigned int ackysc; /* Actual key sector in buffer */
byte misc[48]; /* Transient storage */
} zkey_header;

#define KHEADSIZE sizeof(zkey_header)-48
#define KHEADPAD 512-sizeof(zkey_header)+48

typedef struct { /* Data file header */
char dftype; /* Data file type, "S" or "C" */
unsigned int reclen; /* Record length */
unsigned int norecsl; /* Number of records, lo */
byte norecsh; /* Number of records, hi */
unsigned int davlo; /* Avail list, lo */
byte davhi; /* Avail list, hi */
unsigned int nxdalo; /* Next data record, lo */
byte nxdahi; /* Next data record, hi */
char misc[20]; /* Transient storage */
} zdat_header;
#define DHEADSIZE sizeof(zdat_header)-20
#define IDATOFF sizeof(zdat_header)-20

/*
** ZSAM interface storage.
*/

unsigned int zkfno; /* Current key file number */
unsigned int zdfno; /* Current data file number */
unsigned int zkfhand[nzkfiles]; /* Array of handles to key files */
unsigned int zdfhand[nzdfiles]; /* Array of handles to data files */
zkey_header *keyhead[nzkfiles]; /* Pointers to key file header storage */
zdat_header *dathead[nzdfiles]; /* Pointers to data file header storage */
char *keybuf[nzkfiles]; /* Pointers to key file buffers */
char *datbuf[nzdfiles]; /* Pointers to data file buffers */
long zoffset; /* Offset into files */

/*
** A pointer to the following structure (zsams) is passed
** to the ZSAM assembly routines.
*/
typedef struct {
unsigned int funct; /* Function code */
char *kbuf; /* Pointer to current key buffer */
char *dbuf; /* Pointer to current data buffer */
char *key; /* Pointer to current key */
char *rec; /* Pointer to current data record */
zkey_header *keyhd; /* Pointer to current key file header */
zdat_header *dathd; /* Pointer to current data file header */
} zsamstr;

zsamstr zsams;

/*
** Create a zsam key file.
** Returns 0 if ok, -1 if error (error code in errno).
*/
zcreate_kfile(fname,ftype,keyl)
char *fname; /* File pathname */
char ftype; /* File type */
int keyl; /* Key length for this file */
{
zkey_header create_header; /* Header for file to create */
int fhand;
int werr;
int i;
/* Pad used to "fill out a sector" */
char pad[KHEADPAD];
for(i=0;i pad[i]=' ';
/* Calculate maximum number of keys per node */
create_header.maxks = (512-3) / (keyl+5);

/* Build rest of header stuff */
create_header.kftype = ftype;
create_header.keylen = keyl;
create_header.rootsc = 0;
create_header.nokeysl = 0;
create_header.nokeysh = 0;
create_header.kavsec = 0;
create_header.nxkysc = 1;
create_header.ackysc = 0;

/* Create the file */
fhand = open(fname,O_RDWR | O_CREAT | O_BINARY,
S_IREAD | S_IWRITE);
if(fhand==NULL) return(-1);

/* Write the header information */
werr = write(fhand,&create_header,KHEADSIZE);
if((werr<0)||(werr!=KHEADSIZE)) return (-1);
werr = write(fhand,pad,KHEADPAD);
printf("%d\n",werr);
if((werr<0)||(werr!=KHEADPAD)) return(-1);

return(0);
}

/*
** Create a zsam data file.
*/
zcreate_dfile(fname,ftype,recl)
char *fname;
char ftype;
unsigned int recl;
{
zdat_header create_header;
int fhand;
int werr;

/* Load stuff into the header */
create_header.dftype = ftype;
create_header.reclen = recl;
create_header.norecsl = 0;
create_header.norecsh = 0;
create_header.davlo = 0;
create_header.davhi = 0;
create_header.nxdalo = 1;
create_header.nxdahi = 0;

/* Create the file */
fhand = open(fname,O_RDWR | O_CREAT | O_BINARY,
S_IREAD | S_IWRITE);
if(fhand==NULL) return(-1);

/* Write the header information */
werr = write(fhand,&create_header,DHEADSIZE);
if((werr<0)||(werr!=DHEADSIZE)) return (-1);

return(0);
}

main()
{
char choice[2]; /* Selection choice */
char kfname[40],dfname[40];
char kfilet[2],dfilet[2];
int keylen,reclen;

/* Find out what the use wants to do */
printf("Create ZSAM files...\n\n");

while(1) {

printf("Do you want to create\n");
printf(" K - a key file, or\n");
printf(" D - a data file?\n");
printf("Choice (X to exit):");
scanf("%s",choice);

switch(choice[0]) {

case 'K':
case 'k':
printf("\n");
printf("Enter key file name:");
scanf("%s",kfname);
printf("Enter key length:");
scanf("%d",&keylen);
do {
kfilet[0]=' ';
printf("Enter file type (K or O):");
scanf("%s",kfilet);
} while((kfilet[0]!='K')&&(kfilet[0]!='O'));
zcreate_kfile(kfname,kfilet[0],keylen);
break;

case 'D':
case 'd':
printf("\n");
printf("Enter data file name:");
scanf("%s",dfname);
printf("Enter record length:");
scanf("%d",&reclen);
do {
dfilet[0]=' ';
printf("Enter file type (S or C):");
scanf("%s",dfilet);
} while((dfilet[0]!='S')&&(dfilet[0]!='C'));
zcreate_dfile(dfname,dfilet[0],reclen);
break;

case 'X':
case 'x':
exit(0);

default:
printf("Eh?\n\n");
break;
}

}
}




  3 Responses to “Category : Files from Magazines
Archive   : BYTE0389.ZIP
Filename : ZMAKEF.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/