LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2008, 01:28 AM   #1
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Rep: Reputation: 15
Programming c++ to run commands in a particualr directory


Okay newbie to code I may be but I´m writing an IDE like program at the moment.
The problem is that I need a way to get the program to run a command that the user has
specified at the push of a button, and as you can imagine make won´t do so well when it is running in /usr/bin and the targets are in /home/$USER/generic_program.

Can somebody explain the code needed to make sure that the command is run in the right directory?

PS: if there is a KDE realted method that would be optimal.
 
Old 01-12-2008, 01:52 AM   #2
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
Is man chdir what you want?
 
Old 01-12-2008, 09:21 PM   #3
iwasapenguin
Member
 
Registered: Jul 2007
Posts: 110

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Uncle_Theodore View Post
Is man chdir what you want?
Perhaps... I´ll have to test it on my own computer (I post of a Damn Small box that doesn´t even have man!

Also, what I forgot is even a bigger matter is how to make these commands run inside of an xterm or konsole (as you can probably tell I´ve bitten of more than I can chew here).
 
Old 01-12-2008, 11:34 PM   #4
Uncle_Theodore
Member
 
Registered: Dec 2007
Location: Charleston WV, USA
Distribution: Slackware 12.2, Arch Linux Amd64
Posts: 896

Rep: Reputation: 71
It would be better if you'd specify what you want a little more precise.
What I gathered so far is that you want to call a command from inside a C or C++ program, and this command should be called from a particular directory and in a terminal. This is relatively easy. You get to the proper directory by doing chdir, you execute a command by doing popen() or exec and you specify that your command should run in a terminal by calling this terminal with the corresponding option.

For example,

Code:
#include<stdio.h>
#include <unistd.h>

int main()
{
  if(chdir("/home/kot/Desktop") != -1)
    printf("Successfully changed directory to Desktop!\n");
  else printf("Error changing directory!\n");

  if(fork())
    execl("/usr/bin/xterm", "-e", "/usr/bin/emacs", (char*)0);
  else 
    printf("Close the pop-up window first!\n");

  wait(NULL);
  
  return 0;
}
 
Old 01-13-2008, 07:12 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Uncle_Theodore View Post
This is relatively easy. You get to the proper directory by doing chdir, you execute a command by doing popen() or exec and you specify that your command should run in a terminal by calling this terminal with the corresponding option.
The canonical technique is to do child process setup (such as chdir) after the fork but before the exec. That way, you don’t mess up your own process’ state (i.e., the directory is changed in the child but left alone in the parent).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
maill of of particualr user lhrt Linux - Networking 1 06-07-2006 10:34 AM
Automating simple commands, scripting or programming? entob *BSD 3 09-01-2004 01:56 AM
Cannot run commands after su bugsbunny Linux - Newbie 5 02-07-2003 11:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration