Listing 3

 1:  /*
 2:   * MDBUTIL.C
 3:   *
 4:   * Program:    Mini-Database
 5:   * Written by: Leor Zolman
 6:   * Module:     Utility functions
 7:   */
 8:
 9:  #include <stdio.h>
10:  #include <stdlib.h>
11:  #include "mdb.h"
12:
13:
14:  /*
15:   * Function:       do_menu
16:   * Purpose:        Simple line-oriented menu handler
17:   * Parameters:     None
18:   * Return Value:   None
19:   */
20:
21:  int do_menu(struct menu_item *mnu, char *title)
22:  {
23:      int i, j;
24:      char buf[150];
25:
26:      printf("\n%s -- Options:\n", title);
27:      for (i = 0; mnu[i].action_code != NULL; i++)
28:          printf("%2d) %s\n", i+1, mnu[i].descrip);
29:
30:       while (1)
31:       {
32:           printf("\nYour choice? "};
33:           j = atoi(gets(buf));
34:           if (j >= 1 && j <= i)
35:               break;
36:           printf("Please select from options 1-%d: ", i+1);
37:       }
38:
39:       return mnu[j - 1].action_code;
40:  }
41:
42:
43:  /*
44:   * Function:         error
45:   * Purpose:          Report error end terminate program
46:   * Parameters:       Message to display
47:   * Return Value:     None
48:   */
49:
50:  void error(char *msg)
51:  {
52:      printf ("Fatal Condition: %s\n", msg);
53:      exit(-1);
54:  }
55:
56:
57:  /*
58:   * Function:       alloc_rec
59:   * Purpose:        Allocate memory for a Database record,
60:   *                      checking for an allocation error
61:   * Parameters:     None
62:   * Return Value:   Pointer to memory, or NULL on error
63:   */
64:
65:  struct record *alloc_rec(void)
66:  {
67:      struct record *temp;
68:
69:      if ((temp = malloc(sizeof(struct record))) == NULL)
70:          return NULL;
71:      else
72:          return temp;
73:  }
74:
75:
76:  /*
77:   * Function:       free_up
78:   * Purpose:        De_allocate all records in current Database
79:   * Parameters:     None
80:   * Return Value:   None
81:   */
82:
83:  void free_up()
84:  {
85:      int i;
86:
87:      for (i = 0; i < n_recs; i++)
88:          free(RECS[i]);
89:  }
90:
91: