summaryrefslogtreecommitdiffstats
path: root/bio/vaje/1/programi/fopenmkdir.c
blob: afca4f1f223611b8184ed28db25c5aac0f261315 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include <mkdirp.c>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
FILE * fopen_mkdir(char* filename, char* mode) {
	FILE * file_to_return;
	file_to_return = fopen(filename, mode);
	if(file_to_return == NULL) {
		char folder_of_file[PATH_MAX];
		strcpy(folder_of_file, filename);
		char * p;
		p = strrchr(folder_of_file, '/');
		if (!p) {
			printf("[fopenmkdir.c] filename contains no slash.\n");
			return NULL;
		} else {
			//  printf("folder is %s\n", folder_of_file);
			// printf("filename is %s\n", filename);
			*p = '\0';
		}
		if(mkdir_p(folder_of_file) != 0) {
			return NULL;
		}
		file_to_return = fopen(filename, mode);
		if(file_to_return == NULL) {
			return NULL;
		}
		return file_to_return;
	} else {
		return file_to_return;
	}
}