[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#635379: patch for expand_number()



tags 635379 patch
thanks

Here is a patch.

-- 
Robert Millan
Index: debian/patches/series
===================================================================
--- debian/patches/series	(revision 0)
+++ debian/patches/series	(revision 0)
@@ -0,0 +1 @@
+expand_number.diff
Index: debian/patches/expand_number.diff
===================================================================
--- debian/patches/expand_number.diff	(revision 0)
+++ debian/patches/expand_number.diff	(revision 0)
@@ -0,0 +1,231 @@
+--- a/Makefile
++++ b/Makefile
+@@ -33,6 +33,7 @@
+ 	merge.c \
+ 	humanize_number.c \
+ 	dehumanize_number.c \
++	expand_number.c \
+ 	inet_net_pton.c \
+ 	hash/md5.c \
+ 	pidfile.c \
+@@ -90,6 +91,7 @@
+ 	arc4random_stir.3 \
+ 	arc4random_uniform.3 \
+ 	dehumanize_number.3 \
++	expand_number.3 \
+ 	strtonum.3 \
+ 	strlcpy.3 \
+ 	strlcat.3 \
+--- a/Versions
++++ b/Versions
+@@ -81,6 +81,7 @@
+ 
+ LIBBSD_0.3 {
+     reallocf;
++    expand_number;
+     getpeereid;
+ 
+     mergesort;
+--- a/include/bsd/libutil.h
++++ b/include/bsd/libutil.h
+@@ -53,6 +53,7 @@
+ __BEGIN_DECLS
+ int humanize_number(char *buf, size_t len, int64_t bytes,
+     const char *suffix, int scale, int flags);
++int expand_number(const char *_buf, uint64_t *_num);
+ 
+ int flopen(const char *_path, int _flags, ...);
+ 
+--- /dev/null
++++ b/src/expand_number.3
+@@ -0,0 +1,86 @@
++.\" Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
++.\" Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
++.\" All rights reserved.
++.\"
++.\" Redistribution and use in source and binary forms, with or without
++.\" modification, are permitted provided that the following conditions
++.\" are met:
++.\" 1. Redistributions of source code must retain the above copyright
++.\"    notice, this list of conditions and the following disclaimer.
++.\" 2. Redistributions in binary form must reproduce the above copyright
++.\"    notice, this list of conditions and the following disclaimer in the
++.\"    documentation and/or other materials provided with the distribution.
++.\"
++.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
++.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
++.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++.\" SUCH DAMAGE.
++.\"
++.\" $FreeBSD$
++.\"
++.Dd August 15, 2010
++.Dt EXPAND_NUMBER 3
++.Os
++.Sh NAME
++.Nm expand_number
++.Nd format a number from human readable form
++.Sh LIBRARY
++.Lb libutil
++.Sh SYNOPSIS
++.In libutil.h
++.Ft int
++.Fo expand_number
++.Fa "const char *buf" "uint64_t *num"
++.Fc
++.Sh DESCRIPTION
++The
++.Fn expand_number
++function unformats the
++.Fa buf
++string and stores a unsigned 64-bit quantity at address pointed out by the
++.Fa num
++argument.
++.Pp
++The
++.Fn expand_number
++function
++follows the SI power of two convention.
++.Pp
++The prefixes are:
++.Bl -column "Prefix" "Description" "1000000000000000000" -offset indent
++.It Sy "Prefix" Ta Sy "Description" Ta Sy "Multiplier"
++.It Li k Ta No kilo Ta 1024
++.It Li M Ta No mega Ta 1048576
++.It Li G Ta No giga Ta 1073741824
++.It Li T Ta No tera Ta 1099511627776
++.It Li P Ta No peta Ta 1125899906842624
++.It Li E Ta No exa  Ta 1152921504606846976
++.El
++.Sh RETURN VALUES
++.Rv -std
++.Sh ERRORS
++The
++.Fn expand_number
++function will fail if:
++.Bl -tag -width Er
++.It Bq Er EINVAL
++The given string contains no digits.
++.It Bq Er EINVAL
++An unrecognized prefix was given.
++.It Bq Er ERANGE
++Result doesn't fit into 64 bits.
++.El
++.Sh SEE ALSO
++.Xr humanize_number 3
++.Sh HISTORY
++The
++.Fn expand_number
++function first appeared in
++.Fx 6.3 .
+--- /dev/null
++++ b/src/expand_number.c
+@@ -0,0 +1,101 @@
++/*-
++ * Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
++ * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
++ * All rights reserved.
++ *
++ * Redistribution and use in source and binary forms, with or without
++ * modification, are permitted provided that the following conditions
++ * are met:
++ * 1. Redistributions of source code must retain the above copyright
++ *    notice, this list of conditions and the following disclaimer.
++ * 2. Redistributions in binary form must reproduce the above copyright
++ *    notice, this list of conditions and the following disclaimer in the
++ *    documentation and/or other materials provided with the distribution.
++ *
++ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
++ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
++ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
++ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
++ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
++ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
++ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
++ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
++ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
++ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
++ * SUCH DAMAGE.
++ */
++
++#include <sys/cdefs.h>
++__FBSDID("$FreeBSD$");
++
++#include <sys/types.h>
++#include <ctype.h>
++#include <errno.h>
++#include <inttypes.h>
++#include <libutil.h>
++#include <stdint.h>
++
++/*
++ * Convert an expression of the following forms to a uint64_t.
++ *	1) A positive decimal number.
++ *	2) A positive decimal number followed by a 'b' or 'B' (mult by 1).
++ *	3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10).
++ *	4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20).
++ *	5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30).
++ *	6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40).
++ *	7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50).
++ *	8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60).
++ */
++int
++expand_number(const char *buf, uint64_t *num)
++{
++	uint64_t number;
++	unsigned shift;
++	char *endptr;
++
++	number = strtoumax(buf, &endptr, 0);
++
++	if (endptr == buf) {
++		/* No valid digits. */
++		errno = EINVAL;
++		return (-1);
++	}
++
++	switch (tolower((unsigned char)*endptr)) {
++	case 'e':
++		shift = 60;
++		break;
++	case 'p':
++		shift = 50;
++		break;
++	case 't':
++		shift = 40;
++		break;
++	case 'g':
++		shift = 30;
++		break;
++	case 'm':
++		shift = 20;
++		break;
++	case 'k':
++		shift = 10;
++		break;
++	case 'b':
++	case '\0': /* No unit. */
++		*num = number;
++		return (0);
++	default:
++		/* Unrecognized unit. */
++		errno = EINVAL;
++		return (-1);
++	}
++
++	if ((number << shift) >> shift != number) {
++		/* Overflow */
++		errno = ERANGE;
++		return (-1);
++	}
++
++	*num = number << shift;
++	return (0);
++}
Index: debian/copyright
===================================================================
--- debian/copyright	(revision 3899)
+++ debian/copyright	(working copy)
@@ -451,3 +451,30 @@
     <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
     can do whatever you want with this stuff. If we meet some day, and you think
     this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
+
+    --
+
+    Copyright (c) 2007 Eric Anderson <anderson@FreeBSD.org>
+    Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
+    All rights reserved.
+    
+    Redistribution and use in source and binary forms, with or without
+    modification, are permitted provided that the following conditions
+    are met:
+    1. Redistributions of source code must retain the above copyright
+       notice, this list of conditions and the following disclaimer.
+    2. Redistributions in binary form must reproduce the above copyright
+       notice, this list of conditions and the following disclaimer in the
+       documentation and/or other materials provided with the distribution.
+    
+    THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
+    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
+    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+    SUCH DAMAGE.

Reply to: