Move external code used by libxbps into lib/external.
This commit is contained in:
350
lib/external/dewey.c
vendored
Normal file
350
lib/external/dewey.c
vendored
Normal file
@ -0,0 +1,350 @@
|
||||
/* $NetBSD: dewey.c,v 1.1.1.3 2009/03/08 14:51:37 joerg Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright <20> 2002 Alistair G. Crooks. 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.
|
||||
* 3. The name of the author may not be used to endorse or promote
|
||||
* products derived from this software without specific prior written
|
||||
* permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "xbps_api_impl.h"
|
||||
|
||||
#define PKG_PATTERN_MAX 1024
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
|
||||
#endif
|
||||
|
||||
enum {
|
||||
DEWEY_LT,
|
||||
DEWEY_LE,
|
||||
DEWEY_EQ,
|
||||
DEWEY_GE,
|
||||
DEWEY_GT,
|
||||
DEWEY_NE
|
||||
};
|
||||
|
||||
/* do not modify these values, or things will NOT work */
|
||||
enum {
|
||||
Alpha = -3,
|
||||
Beta = -2,
|
||||
RC = -1,
|
||||
Dot = 0,
|
||||
Patch = 1
|
||||
};
|
||||
|
||||
/* this struct defines a version number */
|
||||
typedef struct arr_t {
|
||||
unsigned c; /* # of version numbers */
|
||||
unsigned size; /* size of array */
|
||||
int *v; /* array of decimal numbers */
|
||||
int revision; /* any "_" suffix */
|
||||
} arr_t;
|
||||
|
||||
/* this struct describes a test */
|
||||
typedef struct test_t {
|
||||
const char *s; /* string representation */
|
||||
unsigned len; /* length of string */
|
||||
int t; /* enumerated type of test */
|
||||
} test_t;
|
||||
|
||||
|
||||
/* the tests that are recognised. */
|
||||
const test_t tests[] = {
|
||||
{ "<=", 2, DEWEY_LE },
|
||||
{ "<", 1, DEWEY_LT },
|
||||
{ ">=", 2, DEWEY_GE },
|
||||
{ ">", 1, DEWEY_GT },
|
||||
{ "==", 2, DEWEY_EQ },
|
||||
{ "!=", 2, DEWEY_NE },
|
||||
{ NULL, 0, 0 }
|
||||
};
|
||||
|
||||
const test_t modifiers[] = {
|
||||
{ "alpha", 5, Alpha },
|
||||
{ "beta", 4, Beta },
|
||||
{ "pre", 3, RC },
|
||||
{ "rc", 2, RC },
|
||||
{ "pl", 2, Dot },
|
||||
{ ".", 1, Dot },
|
||||
{ NULL, 0, 0 }
|
||||
};
|
||||
|
||||
|
||||
|
||||
/* locate the test in the tests array */
|
||||
static int
|
||||
dewey_mktest(int *op, const char *test)
|
||||
{
|
||||
const test_t *tp;
|
||||
|
||||
for (tp = tests ; tp->s ; tp++) {
|
||||
if (strncasecmp(test, tp->s, tp->len) == 0) {
|
||||
*op = tp->t;
|
||||
return tp->len;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*
|
||||
* make a component of a version number.
|
||||
* '.' encodes as Dot which is '0'
|
||||
* 'pl' encodes as 'patch level', or 'Dot', which is 0.
|
||||
* 'alpha' encodes as 'alpha version', or Alpha, which is -3.
|
||||
* 'beta' encodes as 'beta version', or Beta, which is -2.
|
||||
* 'rc' encodes as 'release candidate', or RC, which is -1.
|
||||
* '_' encodes as 'xbps revision', which is used after all other tests
|
||||
*/
|
||||
static int
|
||||
mkcomponent(arr_t *ap, const char *num)
|
||||
{
|
||||
static const char alphas[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
const test_t *modp;
|
||||
int n;
|
||||
const char *cp;
|
||||
|
||||
if (ap->c == ap->size) {
|
||||
if (ap->size == 0) {
|
||||
ap->size = 62;
|
||||
if ((ap->v = malloc(ap->size * sizeof(int))) == NULL) {
|
||||
xbps_dbg_printf("%s: malloc ENOMEM\n",
|
||||
__func__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
} else {
|
||||
ap->size *= 2;
|
||||
ap->v = realloc(ap->v, ap->size * sizeof(int));
|
||||
if (ap->v == NULL) {
|
||||
xbps_dbg_printf("%s: realloc ENOMEM\n",
|
||||
__func__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isdigit((unsigned char)*num)) {
|
||||
for (cp = num, n = 0 ; isdigit((unsigned char)*num) ; num++) {
|
||||
n = (n * 10) + (*num - '0');
|
||||
}
|
||||
ap->v[ap->c++] = n;
|
||||
return (int)(num - cp);
|
||||
}
|
||||
for (modp = modifiers ; modp->s ; modp++) {
|
||||
if (strncasecmp(num, modp->s, modp->len) == 0) {
|
||||
ap->v[ap->c++] = modp->t;
|
||||
return modp->len;
|
||||
}
|
||||
}
|
||||
if (strncasecmp(num, "_", 1) == 0) {
|
||||
for (cp = num, num += 1, n = 0 ; isdigit((unsigned char)*num) ; num++) {
|
||||
n = (n * 10) + (*num - '0');
|
||||
}
|
||||
ap->revision = n;
|
||||
return (int)(num - cp);
|
||||
}
|
||||
if (isalpha((unsigned char)*num)) {
|
||||
ap->v[ap->c++] = Dot;
|
||||
cp = strchr(alphas, tolower((unsigned char)*num));
|
||||
if (ap->c == ap->size) {
|
||||
ap->size *= 2;
|
||||
if ((ap->v = realloc(ap->v, ap->size * sizeof(int))) == NULL) {
|
||||
xbps_dbg_printf("%s: ENOMEM!\n", __func__);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
ap->v[ap->c++] = (int)(cp - alphas) + 1;
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* make a version number string into an array of comparable ints */
|
||||
static int
|
||||
mkversion(arr_t *ap, const char *num)
|
||||
{
|
||||
ap->c = 0;
|
||||
ap->size = 0;
|
||||
ap->v = NULL;
|
||||
ap->revision = 0;
|
||||
|
||||
while (*num) {
|
||||
num += mkcomponent(ap, num);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
freeversion(arr_t *ap)
|
||||
{
|
||||
free(ap->v);
|
||||
ap->v = NULL;
|
||||
ap->c = 0;
|
||||
ap->size = 0;
|
||||
}
|
||||
|
||||
#define DIGIT(v, c, n) (((n) < (c)) ? v[n] : 0)
|
||||
|
||||
/* compare the result against the test we were expecting */
|
||||
static int
|
||||
result(int cmp, int tst)
|
||||
{
|
||||
switch(tst) {
|
||||
case DEWEY_LT:
|
||||
return cmp < 0;
|
||||
case DEWEY_LE:
|
||||
return cmp <= 0;
|
||||
case DEWEY_GT:
|
||||
return cmp > 0;
|
||||
case DEWEY_GE:
|
||||
return cmp >= 0;
|
||||
case DEWEY_EQ:
|
||||
return cmp == 0;
|
||||
case DEWEY_NE:
|
||||
return cmp != 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* do the test on the 2 vectors */
|
||||
static int
|
||||
vtest(arr_t *lhs, int tst, arr_t *rhs)
|
||||
{
|
||||
int cmp;
|
||||
unsigned int c, i;
|
||||
|
||||
for (i = 0, c = MAX(lhs->c, rhs->c) ; i < c ; i++) {
|
||||
if ((cmp = DIGIT(lhs->v, lhs->c, i) - DIGIT(rhs->v, rhs->c, i)) != 0) {
|
||||
return result(cmp, tst);
|
||||
}
|
||||
}
|
||||
return result(lhs->revision - rhs->revision, tst);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare two dewey decimal numbers.
|
||||
*/
|
||||
static int
|
||||
dewey_cmp(const char *lhs, int op, const char *rhs)
|
||||
{
|
||||
arr_t right;
|
||||
arr_t left;
|
||||
int retval;
|
||||
|
||||
if (!mkversion(&left, lhs))
|
||||
return 0;
|
||||
if (!mkversion(&right, rhs)) {
|
||||
freeversion(&left);
|
||||
return 0;
|
||||
}
|
||||
retval = vtest(&left, op, &right);
|
||||
freeversion(&left);
|
||||
freeversion(&right);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns -1, 0 or 1 depending on if the version components of
|
||||
* pkg1 is less than, equal to or greater than pkg2. No comparison
|
||||
* comparison of the basenames is done.
|
||||
*/
|
||||
int
|
||||
xbps_cmpver(const char *pkg1, const char *pkg2)
|
||||
{
|
||||
if (dewey_cmp(pkg1, DEWEY_LT, pkg2))
|
||||
return -1;
|
||||
else if (dewey_cmp(pkg1, DEWEY_GT, pkg2))
|
||||
return 1;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform dewey match on "pkg" against "pattern".
|
||||
* Return 1 on match, 0 on non-match, -1 on error.
|
||||
*/
|
||||
int HIDDEN
|
||||
dewey_match(const char *pattern, const char *pkg)
|
||||
{
|
||||
const char *version;
|
||||
const char *sep, *sep2;
|
||||
int op, op2;
|
||||
int n;
|
||||
|
||||
/* compare names */
|
||||
if ((version=strrchr(pkg, '-')) == NULL) {
|
||||
return 0;
|
||||
}
|
||||
if ((sep = strpbrk(pattern, "<>")) == NULL)
|
||||
return -1;
|
||||
/* compare name lengths */
|
||||
if ((sep-pattern != version-pkg) ||
|
||||
strncmp(pkg, pattern, (size_t)(version-pkg)) != 0)
|
||||
return 0;
|
||||
version++;
|
||||
|
||||
/* extract comparison operator */
|
||||
if ((n = dewey_mktest(&op, sep)) < 0) {
|
||||
return 0;
|
||||
}
|
||||
/* skip operator */
|
||||
sep += n;
|
||||
|
||||
/* if greater than, look for less than */
|
||||
sep2 = NULL;
|
||||
if (op == DEWEY_GT || op == DEWEY_GE) {
|
||||
if ((sep2 = strchr(sep, '<')) != NULL) {
|
||||
if ((n = dewey_mktest(&op2, sep2)) < 0) {
|
||||
return 0;
|
||||
}
|
||||
/* compare upper limit */
|
||||
if (!dewey_cmp(version, op2, sep2+n))
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* compare only pattern / lower limit */
|
||||
if (sep2) {
|
||||
char ver[PKG_PATTERN_MAX];
|
||||
|
||||
strlcpy(ver, sep, MIN((ssize_t)sizeof(ver), sep2-sep+1));
|
||||
if (dewey_cmp(version, op, ver))
|
||||
return 1;
|
||||
} else {
|
||||
if (dewey_cmp(version, op, sep))
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
173
lib/external/fexec.c
vendored
Normal file
173
lib/external/fexec.c
vendored
Normal file
@ -0,0 +1,173 @@
|
||||
/*-
|
||||
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Matthias Scheler.
|
||||
*
|
||||
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "xbps_api_impl.h"
|
||||
|
||||
static int
|
||||
pfcexec(const char *path, const char *file, const char **argv)
|
||||
{
|
||||
pid_t child;
|
||||
int status;
|
||||
bool do_chroot = false;
|
||||
|
||||
child = vfork();
|
||||
switch (child) {
|
||||
case 0:
|
||||
if (getuid() == 0 && access("./bin/sh", X_OK) == 0)
|
||||
do_chroot = true;
|
||||
|
||||
/*
|
||||
* If uid==0 and /bin/sh exists, we can change root directory,
|
||||
* fork and execute the command. Otherwise just change current
|
||||
* directory and fork/execute.
|
||||
*/
|
||||
if (path && do_chroot) {
|
||||
if (chroot(path) == -1)
|
||||
_exit(127);
|
||||
if (chdir("/") == -1)
|
||||
_exit(127);
|
||||
} else if (path && !do_chroot) {
|
||||
if (chdir(path) == -1)
|
||||
_exit(127);
|
||||
} else if (path == NULL && do_chroot) {
|
||||
if (chroot(".") == -1) {
|
||||
if (errno != EPERM)
|
||||
_exit(127);
|
||||
if (chdir(".") == -1)
|
||||
_exit(127);
|
||||
} else {
|
||||
if (chdir("/") == -1)
|
||||
_exit(127);
|
||||
}
|
||||
}
|
||||
|
||||
(void)execv(file, __UNCONST(argv));
|
||||
_exit(127);
|
||||
/* NOTREACHED */
|
||||
case -1:
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (waitpid(child, &status, 0) < 0) {
|
||||
if (errno != EINTR)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!WIFEXITED(status))
|
||||
return -1;
|
||||
|
||||
return WEXITSTATUS(status);
|
||||
}
|
||||
|
||||
static int
|
||||
vfcexec(const char *path, int skipempty, const char *arg, va_list ap)
|
||||
{
|
||||
const char **argv;
|
||||
size_t argv_size, argc;
|
||||
int retval;
|
||||
|
||||
argv_size = 16;
|
||||
if ((argv = malloc(argv_size * sizeof(*argv))) == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
|
||||
argv[0] = arg;
|
||||
argc = 1;
|
||||
|
||||
do {
|
||||
if (argc == argv_size) {
|
||||
argv_size *= 2;
|
||||
argv = realloc(argv, argv_size * sizeof(*argv));
|
||||
if (argv == NULL) {
|
||||
errno = ENOMEM;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
arg = va_arg(ap, const char *);
|
||||
if (skipempty && arg && strlen(arg) == 0)
|
||||
continue;
|
||||
|
||||
argv[argc++] = arg;
|
||||
|
||||
} while (arg != NULL);
|
||||
|
||||
retval = pfcexec(path, argv[0], argv);
|
||||
free(argv);
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
int HIDDEN
|
||||
xbps_file_exec(const char *arg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
va_start(ap, arg);
|
||||
result = vfcexec(NULL, 0, arg, ap);
|
||||
va_end(ap);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int HIDDEN
|
||||
xbps_file_exec_skipempty(const char *arg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
va_start(ap, arg);
|
||||
result = vfcexec(NULL, 1, arg, ap);
|
||||
va_end(ap);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int HIDDEN
|
||||
xbps_file_chdir_exec(const char *path, const char *arg, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int result;
|
||||
|
||||
va_start(ap, arg);
|
||||
result = vfcexec(path, 0, arg, ap);
|
||||
va_end(ap);
|
||||
|
||||
return result;
|
||||
}
|
163
lib/external/humanize_number.c
vendored
Normal file
163
lib/external/humanize_number.c
vendored
Normal file
@ -0,0 +1,163 @@
|
||||
/* $NetBSD: humanize_number.c,v 1.14 2008/04/28 20:22:59 martin Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from software contributed to The NetBSD Foundation
|
||||
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
|
||||
* NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
|
||||
*
|
||||
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <assert.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
|
||||
#include <xbps_api.h>
|
||||
|
||||
#define HN_DECIMAL 0x01
|
||||
#define HN_NOSPACE 0x02
|
||||
#define HN_B 0x04
|
||||
#define HN_DIVISOR_1000 0x08
|
||||
#define HN_GETSCALE 0x10
|
||||
#define HN_AUTOSCALE 0x20
|
||||
|
||||
static int
|
||||
humanize_number(char *buf, size_t len, int64_t bytes,
|
||||
const char *suffix, int scale, int flags)
|
||||
{
|
||||
const char *prefixes, *sep;
|
||||
int b, i, r, maxscale, s1, s2, sign;
|
||||
int64_t divisor, max;
|
||||
size_t baselen;
|
||||
|
||||
assert(buf != NULL);
|
||||
assert(suffix != NULL);
|
||||
assert(scale >= 0);
|
||||
|
||||
if (flags & HN_DIVISOR_1000) {
|
||||
/* SI for decimal multiplies */
|
||||
divisor = 1000;
|
||||
if (flags & HN_B)
|
||||
prefixes = "B\0k\0M\0G\0T\0P\0E";
|
||||
else
|
||||
prefixes = "\0\0k\0M\0G\0T\0P\0E";
|
||||
} else {
|
||||
/*
|
||||
* binary multiplies
|
||||
* XXX IEC 60027-2 recommends Ki, Mi, Gi...
|
||||
*/
|
||||
divisor = 1024;
|
||||
if (flags & HN_B)
|
||||
prefixes = "B\0K\0M\0G\0T\0P\0E";
|
||||
else
|
||||
prefixes = "\0\0K\0M\0G\0T\0P\0E";
|
||||
}
|
||||
|
||||
#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
|
||||
maxscale = 7;
|
||||
|
||||
if (scale >= maxscale &&
|
||||
(scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
|
||||
return (-1);
|
||||
|
||||
if (buf == NULL || suffix == NULL)
|
||||
return (-1);
|
||||
|
||||
if (len > 0)
|
||||
buf[0] = '\0';
|
||||
if (bytes < 0) {
|
||||
sign = -1;
|
||||
bytes *= -100;
|
||||
baselen = 3; /* sign, digit, prefix */
|
||||
} else {
|
||||
sign = 1;
|
||||
bytes *= 100;
|
||||
baselen = 2; /* digit, prefix */
|
||||
}
|
||||
if (flags & HN_NOSPACE)
|
||||
sep = "";
|
||||
else {
|
||||
sep = " ";
|
||||
baselen++;
|
||||
}
|
||||
baselen += strlen(suffix);
|
||||
|
||||
/* Check if enough room for `x y' + suffix + `\0' */
|
||||
if (len < baselen + 1)
|
||||
return (-1);
|
||||
|
||||
if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
|
||||
/* See if there is additional columns can be used. */
|
||||
for (max = 100, i = (int)(len - baselen); i-- > 0;)
|
||||
max *= 10;
|
||||
|
||||
/*
|
||||
* Divide the number until it fits the given column.
|
||||
* If there will be an overflow by the rounding below,
|
||||
* divide once more.
|
||||
*/
|
||||
for (i = 0; bytes >= max - 50 && i < maxscale; i++)
|
||||
bytes /= divisor;
|
||||
|
||||
if (scale & HN_GETSCALE)
|
||||
return (i);
|
||||
} else
|
||||
for (i = 0; i < scale && i < maxscale; i++)
|
||||
bytes /= divisor;
|
||||
|
||||
/* If a value <= 9.9 after rounding and ... */
|
||||
if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
|
||||
/* baselen + \0 + .N */
|
||||
if (len < baselen + 1 + 2)
|
||||
return (-1);
|
||||
b = ((int)bytes + 5) / 10;
|
||||
s1 = b / 10;
|
||||
s2 = b % 10;
|
||||
r = snprintf(buf, len, "%d%s%d%s%s%s",
|
||||
sign * s1, localeconv()->decimal_point, s2,
|
||||
sep, SCALE2PREFIX(i), suffix);
|
||||
} else
|
||||
r = snprintf(buf, len, "%" PRId64 "%s%s%s",
|
||||
sign * ((bytes + 50) / 100),
|
||||
sep, SCALE2PREFIX(i), suffix);
|
||||
|
||||
return (r);
|
||||
}
|
||||
|
||||
/*
|
||||
* Small wrapper for NetBSD's humanize_number(3) with some
|
||||
* defaults set that we care about.
|
||||
*/
|
||||
int
|
||||
xbps_humanize_number(char *buf, int64_t bytes)
|
||||
{
|
||||
assert(buf != NULL);
|
||||
|
||||
return humanize_number(buf, 7, bytes, "B",
|
||||
HN_AUTOSCALE, HN_DECIMAL|HN_NOSPACE);
|
||||
}
|
161
lib/external/match.c
vendored
Normal file
161
lib/external/match.c
vendored
Normal file
@ -0,0 +1,161 @@
|
||||
/* $NetBSD: opattern.c,v 1.5 2009/02/02 12:35:01 joerg Exp $ */
|
||||
|
||||
/*
|
||||
* FreeBSD install - a package for the installation and maintainance
|
||||
* of non-core utilities.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Jordan K. Hubbard
|
||||
* 18 July 1993
|
||||
*
|
||||
* Miscellaneous string utilities.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fnmatch.h>
|
||||
#include <ctype.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "xbps_api_impl.h"
|
||||
|
||||
/*
|
||||
* Perform alternate match on "pkg" against "pattern",
|
||||
* calling pkg_match (recursively) to resolve any other patterns.
|
||||
* Return 1 on match, 0 otherwise or -1 on error.
|
||||
*/
|
||||
static int
|
||||
alternate_match(const char *pattern, const char *pkg)
|
||||
{
|
||||
char *sep;
|
||||
char buf[PATH_MAX];
|
||||
char *last;
|
||||
char *alt;
|
||||
char *cp;
|
||||
int cnt;
|
||||
int found;
|
||||
|
||||
if ((sep = strchr(pattern, '{')) == (char *)NULL)
|
||||
return -1;
|
||||
|
||||
(void)strncpy(buf, pattern, (size_t)(sep - pattern));
|
||||
alt = &buf[sep - pattern];
|
||||
last = (char *)NULL;
|
||||
for (cnt = 0, cp = sep; *cp && last == (char *)NULL; cp++) {
|
||||
if (*cp == '{') {
|
||||
cnt++;
|
||||
} else if (*cp == '}' && --cnt == 0 && last == (char *)NULL) {
|
||||
last = cp + 1;
|
||||
}
|
||||
}
|
||||
if (cnt != 0)
|
||||
return -1;
|
||||
|
||||
for (found = 0, cp = sep + 1; *sep != '}'; cp = sep + 1) {
|
||||
for (cnt = 0, sep = cp; cnt > 0 || (cnt == 0 && *sep != '}' && *sep != ','); sep++) {
|
||||
if (*sep == '{') {
|
||||
cnt++;
|
||||
} else if (*sep == '}') {
|
||||
cnt--;
|
||||
}
|
||||
}
|
||||
(void)snprintf(alt, sizeof(buf) - (alt - buf), "%.*s%s", (int)(sep - cp), cp, last);
|
||||
if (xbps_pkgpattern_match(buf, pkg) == 1)
|
||||
found = 1;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform glob match on "pkg" against "pattern".
|
||||
* Return 1 on match, 0 otherwise
|
||||
*/
|
||||
static int
|
||||
glob_match(const char *pattern, const char *pkg)
|
||||
{
|
||||
return fnmatch(pattern, pkg, FNM_PERIOD) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform simple match on "pkg" against "pattern".
|
||||
* Return 1 on match, 0 otherwise
|
||||
*/
|
||||
static int
|
||||
simple_match(const char *pattern, const char *pkg)
|
||||
{
|
||||
return strcmp(pattern, pkg) == 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Performs a fast check if pattern can ever match pkg.
|
||||
* Returns 1 if a match is possible and 0 otherwise.
|
||||
*/
|
||||
static int
|
||||
quick_pkg_match(const char *pattern, const char *pkg)
|
||||
{
|
||||
#define simple(x) (isalnum((unsigned char)(x)) || (x) == '-')
|
||||
if (!simple(pattern[0]))
|
||||
return 1;
|
||||
if (pattern[0] != pkg[0])
|
||||
return 0;
|
||||
|
||||
if (!simple(pattern[1]))
|
||||
return 1;
|
||||
if (pattern[1] != pkg[1])
|
||||
return 0;
|
||||
return 1;
|
||||
#undef simple
|
||||
}
|
||||
|
||||
/*
|
||||
* Match pkg against pattern, return 1 if matching, 0 otherwise or -1 on error.
|
||||
*/
|
||||
int
|
||||
xbps_pkgpattern_match(const char *pkg, const char *pattern)
|
||||
{
|
||||
if (!quick_pkg_match(pattern, pkg))
|
||||
return 0;
|
||||
|
||||
if (strchr(pattern, '{') != (char *)NULL) {
|
||||
/* emulate csh-type alternates */
|
||||
return alternate_match(pattern, pkg);
|
||||
}
|
||||
if (strpbrk(pattern, "<>") != (char *)NULL) {
|
||||
/* perform relational dewey match on version number */
|
||||
return dewey_match(pattern, pkg);
|
||||
}
|
||||
if (strpbrk(pattern, "*?[]") != (char *)NULL) {
|
||||
/* glob match */
|
||||
if (glob_match(pattern, pkg))
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* no alternate, dewey or glob match -> simple compare */
|
||||
if (simple_match(pattern, pkg))
|
||||
return 1;
|
||||
|
||||
/* globbing patterns and simple matches may be specified with or
|
||||
* without the version number, so check for both cases. */
|
||||
{
|
||||
char *pattern_ver;
|
||||
int retval;
|
||||
|
||||
pattern_ver = xbps_xasprintf("%s-[0-9]*", pattern);
|
||||
if (pattern_ver == NULL)
|
||||
return -1;
|
||||
|
||||
retval = glob_match(pattern_ver, pkg);
|
||||
free(pattern_ver);
|
||||
return retval;
|
||||
}
|
||||
}
|
101
lib/external/mkpath.c
vendored
Normal file
101
lib/external/mkpath.c
vendored
Normal file
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (c) 1983, 1992, 1993
|
||||
* The Regents of the University of California. 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.
|
||||
* 3. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* The following is a modified function from NetBSD's src/bin/mkdir/mkdir.c
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "xbps_api_impl.h"
|
||||
|
||||
int
|
||||
xbps_mkpath(const char *path, mode_t mode)
|
||||
{
|
||||
struct stat sb;
|
||||
char *ppath, *slash;
|
||||
int done = 0, rv;
|
||||
mode_t dir_mode;
|
||||
|
||||
if ((ppath = strdup(path)) == NULL)
|
||||
return -1;
|
||||
|
||||
slash = ppath;
|
||||
/*
|
||||
* The default file mode is a=rwx (0777) with selected permissions
|
||||
* removed in accordance with the file mode creation mask. For
|
||||
* intermediate path name components, the mode is the default modified
|
||||
* by u+wx so that the subdirectories can always be created.
|
||||
*/
|
||||
if (mode == 0)
|
||||
mode = (S_IRWXU | S_IRWXG | S_IRWXO) & ~umask(0);
|
||||
|
||||
dir_mode = mode | S_IWUSR | S_IXUSR;
|
||||
|
||||
for (;;) {
|
||||
slash += strspn(slash, "/");
|
||||
slash += strcspn(slash, "/");
|
||||
|
||||
done = (*slash == '\0');
|
||||
*slash = '\0';
|
||||
|
||||
rv = mkdir(ppath, done ? mode : dir_mode);
|
||||
if (rv < 0) {
|
||||
/*
|
||||
* Can't create; path exists or no perms.
|
||||
* stat() path to determine what's there now.
|
||||
*/
|
||||
int sverrno;
|
||||
|
||||
sverrno = errno;
|
||||
if (stat(ppath, &sb) < 0) {
|
||||
/* Not there; use mkdir()s error */
|
||||
errno = sverrno;
|
||||
free(ppath);
|
||||
return -1;
|
||||
}
|
||||
if (!S_ISDIR(sb.st_mode)) {
|
||||
/* Is there, but isn't a directory */
|
||||
errno = ENOTDIR;
|
||||
free(ppath);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (done)
|
||||
break;
|
||||
|
||||
*slash = '/';
|
||||
}
|
||||
free(ppath);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user