Help Required!!!!!

This is a discussion on Help Required!!!!! within the C Programming forums, part of the General Programming Boards category; Right, anyone interested in helping, i have a small problem! ...I know that the code isn't that great, it hasn't ...

Help Required!!!!!

Right, anyone interested in helping, i have a small problem! ...I know that the code isn't that great, it hasn't yet been tuned up, its just in a working state! ...For example the dynamic 2d array will be converted to a malloc memory space!!!! but for the time being all that is ok!!!!

These functions return information about the specified file. You do not need any access rights to the file to get this information but you need search rights to all directories named in the path leading to the file.

Have a look what happens if you run your program as root or examine errno after stat fails.
Kurt

Thanks for the help kurt, but if you look carefully enough, you will have seen that the program was being run by user root...

Code:

[root@localhost shell]

But thanks anyway!!!!

....I think the problem is that when using -l switch, the program is trying to access the files / directories that are situated in the current working directory, which explains why when you use -al, you can see the attributes for . and .. but not for any of the other files as they don't exist in the current working directory, but the files . and .. do!

OK!!!! ...Done that, but any suggestions as to how to set the switches??? ...I can't see anything wrong with setting them like this! ...I have looked at many examples and they do it the same way! ...What would you suggest to get rid of the warnings??? Cheers!

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

Awesomeo!!! thanks kurt!!! ...I did use that to begin with, but it wasn't setting the bits correct, but i have changed the code to pass the int containing the flags as a parameter between functions rather than keep it as a very bad and horrible global variable!!!!!

And you might want to save strlen()'s return value so you don't have to call it twice.

...I don't really understand what you mean by this! ...I call strlen twice because if the user is using the -a switch, they wish to display all files & directories, therefore, if the file/dir name starts with a leading . the program must display it, if the user is not using switch -a, the program ignores these files/dirs!

Therefore, in the case that the user wishes to display all files/dirs, the program must find the length of the longest filename including the file/dirs that begin with a leading zero! If the user does not wish to list all files/dirs in the directory, the program ignores any file with a leading . therefore, eliminating the need to count the length of that file/dir, as it will not be displayed by the program!!!!

An example, the directory test contains the following files:

Code:

test.c
test.txt
.elephant.txt
tiger.jpg

Now, if you wish to display the contents of this directory, say using the ls command with no switches, the output will be:

Code:

test.c
test.txt
tiger.jpg

However, if you wish to display all files in this directory using ls -a, the output will be:

Code:

.
..
test.c
test.txt
tiger.jpg
.elephant.txt

Now, if the user is not requesting to use the switch -a, it means that the program will not display the file .elephant.txt (which also happens to have the longest name out of all files/dirs contained in the directory) Therefore, the program does not need to count the number of chars in this string, if it did, the array that contains file/dir names would be unecessarily bigger than the largest filename within it...

The code is tidier, but i am not sure if it is any more efficient??? ...as strlen will still only be called once for every loop! ...I think i will use your idea though because the layout is a lot easier to follow!!!! Thanks a lot buddy!!!!!

Was being a bit silly here!!!! ...I realised that i wasn't actually changing to the directory that i wanted to list when calling stat(), therefore, I have now added a new parameter to the addDataToArray function which is the path of where the directory is who's contents require displaying!

So now it changes to the directory to list files from and then stats the files, displaying full file attributes for each file!!!!!!