<commit_before>#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <limits.h>

#include "path.h"
#include "util.h"

#define PARENT_PATH "../"
#define ROOT_PATH "/"
#define FILE_SEPARATOR '/'

static char *
skip_leading(char *s, char c)
{
     assert(s != NULL);

     while (*s == c)
	  s++;

     return s;
}

#define min(x,y) ((x) < (y) ? (x) : (y))

char *
relative_path(char *from, char *to)
{
     char *from_copy;
     char *to_copy;
     char buffer[_POSIX_PATH_MAX];
     int retval;

     assert(from != NULL);
     assert(to != NULL);

     from_copy = xstrdup(from);
     to_copy = xstrdup(to);

     retval = make_relative_path(from_copy, to_copy, buffer, _POSIX_PATH_MAX);

     free(to_copy);
     free(from_copy);

     return retval == 0 ? xstrdup(buffer) : NULL;
}

int
make_relative_path(char *from, char *to, char *buffer, size_t n)
{
     char *from_ctx;
     char *to_ctx;
     char *p;
     char *q;
     int depth;
     size_t remaining;

     assert(from != NULL);
     assert(to != NULL);
     assert(buffer != NULL);

     from = skip_leading(from, FILE_SEPARATOR);
     to = skip_leading(to, FILE_SEPARATOR);

     p = strtok_r(from, ROOT_PATH, &from_ctx);
     q = strtok_r(to, ROOT_PATH, &to_ctx);

     /* Skip the longest common prefix of `from' and `to' */
     while (p != NULL && q != NULL && strcmp(p, q) == 0) {
	  p = strtok_r(NULL, ROOT_PATH, &from_ctx);
	  q = strtok_r(NULL, ROOT_PATH, &to_ctx);
     }

     /*
      * The relative depth of `from' respective to `to'. The initial
      * value is always 0, except in the case of paths that do not
      * share any common parent.<commit_msg>Removed "usage before initialization" warning.<commit_after>#include <assert.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <limits.h>

#include "path.h"
#include "util.h"

#define PARENT_PATH "../"
#define ROOT_PATH "/"
#define FILE_SEPARATOR '/'

static char *
skip_leading(char *s, char c)
{
     assert(s != NULL);

     while (*s == c)
	  s++;

     return s;
}

#define min(x,y) ((x) < (y) ? (x) : (y))

char *
relative_path(char *from, char *to)
{
     char *from_copy;
     char *to_copy;
     char buffer[_POSIX_PATH_MAX];
     int retval;

     assert(from != NULL);
     assert(to != NULL);

     from_copy = xstrdup(from);
     to_copy = xstrdup(to);

     retval = make_relative_path(from_copy, to_copy, buffer, _POSIX_PATH_MAX);

     free(to_copy);
     free(from_copy);

     return retval == 0 ? xstrdup(buffer) : NULL;
}

int
make_relative_path(char *from, char *to, char *buffer, size_t n)
{
     char *from_ctx = NULL;
     char *to_ctx = NULL;
     char *p;
     char *q;
     int depth;
     size_t remaining;

     assert(from != NULL);
     assert(to != NULL);
     assert(buffer != NULL);

     from = skip_leading(from, FILE_SEPARATOR);
     to = skip_leading(to, FILE_SEPARATOR);

     p = strtok_r(from, ROOT_PATH, &from_ctx);
     q = strtok_r(to, ROOT_PATH, &to_ctx);

     /* Skip the longest common prefix of `from' and `to' */
     while (p != NULL && q != NULL && strcmp(p, q) == 0) {
	  p = strtok_r(NULL, ROOT_PATH, &from_ctx);
	  q = strtok_r(NULL, ROOT_PATH, &to_ctx);
     }

     /*
      * The relative depth of `from' respective to `to'. The initial
      * value is always 0, except in the case of paths that do not
      * share any common parent.
{'fraction_non_alphanumeric': 0.14839456175875035, 'fraction_numerical': 0.0017356089094590686, 'mean_word_length': 2.9029345372460496, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3564131', 'n_tokens_mistral': 1321, 'n_tokens_neox': 1278, 'n_words': 345}