mikeizbicki / ucr-cs100

open source software construction course
Other
485 stars 407 forks source link

Hw#1 Home Directory and Absolute Paths #353

Closed jesuscreyes closed 9 years ago

jesuscreyes commented 9 years ago

Normally, when you enter ls ~, you get an output of all of the files in your home directory. But when I do:

opendir("~"),

opendir returns an error. Is this a case that our ls.cpp needs to be able to be able to do successfully? If so, is there a specific way of handling this case?

Also, say my current directory includes a directory called test. And this test directory includes a file called test.cpp. If I were to enter:

ls test/test.cpp

test.cpp is outputted. But similarly, opendir("test/test.cpp") returns an error. Is this also a case that our ls.cpp needs to be able to do? And if so, is there a specific method to handle this case?

Thanks!

mikeizbicki commented 9 years ago

Question 1: You do not have to handle paths containing the ~ character. These are a special feature of bash. In particular, anytime bash sees a ~ at the start of a path, it automatically replaces it with your home directory. Maybe the best way to see this is with an example. If you create a file called test.cpp that contains the following:

#include <iostream>
using namespace std;

int main(int argc, char**argv) {
    cout << argv[1] << endl;
    return 0;
}

Then type these commands in bash:

$ g++ test.cpp
$ ./a.out ~
/home/user

Notice that the output of a.out is my home folder. That's because bash has performed the substitution of the ~ before passing the variables on to the program. If we ran a.out from your rshell program, it would print ~ instead because rshell doesn't do the substitution. tldr: ~ is handled by the shell, not your ls program; don't do anything special in that case.

Question 2: The reason opendir is failing on test/test.cpp is because that is a regular file and not a directory. It makes sense that opendir would not succeed. If opendir fails, you must check whether there is a regular file in the location. If there is, then continue like normal.

PS. These are good questions, and you asked them in a particularly clear and concise way that made it easy to answer. I'm giving you 2 points extra credit just for that.

jesuscreyes commented 9 years ago

Thanks for the in depth response Mike!