Need help in displaying the relative path in C
Hi. I was wondering if somebody can help me on this. I am writing a C program that finds a file and display its relative path if the file is found... I am not sure how I going to display the relative path.. Any suggestions?
int main (int argc, char * argv[]) { int fileDesc; struct stat buf; if ((fileDesc = open(argv[1], O_RDONLY)) < 0) { printf ("%s: %s -- File does not exist.\n",argv[0], argv[1]); return(1); } fstat (fileDesc, & buf); printf ("filename : %s \n", argv[1]); printf ("relative path : \n"); printf ("Protection Mode : %o ", buf.st_mode); printf ("Size : %ld\n", buf.st_size); close (fileDesc); return (0); } |
Three comments.
Hope this helps. |
You are attempting to duplicate the functionality of the find command, plus a minor wrinkle. If you are doing this for a reason other than as a learning exercise, I suggest you use find, launched as a child process with its output captured in a pipe(). From there, it should be a simple exercise in string manipulation to compare the result returned by find (plus any starting path) with the absolute path against which you are establishing relativity.
--- rod. |
The question makes no sense. Paths are, by definition, relative unless they are absolute (in which case, they can't be relative...).
|
Paths can be equally either relative or absolute. The usual reference against which a path can be relative is the current working directory ($PWD), although technically, that needn't be so. For example, I can put in my PATH environment variable, "../../Some/Other/Directory", which would perhaps make sense when $PWD was appropriate, and may make sense at multiple different locations within a filesystem.
It is also unusual to translate an absolute to a path relative to anything, even $PWD, while the reverse translation would be more common. To me, it is a bit like saying "the answer is 7, but what is that, relative to, say, 3?". Sort of 'unsimplifying' an absolute. To apply a different metaphor, on the other hand, it can also be a bit like saying "How do I get there from here (.), rather that from some other place (/)?". It all depends on the circumstance. --- rod. |
All times are GMT -5. The time now is 04:36 PM. |