Skip to content

Commit

Permalink
temp commit .. moving from mac to VM
Browse files Browse the repository at this point in the history
  • Loading branch information
shiva committed Oct 30, 2011
1 parent 6ae6474 commit 42784ba
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 1 deletion.
1 change: 0 additions & 1 deletion README
@@ -1,7 +1,6 @@

Stuff written for fun

The magic that is POSIX
1. experiments with thread local storage
a. thread specific storage
b. thread local storage
Expand Down
105 changes: 105 additions & 0 deletions lib/error_functions.c
@@ -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);
}
22 changes: 22 additions & 0 deletions lib/error_functions.h
@@ -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

17 changes: 17 additions & 0 deletions lib/funwc_hdr.h
@@ -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.

0 comments on commit 42784ba

Please sign in to comment.