- 论坛徽章:
- 0
|
程序如下:
/* pw1_30.c an implementation of the 'pwd' utility
* features:
* usage:
* building:
*/
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string.h>
#define RLEN 32
#define CLEN 256
static int i=0;
char dirname_arr[RLEN][CLEN]={0};
void do_pwd()
{
struct stat stat_st;
stat(".",&stat_st);
DIR * dir_p=opendir(".." ;
struct dirent * dirent_pst;
ino_t temp1=0,temp2=1;
char tempname_arr[CLEN];
int flag=0;
for(;
{
dirent_pst=readdir(dir_p);
if(strcmp(dirent_pst->d_name,"." ==0)temp1=dirent_pst->d_ino;
if(strcmp(dirent_pst->d_name,".." ==0)
{
temp2=dirent_pst->d_ino;
strcpy(tempname_arr,dirent_pst->d_name);
flag=1;
}
if((dirent_pst->d_ino==stat_st.st_ino)&&(flag==1))
{
//dirname_arr[0]=dirent_pst->d_name;
free(&stat_st);
i++;
//printf("i=%d\n",i);
sprintf(dirname_arr[i],"/%s",dirent_pst->d_name);
//printf("path:%s\n",dirname_arr[i]);
chdir(tempname_arr);
free(dirent_pst);
do_pwd();
}
if(temp1==temp2)
{
for(;i>=0;i--)
{
printf("%s",dirname_arr[i]);
}
printf("\n" ;
exit(0);
}
}
}
int main()
{
do_pwd();
return 0;
}
|
|