I've been writing some simple console application in c and on linux and came across a problem. I need to run one object file from another.
When i want to open a text or binary file i would do something like this:
FILE* fp = fopen(path, mode);
and then just write to that file or read from it. But when it comes to object files i don't even know what to start with.
Globally, the situation is that there is a compiled object file which functionality needs to be tested (lets call it "target"). The test is driven by another compiled object file (let's call it "tester"). So, when i run the "tester" it queries for path to "target". When path is acquired, "tester" tests "target".
So, what i ask here is what this whole situation (coding of object files interaction) is called so i could go and rtfm? I lurked a bit and found some concepts like unit testing and APIs but brief view didn't give me strong impression that those things are what i'm looking for.
Thank you!
Related
I'm trying to write a custom filesystem for Tcl using the Tclapi (it's work related, won't go into details), but I'm stuck trying to figure out why this is not working.
In this code segment I'm getting the original/native Tcl_Filesystem, copying over all its contents (function pointers) to my_fs, and then calling Tcl_FSRegister on my_fs. Very simple, thought it should work.
// global scope
const Tcl_Filesystem *ori_fs;
Tcl_Filesystem *my_fs;
...
// in Init
// Get the original Tcl_Filesystem.
Tcl_Obj *root_obj = Tcl_NewStringObj("/", -1);
Tcl_IncrRefCount(root_obj);
ori_fs = Tcl_FSGetFileSystemForPath(root_obj);
Tcl_DecrRefCount(root_obj);
// create a duplicate of the original Tcl_Filesystem struct.
my_fs = malloc(sizeof(Tcl_Filesystem));
memmove(my_fs, ori_fs, ori_fs->structureLength);
int ret = Tcl_FSRegister((ClientData)1, my_fs);
if (ret == TCL_ERROR) {
...
When I ran
load <path to .so>/my_fs[info sharedlibextension]
# sanity check
puts [pwd]
set fp [open test.txt]
however, I get this
<my current directory>
while executing
"open test.txt"
invoked from within
"set fp [open test.txt]"
(file "test.tcl" line 3)
Notice how "puts [pwd]" works but not "open test.txt" ?
Replacing "my_fs" with "ori_fs" in the call to Tcl_FSRegister seems to work...
I've already spent far too much time trying to figure this out. I would appreciate if anyone could help me with this!
The native filesystem is special. In particular, there's some places where its identity is used directly: for example, it's the only FS that can have temporary files made on it, it's assumed to own the roots, and it is handled specially in path management. (Well, according to where in the source code there are direct references to the Tcl internal variable tclNativeFilesystem, which isn't something you can cheat at. It's also possibly in read-only memory, so you can't hack around this.)
For most sane uses of a Tcl virtual filesystem, this doesn't matter. Temp files have to be native because you may well be passing them to the OS (e.g., for loading libraries or running programs that were inside the VFS; with these, they have to be copied out or the OS will think “what are you talking about?!”) and you put the things that you are mounting somewhere other than the native root. So long as you're not trying to use a VFS as a security measure (not recommended; there are safe interpreters for that as they offer a stronger sandboxing solution) it shouldn't be a problem as you can just make your code know that it needs to work below a particular location to get things done. (FWIW, it's a bad idea to cd anyway, except in response to user requests, since it changes the meaning of user-supplied relative paths, so good code handles “make everything relative to a defined location” from the start.)
I want to know how to do something like the following...
I have a directory, let's call this directory "D:\Folder\" and it has some file types like .json, .lua, etc and I need to be able to put the appropriate files in a table based off their file type. How do I do this via Lua without external libraries? Also, how can I get other information on the files, like size, date modified, etc via lua and store that info?
As Yu Hao said in the comment, Lua by itself doesn't have any methods to get the list of files in a folder or access attributes of those files. In terms of external libraries, you can use Lua Filesystem module that has everything you need or winapi if you are looking for Windows-specific solution. Both are small libraries that can be compiled quite easily using mingw.
If you are looking for Windows-only-no-external-library solution, you should be able to run "dir" command and process its results using io.popen. You can parse the captured output and get file names, sizes, and dates based on that. You can also get the file size by using file:seek, but since you may be parsing anyway, you can get it all from the output. I don't think there is anything much simpler than that.
how about searching for a pattern that represents any and all characters a file could posses and then .file_type...and then run that through io.open for example...possible?
You won't be able to "guess" filenames by enumerating possible symbol combinations simply because this .... will .... take .... a .... very .... long .... time.
I am programming with C using Code::Blocks. My project is divided in 3, header, implementation and main.
Whenever I used a project, apart from the source files and the bin and obj folders I had a .depend and a .layout file. All good.
Now I created a new project, and just copied -> pasted everything new in source files. I did this twice.
For each case, I have a .c.save file, which has the same name of the implementation file (ie. the implementation file is called imp, then the file is called imp.c.save). I asked a friend of mine what it might be, and he said I need to beware as he had two random files created, which prevented him from building correctly (he got a stupid error). When the files were deleted everything went back to normal.
I did a short test of the program and I can find nothing different. I am hesitant to delete it since this cropped up twice in two cases, but I don't want to compromise my coding.
Tried to google and I didn't find much. Any help?
Well, it didn't cause any problems so I assuming it is an autosave file.
I'm creating a library to provide access to another library.
The library in question is a vendor, so not everyone should access this library.
One solution to do so is to put the library on a specific group (AIX) and then put everyone that can compile using it in the same group.
This solution don't works here because there are a lot of new people coming in and out, and the user that compiles (Its a process that do this operation) is not the same as the user that can access the code.
The solution i'm trying to archive is.
Every application have an pattern on their directory.
/Aplications/Group/...
So i can get the Group using the folder when the program runs.
What i am trying to do is.
Create a library that checks the directory and then loads the library using (loadAndInit) the dlopen function on AIX that already loads all the symbols, so i don't need the dlsym.
The problem is, i want to put the binary code of the library i want to load inside my source code.
WHY: Because if App -> Lib (Loads) -> Vendor, nothing can limit the developer from doing an link by hand or compiling by hand the App -> Vendor.
And the structure that call the compilation process cannot limit that.
What i have tried.
Convertlib
FILE* file=fopen("vendor.so", "rb");
int c;
do{
c=getc(file);
if(c > 0)
printf("\\x%02x", c);
}while(c!=EOF);
Then i have a little script that puts the output of this file as a
char *lib=(char* (malloc(sizeof(char) * /* lib size */));
lib="/*output of the Convertlib*/
Then i try to load it using fmemopen, so i dont create a temporary file just a file descriptor inside my process area.
To load it i do
FILE* vendorLib=fmemopen(lib, /*libSize*/, "r");
char path[50];
sprintf("/proc/%d/fd/%d", getpid(), fileno(vendorLib));
loadAndInit(path, 0, "");
If i call directly a lib (i have it to test) and dont load using the hex formated library it works.
But as i converted my binary code to hex and i'm trying to load it, it dont work.
Should i convert back to load it on the memory and use it as an library again?
This seems the only solution to work with, as the library is a vendor i cannot change it and it is the only way i see to limit the access because we have here more than 1000 programmers
I solved it using
printf("\x%02x", thechar);
Inside the getc loop
So i wrote it inside a memcpy then i copy to the memory so the binary file is stored as HEX data.
Works perfectly
What's the simplest way to find the path to the file in which I am "executing" some code? By this, I mean that if I have a file foo.py that contains:
print(here())
I would like to see /some/path/foo.py (I realise that in practice what file is "being executed" is complicated, but I think the above is well defined - a source file that contains some function that, when executed, gives the path to said file).
I have needed this in the past to make tests (that require some external file) self-contained, and I am currently wondering if it would be a useful way to locate some support files needed by a program. But I have never found a good way of doing this. The inspect module sounds like it should work, but you seem to need a class or function that is defined in that module.
In particular, the module instances contain __file__ attributes, but I can't see how to get the "current" module. Objects have a __module__ attribute, but that's the module name, not a module instance.
I guess one way is to throw and catch an exception and inspect the contents, but that seems like hard work. Surely there is a simple, easy way that I have missed?
To get the absolute path of the current file:
import os
os.path.abspath(__file__)
To get content of external file distributed with your package you could use pkg_util.get_data()(stdlib) or pkg_resources.resouce_string() (setuptools) to support execution from zip-archives or standalone executables created by py2exe, PyInstaller or similar, example.