Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

411 lines
11 KiB

  1. /*
  2. *
  3. * REFERENCES:
  4. *
  5. * NOTES:
  6. *
  7. * REVISIONS:
  8. *
  9. * v-stebe 29Jul2000 Fixed PREfix errors (bugs #46359-#46361, #112601)
  10. */
  11. // Needed for open system call
  12. #include "cdefine.h"
  13. #include "utils.h"
  14. #include "cfgmgr.h"
  15. //#include "registry.h"
  16. #include "w32utils.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. INT UtilHexCharToInt(char ch)
  21. {
  22. INT int_value = 0;
  23. if(isxdigit(ch)) {
  24. if(isdigit(ch)) {
  25. int_value = ch - '0';
  26. }
  27. else {
  28. int_value = 10 + toupper(ch) - 'A';
  29. }
  30. }
  31. return int_value;
  32. }
  33. INT UtilHexStringToInt(PCHAR aString) {
  34. /* The thinking man's approach to Hex string conversion... */
  35. return((INT)strtoul(aString,(PCHAR*)NULL,16));
  36. }
  37. VOID UtilStoreString(PCHAR& destination, const PCHAR source)
  38. {
  39. if (!source) {
  40. if (destination) free (destination);
  41. destination = (PCHAR)NULL;
  42. }
  43. else if (!destination) {
  44. destination = _strdup(source);
  45. }
  46. else {
  47. if(strlen(destination) < strlen(source)) {
  48. free(destination);
  49. destination = _strdup(source);
  50. }
  51. else {
  52. strcpy(destination, source);
  53. }
  54. }
  55. }
  56. INT UtilTime12to24(PCHAR a12Value, PCHAR a24Value)
  57. {
  58. INT err = ErrNO_ERROR;
  59. PCHAR temp_string=_strdup(a12Value);
  60. PCHAR tok_string=strtok(temp_string, ":");
  61. if(tok_string) {
  62. CHAR hour[32], minute[32];
  63. strcpy(hour, tok_string);
  64. tok_string=strtok(NULL, " ");
  65. if (tok_string) {
  66. strcpy(minute, tok_string);
  67. }
  68. else {
  69. strcpy(minute, "00");
  70. }
  71. CHAR hour_str[32];
  72. if(tok_string) {
  73. tok_string=strtok(NULL," ");
  74. if(tok_string) {
  75. INT hour_val = atoi(hour);
  76. if((*tok_string == 'p') || (*tok_string == 'P')) {
  77. if(hour_val != 12) {
  78. hour_val = hour_val + 12;
  79. }
  80. }
  81. else {
  82. if(hour_val == 12) {
  83. hour_val = 0;
  84. }
  85. }
  86. sprintf(hour_str, "%d", hour_val);
  87. }
  88. else {
  89. strcpy(hour_str, hour);
  90. }
  91. }
  92. sprintf(a24Value, "%s:%s", hour_str, minute);
  93. }
  94. else {
  95. err = ErrBAD_RESPONSE_VALUE;
  96. }
  97. free(temp_string);
  98. return err;
  99. }
  100. struct dayConversions {
  101. PCHAR day;
  102. INT dayOfWeek;
  103. };
  104. struct dayConversions day_conversions[7] =
  105. {
  106. {"Sunday", 0},
  107. {"Monday", 1},
  108. {"Tuesday", 2},
  109. {"Wednesday", 3},
  110. {"Thursday", 4},
  111. {"Friday", 5},
  112. {"Saturday", 6}
  113. };
  114. INT UtilDayToDayOfWeek(PCHAR aDay)
  115. {
  116. INT day_num = 0;
  117. for(INT i=0; i<7; i++) {
  118. if(_strcmpi(day_conversions[i].day, aDay) == 0) {
  119. day_num = i;
  120. break;
  121. }
  122. }
  123. return day_num;
  124. }
  125. PCHAR UtilDayOfWeekToDay(INT aDayOfWeek)
  126. {
  127. return day_conversions[aDayOfWeek].day;;
  128. }
  129. /********************************************************************
  130. Function: ApcStrIntCmpI
  131. Parameters:
  132. 1. String
  133. 2. String to compare against
  134. Purpose:
  135. This function converts two strings into their UpperCase
  136. equivalent and then compares them. The special feature of this
  137. function is that it actually compares the numbers within the
  138. strings. For example, using this function a1400 would be
  139. greater than a600. This function was implemented, because
  140. strcmpi did not take care of this case. Additionally, NULLs are
  141. checked, so this function can be called on NULL pointers, and it
  142. will not crash.
  143. Return Values:
  144. GREATER_THAN (1) -- string 1 is greater than string 2
  145. EQUAL (0) -- string 1 is equal to string 2
  146. LESS_THAN (-1) -- string 1 is less than string 2
  147. ********************************************************************/
  148. INT ApcStrIntCmpI(PCHAR aStr1, PCHAR aStr2){
  149. const cMaxNumString = 10;
  150. INT ret_value = EQUAL;
  151. // if both strings are empty
  152. if(IsEmpty(aStr1) && IsEmpty(aStr2)){
  153. ret_value = EQUAL;
  154. }
  155. // else if string1 is present and string2 is empty then
  156. // string1 > string2
  157. else if(!IsEmpty(aStr1) && IsEmpty(aStr2)){
  158. ret_value = GREATER_THAN;
  159. }
  160. // else if string1 is empty and string2 is present then
  161. // string1 < string2
  162. else if(IsEmpty(aStr1) && !IsEmpty(aStr2)){
  163. ret_value = LESS_THAN;
  164. }
  165. else{
  166. INT str1_index = 0;
  167. INT str2_index = 0;
  168. // continue_loop will be used to control the following while loop
  169. BOOLEAN continue_loop = TRUE;
  170. while(continue_loop){
  171. // Initialize characters for comparison. Note that a toupper
  172. // function is called because we want to make sure that we are
  173. // comparing only UPPERCASE characters.
  174. CHAR char1 = (CHAR) toupper(aStr1[str1_index]);
  175. CHAR char2 = (CHAR) toupper(aStr2[str2_index]);
  176. // if both characters are NULL then the strings are equal
  177. if(char1 == NULL && char2 == NULL){
  178. ret_value = EQUAL;
  179. continue_loop = FALSE;
  180. }
  181. // else if char2 is NULL, then char1 > char2
  182. else if(char1 != NULL && char2 == NULL){
  183. ret_value = GREATER_THAN;
  184. continue_loop = FALSE;
  185. }
  186. // else if char1 is NULL, then char1 < char2
  187. else if(char1 == NULL && char2 != NULL){
  188. ret_value = LESS_THAN;
  189. continue_loop = FALSE;
  190. }
  191. // else we must have two non-NULL characters
  192. else{
  193. // if both characters are digits or numbers then,
  194. // build up a number string
  195. if(isdigit(char1) && isdigit(char2)){
  196. CHAR num1_str[cMaxNumString];
  197. CHAR num2_str[cMaxNumString];
  198. memset(num1_str,NULL,cMaxNumString);
  199. memset(num2_str,NULL,cMaxNumString);
  200. INT num1_index = 0;
  201. INT num2_index = 0;
  202. // while char1 is a number, build number string
  203. while(isdigit(char1)){
  204. num1_str[num1_index] = char1;
  205. num1_index++;
  206. str1_index++;
  207. char1 = (CHAR) toupper(aStr1[str1_index]);
  208. }
  209. // while char2 is a number, build number string
  210. while(isdigit(char2)){
  211. num2_str[num2_index] = char2;
  212. num2_index++;
  213. str2_index++;
  214. char2 = (CHAR) toupper(aStr2[str2_index]);
  215. }
  216. // Convert number strings to actual numbers to compare
  217. INT number1 = atoi(num1_str);
  218. INT number2 = atoi(num2_str);
  219. if(number1 < number2){
  220. ret_value = LESS_THAN;
  221. continue_loop = FALSE;
  222. }
  223. else if(number1 > number2){
  224. ret_value = GREATER_THAN;
  225. continue_loop = FALSE;
  226. }
  227. }
  228. else if(char1 < char2){
  229. ret_value = LESS_THAN;
  230. continue_loop = FALSE;
  231. }
  232. else if(char1 > char2){
  233. ret_value = GREATER_THAN;
  234. continue_loop = FALSE;
  235. }
  236. else{
  237. // increment indicies
  238. str1_index++;
  239. str2_index++;
  240. }
  241. }
  242. }
  243. }
  244. return ret_value;
  245. }
  246. /********************************************************************
  247. Function: IsEmpty()
  248. Parameters:
  249. 1. String
  250. Purpose:
  251. This function is used to determine if a string is empty or not.
  252. It assumes that an empty string can be either NULL or if the
  253. [0] element of the string is NULL.
  254. Return Values:
  255. TRUE (1) - if the string is empty
  256. FALSE (0) - if the string is not empty
  257. ********************************************************************/
  258. BOOLEAN IsEmpty(PCHAR aString){
  259. BOOLEAN ret_value = FALSE;
  260. if(aString == NULL || aString[0] == NULL){
  261. ret_value = TRUE;
  262. }
  263. return ret_value;
  264. }
  265. // @@@ start
  266. /* -------------------------------------------------------------------------
  267. NAME: GetNewUPSName
  268. DESCRIPTION: This function will accept the name of the UPS which is
  269. retrieved from the UPS itself, and will load the corresponding
  270. name from pwrchute.ini, if the language of the OS
  271. exists in the list under the country_list in pwrchute.ini.
  272. INPUTS : 1 - Name of the UPS
  273. OUTPUTS : 1 - Returns a pointer to the UPS name loaded from the ini
  274. file.
  275. ----------------------------------------------------------------------------
  276. */
  277. PCHAR GetNewUPSName(PCHAR currentName)
  278. {
  279. static CHAR new_name[128];
  280. if (currentName!=NULL) {
  281. currentName = _strupr(currentName);
  282. strcpy(new_name,currentName);
  283. }
  284. return new_name;
  285. }
  286. /* -------------------------------------------------------------------------
  287. NAME: SetTimeZone
  288. DESCRIPTION: This sets the following global timezone variables, _timezone,
  289. _daylight and _tzname[0], _tzname[1]. This retrieves timezone
  290. information from the system and sets the above variables based
  291. on this information. These variables are used in calls to
  292. localtime, if they are not set correctly localtime may return the
  293. incorrect time when daylight savings should be used.
  294. This code is taken from _tzset();
  295. INPUTS : None
  296. OUTPUTS : 0 - Successful
  297. -1 - GetTimeZoneInformation() call failed
  298. ----------------------------------------------------------------------------
  299. */
  300. INT SetTimeZone() {
  301. TIME_ZONE_INFORMATION lpInfo;
  302. DWORD tZoneId = 0;
  303. INT returnValue = 0;
  304. tZoneId = GetTimeZoneInformation( &lpInfo );
  305. if (tZoneId == -1) {
  306. returnValue = -1;
  307. }
  308. // set the global _timezone variable
  309. _timezone = lpInfo.Bias * 60L;
  310. if ( lpInfo.StandardDate.wMonth != 0 ) {
  311. _timezone += (lpInfo.StandardBias * 60L);
  312. }
  313. if ( (lpInfo.DaylightDate.wMonth != 0) &&(lpInfo.DaylightBias != 0) ) {
  314. _daylight = 1;
  315. _dstbias = (lpInfo.DaylightBias - lpInfo.StandardBias) * 60L;
  316. }
  317. else {
  318. _daylight = 0;
  319. }
  320. /*
  321. * Try to grab the name strings for both the time zone and the
  322. * daylight zone.
  323. */
  324. wcstombs( _tzname[0], lpInfo.StandardName, 64 );
  325. wcstombs( _tzname[1], lpInfo.DaylightName, 64 );
  326. (_tzname[0])[63] = (_tzname[1])[63] = '\0';
  327. return returnValue;
  328. }
  329. // @@@ end