1
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>


void creatEnv();

char *mygetcwd(char *buf, size_t size) {
  DIR  *dirp_p, *dirp_c;
  struct dirent *dp; 
  ino_t cwd_inode;
  dirp_c = opendir("../../.."); //open current working directory
  dirp_p = opendir("../.."); //open parent directory
  printf("\n");

  /* find inode of current working directory */
  printf("--below this,list of runner\n");
  while((dp = readdir(dirp_c))!= NULL){
    printf("fileN : %s  inode : %lu\n",dp->d_name,dp->d_ino);
  }
  printf("\n--below this,list of as3\n");
   while((dp = readdir(dirp_p))!= NULL){
    printf("fileN : %s  inode : %lu\n",dp->d_name,dp->d_ino);
  }
  return buf;
}

int main(void) {
  pid_t pid;
  int status;
  char buf[255];

  creatEnv();
  chdir("dir/sub");

  printf("original func: %s\n", getcwd(NULL, 0));
  printf("%s\n", mygetcwd(buf, 255));

  return 0;
}

void creatEnv(){
  mkdir("dir", 0755);
  mkdir("dir/sub", 0755);
  mkdir("dir/sub2", 0);

  creat("dir/a", 0755);
  creat("dir/b", 0755);
  creat("dir/sub/x", 0755);
  symlink("dir/a", "dir/sl");
  symlink("dir/x", "dir/dsl");
}

Result is weird. at first part of a picture below, as3 inode is 1024015,

enter image description here

but at second part, "." inode(this have to be same as as3 inode) is 256
and for some reason ".." inode is 256.
Why does this happen? I cannot find reason.
Only btw these two dirctories, The weird result is evoked.
In other directory, It works quite well.
I compiled and ran this code on repl.it!

lurker
  • 56,987
  • 9
  • 69
  • 103
ben kwon
  • 61
  • 4

0 Answers0