/*
 * SPDX-FileCopyrightText: 2026 Didier Kryn <kryn@in2p3.fr>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
/* concatenate directory name and file name and canonicalize the result */
/* This does the opposite of dirname() and basename() and it produces an
   absolute pathname. The resulting pathname is stored in buf, including the
   terminating '\0'. In case of success, the returned value points to buf.
   In case of failure, NULL is returned and errno is set apropriately. The
   resulting path is stored in the array of PATH_MAX characters pointed to by
   buf. If this array is smaller than PATH_MAX, the behaviour is undefined.
   If the resulting file does not exist, NULL is returned. */

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "slash_sys.h"

char *canonpath(const char *dname, const char *fname, char *buf)
{
  size_t bsize, ld, lf;
  char *c;
  
  ld = strlen(dname);
  lf = strlen(fname);
  bsize = ld+lf+2;
  {
    char buf1[bsize]; /* dynamically allocate buf1 */
    strcpy(buf1, dname);
    buf1[ld] = '/';
    strcpy(buf1+ld+1, fname);
    c = realpath(buf1, buf);
  }
  return c;
}
