Using ‘symlinks’ to create embedded CLI applications in C/C++
Use one CLI application for multiple CL entry points.
To create symbolic links which will execute different code section but will point to same program.
What is a symlink?
A symlink (symbolic link) is essentially a pointer to a file or folder located elsewhere, consumes little space and is very fast to create (compared to copying a file and its contents). ~blogs.windows.com
How to create symlinks?
In Linux we can use
ln to create symbolic link with
-s argument. The syntax of the same is —
ln -s <original file path> <link file path>
For example, let’s these files -
Here we can see that after creating a symbolic link named SYMFILE, it creates an entry which is ‘linked’ back to the original file and if we print the SYMFILE it will actually print the MAINFILE.
What is argv?
In short these are used to take arguments from command-line to the program and parse them accordingly. More on these can be read here, but I guess you already know this if you are reading this article, but anyways, here you go —
Command Line Arguments in C++ - Cprogramming.com
In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in…
Practical Understanding and Implementation
Symbolic Calculator Example
Compiling and setting up environment variables
You can compile the code with g++
Before running the program as a command we need to add the current directory to
PATH variable, we can do that by exporting the new
Adding current project directory to
export PATH=`pwd`:$PATH and this will add current directory to PATH.
Now we can run the program just by typing it’s name.
To understand above command better you can check out the following video, I've skipped it to the part where we discuss
` in shell (48:45 in video).
As we can see that running the executable will not do anything as we have set it to act only if the name of the executable is ‘add’, ‘sub’ or ‘mul’.
To check this we can run ‘a.out’ then rename it to ‘add’ and run it again, and it seems to work as expected as now the file name is ‘add’ and it corrosponds to a entry in our switch statement.
We can also use subtraction and multiplication by renaming the file to ‘sub’ and ‘mul’ respectively. But how to do it without renaming the file every time? Yes, “symlinks” !
We can create ‘add’, ‘sub’ and ‘mul’ as symlinks to ‘a.out’ and then we will have all 3 exposed CLI commands.
Now we can access the symlinks as normal commands (also, notice the size of the symlinks just 5 bytes!) -
So why use symlinks?
Here are 3 major reasons —
1. Easy to setup
2. Takes minimum space
3. Easy to update / patch software
4. Links remain if the original file is deleted
5. Links will NOT reference the file anymore if it is moved
Now you have only one application
a.out that you can access via 3 different CL commands and you will get different results.
This is how programs like
sudoedit work in Linux!
So next time when you work in a storage sensitive environment you know how to deal with your applications effectively.