mirror of
https://github.com/sheumann/hush.git
synced 2024-10-05 12:54:51 +00:00
Bug 624 wants quoted char support for printf, so you can do something like:
printf '%d\n' '"x"' and have it print out 120. This is the smallest implementation I can think of at the moment.
This commit is contained in:
parent
f8ec1b51f3
commit
251161f75c
@ -1,20 +1,11 @@
|
|||||||
/* vi: set sw=4 ts=4: */
|
/* vi: set sw=4 ts=4: */
|
||||||
/* printf - format and print data
|
/* printf - format and print data
|
||||||
Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify
|
Copyright 1999 Dave Cinege
|
||||||
it under the terms of the GNU General Public License as published by
|
Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
|
||||||
the Free Software Foundation; either version 2, or (at your option)
|
|
||||||
any later version.
|
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful,
|
Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
|
||||||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
|
||||||
|
|
||||||
/* Usage: printf format [argument...]
|
/* Usage: printf format [argument...]
|
||||||
|
|
||||||
@ -58,14 +49,56 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "busybox.h"
|
#include "busybox.h"
|
||||||
|
|
||||||
static double xstrtod __P((char *s));
|
|
||||||
static long xstrtol __P((char *s));
|
|
||||||
static unsigned long xstrtoul __P((char *s));
|
|
||||||
static void print_esc_string __P((char *str));
|
|
||||||
static int print_formatted __P((char *format, int argc, char **argv));
|
static int print_formatted __P((char *format, int argc, char **argv));
|
||||||
static void print_direc __P( (char *start, size_t length,
|
static void print_direc __P( (char *start, size_t length,
|
||||||
int field_width, int precision, char *argument));
|
int field_width, int precision, char *argument));
|
||||||
|
|
||||||
|
typedef int (*converter)(char *arg, void *result);
|
||||||
|
void multiconvert(char *arg, void *result, converter convert)
|
||||||
|
{
|
||||||
|
char s[16];
|
||||||
|
if (*arg == '"' || *arg == '\'') {
|
||||||
|
sprintf(s,"%d",(unsigned)*(++arg));
|
||||||
|
arg=s;
|
||||||
|
}
|
||||||
|
if(convert(arg,result)) fprintf(stderr, "%s", arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned long xstrtoul(char *arg)
|
||||||
|
{
|
||||||
|
unsigned long result;
|
||||||
|
|
||||||
|
multiconvert(arg,&result, (converter)safe_strtoul);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static long xstrtol(char *arg)
|
||||||
|
{
|
||||||
|
long result;
|
||||||
|
multiconvert(arg, &result, (converter)safe_strtol);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static double xstrtod(char *arg)
|
||||||
|
{
|
||||||
|
double result;
|
||||||
|
multiconvert(arg, &result, (converter)safe_strtod);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_esc_string(char *str)
|
||||||
|
{
|
||||||
|
for (; *str; str++) {
|
||||||
|
if (*str == '\\') {
|
||||||
|
str++;
|
||||||
|
putchar(bb_process_escape_sequence((const char **)&str));
|
||||||
|
} else {
|
||||||
|
putchar(*str);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int printf_main(int argc, char **argv)
|
int printf_main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *format;
|
char *format;
|
||||||
@ -277,40 +310,3 @@ print_direc(char *start, size_t length, int field_width, int precision,
|
|||||||
|
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned long xstrtoul(char *arg)
|
|
||||||
{
|
|
||||||
unsigned long result;
|
|
||||||
if (safe_strtoul(arg, &result))
|
|
||||||
fprintf(stderr, "%s", arg);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static long xstrtol(char *arg)
|
|
||||||
{
|
|
||||||
long result;
|
|
||||||
if (safe_strtol(arg, &result))
|
|
||||||
fprintf(stderr, "%s", arg);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static double xstrtod(char *arg)
|
|
||||||
{
|
|
||||||
double result;
|
|
||||||
if (safe_strtod(arg, &result))
|
|
||||||
fprintf(stderr, "%s", arg);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_esc_string(char *str)
|
|
||||||
{
|
|
||||||
for (; *str; str++) {
|
|
||||||
if (*str == '\\') {
|
|
||||||
str++;
|
|
||||||
putchar(bb_process_escape_sequence((const char **)&str));
|
|
||||||
} else {
|
|
||||||
putchar(*str);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user