diff options
author | Roland McGrath <roland@gnu.org> | 1991-01-30 10:25:34 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 1991-01-30 10:25:34 +0000 |
commit | c2547ce6a46f905a7f876f3e2c35e6e9e9b36527 (patch) | |
tree | 2b619afb1196c20c60ea0272412e589ad8a9039d /assert | |
parent | 1c078555037a905f1e9bb29951e5e16420d66992 (diff) |
Initial revision
Diffstat (limited to 'assert')
-rw-r--r-- | assert/assert.c | 44 | ||||
-rw-r--r-- | assert/assert.h | 63 |
2 files changed, 107 insertions, 0 deletions
diff --git a/assert/assert.c b/assert/assert.c new file mode 100644 index 0000000000..dd54bbc8ed --- /dev/null +++ b/assert/assert.c @@ -0,0 +1,44 @@ +/* Copyright (C) 1991 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 1, or (at your option) +any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the GNU C Library; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#include <ansidecl.h> +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + + +/* This function, when passed a string containing an asserted + expression, a filename, and a line number, prints a message + on the standard error stream of the form: + a.c:10: Assertion `a == b' failed. + It then aborts program execution via a call to abort(). */ + +int +DEFUN(__assert_fail, (assertion, file, line), + CONST char *assertion AND CONST char *file AND unsigned int line) +{ + /* Print the message. */ + (void) fprintf(stderr, "%s:%u: Assertion `%s' failed.\n", + file, line, assertion); + (void) fflush(stderr); + + abort(); + + /* This function never returns, so making it void would make sense, + but returning something makes the assert macro easier to write. */ + return(0); +} diff --git a/assert/assert.h b/assert/assert.h new file mode 100644 index 0000000000..056925119b --- /dev/null +++ b/assert/assert.h @@ -0,0 +1,63 @@ +/* Copyright (C) 1991 Free Software Foundation, Inc. +This file is part of the GNU C Library. + +The GNU C Library is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 1, or (at your option) +any later version. + +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the GNU C Library; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +/* + * ANSI Standard: 4.2 DIAGNOSTICS <assert.h> + */ + +#ifdef _ASSERT_H + +#undef _ASSERT_H +#undef assert +#undef __assert_quotearg + +#endif /* assert.h */ + +#define _ASSERT_H 1 +#include <features.h> + +/* void assert(int expression); + If NDEBUG is defined, do nothing. + If not, and EXPRESSION is zero, print an error message and abort. */ + +#ifdef NDEBUG + +#define assert(expr) ((void) 0) + +#else /* Not NDEBUG. */ + +/* This prints an "Assertion failed" message and aborts. */ +extern int EXFUN(__assert_fail, (CONST char *__assertion, + CONST char *__file, unsigned int __line)); + +/* IGNORE($ */ +#ifdef __STDC__ +/* $) IFANSI($ */ +#define __assert_quotearg(s) #s +/* $) IGNORE($ */ +#else /* Not ANSI C. */ +/* $) IFTRAD($ */ +#define __assert_quotearg(s) "s" +/* $) IGNORE($ */ +#endif /* ANSI C. */ +/* $) */ + +#define assert(expr) \ + ((void) ((expr) || \ + __assert_fail(__assert_quotearg(expr), __FILE__, __LINE__))) + +#endif /* NDEBUG. */ |