Programming
PWD in C

Question Description

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.


Final Answer

#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

UCLA

Anonymous
Thanks for the help.

Anonymous
Outstanding. Studypool always delivers quality work.

Anonymous
Tutor was very helpful and took the time to explain concepts to me. Very responsive, managed to get replies within the hour.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors