]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/libexec/uucp/libuuconf/errstr.c
This commit was generated by cvs2svn to compensate for changes in r53796,
[FreeBSD/FreeBSD.git] / gnu / libexec / uucp / libuuconf / errstr.c
1 /* errstr.c
2    Return a string for a uuconf error.
3
4    Copyright (C) 1992 Ian Lance Taylor
5
6    This file is part of the Taylor UUCP uuconf library.
7
8    This library is free software; you can redistribute it and/or
9    modify it under the terms of the GNU Library General Public License
10    as published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This library is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    Library General Public License for more details.
17
18    You should have received a copy of the GNU Library General Public
19    License along with this library; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21
22    The author of the program may be contacted at ian@airs.com or
23    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
24    */
25
26 #include "uucnfi.h"
27
28 #if USE_RCS_ID
29 const char _uuconf_errstr_rcsid[] = "$FreeBSD$";
30 #endif
31 \f
32 static char *zeprint_num P((char *zbuf, size_t cbuf, int ival));
33 \f
34 /* Return an error string for a uuconf error.  This does not return a
35    uuconf error code, but instead returns the total buffer length.  */
36
37 int
38 uuconf_error_string (pglobal, ierr, zbuf, cbuf)
39      pointer pglobal;
40      int ierr;
41      char *zbuf;
42      size_t cbuf;
43 {
44   struct sglobal *qglobal = (struct sglobal *) pglobal;
45   const char *zfile;
46   size_t cfile;
47   const char *zlineno;
48   char ablineno[100];
49   size_t clineno;
50   const char *zmsg;
51   char abmsg[100];
52   size_t cmsg;
53   const char *zerrno;
54   size_t cerrno;
55   size_t cret;
56   size_t ccopy;
57
58   /* The format of the message is
59
60      filename:lineno: message: errno
61
62      If there is no filename, the trailing colon is not output.  If
63      there is no linenumber, the trailing colon is not output.  If
64      there is no filename, the linenumber is not output, and neither
65      is the space before message.  If there is no errno, the
66      preceeding colon and space are not output.  */
67
68   /* Get the filename to put in the error message, if any.  */
69   if ((ierr & UUCONF_ERROR_FILENAME) == 0
70       || qglobal == NULL
71       || qglobal->zfilename == NULL)
72     {
73       zfile = "";
74       cfile = 0;
75     }
76   else
77     {
78       zfile = qglobal->zfilename;
79       cfile = strlen (zfile) + 1;
80     }
81
82   /* Get the line number to put in the error message, if any.  */
83   if (cfile == 0
84       || (ierr & UUCONF_ERROR_LINENO) == 0
85       || qglobal == NULL
86       || qglobal->ilineno <= 0)
87     {
88       zlineno = "";
89       clineno = 0;
90     }
91   else
92     {
93       zlineno = zeprint_num (ablineno, sizeof ablineno, qglobal->ilineno);
94       clineno = strlen (zlineno) + 1;
95     }
96
97   /* Get the main message.  */
98   switch (UUCONF_ERROR_VALUE (ierr))
99     {
100     case UUCONF_SUCCESS:
101       zmsg = "no error";
102       break;
103     case UUCONF_NOT_FOUND:
104       zmsg = "not found";
105       break;
106     case UUCONF_FOPEN_FAILED:
107       zmsg = "fopen";
108       break;
109     case UUCONF_FSEEK_FAILED:
110       zmsg = "fseek";
111       break;
112     case UUCONF_MALLOC_FAILED:
113       zmsg = "malloc";
114       break;
115     case UUCONF_SYNTAX_ERROR:
116       zmsg = "syntax error";
117       break;
118     default:
119       zmsg = zeprint_num (abmsg, sizeof abmsg, UUCONF_ERROR_VALUE (ierr));
120       zmsg -= sizeof "error " - 1;
121       memcpy ((pointer) zmsg, (pointer) "error ", sizeof "error " - 1);
122       break;
123     }
124
125   cmsg = strlen (zmsg);
126   if (cfile > 0)
127     ++cmsg;
128
129   /* Get the errno string.  Note that strerror is not necessarily
130      reentrant.  */
131   if ((ierr & UUCONF_ERROR_ERRNO) == 0
132       || qglobal == NULL)
133     {
134       zerrno = "";
135       cerrno = 0;
136     }
137   else
138     {
139       zerrno = strerror (qglobal->ierrno);
140       cerrno = strlen (zerrno) + 2;
141     }
142
143   cret = cfile + clineno + cmsg + cerrno + 1;
144
145   if (cbuf == 0)
146     return cret;
147
148   /* Leave room for the null byte.  */
149   --cbuf;
150
151   if (cfile > 0)
152     {
153       ccopy = cfile - 1;
154       if (ccopy > cbuf)
155         ccopy = cbuf;
156       memcpy ((pointer) zbuf, (pointer) zfile, ccopy);
157       zbuf += ccopy;
158       cbuf -= ccopy;
159       if (cbuf > 0)
160         {
161           *zbuf++ = ':';
162           --cbuf;
163         }
164     }
165
166   if (clineno > 0)
167     {
168       ccopy = clineno - 1;
169       if (ccopy > cbuf)
170         ccopy = cbuf;
171       memcpy ((pointer) zbuf, (pointer) zlineno, ccopy);
172       zbuf += ccopy;
173       cbuf -= ccopy;
174       if (cbuf > 0)
175         {
176           *zbuf++ = ':';
177           --cbuf;
178         }
179     }
180       
181   if (cbuf > 0 && cfile > 0)
182     {
183       *zbuf++ = ' ';
184       --cbuf;
185       --cmsg;
186     }
187   ccopy = cmsg;
188   if (ccopy > cbuf)
189     ccopy = cbuf;
190   memcpy ((pointer) zbuf, (pointer) zmsg, ccopy);
191   zbuf += ccopy;
192   cbuf -= ccopy;
193
194   if (cerrno > 0)
195     {
196       if (cbuf > 0)
197         {
198           *zbuf++ = ':';
199           --cbuf;
200         }
201       if (cbuf > 0)
202         {
203           *zbuf++ = ' ';
204           --cbuf;
205         }
206       ccopy = cerrno - 2;
207       if (ccopy > cbuf)
208         ccopy = cbuf;
209       memcpy ((pointer) zbuf, (pointer) zerrno, ccopy);
210       zbuf += ccopy;
211       cbuf -= ccopy;
212     }
213
214   *zbuf = '\0';
215
216   return cret;
217 }
218
219 /* Turn a number into a string.  This should really call sprintf, but
220    since nothing else in the uuconf library calls any print routine,
221    it's more interesting to not call it here either.  */
222
223 static char *
224 zeprint_num (ab, c, i)
225      char *ab;
226      size_t c;
227      register int i;
228 {
229   register char *z;
230
231   z = ab + c;
232   *--z = '\0';
233   do
234     {
235       *--z = i % 10 + '0';
236       i /= 10;
237     }
238   while (i != 0);
239
240   return z;
241 }