summaryrefslogtreecommitdiffstats
path: root/plugins/wdc/wdc-utils.c
blob: 80d99550593402dd7f8bc7fbc7b52dd88137530c (plain)
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
/*
 * Copyright (c) 2017-2018 Western Digital Corporation or its affiliates.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 *
 *   Author: Jeff Lien <jeff.lien@wdc.com>,
 */

#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "wdc-utils.h"

int wdc_UtilsSnprintf(char *buffer, unsigned int sizeOfBuffer, const char *format, ...)
{
	int res = 0;
	va_list vArgs;

	va_start(vArgs, format);
	res = vsnprintf(buffer, sizeOfBuffer, format, vArgs);
	va_end(vArgs);

	return res;
}

void wdc_UtilsDeleteCharFromString(char* buffer, int buffSize, char charToRemove)
{
	int i = 0;
	int count = 0;

	if (!buffer || !buffSize)
		return;

	/*
	 * Traverse the given string. If current character is not charToRemove,
	 * then place it at index count++
	 */
	for (i = 0; ((i < buffSize) && (buffer[i] != '\0')); i++) {
		if (buffer[i] != charToRemove)
			buffer[count++] = buffer[i];
	}
	buffer[count] = '\0';
}

int wdc_UtilsGetTime(PUtilsTimeInfo timeInfo)
{
	time_t currTime;
	struct tm currTimeInfo;

	if(!timeInfo)
		return WDC_STATUS_INVALID_PARAMETER;

	tzset();
	time(&currTime);
	localtime_r(&currTime, &currTimeInfo);

	timeInfo->year			=  currTimeInfo.tm_year + 1900;
	timeInfo->month			=  currTimeInfo.tm_mon + 1;
	timeInfo->dayOfWeek		=  currTimeInfo.tm_wday;
	timeInfo->dayOfMonth		=  currTimeInfo.tm_mday;
	timeInfo->hour			=  currTimeInfo.tm_hour;
	timeInfo->minute		=  currTimeInfo.tm_min;
	timeInfo->second		=  currTimeInfo.tm_sec;
	timeInfo->msecs			=  0;
	timeInfo->isDST			=  currTimeInfo.tm_isdst;
	timeInfo->zone			= -currTimeInfo.tm_gmtoff / 60;

	return WDC_STATUS_SUCCESS;
}

int wdc_UtilsCreateDir(char *path)
{
	int retStatus;
	int status = WDC_STATUS_SUCCESS;

	if  (!path )
		return WDC_STATUS_INVALID_PARAMETER;

	retStatus = mkdir(path, 0x999);
	if (retStatus < 0) {
		if (errno == EEXIST)
			status = WDC_STATUS_DIR_ALREADY_EXISTS;
		else if (errno == ENOENT)
			status = WDC_STATUS_PATH_NOT_FOUND;
		else
			status = WDC_STATUS_CREATE_DIRECTORY_FAILED;
	}

	return status;
}

int wdc_WriteToFile(char *fileName, char *buffer, unsigned int bufferLen)
{
	int          status = WDC_STATUS_SUCCESS;
	FILE         *file;
	size_t       bytesWritten = 0;

	file = fopen(fileName, "ab+");
	if (!file) {
		status = WDC_STATUS_UNABLE_TO_OPEN_FILE;
		goto end;
	}

	bytesWritten = fwrite(buffer, 1, bufferLen, file);
	if (bytesWritten != bufferLen)
		status = WDC_STATUS_UNABLE_TO_WRITE_ALL_DATA;

end:
	if(file)
		fclose(file);
	return status;
}

/**
 * Compares the strings ignoring their cases.
 *
 * @param pcSrc Points to a null terminated string for comapring.
 * @param pcDst Points to a null terminated string for comapring.
 *
 * @returns zero if the string matches or
 *          1 if the pcSrc string is lexically higher than pcDst or
 *         -1 if the pcSrc string is lexically lower than pcDst.
 */
int wdc_UtilsStrCompare(char *pcSrc, char *pcDst)
{
	while ((toupper(*pcSrc) == toupper(*pcDst)) && (*pcSrc != '\0')) {
		pcSrc++;
		pcDst++;
	}
	return *pcSrc - *pcDst;
}