using system() to call ln from a cpp program - c++

I am trying to call system from a cpp program with the following command
system("ln -s -t \"targetDir[0]\" \"baseDir[0]\"");
Both targetDir and baseDir are QStringList. The program compiles and runs but when I execute the command i get the error ln : targetDir[0] is an invalid command. When I test by hard coding the values instead of using variables it works just fine. I can only conclude it is not escaping the string to put the value of the variables int the argument passed to ln. For the life of me I can't figure out why not.
Any Ideas?

You are confused. The system(3) library function (it is not a command, and despite its name is not a system call, those are listed in syscalls(2)) is forking a /bin/sh -c process which obviously don't have any idea about the variables of your C++ program (at runtime, variables don't exist; there are only locations).
BTW, using system(3) without care can be dangerous because of code injection issues. Imagine in your (wrong) approach that the targetDir[0] contains something like foo; rm -rf $HOME ....
To make a symbolic link, forking a process is overkill. Just call the symlink(2) system call (which the ln(1) command will call if invoked as ln -s)
The Qt library offers the QFile class with its QFile::link member function, or the static QFile::link (both will call symlink(2))
Future (or recent) versions of C++, starting from C++17, will provide the std::filesystem::create_symlink function (which on Linux will call
symlink(2)). It is probably inspired by Boost filesystem library.
PS. If coding for Linux or POSIX I recommend reading Advanced Linux Programming (which is freely downloadable). But if you want a source-portable Qt program restrict yourself to the generous Qt API. Or adopt C++17 and use its std::filesystem thing.

C++ by no means performs string interpolation.
If you are actually writing in C++, you can (considering targetDir is char ** or something alike):
std::string command = std::string("ln -s -t \"") + targetDir[0] + "\" \"" + baseDir[0] + "\"";
system(command.c_str());

Related

Compile C/C++ with gVim 8.1 in Windows 10 (MinGW)

Since everything is 32-bit, I used :set makeprg=mingw32-make, and then tried compiling via :!make, which gave the following error message:
'make' is not recognized as an internal or external command, operable program or batch file.
shell returned 1
I tried the basic "Goodbye World" stuff:
#include <iostream>
int main() {
std::cout << "Goodbye World";
return 0;
}
What am I doing wrong? Other than being a noob.
:!make doesn't run 'makeprg'. It runs make. Since you are specifically trying to run mingw32-make.exe, presumably you don't have an executable named make.exe. (See :help :!)
:make runs 'makeprg'. (See :help :make, :help 'makeprg')
my goal is to see the "Goodbye World" either in console or terminal or any observable way. Would this require additional tinkering to the _vimrc file?
You would rather want to do it by tinkering with your makefile. Any arguments to :make will be passed to 'makeprg'. If you defined targets clean (to remove the Make artefacts) or run (to execute the product of your Make), you will be able to write :mak clean or :mak run in Vim to run those targets. Obviously, you can create your own commands and/or mappings to make those :make commands faster.
Note (and I completely overlooked this myself, silly me) that when you run a simple code, such as the one above, that does not need extensive file managements and Makefiles, all you need to go is :!gcc % for C and :!g++ % for C++. I believe (and I may be wrong, but this is a simple observation) that in Vim, :! indicates the following commands are to be executed in the system Command Prompt, and gcc/g++ is the command to invoke the GNU Compiler for C and C++ respectively. Finally, % is used to indicate the current filename, with all extensions intact. This is extremely useful in cases of keymapping, as I have done via the following:
nnoremap <F5> :!g++ %<CR>|"Press Func.5 to Compile via GNU Comp.

using system() in C++;does not work

I need to execute this command in my c++ code:
mkdir -p sample_directory/{1..10}
to make 10 directory . But when I use it in system function in my code:
system("mkdir -p sample_director/{1..10}");
after execution this make just one directory by this name :{1..10}
how can i fix it?
Brace expansion is not in POSIX, so not all shells implement it. In particular, in POSIX, system is supposed to invoke sh, which is supposed to act like a "plain" POSIX shell rather than bash or some other feature-rich shell. So it won't do the brace expansion.
You could directly invoke bash if you want bash to process the command...
system("bash -c 'mkdir -p sample_director/{1..10}'");
or you could just make 10 separate system calls. Or other workarounds, I'm sure you can think of some.
You can probably try to do the same using a loop construct.
# include <cstdlib>
# include <string>
using namespace std;
int main(){
for(int i=1;i<=10;i++){
string str="mkdir -p sample_director/" + to_string(i);
system(str.c_str());
}
return 0;
}
And run your code with c++11 support
$ g++ -std=c++11 my_program.cpp
It doesn't work because dash doesn't support brace expansion (syntax like {1..10}). The system function doesn't care about the value of your SHELL, it always passes provided arguments to /bin/sh. And on Ubuntu /bin/sh refers to dash, which cannot handle brace expansion. On some other distributions (like Arch), /bin/sh is a symlink to bash, so your code will work fine.
If I were you, I wouldn't use system at all in this case. Boost::filesystem (or even mkdir/mkdirat) seems like a better option to me.

How do I open a terminal window with C++ in Ubuntu?

I recently decided to start teaching myself C++ and thought a simple encryption project would be a good place to start, since it covers most of the basics (cout, cin, opening files, etc). Is there a way to have the code open a terminal window similar to the one opened when I compile and run from sublime text?
I have tried this so far, but it hasn't changed anything.
string cmd = "gnome-terminal-x sh-c 'ls-l; exec bash'";
system(cmd.c_str());
Essentially, I would like to be able to run the program by clicking the .exe, and have the terminal where all of the input and output goes pop up.
You don't need to write any code, you just need to configure the shortcut to launch the program in a terminal. Here's a Gnome dialog that shows that option:
Problem seems to be gnome-terminal, or then just my failure to give it the right arguments. For example gnome-terminal -x sh -c 'ls -l ; exec bash' from command line in another terminal just opens an empty gnome-terminal and spits out a bunch of glib warnings to original terminal... (Note to readers: if you can give the right command that works for gnome-terminal, please let me know in comments or just edit this paragraph.)
However, using xterm works, for example xterm -e sh -c 'ls -l; exec bash', or a line for your code:
string cmd = "xterm -e sh -c 'ls -l; exec bash'";
As a side note, the command to open the default x terminal window of the DE is x-terminal-emulator, but it quite often has the practical problem of different terminals taking different arguments, so sadly you're probably better of using a specific terminal, like that xterm, and requiring that to be installed, or letting user to configure what terminal to use, with what arguments (though letting user to specify any command to be run can also be a security risk, if user is not always trusted).
Just be very careful with escaping. For example, when you test the command form command line, and then copy-paste it to C++ string literal, you need to escape every " and \ one more time for C++. If you have trouble with this, check out C++11 raw strings.
Escaping becomes extra important if you construct the command string at runtime, and especially if you accept user input and add that to the string. In that case, better search for and use some existing library like GLib, or sanitize the user input very carefully (ie. just paranoidically reject anything with chars, which may have a special meaning in shell in some context).
If you are actually asking, how can my program open a console window for itself similar to how Windows console programs behave, and redirect it's own stdin, stdout and stderr there, as if it was launched from command line, that that is not very easy from the same binary, and it is not commonly done like that in Unix.
If you want a behaviour like that, you could create a desktop shortcut, but more general way is to write a wrapper shell script, which starts your binary in a terminal. What kind of script exactly, depends on how you want it to behave exactly: what will it do with stdio, will it return or wait for program to exit, how do you want it to find the binary, how does it behave when run from command line instead of double-clicking from GUI, etc.

What is a 'shebang' line?

Currently I'm trying to start programming on my new Mac. I installed TextWrangler, and chose C++ as my language of choice; since I have some prior knowledge of it, from when I used Windows.
So, I wrote the ever so common "Hello World" program. Although, when I tried to run it, I got an error:
"This file doesn’t appear to contain a valid ‘shebang’ line (application error code: 13304)"
I tried searching the error code to find out how to fix this, but I couldn't find anything.. I have no idea what a 'shebang' line is... Can someone help me out?
You need to compile it with a compiler first. I assume you tried to run the source file like ./source but C++ doesn't work this way.
With some compilers however, you can provide a shebang-line as the first line of the source file (the #! is known as shebang or crunchbang, hence the name), like so:
#!/path/to/compiler
So that the shell knows what application is used to run that sort of file, and when you attempt to run the source file by itself, the compiler will compile and run it for you. That's a compiler-dependent feature though, so I recommend just plain compiling with G++ or whatever Macs use to get an executable, then run that.
While I wouldn't recommend it for regular C++ development, I'm using a simple shell script wrapper for small C++ utilities. Here is a Hello World example:
#if 0 // -- build and run wrapper script for C++ ------------------------------
TMP=$(mktemp -d)
c++ -o ${TMP}/a.out ${0} && ${TMP}/a.out ${#:1} ; RV=${?}
rm -rf ${TMP}
exit ${RV}
#endif // ----------------------------------------------------------------------
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello world" << std::endl;
return 0;
}
It does appear that you are trying to run the source file directly, however you will need to compile using a C++ compiler, such as that included in the gcc (GNU Compiler Collection) which contains the C++ compiler g++ for the Mac. It is not included with the Mac, you have to download it first:
from http://www.tech-recipes.com/rx/726/mac-os-x-install-gcc-compiler/ : "To install the gcc compiler, download the xcode package from http://connect.apple.com/. You’ll need to register for an Apple Developer Connection account. Once you’ve registered, login and click Download Software and then Developer Tools. Find the Download link next to Xcode Tools (version) – CD Image and click it!"
Once it's installed, if you are going for a quick Hello World, then, from a terminal window in the directory of your source file, you can execute the command g++ HelloWorld.cpp -o HelloWorld. Then you should be able to run it as ./HelloWorld.
Also, if you're coming from a Visual Studio world, you might want to give Mono and MonoDevelop a try. Mono is a free implementation of C# (and other languages), and MonoDevelop is an IDE which is very similar to Visual Studio. MonoDevelop supports C# and other .NET languages, including Visual Basic .NET, as well as C/C++ development. I have not used it extensively, but it does seem to be very similar to VS, so you won't have to learn new everything all in a day. I also have used KDevelop, which I liked a lot while I was using it, although that's been a while now. It has a lot of support for GNU-style development in C/C++, and was very powerful as I recall.
Good luck with your endeavors!
Links:
Mono: http://mono-project.com/Main_Page
MonoDevelop: http://monodevelop.com/
KDevelop: http://kdevelop.org/
shebang is http://en.wikipedia.org/wiki/Shebang_%28Unix%29.
not sure why your program is not running. you will need to compile and link to make an executable.
What I find confusing (/interesting) is C++ program giving "Shebang line" error. Shebang line is a way for the Unix like operating system to specify which program should be used to interpret the rest of the file. The shebang line usually points to the path of the interpreter. C++ is a compiled language and does not have interpreter for it.
To get the real technical details of how shebang lines work, do a man execve and get that man page online here - man execve.
If you're on a mac then doing something like this on the commandline:
g++ -o program program.cpp
Will compile and link your program into an executable called program. Then you can run it like:
./program
The reason you got the 'shebang' error is probably because you tried to run the cpp file like:
./program.cpp
And the shell tries to find an interpreter to run the code in the file. Because this is C++ there is no relevant interpreter but if your file contains Python or Bash then having a line like this
#!/usr/bin/python
at the 1st line in your source file will tell the shell to use the python interpreter
The lines that start with a pattern like this: #!/.../.../.. is called a shebang line. In other words, a shebang is the character sequence consisting of the characters number sign and exclamation mark (#!).In Unix-like operating systems, when a text file with a shebang is used as if it is an executable, the program loader mechanism parses the rest of the file's initial line as an interpreter directive. The loader executes the specified interpreter program, passing to it as an argument the path that was initially used when attempting to run the script, so that the program may use the file as input data.

Mac OSX and Unix quick questions

I have 3 questions. I am making a C++ executable to launch a Perl program I made. I will compile it for Winows, Mac OSX and Linux. It's pretty much just: system("perl progam.pl");
When compiled with Mac OSX, the program starts in ~. How would I get it to start in the dir it was launched from, or is it just a problem with the compiler?
I'm using - echo -n -e "\033[0;Program\007" - in an attempt to make the windows title "Program". Is this is best way?
I'm using - echo -n -e "\033[7;30;47m" - to make the background of the window black. Is this the best way?
Thanks.
This sounds like something Finder is doing. Launching the app from a shell should work as you expect.
Use tput
See answer to 2, above.
On Mac OS/Unix, invoking system does not change the current working dir. When executing program.pl the current working directory is the same from which you executed the C++ executable. When you launch the executable using Launch Services (e.g. the Finder) the working directory should be /.
On #1 you can refer to the current directory with ./ so system("perl ./progam.pl"); should do it assuming both scripts are sitting in the same folder. ../program.pl would be one level higher.
For #1, use getcwd & then pass an explicit path to system:
cwd=getcwd(NULL, PATH_MAX);
sprintf(cmd, "perl %s/program.pl", cwd);
system(cmd);
free(cwd);
If your perl program itself relies on a specific working directory, then do this instead:
sprintf(cmd, "cd %s && perl program.pl", cwd);
This is probably a silly question, but why are you making an application to launch a perl script? Just add the following to the top of your perl script and use "chmod a+x" to make it executable:
#! /usr/bin/perl
When you use the system command from C and C++, you are basically launching the default system shell and executing the given command in that shell. Doing that is not very portable and somewhat defeats the purpose of using C or C++ (since you could simply create a shell script that does the same thing). If you want to actually do this with C++, you should probably use popen or fork+exec to launch perl. Generally speaking, it isn't nice to end users to play with their Terminal in the manner that you have proposed; most users, by default, have the Terminal configured to display the most recently executed command or their current directory or some other information of their choosing, and changing that is -- on UNIX systems such as Mac OS X and Linux -- considered improper etiquitte. If you are trying to create a terminal interface, though, you might want to look at the curses library.