C++ can't return variable
Hey all! This bit of code always returns 0, even though errcheck will have a non-zero value. If I use return 1; it works as expected. Please help?
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
Here is the full code:
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
umask(0);
mkdir("/tmp/.aget", 0755);
chdir("/tmp/.aget");
for (int i = 1; i < argc; i++)
{
开发者_如何转开发
string target(argv[i]);
string docommand("");
string s1("wget -q http://aur.archlinux.org/packages/");
string s2("/");
string s3(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
docommand += target;
docommand += s3;
cout << "Downloading AUR tarball for '" << target << "'..." << endl;
int errcheck = system(docommand.c_str());
if (errcheck != 0)
{
cerr << "Could not retrieve tarball!" << " Errcheck status (debug): " << errcheck << endl;
return errcheck;
}
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
string s1("tar xf ");
string s2(".tar.gz");
docommand += s1;
docommand += target;
docommand += s2;
cout << "Extracting '" << target << ".tar.gz'..." << endl;
system(docommand.c_str());
}
for (int i = 1; i < argc; i++)
{
string target(argv[i]);
string docommand("");
chdir("/tmp/.aget");
chdir(target.c_str());
system("makepkg -csim --noconfirm > /dev/null");
}
rmdir("/tmp/.aget");
return 0;
}
Unix exit statuses are restricted to values 0-255, the range of an unsigned 8-bit integer. As such, you cannot see 2048.
See Exit Status wiki page for more information.
I suspect that wget always returns 0.
This is because the actual error status from an http request is in the stream.
精彩评论