Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
temp commit .. moving from mac to VM
- Loading branch information
Showing
5 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include <stdarg.h> | ||
#include "error_functions.h" | ||
#include "tlpi_hdr.h" | ||
#include "ename.c.inc" | ||
#ifdef __GNUC__ | ||
__attribute__ ((__noreturn__)) #endif | ||
static void | ||
terminate(Boolean useExit3) | ||
{ | ||
char *s; | ||
/* Defines ename and MAX_ENAME */ | ||
/* Dump core if EF_DUMPCORE environment variable is defined and is a nonempty string; otherwise call exit(3) or _exit(2), depending on the value of 'useExit3'. */ | ||
s = getenv("EF_DUMPCORE"); | ||
if (s != NULL && *s != '\0') | ||
abort(); | ||
else if (useExit3) | ||
exit(EXIT_FAILURE); | ||
else | ||
_exit(EXIT_FAILURE); | ||
} | ||
54 Chapter 3 | ||
static void | ||
outputError(Boolean useErr, int err, Boolean flushStdout, | ||
const char *format, va_list ap) | ||
{ | ||
#define BUF_SIZE 500 | ||
char buf[BUF_SIZE], userMsg[BUF_SIZE], errText[BUF_SIZE]; | ||
vsnprintf(userMsg, BUF_SIZE, format, ap); | ||
if (useErr) | ||
snprintf(errText, BUF_SIZE, " [%s %s]", | ||
(err > 0 && err <= MAX_ENAME) ? ename[err] : "?UNKNOWN?", strerror(err)); | ||
else | ||
snprintf(errText, BUF_SIZE, ":"); | ||
snprintf(buf, BUF_SIZE, "ERROR%s %s\n", errText, userMsg); | ||
} | ||
if (flushStdout) | ||
fflush(stdout); | ||
fputs(buf, stderr); | ||
fflush(stderr); | ||
/* Flush any pending stdout */ | ||
/* In case stderr is not line-buffered */ | ||
void | ||
errMsg(const char *format, ...) | ||
{ | ||
va_list argList; | ||
int savedErrno; | ||
savedErrno = errno; /* In case we change it here */ | ||
va_start(argList, format); | ||
outputError(TRUE, errno, TRUE, format, argList); va_end(argList); | ||
errno = savedErrno; | ||
} | ||
void | ||
errExit(const char *format, ...) | ||
{ | ||
va_list argList; | ||
va_start(argList, format); | ||
outputError(TRUE, errno, TRUE, format, argList); va_end(argList); | ||
terminate(TRUE); | ||
} | ||
System Programming Concepts 55 | ||
56 Chapter 3 | ||
void | ||
err_exit(const char *format, ...) { | ||
va_list argList; | ||
va_start(argList, format); | ||
outputError(TRUE, | ||
va_end(argList); | ||
terminate(FALSE); | ||
} | ||
void | ||
errExitEN(int errnum, | ||
{ | ||
va_list argList; | ||
errno, FALSE, format, argList); | ||
const char *format, ...) | ||
va_start(argList, format); | ||
outputError(TRUE, errnum, TRUE, format, argList); va_end(argList); | ||
terminate(TRUE); | ||
} | ||
void | ||
fatal(const char *format, ...) | ||
{ | ||
va_list argList; | ||
va_start(argList, format); | ||
outputError(FALSE, 0, TRUE, format, argList); va_end(argList); | ||
terminate(TRUE); | ||
} | ||
void | ||
usageErr(const char *format, ...) { | ||
va_list argList; | ||
fflush(stdout); /* Flush any pending stdout */ | ||
fprintf(stderr, "Usage: "); va_start(argList, format); vfprintf(stderr, format, argList); va_end(argList); | ||
fflush(stderr); /* In case stderr is not line-buffered */ | ||
exit(EXIT_FAILURE); | ||
} | ||
void | ||
cmdLineErr(const char *format, ...) | ||
{ | ||
va_list argList; | ||
fflush(stdout); /* Flush any pending stdout */ | ||
fprintf(stderr, "Command-line usage error: "); va_start(argList, format); | ||
vfprintf(stderr, format, argList); va_end(argList); | ||
fflush(stderr); /* In case stderr is not line-buffered */ | ||
exit(EXIT_FAILURE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef ERROR_FUNCTIONS_H | ||
#define ERROR_FUNCTIONS_H | ||
|
||
|
||
void errMsg(const char *format, ...); | ||
|
||
#ifdef __GNUC__ | ||
/* This macro stops 'gcc -Wall' complaining that "control reaches | ||
end of non-void function" if we use the following functions to terminate main() or some other non-void function. */ | ||
#define NORETURN __attribute__ ((__noreturn__)) #else | ||
#define NORETURN | ||
#endif | ||
|
||
void errExit(const char *format, ...) NORETURN ; | ||
void err_exit(const char *format, ...) NORETURN ; | ||
void errExitEN(int errnum, const char *format, ...) NORETURN ; | ||
void fatal(const char *format, ...) NORETURN ; | ||
void usageErr(const char *format, ...) NORETURN ; | ||
void cmdLineErr(const char *format, ...) NORETURN ; | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef FUNWC_HDR | ||
#define FUNWC_HDR /* Prevent accidental double inclusion */ | ||
|
||
#include <sys/types.h> /* Type definitions used by many programs */ | ||
#include <stdio.h> /* Standard I/O functions */ | ||
#include <stdlib.h> /* Prototypes of commonly used library functions, | ||
plus EXIT_SUCCESS and EXIT_FAILURE constants */ | ||
#include <unistd.h> /* Prototypes for many system calls */ | ||
#include <errno.h> /* Declares errno and defines error constants */ | ||
#include <string.h> /* Commonly used string-handling functions */ | ||
|
||
typedef enum { FALSE, TRUE } Boolean; | ||
|
||
#define min(m,n) ((m) < (n) ? (m) : (n)) | ||
#define max(m,n) ((m) > (n) ? (m) : (n)) | ||
|
||
#endif /* FUNWC_HDR */ |
File renamed without changes.