2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afs/param.h>
18 #include <WINNT/afsclass.h>
21 #define LOCAL_CRITSEC_COUNT
25 * VARIABLES __________________________________________________________________
29 #ifdef LOCAL_CRITSEC_COUNT
30 size_t cs_EnterCount = 0;
31 DWORD cs_ThreadID = 0;
34 size_t cRefreshAllReq = 0;
36 CRITICAL_SECTION *pcs = NULL;
37 BOOL fLongServerNames = FALSE;
41 static LPTSTR apszDays[] = {
42 TEXT("sun"), TEXT("mon"), TEXT("tue"),
43 TEXT("wed"), TEXT("thu"), TEXT("fri"), TEXT("sat")
48 * PROTOTYPES _________________________________________________________________
54 * ROUTINES ___________________________________________________________________
58 void AfsClass_InitCriticalSection (void)
62 pcs = New (CRITICAL_SECTION);
63 InitializeCriticalSection (pcs);
65 #ifdef LOCAL_CRITSEC_COUNT
72 LPCRITICAL_SECTION AfsClass_GetCriticalSection (void)
74 AfsClass_InitCriticalSection();
78 void AfsClass_Enter (void)
80 AfsClass_InitCriticalSection();
81 EnterCriticalSection (pcs);
83 #ifdef LOCAL_CRITSEC_COUNT
85 cs_ThreadID = GetCurrentThreadId();
89 void AfsClass_Leave (void)
91 #ifdef LOCAL_CRITSEC_COUNT
92 if (!ASSERT( cs_EnterCount > 0 ))
94 if (!ASSERT( cs_ThreadID == GetCurrentThreadId() ))
98 if (!ASSERT( pcs->RecursionCount > 0 ))
100 if (!ASSERT( pcs->OwningThread == (PVOID)GetCurrentThreadId() ))
103 LeaveCriticalSection (pcs);
106 int AfsClass_GetEnterCount (void)
108 #ifdef LOCAL_CRITSEC_COUNT
109 return (int)cs_EnterCount;
111 return pcs->RecursionCount;
115 void AfsClass_UnixTimeToSystemTime (LPSYSTEMTIME pst, ULONG ut, BOOL fElapsed)
119 memset (pst, 0x00, sizeof(SYSTEMTIME));
123 // A Unix time is the number of seconds since 1/1/1970.
124 // The first step in this conversion is to change that count-of-seconds
125 // into a count-of-100ns-intervals...
128 ldw.QuadPart = (LONGLONG)10000000 * (LONGLONG)ut;
130 // Then adjust the count to be a count-of-100ns-intervals since
131 // 1/1/1601, instead of 1/1/1970. That means adding a *big* number...
133 ldw.QuadPart += (LONGLONG)0x019DB1DED53E8000;
135 // Now the count is effectively a FILETIME, which we can convert
136 // to a SYSTEMTIME with a Win32 API.
139 ft.dwHighDateTime = (DWORD)ldw.HighPart;
140 ft.dwLowDateTime = (DWORD)ldw.LowPart;
141 FileTimeToSystemTime (&ft, pst);
147 pst->wDayOfWeek -= 1;
153 ULONG AfsClass_SystemTimeToUnixTime (LPSYSTEMTIME pst)
161 if (st.wYear == 1970)
165 if (!SystemTimeToFileTime (&st, &ft))
169 now.HighPart = (LONG)ft.dwHighDateTime;
170 now.LowPart = (ULONG)ft.dwLowDateTime;
172 LARGE_INTEGER offset;
173 offset.HighPart = 0x019db1de;
174 offset.LowPart = 0xd53e8000;
176 LARGE_INTEGER result;
177 result.QuadPart = (now.QuadPart - offset.QuadPart) / 10000000;
178 return (ULONG)result.LowPart;
182 void AfsClass_ElapsedTimeToSeconds (ULONG *pcSeconds, LPSYSTEMTIME pet)
187 *pcSeconds += pet->wSecond;
188 *pcSeconds += pet->wMinute * 60L;
189 *pcSeconds += pet->wHour * 60L * 60L;
190 *pcSeconds += pet->wDay * 60L * 60L * 24L;
195 double AfsClass_FileTimeToDouble (FILETIME *pft)
198 rc = (double)pft->dwHighDateTime * (double)0x100000000;
199 rc += (double)pft->dwLowDateTime;
204 void AfsClass_RequestLongServerNames (BOOL fLong)
206 fLongServerNames = fLong;
210 void AfsClass_ParseRecurringTime (BOOL *pfEver, LPSYSTEMTIME pst, LPTSTR pszTime)
213 memset (pst, 0x00, sizeof(SYSTEMTIME));
215 if (lstrcmpi (pszTime, TEXT("never")))
219 if (!lstrncmpi (pszTime, TEXT("at"), lstrlen(TEXT("at"))))
220 pszTime += lstrlen(TEXT("at"));
221 while ((*pszTime == ' ') || (*pszTime == '\t'))
224 // first, does it start with a day-of-week?
226 pst->wDayOfWeek = (WORD)(-1); // daily until proven otherwise
228 for (WORD ii = 0; ii < 7; ++ii)
230 if (!lstrncmpi (apszDays[ii], pszTime, 3))
232 pst->wDayOfWeek = ii;
237 // next, find the hour
239 while (*pszTime && !isdigit(*pszTime))
242 TCHAR szComponent[ cchRESOURCE ];
243 lstrcpy (szComponent, pszTime);
245 for (pch = szComponent; isdigit(*pch); ++pch)
249 pst->wHour = atoi(szComponent);
251 // next, find the minutes
253 while (isdigit(*pszTime))
255 while (*pszTime && !isdigit(*pszTime))
258 lstrcpy (szComponent, pszTime);
259 for (pch = szComponent; isdigit(*pch); ++pch)
263 pst->wMinute = atoi(szComponent);
265 // next, check for a 'am' or 'pm' marker
267 for ( ; *pszTime; ++pszTime)
269 if (tolower(*pszTime) == TEXT('p'))
275 if (tolower(*pszTime) == TEXT('a'))
277 if (pst->wHour >= 12)
286 void AfsClass_FormatRecurringTime (LPTSTR pszTarget, SYSTEMTIME *pst)
290 wsprintf (pszTarget, TEXT("never"));
294 WORD wHour = ((pst->wHour % 12) == 0) ? 12 : (pst->wHour % 12);
296 lstrcpy (szAMPM, (pst->wHour >= 12) ? TEXT("pm") : TEXT("am"));
298 if (pst->wDayOfWeek == (WORD)(-1)) // daily at specified time?
300 wsprintf (pszTarget, TEXT("%u:%02u %s"), wHour, pst->wMinute, szAMPM);
302 else // weekly on specified date at specified time?
304 wsprintf (pszTarget, TEXT("%s %u:%02u %s"), apszDays[ pst->wDayOfWeek ], wHour, pst->wMinute, szAMPM);
310 void AfsClass_SplitFilename (LPSTR pszDirectoryA, LPSTR pszFilenameA, LPTSTR pszFullName)
312 CopyStringToAnsi (pszDirectoryA, pszFullName);
314 LPSTR pszLastSlashA = NULL;
315 for (LPSTR pszA = pszDirectoryA; *pszA; ++pszA)
317 if ((*pszA == '/') || (*pszA == '\\'))
318 pszLastSlashA = pszA;
323 strcpy (pszFilenameA, pszDirectoryA);
328 strcpy (pszFilenameA, 1+pszLastSlashA);
334 void AfsClass_SystemTimeToRestartTime (bos_RestartTime_p prt, BOOL fEnable, LPSYSTEMTIME pst)
336 memset (prt, 0x00, sizeof(bos_RestartTime_t));
338 prt->mask = BOS_RESTART_TIME_NEVER;
341 prt->mask = (bos_RestartTimeFields_t)(BOS_RESTART_TIME_HOUR | BOS_RESTART_TIME_MINUTE);
342 prt->hour = pst->wHour;
343 prt->min = pst->wMinute;
345 if (pst->wDayOfWeek != (WORD)-1)
347 prt->mask = (bos_RestartTimeFields_t)(prt->mask | BOS_RESTART_TIME_DAY);
348 prt->day = pst->wDayOfWeek;
354 void AfsClass_RestartTimeToSystemTime (BOOL *pfEnable, LPSYSTEMTIME pst, bos_RestartTime_p prt)
356 memset (pst, 0x00, sizeof(SYSTEMTIME));
358 if ((!prt->mask) || (prt->mask & BOS_RESTART_TIME_NEVER))
365 pst->wHour = (prt->mask & BOS_RESTART_TIME_HOUR) ? prt->hour : 0;
366 pst->wMinute = (prt->mask & BOS_RESTART_TIME_MINUTE) ? prt->min : 0;
367 pst->wDayOfWeek = (prt->mask & BOS_RESTART_TIME_DAY) ? prt->day : (WORD)-1;
372 void AfsClass_IntToAddress (LPSOCKADDR_IN pAddr, int IntAddr)
374 memset (pAddr, 0x00, sizeof(SOCKADDR_IN));
375 pAddr->sin_family = AF_INET;
376 pAddr->sin_addr.s_addr = htonl(IntAddr);
380 void AfsClass_AddressToInt (int *pIntAddr, LPSOCKADDR_IN pAddr)
382 *pIntAddr = ntohl(pAddr->sin_addr.s_addr);
386 void AfsClass_SpecifyRefreshDomain (DWORD dwWantUser)
392 BOOL AfsClass_Initialize (ULONG *pStatus)
394 if (!Worker_Initialize (pStatus))
401 void AfsClass_GenFullUserName (LPTSTR pszTarget, LPCTSTR pszPrincipal, LPCTSTR pszInstance)
403 lstrcpy (pszTarget, pszPrincipal);
404 if (pszInstance && *pszInstance)
405 wsprintf (&pszTarget[ lstrlen(pszTarget) ], TEXT(".%s"), pszInstance);