FILES = main.o phone.o CC = gcc CFLAGS = -g -Wall -ansi -c # Note that with this setup we can change from gcc compiler to g++ # By altering a single line! # The next line is invoked with "make phone" or just "make" # It causing the forming of the object files and "loads" them # into the executable. phone: $(FILES) $(CC) -o phone $(FILES) # Although will be done automatically if needed, "make main.o" otherwise main.o: phone.h main.c $(CC) $(CFLAGS) main.c # Illustrates the benefits of macros phone.o: phone.c phone.h $(CC) $(CFLAGS) phone.c # To remove unwanted *.o files clean: $(FILES) /usr/bin/rm $(FILES) /*--------------------end of makefile--------------*/ /********************************************************* * * phone.h * * Basic data structure definitions for the phone book * example. * *********************************************************/ #define NAMELENGTH 20 #define PHONELENGTH 10 #ifndef TRUE #define TRUE 1 #endif struct phone_struct { char name[NAMELENGTH]; char phone[PHONELENGTH]; }; void read_phone_file (FILE*); char* get_phone_number (char*); /*-----------------end of phone.h---------------------------------*/ /************************************************************ * * phone.c * * The routines in this file maintain the phone book data * structure. All access to this data structure must be * through the routines in this file. * **********************************************************/ #include #include #include "phone.h" /* * SIZE is the length of the array used to store the * phone list */ #define SIZE 200 /* * phone_table is the array that stores the phone list. * Numbers is the number of entries in the phone list. */ static struct phone_struct phone_table[SIZE]; static int Numbers = 0; /* * read_phone_list * * Purpose: transfer the phone list from disk to internal storage * * Pre-conditions: the phone list is initially empty * * Post-conditions: the phone_table array has the contents of the * disk file */ void read_phone_file (FILE* fid) { int n; int i; i = 0; while (TRUE) { n = fscanf(fid,"%20s %10s", &(phone_table[i].name[0]), &(phone_table[i].phone)[0]); if (n < 0) break; i++; } Numbers = i; } /* * get_phone_number * * Purpose: look up the name corresponding to a phone number * * Pre-conditions: the name is in the phone list * * Post-conditions: returns the number corresponding to the * name that was passed as a parameter */ char* get_phone_number (char* name) { int i; for (i = 0; i < Numbers; i++) { if ( strcmp (name, phone_table[i].name) == 0 ) { return (phone_table[i].phone); } } return (NULL); } /*------------------end of phone.c----------------*/ /********************************************************* * * main.c * * Main program for the phone number program. The main * program is responsible for all interactions with the * user. It first determines the file that the phone * book is stored on and then calls read_phone_file to * read the phone book. The program then asks the user * for the names of people and prints their phone numbers. * The get_phone_number procedure us used to lookup a * phone number given a person's name. The phone book * data structure is maintained by the routines in the * phone.c file. * *******************************************************/ #include #include #include "phone.h" int main (int argc, char** argv) { FILE* fid; char name[NAMELENGTH]; char* phone; if (argc != 2) { printf ("usage: phone phone_file\n"); return (1); } fid = fopen (argv[1], "r"); if (fid == NULL) { printf("unable to open phone file: %s\n", argv[1]); return (2); } read_phone_file (fid); while (TRUE) { printf("name: "); scanf ("%20s", &name[0]); if (strcmp (name, "exit") == 0) break; phone = get_phone_number(name); if (phone == NULL) { printf("sorry, no number for %s\n", name); } else { printf("the phone number for %s is %s\n", name, phone); } } return (0); } /* sample input data file Green 7654 Brown 6789 Yellow 4321 White 8901 Red 3425 Pink 9012 */