home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Frostbyte's 1980s DOS Shareware Collection
/
floppyshareware.zip
/
floppyshareware
/
DOOG
/
CBASE09.ZIP
/
CBASE.ZIP
/
ROLO.C
< prev
next >
Wrap
Text File
|
1989-08-31
|
16KB
|
576 lines
/* Copyright (c) 1989 Citadel */
/* All Rights Reserved */
/* #ident "rolo.c 1.1 - 89/08/31" */
#include <blkio.h>
#include <cbase.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
/* #include <stdlib.h> */
void exit(); /* delete if you have stdlib.h */
#define EXIT_SUCCESS (0)
#define EXIT_FAILURE (1)
/* #include <string.h> */
#include "rolo.h"
#define USAGE ("Usage: rolo")
static void cardput(/*rolo_t *rlp*/);
/*man---------------------------------------------------------------------------
NAME
rolo - card file
SYNOPSIS
rolo
DESCRIPTION
rolo is a rudimentary implementation of a card file program. Its purpose
is to illustrate fundamental usage of the cbase library. In order to
allow rolo to be compiled without any additional libraries, only a
minimal user interface has been implemented.
SEE ALSO
cbase.
------------------------------------------------------------------------------*/
int main(argc, argv)
int argc;
char *argv[];
{
int rs = 0;
cbase_t * cbp = NULL;
rolo_t rl /*= {0}*/;
int field = 0;
int c = 0;
char buf[256];
int i = 0;
/* initialize storage */
memset((void *)&rl, 0, sizeof(rl));
memset((void *)buf, 0, sizeof(buf));
/* process command line arguments */
if (argc != 1) {
printf("%s\n", USAGE);
bexit(EXIT_FAILURE);
}
/* open cbase */
cbp = cbopen(ROLO, "r+", rl_fields, RL_FLDCNT);
if (cbp == NULL) {
if (errno == ENOENT) {
printf("The card file does not exist. Creating.\n");
rs = cbcreate(ROLO, sizeof(rolo_t),
rl_fields, RL_FLDCNT);
if (rs == -1) {
fprintf(stderr, "*** Error creating rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cbp = cbopen(ROLO, "r+", rl_fields, RL_FLDCNT);
if (cbp == NULL) {
printf("*** Error opening rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
} else {
printf("*** Error opening rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
printf("\n--------------------------\n");
printf("| Card File Demo Program |\n");
printf("--------------------------\n");
/* main loop */
field = RL_CONTACT;
while (1) {
printf("\n\n");
printf("---------------------------\n");
printf("| I - Insert new card |\n");
printf("| D - Delete current card |\n");
printf("| S - Search for card\n");
printf("| N - Next card\n");
printf("| P - Previous card\n");
printf("| F - First card\n");
printf("| L - Last card\n");
printf("| C - Change index field\n");
printf("| A - list All\n");
printf("| X - eXit |\n");
printf("---------------------------\n");
printf("Enter selection: ");
c = getchar();
if (getchar() != '\n') {
while(getchar() != '\n') {
}
printf("\a");
continue;
}
c = toupper(c);
if (c == 'X') {
break;
}
switch (c) {
case 'I': /* Insert new card */
/* input new card */
memset((void *)&rl, 0, sizeof(rl));
printf("Contact: ");
gets(buf);
strncpy(rl.rl_contact, buf, sizeof(rl.rl_contact));
rl.rl_contact[sizeof(rl.rl_contact) - 1] = '\0';
printf("Title: ");
gets(buf);
strncpy(rl.rl_title, buf, sizeof(rl.rl_title));
rl.rl_title[sizeof(rl.rl_title) - 1] = '\0';
printf("Company: ");
gets(buf);
strncpy(rl.rl_company, buf, sizeof(rl.rl_company));
rl.rl_company[sizeof(rl.rl_company) - 1] = '\0';
printf("Address (%d lines): ",
(int)(sizeof(rl.rl_addr)/sizeof(rl.rl_addr[0])));
for (i = 0; i < sizeof(rl.rl_addr)/sizeof(rl.rl_addr[0]); i++) {
gets(buf);
strncpy(rl.rl_addr[i], buf, sizeof(rl.rl_addr[0]));
rl.rl_addr[i][sizeof(rl.rl_addr[0]) - 1] = '\0';
}
printf("Phone: ");
gets(buf);
strncpy(rl.rl_phone, buf, sizeof(rl.rl_phone));
rl.rl_phone[sizeof(rl.rl_phone) - 1] = '\0';
printf("Fax: ");
gets(buf);
strncpy(rl.rl_fax, buf, sizeof(rl.rl_fax));
rl.rl_fax[sizeof(rl.rl_fax) - 1] = '\0';
printf("Notes (%d lines): ",
(int)(sizeof(rl.rl_notes)/sizeof(rl.rl_notes[0])));
for (i = 0; i < sizeof(rl.rl_notes)/sizeof(rl.rl_notes[0]); i++) {
gets(buf);
strncpy(rl.rl_notes[i], buf, sizeof(rl.rl_notes[0]));
rl.rl_notes[i][sizeof(rl.rl_notes[0]) - 1] = '\0';
}
/* insert new card in cbase */
rs = cblock(cbp, CB_WRLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently locked by another process.\n");
break;
}
printf("*** Error write locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cbinsert(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error inserting card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'I': */
case 'D': /* Delete current card */
rs = cblock(cbp, CB_WRLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently locked by another process.\n");
break;
}
printf("*** Error write locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
rs = cbkeysrch(cbp, RL_CONTACT, (void *)rl.rl_contact);
if (rs == -1) {
printf("*** Error searching for key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 0) {
printf("There is no current card.\n");
}
rs = cbdelcur(cbp, field);
if (rs == -1) {
printf("*** Error deleting current card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbrcursor(cbp) == NULL) {
memset((void *)&rl, 0, sizeof(rl));
} else {
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'D': */
case 'S': /* Search for card */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (field == RL_CONTACT) {
printf("Enter contact name: ");
} else {
printf("Enter company name: ");
}
if (fgets(buf, sizeof(buf), stdin) == NULL) {
printf("*** Error reading input.\n");
bexit(EXIT_FAILURE);
}
if (buf[strlen(buf) - 1] == '\n') {
buf[strlen(buf) - 1] = '\0';
}
rs = cbkeysrch(cbp, field, (void *)buf);
if (rs == -1) {
printf("*** Error searching for key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 0) {
printf("\nNot found.\n");
memset((void *)&rl, 0, sizeof(rl));
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'S': */
case 'N': /* Next card */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
/* set record cursor */
rs = cbkeysrch(cbp, RL_CONTACT, (void *)rl.rl_contact);
if (rs == -1) {
printf("*** Error searching for key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 0) {
rs = cbsetrcur(cbp, NULL);
if (rs == -1) {
printf("*** Error setting record cursor. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = cbkeyalign(cbp, field);
if (rs == -1) {
printf("*** Error aligning key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cbkeynext(cbp, field);
if (rs == -1) {
printf("*** Error finding next card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbrcursor(cbp) == NULL) {
rs = cbkeynext(cbp, field);
if (rs == -1) {
printf("*** Error finding next card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'N': */
case 'P': /* Previous card */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
/* set record cursor */
rs = cbkeysrch(cbp, RL_CONTACT, (void *)rl.rl_contact);
if (rs == -1) {
printf("*** Error searching for key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (rs == 0) {
rs = cbsetrcur(cbp, NULL);
if (rs == -1) {
printf("*** Error setting record cursor. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = cbkeyalign(cbp, field);
if (rs == -1) {
printf("*** Error aligning key. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cbkeyprev(cbp, field);
if (rs == -1) {
printf("*** Error finding previous card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbrcursor(cbp) == NULL) {
rs = cbkeyprev(cbp, field);
if (rs == -1) {
printf("*** Error finding previous card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'P': */
case 'F': /* First card */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
rs = cbkeyfirst(cbp, field);
if (rs == -1) {
printf("*** Error finding first card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'F': */
case 'L': /* Last card */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
rs = cbkeylast(cbp, field);
if (rs == -1) {
printf("*** Error finding last card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
break; /* case 'L': */
case 'C': /* change index field */
printf("\n1. Contact name.\n");
printf("2. Company name.\n");
printf("Select new index field: ");
c = getchar();
if (getchar() != '\n') {
printf("\a");
continue;
}
switch (c) {
case '1':
field = RL_CONTACT;
break; /* case '1': */
case '2':
field = RL_COMPANY;
break; /* case '2': */
default:
printf("\a");
continue;
break; /* default: */
}
break; /* case 'C': */
case 'A': /* list All */
rs = cblock(cbp, CB_RDLCK);
if (rs == -1) {
if (errno == EAGAIN) {
printf("cbase currently write locked by another process.\n");
break;
}
printf("*** Error read locking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbreccnt(cbp) == 0) {
printf("\nThe card file is empty.\n");
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
continue;
}
rs = cbsetkcur(cbp, field, NULL);
if (rs == -1) {
printf("*** Error setting key cursor. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
while (1) {
rs = cbkeynext(cbp, field);
if (rs == -1) {
printf("*** Error finding next card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
if (cbkcursor(cbp, field) == NULL) {
break;
}
rs = cbgetr(cbp, (void *)&rl);
if (rs == -1) {
printf("*** Error reading card. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
cardput(&rl);
}
rs = cblock(cbp, CB_UNLCK);
if (rs == -1) {
printf("*** Error unlocking rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
break; /* case 'A': */
default:
printf("\a");
continue;
break; /* default: */
}
}
/* close cbase */
rs = cbclose(cbp);
if (rs == -1) {
printf("*** Error closing rolo. Error number %d.\n", errno);
bexit(EXIT_FAILURE);
}
bexit(EXIT_SUCCESS);
}
static void cardput(rlp)
rolo_t *rlp;
{
int i = 0;
rolo_t rl /*= {0}*/;
printf("\n-----------------------CARD-----------------------\n");
printf("| Contact: %s\n", rlp->rl_contact);
printf("| Title: %s\n", rlp->rl_title);
printf("| Company: %s\n", rlp->rl_company);
printf("| Address: %s\n", rlp->rl_addr[0]);
for (i = 1; i < sizeof(rl.rl_addr)/sizeof(rl.rl_addr[0]); i++) {
printf("| %s\n", rlp->rl_addr[i]);
}
printf("| Phone: %s\n", rlp->rl_phone);
printf("| Fax: %s\n", rlp->rl_fax);
printf("| Notes: %s\n", rlp->rl_notes[0]);
for (i = 1; i < sizeof(rl.rl_notes)/sizeof(rl.rl_notes[0]); i++) {
printf("| %s\n", rlp->rl_notes[i]);
}
printf("--------------------------------------------------\n");
return;
}