Пример #1
0
// Prints the descendants of 'cat' down to 'max_depth'.
void query_descendants(CatID cat, int max_depth) {
  cat = getCat(cat);

  mark_descendants(cat, max_depth, 0);
  print_descendants(cat, cat, max_depth, 0);
  clear_descendants(cat, max_depth, 0);
}
int main(int argc, char **argv) {
  struct node* root;

  print_descendants(argv[1], &root);

  printCopies(root);

  return EXIT_SUCCESS;
}
void print_descendants(char *pathname, struct node** rt) {
  if (is_dir(pathname)) {
    DIR *d;
    struct dirent *p;
    char path[MAX_PATH_LENGTH];

    char *hashStr;
    char *pathStr;
    struct node* temp;
    
    if ((d = opendir(pathname)) == NULL){
      fprintf(stderr, "opendir %s  %s\n", path, strerror(errno));
      return;
    } 

    while ((p = readdir(d)) != NULL) {
      if (strcmp(".", p->d_name)==0 || /* skip "." and ".." */
	  strcmp("..", p->d_name)==0 || strcmp(".git", p->d_name)==0)
	continue;
     

      //printf("%s\n", p->d_name);

      snprintf(path, MAX_PATH_LENGTH, "%s/%s", pathname, p->d_name);

      if(!is_dir(path)) {
      // Malloc hashStr
      if ((hashStr = (char *)malloc(SHA_DIGEST_LENGTH*2+1)) == NULL)
      {
        printf("MALLOC hashStr Error\n");
        exit(1);
      }

      getHash(path, hashStr);

      // Malloc pathStr
      if ((pathStr = (char *)malloc(MAX_PATH_LENGTH)) == NULL)
      {
        printf("MALLOC pathStr Error\n");
        exit(1);
      }

      pathStr = path;

      temp = createNode(hashStr, pathStr);
      
      //printf("temp->filename: %s\n", temp->fileName);
      insert(temp, rt);
      //printf("temp->copy after insert: %d\n", temp->copy);
      }

      print_descendants(path, rt);
    }
    closedir(d);
  }
}
Пример #4
0
// Prints the descendants of 'curr'.
void print_descendants(CatID root, CatID curr, int max_depth, int curr_depth) {
  if (curr == NIL) return;
  else if (curr_depth > max_depth) return;
  else {
    // print self
    if (curr_depth == cats[curr].counter) {
      printf("At generation %d, ", curr_depth);
      print_relationship(curr, curr_depth, root, 0);

      // ensure we don't print this cat again
      cats[curr].counter = COUNTER_SENTINEL;
    }

    // recur on children by looping though the eldest's siblings
    for (CatID child = cats[curr].eldest_child;
         child != NIL; child = get_sibling(child, is_male(curr))) {
      print_descendants(root, child, max_depth, curr_depth + 1);
    }
  }
}