The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How ls | wc -l works? krishmaths UNIX for Dummies Questions & Answers 3 04-11-2008 01:16 PM
Works with FTP but not NFS pbonilla UNIX for Dummies Questions & Answers 1 01-08-2008 02:31 PM
How the for loop works? neelaksh UNIX for Dummies Questions & Answers 6 02-04-2007 10:42 AM
Code works sometimes and sometimes not hp-aix-lnx-user Shell Programming and Scripting 2 04-06-2006 08:43 AM
how sendmail works alzep UNIX for Dummies Questions & Answers 3 02-18-2004 01:33 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 04-21-2007
Registered User
 

Join Date: Apr 2007
Location: Melbourne, Victoria Australia
Posts: 5
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.
Reply With Quote
Forum Sponsor
  #2  
Old 04-21-2007
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,616
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.
Reply With Quote
  #3  
Old 04-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
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); }
Reply With Quote
  #4  
Old 04-22-2007
Registered User
 

Join Date: Apr 2007
Location: Melbourne, Victoria Australia
Posts: 5
Quote:
My guess would be that it worked but you don't know where it worked. Or that it failed because testDir already exists.
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 ~/ !!!


Quote:
try "perror()"
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... :-) 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.
Reply With Quote
  #5  
Old 04-22-2007
Registered User
 

Join Date: Apr 2007
Location: Melbourne, Victoria Australia
Posts: 5
Quote:
You are susposed to check the return code from a system call
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 ~/
Reply With Quote
  #6  
Old 04-22-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by jasonkb
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?
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 ?
Reply With Quote
  #7  
Old 04-22-2007
Registered User
 

Join Date: Apr 2007
Location: Melbourne, Victoria Australia
Posts: 5
Quote:
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?
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... :-( 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... :-| 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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0