sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libcryptoutil / common / util.c
blob6fbf175d773ad4db8bf96e93ac8bf16d3f972a4e
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <cryptoutil.h>
29 #include <strings.h>
30 #include <stdio.h>
31 #include <tzfile.h>
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.
43 char *
44 get_fullpath(char *dir, char *filepath)
46 char *fullpath = NULL;
47 int pathlen = 0;
48 int dirlen = 0;
50 if (filepath != NULL)
51 pathlen = strlen(filepath);
53 if (dir != NULL)
54 dirlen = strlen(dir);
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);
66 } else if (pathlen > 0) {
67 fullpath = (char *)strdup(filepath);
68 } else if (dirlen > 0) {
69 fullpath = (char *)strdup(dir);
72 return (fullpath);
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).
83 int
84 str2lifetime(char *ltimestr, uint32_t *ltime)
86 int num;
87 char timetok[10];
89 if (ltimestr == NULL || !strlen(ltimestr)) {
90 *ltime = 0;
91 return (0);
94 (void) memset(timetok, 0, sizeof (timetok));
95 if (sscanf(ltimestr, "%d-%08s", &num, timetok) != 2)
96 return (-1);
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);
115 return (0);