2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Helper functions to skip white spaces, find tokens, find separators and free
2N/A * memory.
2N/A */
2N/A
2N/A#include <errno.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <ctype.h>
2N/A
2N/A#include "commp_util.h"
2N/A
2N/A
2N/A/*
2N/A * Skip to the next non-whitespace
2N/A */
2N/Aint
2N/Acommp_skip_white_space(const char **begin, const char *end)
2N/A{
2N/A while (*begin < end) {
2N/A if (!isspace(**begin))
2N/A return (0);
2N/A (*begin)++;
2N/A }
2N/A return (1);
2N/A}
2N/A
2N/A/*
2N/A * Finds the token in the char buffer. *current will be pointing to the
2N/A * token when function returns. If the char buffer has leading token,
2N/A * it returns 1.
2N/A */
2N/Aint
2N/Acommp_find_token(const char **begin, const char **current, const char *end,
2N/A char token, boolean_t last)
2N/A{
2N/A *current = *begin;
2N/A while (*current < end) {
2N/A if (!last && (**current == token))
2N/A break;
2N/A else if (isspace(**current))
2N/A return (1);
2N/A (*current)++;
2N/A }
2N/A /* Checks for leading white space */
2N/A if (*current == *begin)
2N/A return (1);
2N/A else
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * atoi function
2N/A */
2N/Aint
2N/Acommp_atoi(const char *begin, const char *end, int *num)
2N/A{
2N/A boolean_t num_found = B_FALSE;
2N/A
2N/A *num = 0;
2N/A while (begin < end) {
2N/A if (isdigit(*begin)) {
2N/A *num = (*num * 10) + (*begin - '0');
2N/A num_found = B_TRUE;
2N/A begin++;
2N/A } else {
2N/A break;
2N/A }
2N/A }
2N/A if (!num_found || (begin != end))
2N/A return (EINVAL);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Given a string converts it to unsigned long long int.
2N/A */
2N/Aint
2N/Acommp_strtoull(const char *begin, const char *end, uint64_t *num)
2N/A{
2N/A boolean_t num_found = B_FALSE;
2N/A
2N/A *num = 0;
2N/A while (begin < end) {
2N/A if (isdigit(*begin)) {
2N/A *num = (*num * 10) + (*begin - '0');
2N/A num_found = B_TRUE;
2N/A begin++;
2N/A } else {
2N/A break;
2N/A }
2N/A }
2N/A if (!num_found || (begin != end))
2N/A return (EINVAL);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Given a string converts it to unsigned byte
2N/A */
2N/Aint
2N/Acommp_strtoub(const char *begin, const char *end, uint8_t *num)
2N/A{
2N/A boolean_t num_found = B_FALSE;
2N/A
2N/A *num = 0;
2N/A while (begin < end) {
2N/A if (isdigit(*begin)) {
2N/A *num = (*num * 10) + (*begin - '0');
2N/A num_found = B_TRUE;
2N/A begin++;
2N/A } else {
2N/A break;
2N/A }
2N/A }
2N/A if (!num_found || (begin != end))
2N/A return (EINVAL);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Given a string converts it to unsigned int
2N/A */
2N/Aint
2N/Acommp_atoui(const char *begin, const char *end, uint_t *num)
2N/A{
2N/A boolean_t num_found = B_FALSE;
2N/A
2N/A *num = 0;
2N/A while (begin < end) {
2N/A if (isdigit(*begin)) {
2N/A *num = (*num * 10) + (*begin - '0');
2N/A num_found = B_TRUE;
2N/A begin++;
2N/A } else {
2N/A break;
2N/A }
2N/A }
2N/A if (!num_found || (begin != end))
2N/A return (EINVAL);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * allocates memory and copies string to new memory
2N/A */
2N/Aint
2N/Acommp_add_str(char **dest, const char *src, int len)
2N/A{
2N/A if (len == 0)
2N/A return (EINVAL);
2N/A (*dest) = calloc(1, len + 1);
2N/A if (*dest == NULL)
2N/A return (ENOMEM);
2N/A (void) strncpy(*dest, src, len);
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * This function converts strings like "5d" to equivalent time in secs.
2N/A * For eg. 1h = 3600, 10d = 86400
2N/A */
2N/Aint
2N/Acommp_time_to_secs(const char *begin, const char *end, uint64_t *num)
2N/A{
2N/A uint_t factor = 0;
2N/A
2N/A if (!isdigit(*(end - 1))) {
2N/A switch (*(end - 1)) {
2N/A case 'd':
2N/A factor = COMMP_SECS_IN_DAY;
2N/A break;
2N/A case 'h':
2N/A factor = COMMP_SECS_IN_HOUR;
2N/A break;
2N/A case 'm':
2N/A factor = COMMP_SECS_IN_MIN;
2N/A break;
2N/A case 's':
2N/A factor = 1;
2N/A break;
2N/A default:
2N/A return (EINVAL);
2N/A }
2N/A --end;
2N/A }
2N/A if (commp_strtoull(begin, end, num) != 0)
2N/A return (EINVAL);
2N/A if (factor != 0)
2N/A (*num) = (*num) * factor;
2N/A return (0);
2N/A}