Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / stacks / AROSTCP / netlib / dosio_sprintf.c
blobc4d3f066d0a3bbd7dd536d0813c59b4b2487bd9d
1 /* $Id$
3 * dosio_sprintf.c - formatted print to a buffer using RawDoFmt()
5 * Copyright © 1994 AmiTCP/IP Group,
6 * Network Solutions Development Inc.
7 * All rights reserved.
8 */
10 #include <sys/cdefs.h> /* REG() and ASM macros */
11 #include <dos/rdargs.h> /* CSource */
12 #include <stdarg.h>
14 #if __GNUC__
15 #include <inline/exec.h>
16 #elif __SASC
17 #include <proto/exec.h>
18 #else
19 #include <clib/exec_protos.h>
20 #endif
22 static void ASM
23 __stuffchar(REG(d0) char ch,
24 REG(a3) struct CSource * sc)
26 if (sc->CS_CurChr < sc->CS_Length
27 && (sc->CS_Buffer[sc->CS_CurChr] = ch))
28 sc->CS_CurChr++;
31 /****** net.lib/SPrintf ******************************************
33 NAME
34 SPrintf -- formatted print to a buffer
36 SYNOPSIS
37 len = SPrintf(Buffer, FormatString, Arguments...)
38 len = VSPrintf(Buffer, FormatString, ap)
40 ULONG SPrintf(STRPTR, const char *, ...)
41 ULONG VSPrintf(STRPTR, const char *, va_list)
43 FUNCTION
44 Prints to a simple buffer or to a CSource buffer. These functions
45 are similar to C-library sprintf() with RawDoFmt() formatting.
47 INPUTS
48 Buffer - Pointer to buffer.
49 FormatString - This is a printf()-style format string as defined
50 in exec.library/RawDoFmt().
51 Arguments - as in printf() .
53 Result - Pointer to CSource structure.
55 RESULT
56 Number of characters printed.
58 EXAMPLE
59 SPrintf(mybuf, "line=%ld, val=%lx\n",
60 __LINE__, very.interesting->value);
62 BUGS
63 Function SPrintf() assumes that no print is longer than 1024 chars.
64 It does not check for buffer owerflow (there no way to check, the
65 definition of sprintf misses it).
67 SPrintf strings are truncated to maximum of 1024 chars (including
68 final NUL)
70 SEE ALSO
71 exec.library/RawDoFmt()
73 ******************************************************************************
77 ULONG
78 VCSPrintf(struct CSource *buf, const char *fmt, va_list ap)
80 ULONG start = buf->CS_CurChr;
82 if (buf->CS_Length && buf->CS_CurChr < buf->CS_Length) {
83 RawDoFmt((STRPTR)fmt, ap, __stuffchar, buf);
85 if (buf->CS_CurChr == buf->CS_Length) {
86 buf->CS_CurChr--; /* must NUL terminate */
88 buf->CS_Buffer[buf->CS_CurChr] = '\0';
90 return buf->CS_CurChr - start;
91 } else {
92 /* A pathological case */
93 return 0;
97 ULONG
98 CSPrintf(struct CSource *buf, const char *fmt, ...)
100 va_list ap;
101 ULONG len;
103 va_start(ap, fmt);
104 len = VCSPrintf(buf, fmt, ap);
105 va_end(ap);
106 return len;
109 ULONG
110 VSPrintf(char *buf, const char *fmt, va_list ap)
112 struct CSource cs;
114 cs.CS_Buffer = buf;
115 cs.CS_CurChr = 0;
116 cs.CS_Length = 1024; /* This is not probably the cleanest way :-) */
118 return VCSPrintf(&cs, fmt, ap);
121 ULONG
122 SPrintf(char *buf, const char *fmt, ...)
124 va_list ap;
125 ULONG len;
127 va_start(ap, fmt);
128 len = VSPrintf(buf, fmt, ap);
129 va_end(ap);
130 return len;