cwk_path_normalize

(since v1.0.0)
Creates a normalized version of the path.

Description

size_t cwk_path_normalize(const char *path, char *buffer, size_t buffer_size);

This function creates a normalized version of the path within the specified buffer. This function will not write out more than the specified buffer can contain. However, the generated string is always null-terminated - even if not the whole path is written out. The returned value is the amount of characters which the resulting path would take if it was not truncated (excluding the null-terminating character). The path may be the same memory address as the buffer.

The following will be true for the normalized path:

Parameters

Return Value

The size which the complete normalized path has if it was not truncated.

Outcomes

Input Output
/var /var
/var/logs/test/../../ /var
/var/logs/test/../../../../../../ /
rel/../../ ..
/var////logs//test/ /var/logs/test
/var/././././ /var
/var/./logs/.//test/..//..////// /var

Example

#include <cwalk.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char result[FILENAME_MAX];

  // The following function cleans up the input path and writes it
  // to the result buffer.
  cwk_path_normalize("/var/log/weird/////path/.././..///", result,
    sizeof(result));
  
  printf("%s\n", result);
  return EXIT_SUCCESS;
}

Output:

/var/log

Changelog

Version Description
v1.2.7 Fixed windows paths with forward slashes in root.
v1.0.0 The function is introduced.