0

Here is my C program (mycd):

#include <stdio.h>

int main(int ac, char * av[]){
    char * dir;
    int t;
    if (ac < 2){
        dir = getenv("HOME");
        if (dir == 0)
            dir = "/tmp";
    } else if (ac > 2){
        fprintf(stderr, "usage: %s [dir]\n", av[0]);
        return 1;
    } else
        dir = av[1];
    t = chdir(dir);
    if (t < 0){
        perror(dir);
        return 1;
    }
    return 0;
}

So after run when i type in shell o in bash ./mycd i get nothing.

Why ?

Thanks in advance.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Yoan15
  • 33
  • 2
  • 5
  • 1
    Is this a full code? Where are the includes? Is it compiling without errors/warnings? – Eugene Sh. Nov 25 '15 at 17:56
  • 3
    You cannot change the working directory from another process. That's why `cd` is a shell builtin, not an external command. – chepner Nov 25 '15 at 17:59
  • You might want to check your if statements involving the number of arguments. Try running your code with two sets of characters after it separated by spaces. for example: `/mycd something whatever` – Mike -- No longer here Nov 25 '15 at 18:04
  • It is compiling without errors. It's a correct program. I'd like just to know why i get nothing when i type it in bash o in shell – Yoan15 Nov 25 '15 at 18:04
  • `ac` will be `1` so `dir` will point to the value of the `HOME` environment variable or will be `"/tmp"`. Both of those are likely to succeed when passed to `chdir`, so that's why `chdir` isn't returning `-1`, so `perror` is not called. – Ian Abbott Nov 25 '15 at 18:05
  • @Yoan15 What did you expect it to do? – Ian Abbott Nov 25 '15 at 18:12
  • okay Ian Abbott, thank you for your answer but i'd like to make an internal command mycd whith output – Yoan15 Nov 25 '15 at 18:12
  • How i can do an internal command mycd with output in shell o bash ? – Yoan15 Nov 25 '15 at 18:15
  • Now it's ok. Thank you ;) – Yoan15 Nov 25 '15 at 18:29
  • Why when i try to put in shell "./mycd something", i have this output something : No such file or directory ? – Yoan15 Nov 25 '15 at 19:33

0 Answers0