handle null pointers and memory streams

This commit is contained in:
vscosta 2016-04-08 01:18:28 -07:00
parent c3488eeaed
commit 948c8e7cbb

View File

@ -93,6 +93,7 @@ static char SccsId[] = "%W% %G%";
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
#define SYSTEM_STAT _stat #define SYSTEM_STAT _stat
//#include <winbase.h>
#else #else
#define SYSTEM_STAT stat #define SYSTEM_STAT stat
#endif #endif
@ -246,7 +247,20 @@ has_reposition(int sno,
} }
} }
char *Yap_guessFileName(int f, int sno, char *nameb, size_t max) { char *Yap_guessFileName(FILE* file, int sno, char *nameb, size_t max) {
if (!nameb) {
nameb = malloc(max(256, max));
}
if (!file) {
strcpy(nameb, "memory buffer");
return nameb;
}
int f = fileno(file);
if (f < 0) {
strcpy(nameb, "???");
return nameb;
}
#if __linux__ #if __linux__
char path[256]; char path[256];
if (snprintf(path, 255, "/proc/self/fd/%d", f) && readlink(path, nameb, max)) if (snprintf(path, 255, "/proc/self/fd/%d", f) && readlink(path, nameb, max))
@ -255,15 +269,15 @@ char *Yap_guessFileName(int f, int sno, char *nameb, size_t max) {
if (fcntl(f, F_GETPATH, nameb) != -1) { if (fcntl(f, F_GETPATH, nameb) != -1) {
return nameb; return nameb;
} }
#elif __WIN32_ #else
FILE_NAME_INFO *fni = (FILE_NAME_INFO *)malloc(sizeof(FILE_NAME_INFO) + TCHAR path[MAX_PATH+1];
sizeof(WCHAR) * MAXPATHLEN); if (!GetFullPathName(path, MAX_PATH, path, NULL))
HANDLE handle = (HANDLE)_get_osfhandle(f); return NULL;
if (GetFileInformationByHandleEx(handle, FileNameInfo, &fni, max)) { else {
int i; int i;
char *ptr = nameb; char *ptr = nameb;
for (i = 0; i < fni->FileNameLength; i++) for (i = 0; i < strlen(path); i++)
*ptr = _PL__utf8_put_char(ptr, fni->FileName[i]); ptr += put_utf8(ptr, path[i]);
*ptr = '\0'; *ptr = '\0';
return nameb; return nameb;
} }