Works from console but not when clicked in KDE

why is it that:

#include <sys/stat.h>
#include <stdio.h>

int main(int argc, char **argv)
{
mkdir("testDir", S_IRUSR|S_IWUSR|S_IXUSR);

return 0;

}

works from the console when i type ./a.out but when clicked on from a window manager, it does diddly squat??? Interested to hear your answers.

Regards,
Jason.

My guess would be that it worked but you don't know where it worked. Or that it failed because testDir already exists.

But why guess? You are susposed to check the return code from a system call. If the system call fails, you will get -1 and errno will be set to a value describing what went wrong. You should put code in your program to display the error code.

  1. What is the current directory when the program runs?

  2. write a syslog or /tmp/myerror.log from the program to say what happened

  3. try "perror()" as in

    if (mkdir(.....)) { perror("mkdir failed\n"); exit(1); }

After reading that I did a search and lo and behold, when I execute from the console, testDir is created in the current directory. When executed from KDE, it creates the directory but in ~/ !!!

Thanks very much for pointing me to that function. It's really very useful. It actually tells you why something works or doesn't!

(just to clear things up, I am totally new to system programming being a java programmer... I have dabbled in Fortran but wanted to tackle Unix and C so here I am slaving away with a really really thick book... :slight_smile: Thanks for your patience.)

So my next question is:
Why on earth does it begin executing from home when it's clicked from another directory? Seems like a weird thing to do. Is their a function that can test where it was clicked from and to then change to that dir so it is now current?

You guys have been very helpful. Thanks.
Jason.

Thanks for pointing that out by the way. I had wondered the reason for all the if(systemcall(...) ) in my book. Thought they were kinda overkill but when I kept getting -1 from it that prompted me to check for the testDir and I found it in ~/

Say click on a program in /bin should the current directory be /bin?

NB: UNIX programs by themselves know *nothing* about KDE.

I suggest you look at what you are trying to do and work out is creating a directory in the same directory as the program a good idea? Unix programs normally separate out code from config from data. You also need to deal with multiuser issues, what happens if two people run the program at exactly the same time? If this is supposed to be a temporary directory why not create it in /tmp, /usr/tmp or $TMP ?

Well I guess this is a problem. I don't have a specific thing that I am trying to do- just learning how all this works for now, with the unfortunate beginning from a Java developers perspective (there are some major differences although much is similar). If I executed a Java application from clicking on it, the current directory is the execution dir, and it was simply possible to grab the execution path. However what you have said makes real sense- what if two people run at the same time??? Can't have two separate testDir. They would need separate directories to put results into located in their home directory. But say that the program had config files it had to read in-would make sense to put them in the same directory as the executable...

Seeing as I am not anywhere near that level yet to even begin thinking of these matters or working on my app I am writing no-brainers to learn how functions for the Unix environment work.

Still leaves me with the problem of determining where the program is actually located so I could read in config files... :frowning: I have used make and make install for other programs before. Can one rely on the installation directory being constant on all Unix systems should make be used? If so I'll find some stuff to read up on it. That way I could hard code the installation directory into the app... Much to learn... :expressionless: I sense some frustration in your last post, sorry if these noob questions are getting on your nerves. If you have any tutorials on say program design in the Unix environment, you can post them I'll read them to hopefully ask more sensible questions.

Cheers
Jason.

Or why not have a look and see how other applications solve problems like this on UNIX?

Let application name="foo"

UNIX apps typically do the following....

  1. check $HOME directory for directory called .foo and finds config in there, such as $HOME/.foo/foo.conf

  2. check a package directory, eg /usr/pkg/etc/foo.conf or /opt/MYfoo/etc/foo.conf

  3. check the gobals, /etc/foo.conf

  4. use sensible defaults

  5. have a wrapper script that does the setting up of environment before running the actually binary with exec, allows installation to code directory paths in a shell script outside of the binary.

NB, There is no portable way to determine the filename of a program at startup.

Those last suggestions of yours were really good. I had a look in the ~/ dir and ls -a showed a hidden directory for every program I have installed. When I checked in each they had funnily enough config files that as you said:

had package lists and sensible program defaults in them.

I think I'll copy that style of application launching.
Thanks for your patience, it has been really helpful for me.

Jason.