-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualMachineUtils.c
More file actions
49 lines (40 loc) · 1.34 KB
/
VirtualMachineUtils.c
File metadata and controls
49 lines (40 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "VirtualMachine.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#define SMALL_BUFFER_SIZE 256
void *VMLibraryHandle = NULL;
TVMMainEntry VMLoadModule(const char *module){
VMLibraryHandle = dlopen(module, RTLD_NOW);
if(NULL == VMLibraryHandle){
fprintf(stderr,"Error dlopen failed %s\n",dlerror());
return NULL;
}
return (TVMMainEntry)dlsym(VMLibraryHandle, "VMMain");
}
void VMUnloadModule(void){
if(NULL != VMLibraryHandle){
dlclose(VMLibraryHandle);
}
VMLibraryHandle = NULL;
}
TVMStatus VMFilePrint(int filedescriptor, const char *format, ...){
va_list ParamList;
char *OutputBuffer;
char SmallBuffer[SMALL_BUFFER_SIZE];
int SizeRequired;
TVMStatus ReturnValue;
va_start(ParamList, format);
OutputBuffer = SmallBuffer;
SizeRequired = vsnprintf(OutputBuffer, SMALL_BUFFER_SIZE, format, ParamList);
if(SizeRequired < SMALL_BUFFER_SIZE){
ReturnValue = VMFileWrite(filedescriptor, OutputBuffer, &SizeRequired);
return ReturnValue;
}
OutputBuffer = (char *)malloc(sizeof(char) *(SizeRequired + 1));
SizeRequired = vsnprintf(OutputBuffer, SizeRequired, format, ParamList);
ReturnValue = VMFileWrite(filedescriptor, OutputBuffer, &SizeRequired);
free(OutputBuffer);
return ReturnValue;
}