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