Fix for the (stupid) case of app (always) passing
[tangerine.git] / rom / dos / fault.c
blob63f8b8be9d8f4055b92fed296365d1ca2069c0b0
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: dos.library function Fault()
6 Lang: english
7 */
8 #include <aros/options.h>
9 #include "dos_intern.h"
10 #if PassThroughErrnos
11 # include <errno.h>
12 #endif
13 #include <string.h>
15 /*****************************************************************************
17 NAME */
18 #include <proto/dos.h>
20 AROS_LH4(BOOL, Fault,
22 /* SYNOPSIS */
23 AROS_LHA(LONG, code, D1),
24 AROS_LHA(CONST_STRPTR, header, D2),
25 AROS_LHA(STRPTR, buffer, D3),
26 AROS_LHA(LONG, len, D4),
28 /* LOCATION */
29 struct DosLibrary *, DOSBase, 78, Dos)
31 /* FUNCTION
32 Fault will obtain the error message string for the given error
33 code. First the header string is copied to the buffer, followed
34 by a ":" (colon), then the NULL terminated string for the error
35 message into the buffer.
37 By convention, error messages are ALWAYS less than 80 (plus 1 for
38 NULL termination), and ideally less than 60 characters.
40 If the error code is not know, then the string "Unknown error"
41 followed by the error number will be added to the string.
43 INPUTS
44 code - The error code.
45 header - The string to prepend to the buffer before the error
46 text. This may be NULL in which case nothing is prepended.
47 buffer - The destination buffer.
48 len - Length of the buffer.
50 RESULT
51 Number of characters placed in the buffer, may be 0.
53 NOTES
55 EXAMPLE
57 BUGS
59 SEE ALSO
61 INTERNALS
63 *****************************************************************************/
65 AROS_LIBFUNC_INIT
66 AROS_LIBBASE_EXT_DECL(struct DosLibrary *,DOSBase)
68 LONG index = 0;
69 STRPTR theString;
71 if (code == 0)
73 *buffer = '\0';
74 return 0;
77 /* Do this to make sure there is room for a NULL terminator */
78 len--;
80 if (header)
82 while((index < len) && *header)
84 buffer[index++] = *header++;
87 buffer[index++] = ':';
88 buffer[index++] = ' ';
91 theString = DosGetString(code);
92 #if PassThroughErrnos
93 if ((!theString) && (code & PassThroughErrnos))
95 theString = strerror (code ^ PassThroughErrnos);
97 #endif
98 if(theString)
100 while((index < len) && *theString)
102 buffer[index++] = *theString++;
105 else
107 /* String buffer/index for long 2 string */
108 UBYTE l2str[12], l2idx = 11;
110 theString = "Unknown error ";
111 while((index < len) && *theString)
113 buffer[index++] = *theString++;
116 /* If the number is negative, whack in a - sign. */
117 if(code < 0)
119 code = -code;
120 buffer[index++] = '-';
123 /* Convert the number to a string, I work backwards, its easier */
124 l2str[l2idx--] = '\0';
125 while(code != 0)
127 l2str[l2idx--] = (code % 10) + '0';
128 code /= 10;
131 l2str[l2idx] = ' ';
133 /* Copy the number onto the fault string */
134 while((index < len) && l2str[l2idx])
136 buffer[index++] = l2str[l2idx++];
139 buffer[index] = '\0';
140 return (len - index + 1);
142 AROS_LIBFUNC_EXIT
143 } /* Fault */