-rw-r--r-- | gammu/emb/common/misc/misc.c | 15 |
1 files changed, 12 insertions, 3 deletions
diff --git a/gammu/emb/common/misc/misc.c b/gammu/emb/common/misc/misc.c index c2f09e4..7227e7b 100644 --- a/gammu/emb/common/misc/misc.c +++ b/gammu/emb/common/misc/misc.c @@ -1,61 +1,64 @@ /* (c) 2002-2004 by Marcin Wiacek and Michal Cihar */ /* Checking used compiler (c) 2002 by Michal Cihar */ #include <string.h> #include <ctype.h> #include <time.h> #include <stdarg.h> #include <stdio.h> #include <locale.h> #include <sys/timeb.h> #ifdef WIN32 # include "windows.h" #endif +#if defined(linux) || defined(__linux) || defined(__linux__) +# include <sys/utsname.h> +#endif #include "../gsmstate.h" #include "misc.h" /* Based on article in Polish PC-Kurier 8/1998 page 104 * Archive on http://www.pckurier.pl */ char *DayOfWeek (int year, int month, int day) { int p,q,r,w; static char DayOfWeekChar[10]; p=(14-month) / 12; q=month+12*p-2; r=year-p; w=(day+(31*q) / 12 + r + r / 4 - r / 100 + r / 400) % 7; strcpy(DayOfWeekChar,""); switch (w) { case 0: strcpy(DayOfWeekChar,"Sun"); break; case 1: strcpy(DayOfWeekChar,"Mon"); break; case 2: strcpy(DayOfWeekChar,"Tue"); break; case 3: strcpy(DayOfWeekChar,"Wed"); break; case 4: strcpy(DayOfWeekChar,"Thu"); break; case 5: strcpy(DayOfWeekChar,"Fri"); break; case 6: strcpy(DayOfWeekChar,"Sat"); break; } return DayOfWeekChar; } void Fill_GSM_DateTime(GSM_DateTime *Date, time_t timet) { struct tm *now; now = localtime(&timet); Date->Year = now->tm_year; Date->Month = now->tm_mon+1; Date->Day = now->tm_mday; Date->Hour = now->tm_hour; Date->Minute = now->tm_min; Date->Second = now->tm_sec; } void GSM_GetCurrentDateTime (GSM_DateTime *Date) { Fill_GSM_DateTime(Date, time(NULL)); if (Date->Year<1900) { if (Date->Year>90) Date->Year = Date->Year+1900; else Date->Year = Date->Year+2000; @@ -186,100 +189,102 @@ char *OSDate (GSM_DateTime dt) /* Based on article in Polish PC-Kurier 8/1998 page 104 * Archive on http://www.pckurier.pl */ p=(14-dt.Month) / 12; q=dt.Month+12*p-2; r=dt.Year-p; w=(dt.Day+(31*q) / 12 + r + r / 4 - r / 100 + r / 400) % 7; timeptr.tm_yday = 0; /* FIXME */ timeptr.tm_isdst = -1; /* FIXME */ timeptr.tm_year = dt.Year - 1900; timeptr.tm_mon = dt.Month - 1; timeptr.tm_mday = dt.Day; timeptr.tm_hour = dt.Hour; timeptr.tm_min = dt.Minute; timeptr.tm_sec = dt.Second; timeptr.tm_wday = w; #ifdef _BSD_SOURCE timeptr.tm_zone = NULL; #endif #ifdef WIN32 strftime(retval2, 200, "%#x", &timeptr); #else strftime(retval2, 200, "%x", &timeptr); #endif /* If don't have weekday name, include it */ strftime(retval, 200, "%A", &timeptr); if (strstr(retval2,retval)==NULL) { /* Check also for short name */ strftime(retval, 200, "%a", &timeptr); if (strstr(retval2,retval)==NULL) { strcat(retval2," ("); strcat(retval2,retval); strcat(retval2,")"); } } #ifdef WIN32 setlocale(LC_ALL, ".ACP"); #endif return retval2; } bool CheckDate(GSM_DateTime *date) { - /* FIXME: This could also check if day is correct for selected month */ + const unsigned int days[]={31,29,31,30,31,30,31,31,30,31,30,31}; + + /* FIXME: This could also check for leap years */ return date->Year != 0 && date->Month >= 1 && date->Month <= 12 && - date->Day >= 1 && date->Day <= 31; + date->Day >= 1 && date->Day <= days[date->Month]; } bool CheckTime(GSM_DateTime *date) { return date->Hour <= 23 && date->Hour >= 0 && date->Minute <= 59 && date->Minute >= 0 && date->Second <= 59 && date->Second >= 0; } int GetLine(FILE *File, char *Line, int count) { int num; if (fgets(Line, count, File) != NULL) { num = strlen(Line) - 1; while(1) { if (Line[num] != '\n' && Line[num] != '\r') break; if (num == 0) break; Line[num--] = 0; } return strlen(Line); } return -1; } void SplitLines(unsigned char *message, int messagesize, GSM_Lines *lines, unsigned char *whitespaces, int spaceslen, bool eot) { int i,number=0,j; bool whitespace=true, nowwhite; for (i=0;i<MAX_LINES*2;i++) lines->numbers[i]=0; for (i=0;i<messagesize;i++) { nowwhite = false; for (j=0;j<spaceslen;j++) { if (whitespaces[j] == message[i]) { nowwhite = true; break; } } if (whitespace) { if (!nowwhite) { lines->numbers[number]=i; number++; whitespace=false; } } else { if (nowwhite) { @@ -406,166 +411,170 @@ bool GSM_SetDebugLevel(char *info, Debug_Info *di) if (!strcmp(info,"errors")) {di->dl = DL_TEXTERROR; return true;} if (!strcmp(info,"textdate")) {di->dl = DL_TEXTDATE; return true;} if (!strcmp(info,"textalldate")) {di->dl = DL_TEXTALLDATE; return true;} if (!strcmp(info,"errorsdate")) {di->dl = DL_TEXTERRORDATE; return true;} return false; } /* Dumps a message */ void DumpMessage(FILE *df, Debug_Level dl, const unsigned char *message, int messagesize) { int i,j=0,len=16; unsigned char buffer[200]; if (df==NULL || messagesize == 0) return; smfprintf(df, dl, "\n"); memset(buffer,0x20,sizeof(buffer)); buffer[len*5-1]=0; for (i = 0; i < messagesize; i++) { sprintf(buffer+j*4,"%02X",message[i]); buffer[j*4+2] = 0x20; if (isprint(message[i]) && message[i]!=0x09) { if (j != len-1) buffer[j*4+2] = message[i]; buffer[(len-1)*4+j+3] = message[i]; } else { buffer[(len-1)*4+j+3] = '.'; } if (j != len-1 && i != messagesize-1) buffer[j*4+3] = '|'; if (j == len-1) { smfprintf(df, dl, "%s\n", buffer); memset(buffer,0x20,sizeof(buffer)); buffer[len*5-1]=0; j = 0; } else { j++; } } if (j != 0) smfprintf(df, dl, "%s\n", buffer); } char *GetOS(void) { #ifdef WIN32 OSVERSIONINFOEX Ver; bool Extended = true; #endif +#if defined(linux) || defined(__linux) || defined(__linux__) + struct utsname Ver; +#endif static char Buffer[100] = {0x00}; #ifdef WIN32 memset(&Ver,sizeof(OSVERSIONINFOEX),0); Ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); if (!GetVersionEx((OSVERSIONINFO *)&Ver)) { Extended = false; Ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); if (!GetVersionEx((OSVERSIONINFO *)&Ver)) { //#ifdef _MSC_VER
// Ver.dwMajorVersion = _winmajor; // Ver.dwMinorVersion = _winminor; // Ver.dwBuildNumber = _osver; //#else sprintf(Buffer, "Windows"); return Buffer; //#endif } } /* ----------------- 9x family ------------------ */ /* no info about Win95 SP1, Win95 OSR2.1, Win95 OSR2.5.... */ if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 0 && Ver.dwBuildNumber == 950) { sprintf(Buffer,"Win 95"); } else if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 0 && Ver.dwBuildNumber == 1111) { sprintf(Buffer,"Win 95 OSR2.x"); /* no info about Win98 SP1.... */ } else if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 10 && Ver.dwBuildNumber == 1998) { sprintf(Buffer,"Win 98"); } else if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 10 && Ver.dwBuildNumber == 2222) { sprintf(Buffer,"Win 98 SE"); } else if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 90 && Ver.dwBuildNumber == 3000) { sprintf(Buffer,"Win ME"); /* ---------------- NT family ------------------- */ } else if (Ver.dwMajorVersion == 4 && Ver.dwMinorVersion == 0 && Ver.dwBuildNumber == 1381) { sprintf(Buffer,"Win NT 4.0"); } else if (Ver.dwMajorVersion == 5 && Ver.dwMinorVersion == 0 && Ver.dwBuildNumber == 2195) { sprintf(Buffer,"Win 2000"); } else if (Ver.dwMajorVersion == 5 && Ver.dwMinorVersion == 1 && Ver.dwBuildNumber == 2600) { sprintf(Buffer,"Win XP"); #if _MSC_VER > 1200 //6.0 has it undeclared
if (Extended) { if (Ver.wSuiteMask & VER_SUITE_PERSONAL) { sprintf(Buffer+strlen(Buffer)," Home"); } else { sprintf(Buffer+strlen(Buffer)," Pro"); } }
#endif } else if (Ver.dwMajorVersion == 5 && Ver.dwMinorVersion == 2) { sprintf(Buffer,"Win 2003"); } else { sprintf(Buffer, "Windows %i.%i.%i",Ver.dwMajorVersion,Ver.dwMinorVersion,Ver.dwBuildNumber); } if (Extended && Ver.wServicePackMajor != 0) { sprintf(Buffer+strlen(Buffer)," SP%i",Ver.wServicePackMajor); } #elif defined(linux) || defined(__linux) || defined(__linux__) - sprintf(Buffer, "Linux"); + uname(&Ver); + sprintf(Buffer, "Linux, kernel %s",Ver.release); #elif defined(__FreeBSD__) sprintf(Buffer, "FreeBSD"); #elif defined(__NetBSD__) sprintf(Buffer, "NetBSD"); #elif defined(__OpenBSD__) sprintf(Buffer, "OpenBSD"); #elif defined(__GNU__) sprintf(Buffer, "GNU/Hurd"); #elif defined(sun) || defined(__sun) || defined(__sun__) # ifdef __SVR4 sprintf(Buffer, "Sun Solaris"); # else sprintf(Buffer, "SunOS"); # endif #elif defined(hpux) || defined(__hpux) || defined(__hpux__) sprintf(Buffer, "HP-UX"); #elif defined(ultrix) || defined(__ultrix) || defined(__ultrix__) sprintf(Buffer, "DEC Ultrix"); #elif defined(sgi) || defined(__sgi) sprintf(Buffer, "SGI Irix"); #elif defined(__osf__) sprintf(Buffer, "OSF Unix"); #elif defined(bsdi) || defined(__bsdi__) sprintf(Buffer, "BSDI Unix"); #elif defined(_AIX) sprintf(Buffer, "AIX Unix"); #elif defined(_UNIXWARE) sprintf(Buffer, "SCO Unixware"); #elif defined(DGUX) sprintf(Buffer, "DG Unix"); #elif defined(__QNX__) sprintf(Buffer, "QNX"); #endif return Buffer; } char *GetCompiler(void) { static char Buffer[100] = {0x00}; #ifdef WIN32 # ifdef _MSC_VER if (_MSC_VER == 1200) { //? sprintf(Buffer, "MS VC 6.0"); } else if (_MSC_VER == 1300) { sprintf(Buffer, "MS VC .NET 2002"); } else if (_MSC_VER == 1310) { sprintf(Buffer, "MS VC .NET 2003"); |