summaryrefslogtreecommitdiffstats
path: root/src/b_sqb_file.c
blob: b69f16036fd338d05e739421b69e56b222c77ca4 (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_sqb_file.c                                       :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <sys/stat.h>
#include <libft.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>

#include "d_define.h"

static t_bool
	b_file_tests_one(uint8_t id,
					struct stat *st)
{
	if (id == FT_ID_SQB_B && S_ISBLK(st->st_mode) == 1)
		return (TRUE);
	else if (id == FT_ID_SQB_C && S_ISCHR(st->st_mode) == 1)
		return (TRUE);
	else if (id == FT_ID_SQB_D && S_ISDIR(st->st_mode) == 1)
		return (TRUE);
	else if (id == FT_ID_SQB_F && S_ISREG(st->st_mode) == 1)
		return (TRUE);
	else if ((id == FT_ID_SQB_H || id == FT_ID_SQB_L_MAJ)
		&& S_ISLNK(st->st_mode) == 1)
		return (TRUE);
	else if (id == FT_ID_SQB_P && S_ISFIFO(st->st_mode) == 1)
		return (TRUE);
	return (FALSE);
}

uint8_t
	b_sqb_file_tests(uint8_t id,
					char *argv[])
{
	struct stat	st;
	int32_t		fd;

	if ((fd = open(argv[1], O_RDONLY)) == -1)
		return (1);
	if (id == FT_ID_SQB_E)
	{
		close(fd);
		return (0);
	}
	fstat(fd, &st);
	close(fd);
	if (b_file_tests_one(id, &st) == TRUE)
		return (0);
	return (1);
}