How can I change directory in C without resolving symlinks? - c

I'm trying to create a C program that emulates a Bash shell.
For the cd file instruction I use chdir("file") but it isn't correct because chdir() always resolves symlinks while cd doesn't by default.
I'm looking for a way to get a function that changes the current working directory without resolving symlinks.

Related

Calling Custom path binaries using System command

In a linux box my rpm's used to install my software binaries to a predefined "/opt//bin" and "/opt//lib" , and from some binaries(c executable) i used to call these binaries located in /opt//bin by harcoding its full path using system call.
for example : system("/opt/<my_loc>/bin/myBin");
Now I would like to install my software to a custom path so what's the best approach to call the binaries from the new custom path ?
Stop hard-coding the path in each system call. Or at least use a #define so you can update it from your old path to your new path. Then you can generate a suitable config file at build-time via a script.
The better approach is to just configure your PATH environment variable correctly, so you can just tell it what binary you want to run. I order it like this: ~/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin (if your /bin and /sbin is not merged into /usr you will need to add those too). system() uses sh which on my system is dash so you would set your PATH in ~/.profile and/or /etc/profile.
I don't like a binary path per package, so I use the program stow to merge a bunch of per package directories in /usr/local/stow/$package/bin to /usr/local/bin.

CMake, how to set different value in a configure_file in Build vs Install

I have a simple CMake project with CTest and CPack. It uses the Lua C API to load and execute an script file called script.lua.
This script will be in different location when built vs when installed/packed, it's location would be:
[build] : ${CMAKE_CURRENT_SOURCE_DIR}/src/scripts
[install]: ../scripts (relative to app which is in bin directory)
What I'm trying to achieve here is to have install step regenerate configure_file then rebuild using new configure_file and only then proceed to do the normal install step and of course revert the configure_file back to it's original state afterwards.
Any help regarding this issue is appreciated.
My understanding is that CMake's configure_file command has its full effect during the execution of the cmake program. It has no representation in generated makefiles, or whatever other build system components cmake generates. Thus, if you want to configure a file differently for installation than for pre-installation testing,
You would need to perform completely separate builds (starting with executing cmake) for the two cases, and
You would need to use some attribute of the cmake command line or execution environment to convey the wanted information, such as using a -D option to define a CMake variable on the command line.
I advise you not to pursue this route. Aside from being overcomplicated, it's also poor form to install a different build of the software than is tested.
You have a variety of alternatives that could serve better. Among those are
Give the program itself the ability to accept a custom location for the Lua script. That is, make it recognize a command-line argument or environment variable that serves this purpose. Make use of that during pre-installation testing.
If indeed the program is using a relative path to locate the script at runtime, then just (have CMake) put a copy of the script at the appropriate location in the build tree, so that the program will find it normally during testing.

Compile C source file using Command Prompt

I am having trouble while using the codeblocks-16.01mingw-setup.exe - installed in a file whose path does not contain spaces - when trying to compile at a comand prompt. A beginner's guide recommended using the following line in command prompt:
gcc cards.c -o cards
for the source file named cards.c (on my desktop). This gives the error
'gcc' is not recognised as an internal or external command, operable program or batch file.
When trying to figure this out, I have found out that you can drag and drop files in your command prompt and it specifies their path. Doing this with the gcc.exe taken from F:\Programare\Codeblocks\MinGW\bin\gcc.exe and adding the c source file as C:\Users\dream\Desktop\cards.c gives the error
as.exe - System error: The program can't start because libintl-8.dll is missing from your computer. Try reinstalling the program to fix this problem.
I've reinstated my Codeblocks to no avail. I've also tried matching the paths of the compiler with the source file, bringing the source code where the compiler was, again to no avail.
Please help me understand the issue. I must say that the file named libintl-8.dll is right there where gcc.exe is, trying to add it in the command prompt gives some syntax error.
gcc is trying to find that dll file in the working directory, so you need to cd into that directory cd /d F:\Programare\Codeblocks\MinGW\bin.
Dynamic-Link Library Search Order (Windows)
The directory specified by lpFileName.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
You have to add F:\Programare\Codeblocks\MinGW\bin\ to your users or systems PATH environment variable.
Once you have done this you can simply type
gcc cards.c -o cards

Virtual environment for C library development

Original
I am looking for a way to create a non-isolated development environment for a C-library.
I will most likely use cmake to build the library and my IDE is a simple text editor.
The problem now is that I do not only create the library but also some sample "applications" using the library.
Therefore I need to install the library's headers and the shared object (I'm using GNU/Linux) somewhere and I do not want to install it to /usr/local/lib or (the even worse) /usr/lib.
Is there a way to create a virtual environment similar to python's pyvenv (and similar) where I can install the everything to but still have access to the host libraries?
Also I do not want to rewrite my $PATH/$LD_LIBRARY_PATH, setup a VM, container, or chroot.
The usage would then look like:
# switch to environment somehow
loadenv library1
# for library
cd library
make && make install
# for application
cd ../application1
make && ./application1
Is this possible?
Edit 1
So basically my directory structure will look like this:
library/
library/src/
library/src/<files>.c
library/include/<files>.h
application/
application/src/
application/src/<files>.c
First I need to compile the library and install the binary and header files.
These should be installed in a fake system-location.
Then I can compile the application and run it.
Edit 2
I thought a bit about it and it seems all I need is a filesystem sandbox.
So basically I want to open up a shell where every write to disk is not committed to the filesystem but rather temporarily saved in e.g. a ramfs/tmpfs just to be dropped when the shell exits.
This way I can exactly test how everything would behave if compiled, deployed and executed on the real machine without any danger to existing files or directories and without accidentally creating files or directories without cleaning them up.
You don't really need to 'install' the library, you can work in the development tree.
(1) for compilation all you need to do is use -I flag to specify where the libraries header files are, and this can be a relative path, for example in your case you could do -I../../library/include
(2) for linking you need to tell the linker where the library is located at, you can use the -L flag append to the library search order.
(3) for testing the application, you are correct that the application needs to be able to find the library. You have a couple of options:
(a) make sure the library and the executable are in the same directory
(b) you can temporarily modify your LD_LIBRARY_PATH, in your current shell only, for testing:
export LD_LIBRARY_PATH=abs_path_to_library:$LD_LIBRARY_PATH
note that this will only effect the current shell (command terminal) you are working in. Any other shells you may have open, or open later will have your normal LD_LIBRARY_PATH. I know you specified that you don't want to modify your PATH or LD_LIBRARY_PATH, but being local to the shell that the command is executed it is a nice, easy way to do this.
(c) embed the path to the library in the client executable. To do this you need to pass an option to the linker. The command for gcc is:
-Wl,-rpath,$(DEFAULT_LIB_INSTALL_PATH)
see this how-to

link Rscript to non-existant directory

I am using program which automatically assumes that Rscript is installed in my home directory. Not even it is not, but the directory /home/username/bin does not even exist - so calling /home/username/bin/Rscript results in an error.
However typing "Rscript" works from everywhere. Is there any way how to run Rscript whenever this program calls this non-existent directory? I would rather not change code of the program I am using.
Thanks a lot.
You can create a symlink to the real file, using the ln command:
ln -s /real/path/to/rscript /home/username/bin/rscript
Setting $HOME variable solved this issue.

Resources