skip to Main Content

I had to write a program for Uni which prints some info about a file, or files in a directory recursively.

The output of the file when called like
./listfiles xxx listfiles.c listfiles-example-dir /dev/tty
is:

xxx: No such file or directory (errno 2)
listfiles.c (regular, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
example-subdir (directory)

listfiles-example-dir/example-subdir:
example.txt (regular, 18 Byte)

which is perfectly correct and as expected.

BUT when redirected to a file with
./listfiles xxx listfiles.c listfiles-example-dir /dev/tty > out.txt 2>&1
the out.txt contains the following:

xxx: No such file or directory (errno 2)
listfiles.c (regular, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
e-dir:
-subdir (directory)

listfiles-example-dir/e-dir:
e-dir:
-subdir (directory)

listfiles-example-dir/:
example.txt (regular, 18 Byte)

How and why is this happening?

Im currently running openSUSE Tumbleweed, but I’ve also tried openSUSE on WLS which gives the correct output, but when redirected to a file looks like:

xxx: No such file or directory (errno 2)
listfiles.c (regular, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
other)

listfiles-example-dir:
����� (directory)

listfiles-example-dir/other)

listfiles-example-dir:
other)

listfiles-example-dir:
����� (directory)

listfiles-example-dir/:
example.txt (regular, 18 Byte)

and Ubuntu on WSL gives following output:

xxx: No such file or directory (errno 2)
listfiles.c (regular, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
ir:
227 Byte)
 (directory)

listfiles-example-dir/ir/
227 Byte)
:
example.txt (regular, 18 Byte)

and it’s out.txt look even worse

xxx: No such file or directory (errno 2)
listfiles.c (regular, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
r, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
����� (directory)

listfiles-example-dir/r, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
r, 1227 Byte)
/dev/tty (other)

listfiles-example-dir:
����� (directory)

listfiles-example-dir/:
example.txt (regular, 18 Byte)

You can find my code at https://github.com/FabiSahne/SysProg/tree/main/aufgabe4 for anyone to critique, but I’m mostly here on why the output would change so much just by redirecting it to a file.

2

Answers


  1. Chosen as BEST ANSWER

    Ok, so turns out, it was an issue with my code after all. Props to my professor for finding the mistake.

    I used a char* to store the name of the file/directory, whereby I (for unknown reasons) assumed strcpy would allocate memory for that. Obviously it doesn't, why would it, and so very weird things - a.k.a. undefined behavior - happened, when trying to print those names.

    Changing this variable to char[] solved all my issues. I probably could've also just allocated memory myself, but the array was actually the solution intended by my prof.

    Still, very weird, that the behavior would change that much, just by redirecting output.


  2. It looks like you are overwriting the elements of the argument vector.

    You create a fileinfo for each argument:

    fileinfo* fileinfo_create(char* path);
    fileinfo_create(argv[i]);
    

    Then you concatenate strings to it:

    strcat(path, "/*");
    

    This overwrites elements of the argument vector since they may be placed contiguously in memory. You should allocate memory and copy the strings to the new location.

    Also, the * is called a glob and it is a feature of your shell. You cannot use it to recursively enumerate directories in C unless you implement it yourself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search