cchdir

Why is this C program doing nothing in Ubuntu?


My very simple C program just hangs and I don’t know why.

I am trying to make a simple executable to handle multiple monotonous actions for me every time I start a new programming session.

So I decided with something simple (below) yet every time I run it, the app just hangs, never returns. So I have to Ctrl-C out of it. I have added printf commands to see if it goes anywhere, but those never appear.

My build command returns no error messages:

gcc -o tail tail.c

Just curious what I am missing.

#include <stdio.h>
#include <unistd.h>
int main() {

    chdir("\\var\\www");
    return 0;
}

Solution

  • The other answers adequately cover the issues in your C code. However, the reason you are seeing it hang is because you chose the name tail for your program.

    In Linux, tail is a command in /usr/bin in most setups, and if you just type tail at the command line, the shell searches the $PATH first, and runs this. Without any parameters, it waits for input on its stdin. You can end it by pressing control-d to mark the end of file.

    You can bypass the $PATH lookup by typing ./tail instead.

    $ tail
    [system tail]
    $ ./tail
    [tail in your current directory]
    

    It is a good idea to use ./ as a habit, but you can also avoid confusion by not naming your program the same as common commands. Another name to avoid is test which is a shell built-in for testing various aspects of files, but appears to do nothing as it reports results in its system return code.