1 /*
2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "common_util.h"
17 #include <ctype.h>
18 #include <securec.h>
19 #include <stdint.h>
20 #include <cstdio>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <unistd.h>
25 #include "dhcp_s_define.h"
26 #include "dhcp_logger.h"
27
28 #define NEW_FILEPATH_MODE 0755
29 #define TIME_BASE_YEAR 1900
30 #define TIME_SEC_TO_USEC (1000 * 1000)
31
32 DEFINE_DHCPLOG_DHCP_LABEL("DhcpServerCommon");
33
34 typedef struct tm *ptm;
35
Tmspusec(void)36 uint64_t Tmspusec(void)
37 {
38 struct timeval t;
39 gettimeofday(&t, 0);
40 return (uint64_t)((long long)t.tv_sec * TIME_SEC_TO_USEC + t.tv_usec);
41 }
42
Tmspsec(void)43 uint64_t Tmspsec(void)
44 {
45 struct timeval t;
46 gettimeofday(&t, 0);
47 return (uint64_t)((long long)t.tv_sec);
48 }
49
LeftTrim(char * buf)50 static void LeftTrim(char *buf)
51 {
52 if (buf == nullptr || (buf[0] != ' ' && buf[0] != '\t')) {
53 return;
54 }
55 int i = 0;
56 while (buf[i] == ' ' || buf[i] == '\t') {
57 ++i;
58 }
59 int j = 0;
60 while (buf[i] != '\0') {
61 buf[j++] = buf[i++];
62 }
63 buf[j] = '\0';
64 return;
65 }
66
RightTrim(char * buf)67 static void RightTrim(char *buf)
68 {
69 if (buf == nullptr || buf[0] == '\0') {
70 return;
71 }
72 int i = strlen(buf) - 1;
73 while (i >= 0 && (buf[i] == ' ' || buf[i] == '\t')) {
74 buf[i] = '\0';
75 --i;
76 }
77 return;
78 }
79
TrimString(char * buf)80 void TrimString(char *buf)
81 {
82 RightTrim(buf);
83 LeftTrim(buf);
84 return;
85 }
86
GetFilePath(const char * fileName)87 const char *GetFilePath(const char *fileName)
88 {
89 static char currFilePath[DHCP_MAX_PATH_LENGTH];
90 if (!fileName) {
91 return 0;
92 }
93 int flen = strlen(fileName);
94 if (flen == 0) {
95 return 0;
96 }
97 char currName[DHCP_MAX_PATH_LENGTH] = {'\0'};
98 if (memcpy_s(currName, sizeof(currName), fileName, strlen(fileName)) != EOK) {
99 return 0;
100 }
101 char *last = strrchr(currName, '/');
102 if (last) {
103 *last = '\0';
104 }
105 if (memset_s(currFilePath, sizeof(currFilePath), '\0', sizeof(currFilePath)) != EOK) {
106 return 0;
107 }
108 if (memcpy_s(currFilePath, sizeof(currFilePath), currName, strlen(currName)) != EOK) {
109 return 0;
110 }
111 return currFilePath;
112 }
113
CreatePath(const char * fileName)114 int CreatePath(const char *fileName)
115 {
116 if (!fileName) {
117 return RET_FAILED;
118 }
119 int len = strlen(fileName);
120 if (!len) {
121 return RET_FAILED;
122 }
123 char filePath[DHCP_MAX_PATH_LENGTH] = {'\0'};
124 if (strncpy_s(filePath, sizeof(filePath), fileName, len) != EOK) {
125 return RET_FAILED;
126 }
127 for (int i = 0; i < len; i++) {
128 if (filePath[i] == '/') {
129 filePath[i] = '\0';
130 if (access(filePath, 0) != 0) {
131 mkdir(filePath, NEW_FILEPATH_MODE);
132 }
133 filePath[i] = '/';
134 }
135 }
136 if (len > 0 && access(filePath, 0) != 0) {
137 mkdir(filePath, NEW_FILEPATH_MODE);
138 }
139 return RET_SUCCESS;
140 }
141
RemoveSpaceCharacters(char * buf,size_t bufSize)142 int RemoveSpaceCharacters(char *buf, size_t bufSize)
143 {
144 if ((buf == nullptr) || (strlen(buf) == 0) || (bufSize == 0)) {
145 DHCP_LOGE("RemoveSpaceCharacters() buf == nullptr or len == 0!");
146 return DHCP_FALSE;
147 }
148
149 /* Handle rightmost spaces. */
150 int nEnd = strlen(buf) - 1;
151 while ((nEnd >= 0) && isspace(buf[nEnd])) {
152 buf[nEnd--] = '\0';
153 }
154
155 /* Handle leftmost spaces. */
156 int i = 0;
157 while (isspace(buf[i])) {
158 buf[i++] = '\0';
159 }
160
161 int j = 0;
162 while (buf[i] != '\0') {
163 buf[j++] = buf[i++];
164 }
165 buf[j] = '\0';
166
167 return DHCP_TRUE;
168 }
169