summaryrefslogtreecommitdiff
path: root/C/final-project-z2h/src/file.c
blob: fcdb036fc1e51516a90a37249e4c5dedd286fdcc (plain)
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
35
36
37
38
39
#include <stdio.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "file.h"
#include "common.h"


int create_db_file(char *filename) {
	int fd = open(filename, O_RDWR);
	if (fd != -1) {
		close(fd);
		printf("File already exists\n");
		return STATUS_ERROR;
	}

	fd = open(filename, O_RDWR | O_CREAT, 0644);
	if (fd == -1) {
		perror("open");
		return STATUS_ERROR;
	}

	return fd;
}

int open_db_file(char *filename) {
	int fd = open(filename, O_RDWR, 0644);
	if (fd == -1) {
		perror("open");
		return STATUS_ERROR;
	}

	return fd;
}