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 05-02-2009, 01:17 AM   #1
nikh.jas
LQ Newbie
 
Registered: May 2009
Posts: 1

Rep: Reputation: 0
Help req on CD command...... to be implemented in C


I am facing some problem in implementing CD command using C language.
This a small code that I have written but it is not changing the directory....

please suggest something


#include <unistd.h>
#include<stdio.h>
int main(int argc, char** argv)
{
if (argc == 1) // no args
{
const char* home = getenv("HOME");
chdir(home ? home : "."); // in case HOME is not defined :-)
}
else // arg given (could it be a path?)
{
}
char *directory = "/tr02/mar09ft1/t401579/nik";
int ret;

ret = chdir (directory);
if (ret ==0) // just to check if chdir() is working or not//
printf("good");
else
printf("bad");
}
 
Old 05-04-2009, 04:10 AM   #2
rylan76
Senior Member
 
Registered: Apr 2004
Location: Potchefstroom, South Africa
Distribution: Fedora 17 - 3.3.4-5.fc17.x86_64
Posts: 1,552

Rep: Reputation: 103Reputation: 103
Hmm why are you trying to do this?

I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.

In a C programming running in a terminal emulator or from the command prompt, the "pwd" directory should be the directory that the executable started up in, right? So if you need files in sub-directories, or files from elsewhere on the filing system, why not just use absolute paths?

Then there is no logical need to "change directories"... or am I completely misunderstanding you?? (I Suspect the latter...!)

Last edited by rylan76; 05-04-2009 at 04:25 AM.
 
Old 05-04-2009, 05:48 AM   #3
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by rylan76 View Post
I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.
Not so. The environment variable PWD is a mere convenience, and is not necessarily accurate. If you run the following bash script:
Code:
cat > 1.c <<EOD; gcc -Wall 1.c -o 1; ./1
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
where(void)
{
  char buffer[1024];

  if(getcwd(buffer,sizeof(buffer))==NULL)
  {
    abort();
  }

  printf("current directory is      %s\n",
         buffer
        );

  printf("environment variable says %s\n",
         getenv("PWD")
        );

} /* where() */

int
main(void)
{
  system("rm -rf subdirectory");

  if(mkdir("subdirectory",0700))
  {
    abort();
  }

  printf("before changing directory:\n");

  where();

  if(chdir("subdirectory"))
  {
    abort();
  }

  printf("after  changing directory:\n");

  where();

  return 0;

} /* main() */
you should get output something like this:
Code:
before changing directory:
current directory is      /u/wally/monday/2
environment variable says /u/wally/monday/2
after  changing directory:
current directory is      /u/wally/monday/2/subdirectory
environment variable says /u/wally/monday/2
I wrote a C application which I run all the time which changes the current directory; whether to do so is a matter of preference.

The directory is changed for the current process (the one running the C program) and for any descendants of that process (typically started with a fork() call). But it is not changed for the parent process. When you're sitting at the shell prompt, you are running a particular process. When you run a C program, that's a child of this process. So running the C program will change the current directory for its own process, (if you have code in the C program to do that), but the current directory will retain its old value when you return the the shell prompt.
 
Old 05-04-2009, 09:56 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by wje_lq View Post
Not so. The environment variable PWD is a mere convenience, and is not necessarily accurate.
Well it's accurate inside shell scripts:
Code:
$ man bash
...
   Shell Variables
       The following variables are set by the shell:
...
       PWD    The current working directory as set by the cd command.
 
Old 05-04-2009, 03:02 PM   #5
dave.donaghy
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
When chdir fails, it will set errno (man chdir will give full details).

As well as giving useful info if you're just learning about this kind of stuff, errno can also play a useful part in working out how to handle errors: for example, your code may well do different stuff if chdir returns ENOENT or if it returns ENOMEM.

To find out what errno is, I recommend including <errno.h> and <string.h>, and printing out strerror(errno), which is a text string representing the error.

My guess is that you'll get ENOENT, which will come out as text as "No such file or directory"; that will just mean that the directory you're trying to change into doesn't exist.
 
Old 05-05-2009, 08:32 AM   #6
dave.donaghy
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by rylan76 View Post
Hmm why are you trying to do this?

I.e. as far as I know the directory you are currently "in" is something that is held in the terminal environment, i. e. inside BASH or CSH.

In a C programming running in a terminal emulator or from the command prompt, the "pwd" directory should be the directory that the executable started up in, right? So if you need files in sub-directories, or files from elsewhere on the filing system, why not just use absolute paths?

Then there is no logical need to "change directories"... or am I completely misunderstanding you?? (I Suspect the latter...!)
This isn't really the whole truth ...

Rather, a process (not a terminal, or a shell, ...) has a current directory. There could be any number of reasons why a process might want to change directory, and subject to a certain number of practical criteria (see below) this should always be possible.

The practical critera are pretty much that you can only change directory to a directory that exists and is useable by you. (I've glossed over a bit of detail here, but see the chdir man/info page, for example, for more details.)

I think rylan76 has missed quite a lot of the underlying issue here, and a quick reading of his post might suggest that only terminals and shells have current directories: the truth is very different from that.
 
Old 05-07-2009, 05:58 AM   #7
amysaraantony
Member
 
Registered: Apr 2009
Posts: 42

Rep: Reputation: 16
Googling for this gave me tons of results and some code ... you should try it too !


Debian

Last edited by amysaraantony; 05-15-2009 at 08:17 PM.
 
  


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
help req in chown command vishnu Solaris / OpenSolaris 1 07-21-2006 07:05 AM
nslookup, command not implemented? digimars Linux - Networking 2 11-09-2005 06:59 PM
502 CHMOD command not implemented. dsnow Linux - Newbie 1 03-31-2002 01:04 AM
502 CHMOD command not implemented? jobesd Linux - General 2 11-20-2001 08:46 PM

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

All times are GMT -5. The time now is 05:30 AM.

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