Hi there,
I am trying to create a command line utility that allows me to change directories in a terminal efficiently. Basically I want to be able to programmatically set the current working directory in Terminal.app (using either c++ or objective-c++).
The following code (together with provided output) shows an example of the problem I am experiencing:
And this output:
It appears that the current working directory is being changed successfully within the scope of the application, however the current working directory of Terminal.app does not change (as is apparent by the output of pwd after execution of the application).
I have tried both of the shown implementations within setCWD, (ie traditional UNIX way and the Cocoa way) yet both exhibit this same behaviour.
Does anyone have any idea why this is happening?
Does anyone know how I can solve this issue?
Help would be much appreciated.
Thanks in advance!
I am trying to create a command line utility that allows me to change directories in a terminal efficiently. Basically I want to be able to programmatically set the current working directory in Terminal.app (using either c++ or objective-c++).
The following code (together with provided output) shows an example of the problem I am experiencing:
Code:
#include <iostream>
#include <string>
using namespace std;
#define COLOR_RED "\E[0;31;48m"
#define COLOR_BLACK "\E[0;30;48m"
#define PERROR() fprintf(stderr," %sERROR:%s ", COLOR_RED, COLOR_BLACK)
#define D_ERROR( e, msg, ... ) do{ PERROR(); fprintf( stderr, msg , ## __VA_ARGS__); fprintf(stderr,"\n"); exit(e); } while(0)
//#define _SOL1
string getCWD() {
char result[FILENAME_MAX];
if( !getcwd( result, sizeof(result) ) )
D_ERROR( -1, "Could not get current working directory.\n" );
return string( result, (strlen(result) > 0) ? strlen(result) : 0 );
}
int setCWD( const char *path ) {
#ifdef _SOL1
BOOL success;
NSFileManager *fm = [NSFileManager defaultManager];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *nsPath = [NSString stringWithCString:path encoding:[NSString defaultCStringEncoding]];
success = [fm changeCurrentDirectoryPath: nsPath];
[pool release];
if( success == YES ) return 0;
#else
if( !chdir( path ) ) return 0;
#endif
return -1;
}
int main( int argc, char **argv ) {
fprintf( stdout, " Current Directory: %s\n", getCWD().c_str() );
fprintf( stdout, " Enter Desired CWD: " );
string dir;
getline( cin, dir );
int e = setCWD( dir.c_str() );
if( e == 0 ) {
fprintf( stdout, " Current Directory: %s\n", getCWD().c_str() );
return 0;
}
fprintf( stderr, "Could not change cwd to %s.\n", dir.c_str() );
return -1;
}
And this output:
Code:
birchy-mac: pwd
/Users/birchy/Dev/cwd
birchy-mac: ./cwd.out
Current Directory: /Users/birchy/Dev/cwd
Enter Desired CWD: /Users/
Current Directory: /Users
birchy-mac: pwd
/Users/birchy/Dev/cwd
birchy-mac:
It appears that the current working directory is being changed successfully within the scope of the application, however the current working directory of Terminal.app does not change (as is apparent by the output of pwd after execution of the application).
I have tried both of the shown implementations within setCWD, (ie traditional UNIX way and the Cocoa way) yet both exhibit this same behaviour.
Does anyone have any idea why this is happening?
Does anyone know how I can solve this issue?
Help would be much appreciated.
Thanks in advance!