private_at_functions.c 1.63 KB
Newer Older
1 2 3 4 5
/****************************************************************************
 * Private *at functions                                                    *
 ****************************************************************************/

/**
6 7
 * @file private_at_functions.c
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
8 9 10 11
 * @date 15 June 2015
 * @brief Private replacement functions for *at functions.
 */

12 13 14 15 16 17

#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
18 19
#include <stdlib.h>
#include <dirent.h>
20 21

#include "private_at_functions.h"
22 23 24 25
#include "obidebug.h"
#include "obierrno.h"


26
#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)
27 28


29
char* get_full_path(int directory_file_descriptor, const char* path_name)
30
{
31
	char* full_path;
32

33 34
	full_path = (char*) malloc((MAX_PATH_LEN)*sizeof(char));
	if (full_path == NULL)
35 36
	{
		obidebug(1, "\nError allocating memory for the char* path to a file or directory");
37
		return NULL;
38
	}
39

40
	if (fcntl(directory_file_descriptor, F_GETPATH, full_path) < 0)
41 42
	{
		obidebug(1, "\nError getting the path to a file or directory");
43
		return NULL;
44
	}
45

46
	// TODO check errors?
47 48
	strlcat(full_path, "/", MAX_PATH_LEN);
	strlcat(full_path, path_name, MAX_PATH_LEN);
49 50 51 52 53

	return full_path;
}


54
DIR* private_opendirat(int directory_file_descriptor, const char* path_name)
55
{
56 57
	char* full_path;
	DIR* directory;
58 59 60

	full_path = get_full_path(directory_file_descriptor, path_name);
	if (full_path == NULL)
61
		return NULL;
62

63
	directory = opendir(full_path);
64 65
	if (directory == NULL)
		obidebug(1, "\nError opening a directory");
66

67 68 69
	free(full_path);

	return directory;
70
}
71

72