1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
* parser.c - NILFS parser library
*
* Copyright (C) 2009-2012 Nippon Telegraph and Telephone Corporation.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* Written by Ryusuke Konishi <konishi.ryusuke@gmail.com>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#if HAVE_LIMITS_H
#include <limits.h>
#endif /* HAVE_LIMITS_H */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include "nilfs.h"
nilfs_cno_t nilfs_parse_cno(const char *arg, char **endptr, int base)
{
/* ensure the number we are about to parse is not negative, which
* strtoull() will happily accept and cast to an unsigned value. */
while (isspace(*arg))
arg++;
if (*arg == '-')
return NILFS_CNO_MAX;
return strtoull(arg, endptr, base);
}
int nilfs_parse_cno_range(const char *arg, __u64 *start, __u64 *end, int base)
{
const char *delim;
char *endptr;
nilfs_cno_t cno, cno2;
assert(arg && *arg != '\0');
delim = strstr(arg, "..");
if (delim && delim == arg) {
if (arg[2] != '\0') {
/* ..yyy */
cno = nilfs_parse_cno(arg + 2, &endptr, base);
if (cno < NILFS_CNO_MAX && *endptr == '\0') {
/* ..CNO */
*start = NILFS_CNO_MIN;
*end = cno;
return 0;
}
}
} else if (!delim) {
/* xxx */
cno = nilfs_parse_cno(arg, &endptr, base);
if (cno < NILFS_CNO_MAX && *endptr == '\0') {
/* CNO */
*start = *end = cno;
return 0;
}
} else {
/* xxx..yyy */
cno = nilfs_parse_cno(arg, &endptr, base);
if (cno < NILFS_CNO_MAX && endptr == delim) {
if (delim[2] == '\0') {
/* CNO.. */
*start = cno;
*end = NILFS_CNO_MAX;
return 0;
}
cno2 = nilfs_parse_cno(delim + 2, &endptr, base);
if (cno2 < NILFS_CNO_MAX && *endptr == '\0') {
/* CNO..CNO */
*start = cno;
*end = cno2;
return 0;
}
}
}
return -1; /* parse error */
}
int nilfs_parse_protection_period(const char *arg, unsigned long *period)
{
unsigned long long val;
char *endptr;
int ret = 0;
val = strtoull(arg, &endptr, 10);
if (endptr == arg) {
errno = EINVAL;
ret = -1;
goto out;
} else if (endptr[0] != '\0' && endptr[1] == '\0' && val < ULONG_MAX) {
switch (endptr[0]) {
case 's':
break;
case 'm':
val *= 60;
break;
case 'h':
val *= 3600;
break;
case 'd':
val *= 86400;
break;
case 'w':
val *= 604800;
break;
case 'M':
val *= 2678400;
break;
case 'Y':
val *= 31536000;
break;
default:
ret = -1;
goto out;
}
}
if (val >= ULONG_MAX) {
errno = ERANGE;
ret = -1;
goto out;
}
*period = val;
out:
return ret;
}
|