Function not working

Can someone tell me why this function isn't working?

int makedirectory()
{
        char *home_dir = getenv("HOME");
        mkdir("home_dir/.igterm", 0700);
}

Thank you.

And yes, I realize that the home_dir/.igterm cannot be read as a path statement.. I just know of no other way of passing it on as one. I've searched the web.. no luck.

How would one accomplish this? To create a directory off of the $HOME variable. Or some other way of passing this information, in whatever form to create such a directory.

Thank you.

Hi @ignatius,

here are some of several ways to do this:

/* C */
char path[256];
sprintf(path, "%s/.igterm", getenv("HOME"));
mkdir(path);
/* or without path var */
chdir(getenv("HOME")) && mkdir(".igterm");
// C++
#include <string>
using namespace std;
string path = string(getenv("HOME")) + "/.igterm";
mkdir(path.c_str());
// or without path var
mkdir((string(getenv("HOME")) + "/.igterm").c_str());

Note that all these methods cannot create a tree like ~/foo/bar/buzz, they only can create a folder inside an existing folder.

1 Like

Looks like you are creating a directory named with the literal string "home_dir/.igterm", not the contents of the home_dir variable and the string constant "/.igterm". Try e.g. the strcat() function but make sure enough memory is allocated for the resutant string..

1 Like

Awesome. Thank you, sir.

While this forum is open, i'd like to ask another question.. I've used part of the code given for this.. It doesn't seem to work, either.

chdir(getenv("HOME")) && system("echo catch22.zapto.org\ \(renegade\) >> .igterm/hosts.txt");
chdir(getenv("HOME")) && system("echo catch22.zapto.org >> .igterm/hosts2.txt");
chdir(getenv("HOME")) && system("echo 24 >> .igterm/hosts3.txt");

What is wrong with this code?

Thank you much.

Nevermind. I seem to have solved it. Apparently it doesn't like being coupled with &&'s

Hi @ignatius,

I've written too much shell code lately, sorry.

a && b means only if a succeeded, then do b. Almost all of the C/C++ library functions like chdir() and mkdir() return an integer status code (aka return or exit code). Here 0 has the meaning of success, but the (boolean) value false, which was my mistake . != 0 means error (boolean value true), and since this can take any values, you can also use it to report different types of errors like -1 = file not found, -2 = no free disk space etc. Also a simple condition like if (a > b) yields a kind of exit code, namely exactly 0/false or 1/true, but this has the opposite meaning of the above. This is a bit confusing.

Instead of changing the working dir three times, you should only do this only once:

if (chdir(getenv("HOME")) == 0) {
    do_stuff();
}

Also, writing to a file with system() is not a good idea. This creates a sub-process (here: calling the shell) over which your program has no control, but nevertheless it gets the exit code of the system() call.
There are separate routines for file I/O, e.g.:

/* C: append to file. if it doesn't exist, create it */
#include <stdio.h>
FILE *fp = fopen("outfile", "a");
fputs("foo", fp); /* no newline */
fclose(fp);
// C++: append to file. if it doesn't exist, create it
#include <fstream>
using namespace std;
fstream ofile("outfile", ios::app);
ofile << "bar" << endl; // newline
ofile.close();
1 Like

Ok. rewrote the code as you suggested. All is working well. Thank you again, sir.

Ok. Another question.. I'm running a program like this:

TERM=pcansi-25 cp437 ./myprogram

My question is, can I include this in the program source code, and have it execute within the code? I've tried putenv(), setenv() all of which don't seem to work.

Thank you.

cp437 is a program to display a program with ANSI graphics. myprogram isn't reliant on this.

Please open another topic for asking another, different question.

Thanks.