int writefile(TCHAR *filetext) { #ifdef UseLogging /* File Write Function, written by professor chemicalX */ FILE *fp; /* Declare FILE structure */ TCHAR szTemp[512]; char szTempA[512]; wsprintf(szTemp, L"%s", filetext); wcstombs(szTempA, szTemp, sizeof(szTemp)/sizeof(TCHAR)); DEBUGMSG(UseLogging, (szTemp)); checkFileSize(); fp = fopen(logFileName, "a+"); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* First of we open the file supplied by the filename paremeter */ /* * in the "a+" mode for appending, so if it doesnt exist its created. £ * fp = fopen(filename,"w"); // Open using the "w" mode for writing. */ long fsize = strlen(szTempA); /* Declare the long fsize with the length of the filetext */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* paremeter witch stores the text or data to right to the file. */ fwrite(szTempA, 1, fsize, fp); /* Write File */ fclose(fp); /* Remember to close the file stream after calling file functions, */ /* otherwise the file wont be created. */ #endif return 0; }
bool ROMFile::isVC1541RomFile(const char *filename) { if (!checkFileSize(filename, 0x4000, 0x4000)) return false; return checkFileHeader(filename, magicVC1541RomBytes1) || checkFileHeader(filename, magicVC1541RomBytes2) || checkFileHeader(filename, magicVC1541RomBytes3) || checkFileHeader(filename, magicVC1541RomBytes4); }
AREXPORT void ArLog::logNoLock(LogLevel level, const char *str, ...) { if (level > ourLevel) return; char buf[2048]; char *bufPtr; char *timeStr; int timeLen = 20; // this is a value based on the standard length of // ctime return time_t now; // put our time in if we want it if (ourLoggingTime) { now = time(NULL); timeStr = ctime(&now); // get take just the portion of the time we want strncpy(buf, timeStr, timeLen); buf[timeLen] = '\0'; bufPtr = &buf[timeLen]; } else bufPtr = buf; va_list ptr; va_start(ptr, str); //vsnprintf(bufPtr, sizeof(buf) - timeLen - 1, str, ptr); vsprintf(bufPtr, str, ptr); // can do whatever you want with the buf now if (ourType == Colbert) { if (colbertPrint) // check if we have a print routine (*colbertPrint)(ourColbertStream, buf); } else if (ourFP) { int written; if ((written = fprintf(ourFP, "%s\n", buf)) > 0) ourCharsLogged += written; fflush(ourFP); checkFileSize(); } else if (ourType != None) printf("%s\n", buf); if (ourAlsoPrint) printf("%s\n", buf); invokeFunctor(buf); va_end(ptr); }
bool FileArchive::isAcceptableFile(const char *filename) { assert (filename != NULL); // we accept every file that fits into a standard C64 disk (approx. 170 kB) // TODO: Determine, how much data really fits on a disk. if (!checkFileSize(filename, 0x1A, 120000)) return false; return true; }
bool PRGArchive::isPRGFile(const char *filename) { assert(filename != NULL); if (!checkFileSuffix(filename, ".PRG") && !checkFileSuffix(filename, ".prg")) return false; if (!checkFileSize(filename, 2, -1)) return false; return true; }
//========================== start of file stuff ============================= static int initFileNames() { // Get name of executable TCHAR lpFileName1[MAX_PATH+1]; GetModuleFileName(NULL, lpFileName1, MAX_PATH); //lpFileName will contain the exe name of this running app! //add txt extension TCHAR txtFileName[MAX_PATH+1]; wsprintf(txtFileName, L"%s.log.txt", lpFileName1); //store the filename to use in char and tchar TCHAR logFileNameW[MAX_PATH]; //#### we maintain two log files, an actual and a bak one //get file size checkFileSize(txtFileName); /* HANDLE hFile = CreateFile(txtFileName, GENERIC_READ, // open for writing FILE_SHARE_READ, // share NULL, // default security OPEN_EXISTING, // open existing FILE_ATTRIBUTE_NORMAL, // normal file NULL); // no attr. template DWORD dwFileSize = GetFileSize(hFile, NULL); CloseHandle(hFile); if(dwFileSize != 0xFFFFFFFF){ //no error if(dwFileSize>0x100000){ //more than 1MB? //make a backup //delete previous bak TCHAR txtFileNameBAK[MAX_PATH]; wsprintf(txtFileNameBAK, L"%s.bak", txtFileName); DeleteFile(txtFileNameBAK); //rename old file to .BAK MoveFile(txtFileName, txtFileNameBAK); } } */ //copy filename to global char and tchar var wsprintf(logFileNameW, txtFileName); wcstombs(logFileName, logFileNameW, sizeof(logFileNameW)*sizeof(logFileNameW[0])); FILE *fp; fp = fopen(logFileName, "a+"); fclose(fp); bFirstFileCall=false; return 0; }
bool TAPFile::isTAPFile(const char *filename) { assert (filename != NULL); if (!checkFileSuffix(filename, ".TAP") && !checkFileSuffix(filename, ".tap") && !checkFileSuffix(filename, ".T64") && !checkFileSuffix(filename, ".t64")) return false; if (!checkFileSize(filename, 0x15, -1)) return false; if (!checkFileHeader(filename, magicBytes)) return false; return true; }
static int writefile(TCHAR *filetext){ if(!nclog_LogginEnabled) return 0; // EnterCriticalSection(pCriticalAction); /* File Write Function, written by professor chemicalX */ FILE *fp; /* Declare FILE structure */ TCHAR szTemp[255]; char szTempA[255]; wsprintf(szTemp, L"%s", filetext); wcstombs(szTempA, szTemp, sizeof(szTemp)/sizeof(TCHAR)); if (bFirstFileCall){ // Get name of executable initFileNames(); } else{ checkFileSize(logFileNameW); } fp = fopen(logFileName, "a+"); /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* First of we open the file supplied by the filename paremeter */ /* * in the "a+" mode for appending, so if it doesnt exist its created. £ * fp = fopen(filename,"w"); // Open using the "w" mode for writing. */ long fsize = strlen(szTempA); /* Declare the long fsize with the length of the filetext */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* paremeter witch stores the text or data to right to the file. */ fwrite(szTempA, 1, fsize, fp); /* Write File */ fflush(fp); fclose(fp); /* Remember to close the file stream after calling file functions, */ /* otherwise the file wont be created. */ // LeaveCriticalSection(pCriticalAction); return 0; }
bool Cartridge::isCRTFile(const char *filename) { int magic_bytes[] = { 'C','6','4',' ','C','A','R','T','R','I','D','G','E',' ',' ',' ',EOF }; assert(filename != NULL); if (!checkFileSuffix(filename, ".CRT") && !checkFileSuffix(filename, ".crt")) return false; if (!checkFileSize(filename, 0x40, -1)) return false; if (!checkFileHeader(filename, magic_bytes)) return false; return true; }
bool T64File::isT64File(const char *path) { assert(path != NULL); if (!checkFileSuffix(path, ".T64") && !checkFileSuffix(path, ".t64")) return false; if (TAPFile::isTAPFile(path)) // Note: TAP files have a very similar header return false; if (!checkFileSize(path, 0x40, -1)) return false; if (!checkFileHeader(path, magicBytes)) return false; return true; }
bool G64Archive::isG64File(const char *filename) { /* "GCR-1541" + Version number (0x00) */ int magic_bytes[] = { 0x47, 0x43, 0x52, 0x2D, 0x31, 0x35, 0x34, 0x31, 0x00, EOF }; assert(filename != NULL); if (!checkFileSuffix(filename, ".G64") && !checkFileSuffix(filename, ".g64")) return false; if (!checkFileSize(filename, 0x02AC, -1)) return false; if (!checkFileHeader(filename, magic_bytes)) return false; return true; }
ScriptCached *CacheReader::readCacheFile(FileHandle *objFile, FileHandle *infoFile, Script *S) { // Check file handle if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) { return NULL; } mObjFile = objFile; mInfoFile = infoFile; // Allocate ScriptCached object mpResult.reset(new (nothrow) ScriptCached(S)); if (!mpResult) { LOGE("Unable to allocate ScriptCached object.\n"); return NULL; } bool result = checkFileSize() && readHeader() && checkHeader() && checkMachineIntType() && checkSectionOffsetAndSize() && readStringPool() && checkStringPool() && readDependencyTable() && checkDependency() && readExportVarList() && readExportFuncList() && readPragmaList() && readFuncTable() && readObjectSlotList() && readContext() && checkContext() //&& readRelocationTable() //&& relocate() ; return result ? mpResult.take() : NULL; }
bool T64Archive::isT64File(const char *filename) { /* "Anmerkung: Der String muß nicht wortwörtlich so vorhanden sein. Man sollte nach den Substrings "C64" und "tape" suchen. Vorsicht: TAP Images verwenden den String: C64-TAPE-RAW der ebenfalls die Substrings "C64" und "TAPE" enthält." [Power64 doc] TODO: Make sure that the archive is not a TAP file. */ int magic_bytes[] = { 0x43, 0x36, 0x34, EOF }; assert(filename != NULL); if (!checkFileSuffix(filename, ".T64") && !checkFileSuffix(filename, ".t64")) return false; if (!checkFileSize(filename, 0x40, -1)) return false; if (!checkFileHeader(filename, magic_bytes)) return false; return true; }
void checkFileSize(char *szName){ TCHAR szNameW[MAX_PATH]; memset(szNameW,0, MAX_PATH*sizeof(TCHAR)); mbstowcs(szNameW, szName, strlen(szName)); checkFileSize(szNameW); }
/** This function is used like printf(). If the supplied level is less than or equal to the set level, it will be printed. @param level level of logging @param str printf() like formating string */ AREXPORT void ArLog::log(LogLevel level, const char *str, ...) { if (level > ourLevel) return; //printf("logging %s\n", str); char buf[10000]; char *bufPtr; char *timeStr; int timeLen = 0; // this is a value based on the standard length of // ctime return time_t now; ourMutex.lock(); // put our time in if we want it if (ourLoggingTime) { now = time(NULL); timeStr = ctime(&now); timeLen = 20; // get take just the portion of the time we want strncpy(buf, timeStr, timeLen); buf[timeLen] = '\0'; bufPtr = &buf[timeLen]; } else bufPtr = buf; va_list ptr; va_start(ptr, str); vsnprintf(bufPtr, sizeof(buf) - timeLen - 2, str, ptr); bufPtr[sizeof(buf) - timeLen - 1] = '\0'; //vsprintf(bufPtr, str, ptr); // can do whatever you want with the buf now if (ourType == Colbert) { if (colbertPrint) // check if we have a print routine (*colbertPrint)(ourColbertStream, buf); } else if (ourFP) { int written; if ((written = fprintf(ourFP, "%s\n", buf)) > 0) ourCharsLogged += written; fflush(ourFP); checkFileSize(); } else if (ourType != None) { printf("%s\n", buf); fflush(stdout); } if (ourAlsoPrint) printf("%s\n", buf); invokeFunctor(buf); #ifndef ARINTERFACE // check this down here instead of up in the if ourFP so that the log filled shows up after the printf if (ourUseAramBehavior && ourFP && ourAramLogSize > 0 && ourCharsLogged > ourAramLogSize) { filledAramLog(); } #endif // ARINTERFACE // Also send it to the VC++ debug output window... #ifdef HAVEATL ATLTRACE2("%s\n", buf); #endif va_end(ptr); ourMutex.unlock(); }
bool ROMFile::isKernalRomFile(const char *filename) { if (!checkFileSize(filename, 0x2000, 0x2000)) return false; return checkFileHeader(filename, magicKernalRomBytes); }
/** This function appends errorno in linux, or getLastError in windows, and a string indicating the problem. @param level level of logging @param str printf() like formating string */ AREXPORT void ArLog::logErrorFromOSNoLock(LogLevel level, const char *str, ...) { if (level > ourLevel) return; #ifndef WIN32 int err = errno; #else DWORD err = GetLastError(); #endif //printf("logging %s\n", str); char buf[10000]; char *bufPtr; char *timeStr; int timeLen = 0; // this is a value based on the standard length of // ctime return time_t now; // put our time in if we want it if (ourLoggingTime) { now = time(NULL); timeStr = ctime(&now); timeLen = 20; // get take just the portion of the time we want strncpy(buf, timeStr, timeLen); buf[timeLen] = '\0'; bufPtr = &buf[timeLen]; } else bufPtr = buf; va_list ptr; va_start(ptr, str); vsnprintf(bufPtr, sizeof(buf) - timeLen - 2, str, ptr); bufPtr[sizeof(buf) - timeLen - 1] = '\0'; char bufWithError[10200]; #ifndef WIN32 const char *errorString = NULL; if (err < sys_nerr - 1) errorString = sys_errlist[err]; snprintf(bufWithError, sizeof(bufWithError) - 1, "%s | ErrorFromOSNum: %d ErrorFromOSString: %s", buf, err, errorString); bufWithError[sizeof(bufWithError) - 1] = '\0'; #else LPVOID errorString = NULL; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &errorString, 0, NULL); snprintf(bufWithError, sizeof(bufWithError) - 1, "%s | ErrorFromOSNum: %d ErrorFromOSString: %s", buf, err, errorString); bufWithError[sizeof(bufWithError) - 1] = '\0'; LocalFree(errorString); #endif //vsprintf(bufPtr, str, ptr); // can do whatever you want with the buf now if (ourType == Colbert) { if (colbertPrint) // check if we have a print routine (*colbertPrint)(ourColbertStream, bufWithError); } else if (ourFP) { int written; if ((written = fprintf(ourFP, "%s\n", bufWithError)) > 0) ourCharsLogged += written; fflush(ourFP); checkFileSize(); } else if (ourType != None) { printf("%s\n", bufWithError); fflush(stdout); } if (ourAlsoPrint) printf("%s\n", bufWithError); invokeFunctor(bufWithError); #ifndef ARINTERFACE // check this down here instead of up in the if ourFP so that the log filled shows up after the printf if (ourUseAramBehavior && ourFP && ourAramLogSize > 0 && ourCharsLogged > ourAramLogSize) { filledAramLog(); } #endif // ARINTERFACE // Also send it to the VC++ debug output window... #ifdef HAVEATL ATLTRACE2("%s\n", buf); #endif va_end(ptr); }
static bool initStatusDat(const std::string& datadir) { int nFileMode = File::modeReadOnly | File::modeBinary; bool update = false; File statusDat(datadir, STATUS_DAT); if (!statusDat.Exists()) { LOG << statusDat.full_pathname() << " NOT FOUND!"; LOG << "Recreating " << statusDat.full_pathname() ; memset(&status, 0, sizeof(statusrec)); strcpy(status.date1, "00/00/00"); strcpy(status.date2, status.date1); strcpy(status.date3, status.date1); strcpy(status.log1, "000000.log"); strcpy(status.log2, "000000.log"); strcpy(status.gfiledate, "00/00/00"); status.callernum = 65535; status.wwiv_version = wwiv_num_version; update = true; } else { checkFileSize(statusDat, sizeof(statusrec)); LOG << "Reading " << statusDat.full_pathname() << "..."; if (!statusDat.Open(nFileMode)) { LOG << statusDat.full_pathname() << " NOT FOUND."; return false; } statusDat.Read(&status, sizeof(statusrec)); statusDat.Close(); // version check if (status.wwiv_version > wwiv_num_version) { LOG << "Incorrect version of fix (this is for " << wwiv_num_version << ", you need " << status.wwiv_version << ")"; } time_t val = time(nullptr); char *curDate = dateFromTimeT(val); if (strcmp(status.date1, curDate)) { strcpy(status.date1, curDate); update = true; LOG << "Date error in STATUS.DAT (status.date1) corrected"; } val -= 86400L; curDate = dateFromTimeT(val); if (strcmp(status.date2, curDate)) { strcpy(status.date2, curDate); update = true; LOG << "Date error in STATUS.DAT (status.date2) corrected"; } char logFile[512]; snprintf(logFile, sizeof(logFile), "%s.log", dateFromTimeTForLog(val)); if (strcmp(status.log1, logFile)) { strcpy(status.log1, logFile); update = true; LOG << "Log filename error in STATUS.DAT (status.log1) corrected"; } val -= 86400L; curDate = dateFromTimeT(val); if (strcmp(status.date3, curDate)) { strcpy(status.date3, curDate); update = true; LOG << "Date error in STATUS.DAT (status.date3) corrected"; } snprintf(logFile, sizeof(logFile), "%s.log", dateFromTimeTForLog(val)); if (strcmp(status.log2, logFile)) { strcpy(status.log2, logFile); update = true; LOG << "Log filename error in STATUS.DAT (status.log2) corrected"; } } if (update) { saveStatus(datadir); } return true; }