Graphics Programs Reference
In-Depth Information
This same technique can be used in a multiuser note-taking program.
The next program will be a modification of the simplenote program; it will
also record the user ID of each note's original author. In addition, a new
syntax for #include will be introduced.
The ec_malloc() and fatal() functions have been useful in many of our
programs. Rather than copy and paste these functions into each program,
they can be put in a separate include file.
hacking.h
// A function to display an error message and then exit
void fatal(char *message) {
char error_message[100];
strcpy(error_message, "[!!] Fatal Error ");
strncat(error_message, message, 83);
perror(error_message);
exit(-1);
}
// An error-checked malloc() wrapper function
void *ec_malloc(unsigned int size) {
void *ptr;
ptr = malloc(size);
if(ptr == NULL)
fatal("in ec_malloc() on memory allocation");
return ptr;
}
In this new program, hacking.h, the functions can just be included. In C,
when the filename for a #include is surrounded by < and > , the compiler looks
for this file in standard include paths, such as /usr/include/. If the filename
is surrounded by quotes, the compiler looks in the current directory. There-
fore, if hacking.h is in the same directory as a program, it can be included
with that program by typing #include "hacking.h" .
The changed lines for the new notetaker program (notetaker.c) are
displayed in bold.
notetaker.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"
void usage(char *prog_name, char *filename) {
printf("Usage: %s <data to add to %s>\n", prog_name, filename);
exit(0);
Search WWH ::




Custom Search