PWD in C

Programming
Tutor: None Selected Time limit: 3 Days

way to make pwd version in C. But not sure how to approach it in C. The idea is first, call lstat() on "." to find out the inode number of the current directory (the "st_ino" member of the struct stat).

Then I change our current directory to the parent directory, with chdir("..").

Then I want to read the parent directory with opendir()/readdir() (using "opendir(".")" because we've now cd'd there),I am mostly interested in the d_ino value in the struct returned by readdir(). I am looking for the original directory in the parent directory. When we find it, we print its name.

Then call chdir("..") again, and repeat until the inode number is 2, at which point you can print "and then we're at the root directory" (or some such similar message). The idea is great :) Any ideas? Thanks.


Nov 5th, 2013
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
int main()
{
//char buf[1024];

char * buf;
char * cwd;
buf = (char *)malloc(sizeof(char) * 1024);

if((cwd = getcwd(buf, sizeof(buf))) != NULL)
printf("pwd : %s\n", cwd);
else
perror("getcwd() error : ");
return 0;
}
But with pointer it shows following error

getcwd() error : : Numerical result out of range
Nov 6th, 2013

Studypool's Notebank makes it easy to buy and sell old notes, study guides, reviews, etc.
Click to visit
The Notebank
...
Nov 5th, 2013
...
Nov 5th, 2013
Dec 5th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer