mirror of
https://git.savannah.gnu.org/git/inetutils.git
synced 2026-01-19 00:08:17 +08:00
ftp/extern.h ftp/ftp.1 ftp/ftp.c ftp/ftp_var.h ftp/main.c ftp/ruserpass.c ftpd/ChangeLog ftpd/extern.h ftpd/ftpcmd.y ftpd/ftpd.8 ftpd/ftpd.c ftpd/popen.c headers/ChangeLog headers/paths.h headers/syslog-int.h headers/tftpsubs.h headers/arpa/ftp.h headers/arpa/telnet.h headers/arpa/tftp.h headers/protocols/talkd.h inetd/ChangeLog inetd/inetd.8 inetd/inetd.c libinetutils/ChangeLog libinetutils/daemon.c libinetutils/des_rw.c libinetutils/forkpty.c libinetutils/iruserok.c libinetutils/kcmd.c libinetutils/krcmd.c libinetutils/login_tty.c libinetutils/openpty.c libinetutils/snprintf.c libinetutils/snprintf.h libinetutils/syslog.c libinetutils/tftpsubs.c libinetutils/ttymsg.c libinetutils/utmp_init.c libls/cmp.c libls/extern.h libls/ls.1 libls/ls.c libls/ls.h libls/print.c libls/stat_flags.c libls/util.c libtelnet/ChangeLog libtelnet/auth-proto.h libtelnet/auth.c libtelnet/auth.h libtelnet/enc-proto.h libtelnet/enc_des.c libtelnet/encrypt.c libtelnet/encrypt.h libtelnet/forward.c libtelnet/genget.c libtelnet/getent.c libtelnet/kerberos.c libtelnet/kerberos5.c libtelnet/key-proto.h libtelnet/misc-proto.h libtelnet/misc.c libtelnet/misc.h libtelnet/read_passwd.c rcp/ChangeLog rcp/extern.h rcp/rcp.1 rcp/rcp.c rcp/util.c rexecd/ChangeLog rexecd/rexecd.8 rexecd/rexecd.c rlogin/ChangeLog rlogin/rlogin.1 rlogin/rlogin.c rlogind/ChangeLog rlogind/rlogind.8 rlogind/rlogind.c rsh/ChangeLog rsh/rsh.1 rsh/rsh.c rshd/ChangeLog rshd/rshd.8 rshd/rshd.c syslog/ChangeLog syslog/syslog.1 syslog/syslog.c syslogd/ChangeLog syslogd/syslog.conf.5 syslogd/syslogd.8 syslogd/syslogd.c talk/ChangeLog talk/ctl.c talk/ctl_transact.c talk/display.c talk/get_addrs.c talk/get_names.c talk/init_disp.c talk/invite.c talk/io.c talk/look_up.c talk/msgs.c talk/talk.1 talk/talk.c talk/talk.h talk/talk_ctl.h talkd/ChangeLog talkd/announce.c talkd/print.c talkd/process.c talkd/table.c talkd/talkd.8 talkd/talkd.c telnet/ChangeLog telnet/authenc.c telnet/commands.c telnet/defines.h telnet/externs.h telnet/fdset.h telnet/general.h telnet/main.c telnet/network.c telnet/ring.c telnet/ring.h telnet/sys_bsd.c telnet/telnet.1 telnet/telnet.c telnet/terminal.c telnet/tn3270.c telnet/types.h telnet/utilities.c telnetd/ChangeLog telnetd/authenc.c telnetd/defs.h telnetd/ext.h telnetd/global.c telnetd/slc.c telnetd/state.c telnetd/sys_term.c telnetd/telnetd.8 telnetd/telnetd.c telnetd/telnetd.h telnetd/termstat.c telnetd/utility.c tftp/ChangeLog tftp/extern.h tftp/main.c tftp/tftp.1 tftp/tftp.c tftpd/ChangeLog tftpd/tftpd.8 tftpd/tftpd.c uucpd/ChangeLog uucpd/uucpd.c whois/ChangeLog whois/main.c whois/net.c * *: Clause 3 removed. excerpt from email with RMS: "UCB has agreed to drop the advertising clause from the license for all BSD software. The advertising clause is this one: 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by the University of California, Berkeley and its contributors. This means that we can delete that paragraph from all files which give the University of California as the only copyright holder."
205 lines
5.2 KiB
C
205 lines
5.2 KiB
C
/*
|
|
Unix snprintf implementation.
|
|
Version 1.3
|
|
|
|
This program 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 2 of the License, or
|
|
(at your option) any later version.
|
|
It can be redistribute also under the terms of GNU Library General
|
|
Public Lincense.
|
|
|
|
This program 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 this program; if not, write to the Free Software
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
Revision History:
|
|
see header of snprintf.c.
|
|
|
|
format:
|
|
int snprintf(holder, sizeof_holder, format, ...)
|
|
|
|
Return values:
|
|
(sizeof_holder - 1)
|
|
|
|
|
|
THANKS(for the patches and ideas):
|
|
Miles Bader
|
|
Cyrille Rustom
|
|
Jacek Slabocewiz
|
|
Mike Parker(mouse)
|
|
|
|
Alain Magloire: alainm@rcsm.ee.mcgill.ca
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include <config.h>
|
|
#endif
|
|
|
|
#if defined(HAVE_STDARG_H) && defined(__STDC__) && __STDC__
|
|
#include <stdarg.h>
|
|
#else
|
|
#include <varargs.h>
|
|
#endif
|
|
|
|
#include <stdlib.h> /* for atoi() */
|
|
#include <ctype.h>
|
|
|
|
|
|
/*
|
|
* For the FLOATING POINT FORMAT :
|
|
* the challenge was finding a way to
|
|
* manipulate the Real numbers without having
|
|
* to resort to mathematical function(it
|
|
* would require to link with -lm) and not
|
|
* going down to the bit pattern(not portable)
|
|
*
|
|
* so a number, a real is:
|
|
|
|
real = integral + fraction
|
|
|
|
integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
|
|
fraction = b(1)*10^-1 + b(2)*10^-2 + ...
|
|
|
|
where:
|
|
0 <= a(i) => 9
|
|
0 <= b(i) => 9
|
|
|
|
from then it was simple math
|
|
*/
|
|
|
|
/*
|
|
* size of the buffer for the integral part
|
|
* and the fraction part
|
|
*/
|
|
#define MAX_INT 99 + 1 /* 1 for the null */
|
|
#define MAX_FRACT 29 + 1
|
|
|
|
/*
|
|
* numtoa() uses PRIVATE buffers to store the results,
|
|
* So this function is not reentrant
|
|
*/
|
|
#define itoa(n) numtoa(n, 10, 0, (char **)0)
|
|
#define otoa(n) numtoa(n, 8, 0, (char **)0)
|
|
#define htoa(n) numtoa(n, 16, 0, (char **)0)
|
|
#define dtoa(n, p, f) numtoa(n, 10, p, f)
|
|
|
|
#define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}
|
|
|
|
/* this struct holds everything we need */
|
|
struct DATA {
|
|
int length;
|
|
char *holder;
|
|
int counter;
|
|
#ifdef __STDC__
|
|
const char *pf;
|
|
#else
|
|
char *pf;
|
|
#endif
|
|
/* FLAGS */
|
|
int width, precision;
|
|
int justify; char pad;
|
|
int square, space, star_w, star_p, a_long ;
|
|
};
|
|
|
|
#define PRIVATE static
|
|
#define PUBLIC
|
|
/* signature of the functions */
|
|
#ifdef __STDC__
|
|
/* the floating point stuff */
|
|
PRIVATE double pow_10(int);
|
|
PRIVATE int log_10(double);
|
|
PRIVATE double integral(double, double *);
|
|
PRIVATE char * numtoa(double, int, int, char **);
|
|
|
|
/* for the format */
|
|
PRIVATE void conv_flag(char *, struct DATA *);
|
|
PRIVATE void floating(struct DATA *, double);
|
|
PRIVATE void exponent(struct DATA *, double);
|
|
PRIVATE void decimal(struct DATA *, double);
|
|
PRIVATE void octal(struct DATA *, double);
|
|
PRIVATE void hexa(struct DATA *, double);
|
|
PRIVATE void strings(struct DATA *, char *);
|
|
|
|
#else
|
|
/* the floating point stuff */
|
|
PRIVATE double pow_10();
|
|
PRIVATE int log_10();
|
|
PRIVATE double integral();
|
|
PRIVATE char * numtoa();
|
|
|
|
/* for the format */
|
|
PRIVATE void conv_flag();
|
|
PRIVATE void floating();
|
|
PRIVATE void exponent();
|
|
PRIVATE void decimal();
|
|
PRIVATE void octal();
|
|
PRIVATE void hexa();
|
|
PRIVATE void strings();
|
|
#endif
|
|
|
|
/* those are defines specific to snprintf to hopefully
|
|
* make the code clearer :-)
|
|
*/
|
|
#define RIGHT 1
|
|
#define LEFT 0
|
|
#define NOT_FOUND -1
|
|
#define FOUND 1
|
|
#define MAX_FIELD 15
|
|
|
|
/* the conversion flags */
|
|
#define isflag(c) ((c) == '#' || (c) == ' ' || \
|
|
(c) == '*' || (c) == '+' || \
|
|
(c) == '-' || (c) == '.' || \
|
|
isdigit(c))
|
|
|
|
/* round off to the precision */
|
|
#define ROUND(d, p) \
|
|
(d < 0.) ? \
|
|
d - pow_10(-(p)->precision) * 0.5 : \
|
|
d + pow_10(-(p)->precision) * 0.5
|
|
|
|
/* set default precision */
|
|
#define DEF_PREC(p) \
|
|
if ((p)->precision == NOT_FOUND) \
|
|
(p)->precision = 6
|
|
|
|
/* put a char */
|
|
#define PUT_CHAR(c, p) \
|
|
if ((p)->counter < (p)->length) { \
|
|
*(p)->holder++ = (c); \
|
|
(p)->counter++; \
|
|
}
|
|
|
|
#define PUT_PLUS(d, p) \
|
|
if ((d) > 0. && (p)->justify == RIGHT) \
|
|
PUT_CHAR('+', p)
|
|
|
|
#define PUT_SPACE(d, p) \
|
|
if ((p)->space == FOUND && (d) > 0.) \
|
|
PUT_CHAR(' ', p)
|
|
|
|
/* pad right */
|
|
#define PAD_RIGHT(p) \
|
|
if ((p)->width > 0 && (p)->justify != LEFT) \
|
|
for (; (p)->width > 0; (p)->width--) \
|
|
PUT_CHAR((p)->pad, p)
|
|
|
|
/* pad left */
|
|
#define PAD_LEFT(p) \
|
|
if ((p)->width > 0 && (p)->justify == LEFT) \
|
|
for (; (p)->width > 0; (p)->width--) \
|
|
PUT_CHAR((p)->pad, p)
|
|
|
|
/* if width and prec. in the args */
|
|
#define STAR_ARGS(p) \
|
|
if ((p)->star_w == FOUND) \
|
|
(p)->width = va_arg(args, int); \
|
|
if ((p)->star_p == FOUND) \
|
|
(p)->precision = va_arg(args, int)
|