1*3089Swyllys /*
2*3089Swyllys * CDDL HEADER START
3*3089Swyllys *
4*3089Swyllys * The contents of this file are subject to the terms of the
5*3089Swyllys * Common Development and Distribution License (the "License").
6*3089Swyllys * You may not use this file except in compliance with the License.
7*3089Swyllys *
8*3089Swyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3089Swyllys * or http://www.opensolaris.org/os/licensing.
10*3089Swyllys * See the License for the specific language governing permissions
11*3089Swyllys * and limitations under the License.
12*3089Swyllys *
13*3089Swyllys * When distributing Covered Code, include this CDDL HEADER in each
14*3089Swyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3089Swyllys * If applicable, add the following below this CDDL HEADER, with the
16*3089Swyllys * fields enclosed by brackets "[]" replaced with your own identifying
17*3089Swyllys * information: Portions Copyright [yyyy] [name of copyright owner]
18*3089Swyllys *
19*3089Swyllys * CDDL HEADER END
20*3089Swyllys */
21*3089Swyllys /*
22*3089Swyllys * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*3089Swyllys * Use is subject to license terms.
24*3089Swyllys */
25*3089Swyllys
26*3089Swyllys #pragma ident "%Z%%M% %I% %E% SMI"
27*3089Swyllys
28*3089Swyllys #include <cryptoutil.h>
29*3089Swyllys #include <strings.h>
30*3089Swyllys #include <stdio.h>
31*3089Swyllys #include <tzfile.h>
32*3089Swyllys
33*3089Swyllys /*
34*3089Swyllys * This function returns a fullpath based on the "dir" and "filepath" input
35*3089Swyllys * arugments.
36*3089Swyllys * - If the filepath specified does not start with a "/" and the directory
37*3089Swyllys * is also given, prepend the directory to the filename.
38*3089Swyllys * - If only dir or filepath is given, this function returns a copy of the
39*3089Swyllys * given argument.
40*3089Swyllys * - If the filepath is fully qualified already and the "dir" is also
41*3089Swyllys * given, return NULL to indicate an error.
42*3089Swyllys */
43*3089Swyllys char *
get_fullpath(char * dir,char * filepath)44*3089Swyllys get_fullpath(char *dir, char *filepath)
45*3089Swyllys {
46*3089Swyllys char *fullpath = NULL;
47*3089Swyllys int pathlen = 0;
48*3089Swyllys int dirlen = 0;
49*3089Swyllys
50*3089Swyllys if (filepath != NULL)
51*3089Swyllys pathlen = strlen(filepath);
52*3089Swyllys
53*3089Swyllys if (dir != NULL)
54*3089Swyllys dirlen = strlen(dir);
55*3089Swyllys
56*3089Swyllys if (pathlen > 0 && dirlen > 0) {
57*3089Swyllys if (filepath[0] != '/') {
58*3089Swyllys int len = pathlen + dirlen + 2;
59*3089Swyllys fullpath = (char *)malloc(len);
60*3089Swyllys if (fullpath != NULL)
61*3089Swyllys (void) snprintf(fullpath, len, "%s/%s",
62*3089Swyllys dir, filepath);
63*3089Swyllys } else {
64*3089Swyllys return (NULL);
65*3089Swyllys }
66*3089Swyllys } else if (pathlen > 0) {
67*3089Swyllys fullpath = (char *)strdup(filepath);
68*3089Swyllys } else if (dirlen > 0) {
69*3089Swyllys fullpath = (char *)strdup(dir);
70*3089Swyllys }
71*3089Swyllys
72*3089Swyllys return (fullpath);
73*3089Swyllys }
74*3089Swyllys
75*3089Swyllys /*
76*3089Swyllys * This function converts the input string to the value of time
77*3089Swyllys * in seconds.
78*3089Swyllys * - If the input string is NULL, return zero second.
79*3089Swyllys * - The input string needs to be in the form of:
80*3089Swyllys * number-second(s), number-minute(s), number-hour(s) or
81*3089Swyllys * number-day(s).
82*3089Swyllys */
83*3089Swyllys int
str2lifetime(char * ltimestr,uint32_t * ltime)84*3089Swyllys str2lifetime(char *ltimestr, uint32_t *ltime)
85*3089Swyllys {
86*3089Swyllys int num;
87*3089Swyllys char timetok[10];
88*3089Swyllys
89*3089Swyllys if (ltimestr == NULL || !strlen(ltimestr)) {
90*3089Swyllys *ltime = 0;
91*3089Swyllys return (0);
92*3089Swyllys }
93*3089Swyllys
94*3089Swyllys (void) memset(timetok, 0, sizeof (timetok));
95*3089Swyllys if (sscanf(ltimestr, "%d-%08s", &num, timetok) != 2)
96*3089Swyllys return (-1);
97*3089Swyllys
98*3089Swyllys if (!strcasecmp(timetok, "second") ||
99*3089Swyllys !strcasecmp(timetok, "seconds")) {
100*3089Swyllys *ltime = num;
101*3089Swyllys } else if (!strcasecmp(timetok, "minute") ||
102*3089Swyllys !strcasecmp(timetok, "minutes")) {
103*3089Swyllys *ltime = num * SECSPERMIN;
104*3089Swyllys } else if (!strcasecmp(timetok, "day") ||
105*3089Swyllys !strcasecmp(timetok, "days")) {
106*3089Swyllys *ltime = num * SECSPERDAY;
107*3089Swyllys } else if (!strcasecmp(timetok, "hour") ||
108*3089Swyllys !strcasecmp(timetok, "hours")) {
109*3089Swyllys *ltime = num * SECSPERHOUR;
110*3089Swyllys } else {
111*3089Swyllys *ltime = 0;
112*3089Swyllys return (-1);
113*3089Swyllys }
114*3089Swyllys
115*3089Swyllys return (0);
116*3089Swyllys }
117