1/* *****************************************************************************
2
3libcsc: Debug Subsystem
4
5	----------------------------------------------------------------
6
7Copyright (c) 1999, 2001, 2002 Douglas R. Jerome, Peoria, AZ USA
8
9	This program is free software; you can redistribute it and/or modify
10	it under the terms of the GNU Library General Public License as
11	published by the Free Software Foundation; either version 2 of the
12	License, or (at your option) any later version.
13
14	This program is distributed in the hope that it will be useful,
15	but WITHOUT ANY WARRANTY; without even the implied warranty of
16	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17	GNU General Public License for more details.
18
19	You should have received a copy of the GNU Library General Public
20	License along with this program; if not, write to the Free Software
21	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23	----------------------------------------------------------------
24
25FILE NAME
26
27	$RCSfile: xcsc_debug.c,v $
28	$Revision: 1.3 $
29	$Date: 2003/03/24 04:26:45 $
30
31PROGRAM INFORMATION
32
33	Developed by:	libcsc project
34	Developer:	Douglas R. Jerome, drj, <jerome@primenet.com>
35
36FILE DESCRIPTION
37
38	This file provides the libcsc debuging functions. These functions are a
39	few assertion catching functions, and wrapper functions for common libc
40	functions which take pointers for arguments.
41
42CHANGE LOG
43
44	23mar03	drj	Added #include <stdlib.h>.
45
46	24apr02	drj	Added newline at end of user_string in xcsc_assert_fail.
47
48	20apr02	drj	Small comment changes.
49
50	25jun01	drj	Converted to libcsc: renamed everything from rt to csc,
51			reorganized everything.
52
53	29apr99	drj	Rebaselined from librt version 0.3.1.
54
55***************************************************************************** */
56
57
58/* ************************************************************************* */
59/*                                                                           */
60/*      F e a t u r e   S w i t c h e s                                      */
61/*                                                                           */
62/* ************************************************************************* */
63
64/*
65 * Select these feature by moving them from the `if UNDEF' into the `else'
66 * section.
67 */
68#ifdef	UNDEF
69#   define	_BSD_SOURCE	1	/* 4.3+bsd subsystems           */
70#   define	_POSIX_SOURCE	1	/* posix.1                      */
71#   define	_POSIX_C_SOURCE	199309L	/* posix.1 and posix.4          */
72#else
73#   define	_POSIX_C_SOURCE	199506L	/* posix.1 and posix.4 and MORE */
74#   ifndef	_REENTRANT
75#      define	_REENTRANT		/* thread-safe for glibc        */
76#   endif
77#endif
78
79#ifndef	DEBUG
80#   define	DEBUG
81#endif
82
83
84/* ************************************************************************* */
85/*                                                                           */
86/*      I n c l u d e d   F i l e s                                          */
87/*                                                                           */
88/* ************************************************************************* */
89
90/*
91 * OS Specific Header Files
92 */
93/*  (None.)  */
94
95/*
96 * Standard C (ANSI) Header Files
97 */
98#include	<errno.h>
99#include	<stdio.h>
100#include	<stdlib.h>
101#include	<string.h>
102
103/*
104 * Posix Header Files
105 */
106#include	<unistd.h>
107
108/*
109 * 4.3+BSD Header Files
110 */
111/*  (None.)  */
112
113/*
114 * Project Specific Header Files
115 */
116#include	"libcsc_debug.h"
117
118
119/* ************************************************************************* */
120/*                                                                           */
121/*      M a n i f e s t   C o n s t a n t s                                  */
122/*                                                                           */
123/* ************************************************************************* */
124
125/*  (None.)  */
126
127
128/* ************************************************************************* */
129/*                                                                           */
130/*      E x t e r n a l   R e f e r e n c e s                                */
131/*                                                                           */
132/* ************************************************************************* */
133
134/*  (None.)  */
135
136
137/* ************************************************************************* */
138/*                                                                           */
139/*      S c a l a r   D a t a   T y p e s                                    */
140/*                                                                           */
141/* ************************************************************************* */
142
143/*  (None.)  */
144
145
146/* ************************************************************************* */
147/*                                                                           */
148/*      N o n - S c a l a r   D a t a   S t r u c t u r e s                  */
149/*                                                                           */
150/* ************************************************************************* */
151
152/*  (None.)  */
153
154
155/* ************************************************************************* */
156/*                                                                           */
157/*      P u b l i c   G l o b a l   V a r i a b l e s                        */
158/*                                                                           */
159/* ************************************************************************* */
160
161/*  (None.)  */
162
163
164/* ************************************************************************* */
165/*                                                                           */
166/*      P r i v a t e   G l o b a l   V a r i a b l e s                      */
167/*                                                                           */
168/* ************************************************************************* */
169
170/*  (None.)  */
171
172
173/* ************************************************************************* */
174/*                                                                           */
175/*      E x e c u t a b l e   C o d e   (Locally Used Functions)             */
176/*                                                                           */
177/* ************************************************************************* */
178
179
180/**************************************************************************
181 * Private Function Prototypes
182 **************************************************************************/
183
184/*  (None.)  */
185
186
187/* ---------------------------------------------------------------------- */
188
189
190/**************************************************************************
191 * Private Function
192 **************************************************************************/
193
194/*  (None.)  */
195
196
197/* ************************************************************************* */
198/*                                                                           */
199/*      E x e c u t a b l e   C o d e   (External Interface Functions)       */
200/*                                                                           */
201/* ************************************************************************* */
202
203
204/**************************************************************************
205 * Public Function xcsc_assert_fail
206 **************************************************************************/
207
208void   xcsc_assert_fail (
209                        const char*          file_name,
210                              unsigned int   line_number,
211                        const char*          assert_expression,
212                        const char*          user_string,
213                              int            fail_flag
214                        )
215   {
216   /*
217    * Make sure all the user's output is flushed before taking any debugging
218    * steps.
219    */
220   (void)fflush (stdout);
221   (void)fflush (stderr);
222
223   /*
224    * Display the assertion data on the standard error device. Flush this
225    * output in case any further debugging steps do something bad.
226    */
227   (void)fprintf (
228                 stderr,
229                 "Assertion failed: FILE %s, LINE %u, EPRESSION `%s'.\n",
230                 file_name, line_number, assert_expression
231                 );
232   if (user_string != (void*)0)
233      {
234      (void)fprintf (stderr, "\t-- %s\n", user_string);
235      }
236   (void)fflush (stderr);
237
238   /*
239    * Do something if the fail_flag is set, otherwise simply return.
240    */
241   if (fail_flag) abort();
242
243   return;
244   }
245
246
247/**************************************************************************
248 * Public Function xcsc_assert_errno
249 **************************************************************************/
250
251void   xcsc_assert_errno (
252                         const char*          file_name,
253                               unsigned int   line_number,
254                                        int   error_number,
255                                        int   fail_flag
256                         )
257   {
258   /*
259    * Make sure all the user's output is flushed before taking any debugging
260    * steps.
261    */
262   (void)fflush (stdout);
263   (void)fflush (stderr);
264
265   /*
266    * Display the assertion data on the standard error device. Flush this
267    * output in case any further debugging steps do something bad.
268    */
269   (void)fprintf (
270                 stderr,
271                 "Assertion failed: FILE %s, LINE %u, errno %d.\n",
272                 file_name, line_number, error_number
273                 );
274   errno = error_number;
275   (void)perror ("\tperror");
276   (void)fflush (stderr);
277
278   /*
279    * Do something if the fail_flag is set, otherwise simply return.
280    */
281   if (fail_flag) abort();
282
283   return;
284   }
285
286
287/***************************************************************************
288 * Public Function xcsc_memcpy
289 ***************************************************************************/
290
291void*   xcsc_memcpy (
292                     const char*          file_name,
293                           unsigned int   line_number,
294                           void*          dst,
295                     const void*          src,
296                           size_t         n
297                     )
298   {
299   if (dst == NULL)
300      {
301      fprintf (
302              stderr,
303              "memcpy: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
304              file_name, line_number
305              );
306      return (NULL);
307      }
308   if (src == NULL)
309      {
310      fprintf (
311              stderr,
312              "memcpy: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
313              file_name, line_number
314              );
315      return (NULL);
316      }
317   return (memcpy(dst,src,n));
318   }
319
320
321/***************************************************************************
322 * Public Function xcsc_memset
323 ***************************************************************************/
324
325void*   xcsc_memset (
326                     const char*          file_name,
327                           unsigned int   line_number,
328                           void*          dst,
329                           int            c,
330                           size_t         n
331                     )
332   {
333   if (dst == NULL)
334      {
335      fprintf (
336              stderr,
337              "memset: caught NULL pointer in FILE %s, LINE %u.\n",
338              file_name, line_number
339              );
340      return (NULL);
341      }
342   return (memset(dst,c,n));
343   }
344
345
346/***************************************************************************
347 * Public Function xcsc_strcat
348 ***************************************************************************/
349
350char*   xcsc_strcat (
351                    const char*          file_name,
352                          unsigned int   line_number,
353                          char*          dst,
354                    const char*          src
355                    )
356   {
357   if (dst == NULL)
358      {
359      fprintf (
360              stderr,
361              "strcat: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
362              file_name, line_number
363              );
364      return (NULL);
365      }
366   if (src == NULL)
367      {
368      fprintf (
369              stderr,
370              "strcat: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
371              file_name, line_number
372              );
373      return (NULL);
374      }
375   return (strcat(dst,src));
376   }
377
378
379/***************************************************************************
380 * Public Function xcsc_strcmp
381 ***************************************************************************/
382
383int   xcsc_strcmp (
384                  const char*          file_name,
385                        unsigned int   line_number,
386                  const char*          str1,
387                  const char*          str2
388                  )
389   {
390   if (str1 == NULL)
391      {
392      fprintf (
393              stderr,
394              "strcmp: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
395              file_name, line_number
396              );
397      return (-1);
398      }
399   if (str2 == NULL)
400      {
401      fprintf (
402              stderr,
403              "strcmp: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
404              file_name, line_number
405              );
406      return (1);
407      }
408   return (strcmp(str1,str2));
409   }
410
411
412/***************************************************************************
413 * Public Function xcsc_strncmp
414 ***************************************************************************/
415
416int   xcsc_strncmp (
417                   const char*          file_name,
418                         unsigned int   line_number,
419                   const char*          str1,
420                   const char*          str2,
421                         size_t         n
422                   )
423   {
424   if (str1 == NULL)
425      {
426      fprintf (
427              stderr,
428              "strncmp: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
429              file_name, line_number
430              );
431      return (-1);
432      }
433   if (str2 == NULL)
434      {
435      fprintf (
436              stderr,
437              "strncmp: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
438              file_name, line_number
439              );
440      return (1);
441      }
442   return (strncmp(str1,str2,n));
443   }
444
445
446/***************************************************************************
447 * Public Function xcsc_strcpy
448 ***************************************************************************/
449
450char*   xcsc_strcpy (
451                    const char*          file_name,
452                          unsigned int   line_number,
453                          char*          dst,
454                    const char*          src
455                    )
456   {
457   if (dst == NULL)
458      {
459      fprintf (
460              stderr,
461              "strcpy: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
462              file_name, line_number
463              );
464      return (NULL);
465      }
466   if (src == NULL)
467      {
468      fprintf (
469              stderr,
470              "strcpy: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
471              file_name, line_number
472              );
473      return (NULL);
474      }
475   return (strcpy(dst,src));
476   }
477
478
479/***************************************************************************
480 * Public Function xcsc_strncpy
481 ***************************************************************************/
482
483char*   xcsc_strncpy (
484                     const char*          file_name,
485                           unsigned int   line_number,
486                           char*          dst,
487                     const char*          src,
488                           size_t         n
489                     )
490   {
491   if (dst == NULL)
492      {
493      fprintf (
494              stderr,
495              "strncpy: 1st argument is NULL pointer in FILE %s, LINE %u.\n",
496              file_name, line_number
497              );
498      return (NULL);
499      }
500   if (src == NULL)
501      {
502      fprintf (
503              stderr,
504              "strncpy: 2nd argument is NULL pointer in FILE %s, LINE %u.\n",
505              file_name, line_number
506              );
507      return (NULL);
508      }
509   return (strncpy(dst,src,n));
510   }
511
512
513/***************************************************************************
514 * Public Function xcsc_strlen
515 ***************************************************************************/
516
517int   xcsc_strlen (
518                  const char*          file_name,
519                        unsigned int   line_number,
520                  const char*          ptr
521                  )
522   {
523   if (ptr == NULL)
524      {
525      fprintf (
526              stderr,
527              "strlen: caught NULL pointer in FILE %s, LINE %u.\n",
528              file_name, line_number
529              );
530      return (0);
531      }
532   return (strlen(ptr));
533   }
534
535
536/***************************************************************************
537 * Public Function xcsc_strrchr
538 ***************************************************************************/
539
540char*   xcsc_strrchr (
541                     const char*          file_name,
542                           unsigned int   line_number,
543                     const char*          ptr,
544                           int            c
545                     )
546   {
547   if (ptr == NULL)
548      {
549      fprintf (
550              stderr,
551              "strrchr: caught NULL pointer in FILE %s, LINE %u.\n",
552              file_name, line_number
553              );
554      return (NULL);
555      }
556   return (strrchr(ptr,c));
557   }
558
559
560/* End of file. */
561