/* pwd command path name through the stack storage, first into the stack storage, and then out of the stack output */#include <stdio.h > #include <string.h> #include <unistd.h> #include <dirent.h> #include <string.h> #include <stdlib.h> #define NUM 40/* defining Nodes */struct node { char name[num]; struct node * pre;};/ * definition Stack */struct stack { int num; struct node* top;};/ * into the stack c1 for the stack element */void push (STRUCT&NBSP;STACK*&NBSP;S1,&NBSP;CHAR&NBSP;*C1) { struct node *n; n = (struct node *) malloc (sizeof (Struct node)); if (n == null) { perror ("Malloc error"); exit (1); } strcpy (N->NAME,&NBSP;C1); if (s1->num == 0) n- >pre = null; else n- >pre = s1->top; s1->num++; s1->top =n ;} /* out stack out stack element stored in C1 */void *pop (STRUCT&NBSP;STACK&NBSP;*S1,&NBSP;CHAR*&NBSP;C1) { struct node *n; if (s1->num == 0) return NULL; n = s1->top; strcpy (c1, n->name); s1- >top = s1->top->pre; s1->num--; free (n);} /* Output path name */void output (STRUCT&NBSP;STACK&NBSP;*S1) { char name[num]; while (s1->num >0) { pop (s1, name); /* "/" in front of each pathname, this program is only stored in the root directory of the lower directory, if run under/HOME/ABC stack elements from top to bottom for abc home after output is/home/abc */ printf ("/%s", name); } printf ("\ n");} Int main (void) { dir *d; struct dirent *dir; struct stack *s1; //node Current Node inode, Fnode Save Parent Directory inode int node; int fnode; s1->num = 0; s1->top = null; if ((d = Opendir (".")) == NULL ) perror ("Open error"); while ((Dir = readdir (d) != null) if (!STRCMP (".", dir->d_name)) { node = dir->d_ino; break; } Closedir (d); while (1) { if (ChDir ("..") == -1) { perror ("ChDir error "); exit (1); &NBSP;&NBSP;&NBsp; } if ((d = Opendir (".")) == null) { perror ("Open error "); exit (1); } while (dir = readdir (d)) != null) if (Dir->d_ino == node) { push (S1, dir->d_name); break; } rewinddir (d); while ((Dir = readdir (d)) != null) { if (!STRCMP (".", dir->d_name)) node = dir->d_ino; if (!STRCMP ("..", dir->d_ Name)) fnode = dir->d_ino; } if (Node == fnode) break; closedir (d); } output (s1);}
Summarize:
1. POSIX provides a function to get the current path:
#include <unistd.h>char *getcwd (char *buf, size_t size);
2 . Current directory Issues
For example, the executable file is placed under/home/abc/123, named pwd
Current directory is/HOME/ABC
If you need to enter the file to execute: 123/pwd
And the output is/home/abd instead of/home/abc/123.
The current directory is the directory where the program is run, not where it is located.
3.
while ((Dir = readdir (d) != null) if (Dir->d_ino == node) { push (S1, dir->d_name); break; } Rewinddir (d); while ((Dir = readdir (d)) != null) { if (!strcmp ("." , dir->d_name)) node = dir->d_ino; if (!strcmp (".. ", dir->d_name)) fnode = dir->d_ino; }
There is a need to facilitate the directory two times, the beginning of the design algorithm, the two traversal was placed in the inside, which led to the node parameter changes, logic problems
Wasted a lot of time
Simple PWD command implemented under Linux