40 lines
673 B
C
40 lines
673 B
C
|
#ifndef _MAKE_PATH_H
|
||
|
#define _MAKE_PATH_H
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
|
||
|
int flag = 0;
|
||
|
|
||
|
if (src == NULL) {
|
||
|
flag = 1;
|
||
|
src = "";
|
||
|
}
|
||
|
|
||
|
if (dst == NULL) {
|
||
|
flag = 1;
|
||
|
dst = "";
|
||
|
}
|
||
|
|
||
|
size_t len = strlen(src) + strlen(dst) + 2;
|
||
|
char *full_path = malloc(len + 1);
|
||
|
if (full_path == NULL) {
|
||
|
if (prog_name != NULL)
|
||
|
fprintf(stderr, "%s: malloc() failed\n", prog_name);
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
if (flag || src[strlen(src) - 1] == '/')
|
||
|
snprintf(full_path, len, "%s%s", src, dst);
|
||
|
|
||
|
else
|
||
|
snprintf(full_path, len, "%s/%s", src, dst);
|
||
|
|
||
|
return full_path;
|
||
|
}
|
||
|
|
||
|
#endif
|