summaryrefslogtreecommitdiffstats
path: root/src/b_sqb_file.c
blob: 5029d0aa303ab55da6152f0a2442ab37ba450ee2 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   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/types.h>
#include <sys/stat.h>
#include <libft.h>
#include <fcntl.h>
#include <unistd.h>

#include "d_define.h"

static t_bool	b_file_tests(unsigned char id,
							const char path[],
							struct stat sb)
{
	if (id == B_ID_SQB_B && S_ISBLK(sb.st_mode) == 1)
		return (TRUE);
	else if (id == B_ID_SQB_C && S_ISCHR(sb.st_mode) == 1)
		return (TRUE);
	else if (id == B_ID_SQB_D && S_ISDIR(sb.st_mode) == 1)
		return (TRUE);
	else if (id == B_ID_SQB_F && S_ISREG(sb.st_mode) == 1)
		return (TRUE);
	else if ((id == B_ID_SQB_H || id == B_ID_SQB_L_MAJ)
		&& S_ISLNK(sb.st_mode) == 1)
		return (TRUE);
	else if (id == B_ID_SQB_P && S_ISFIFO(sb.st_mode) == 1)
		return (TRUE);
	else if (id == B_ID_SQB_R && access(path, R_OK) == 0)
		return (TRUE);
	else if (id == B_ID_SQB_S && sb.st_size > 0)
		return (TRUE);
	else if (id == B_ID_SQB_W && access(path, W_OK) == 0)
		return (TRUE);
	else if (id == B_ID_SQB_X && access(path, X_OK) == 0)
		return (TRUE);
	else if (id == B_ID_SQB_S_MAJ && S_ISSOCK(sb.st_mode) == 0)
		return (TRUE);
	return (FALSE);
}

unsigned char	b_sqb_file_tests(unsigned char id, char *argv[])
{
	struct stat	sb;

	if (access(argv[1], 0) == -1)
		return (1);
	if (id == B_ID_SQB_E)
		return (0);
	if (id == B_ID_SQB_H || id == B_ID_SQB_L_MAJ)
		lstat(argv[1], &sb);
	else
		stat(argv[1], &sb);
	if (b_file_tests(id, argv[1], sb) == TRUE)
		return (0);
	return (1);
}

static t_bool	b_sqb_file_comp_tests(unsigned char id,
									struct stat sb1,
									struct stat sb2)
{
	if (id == B_ID_SQB_NT
		&& sb1.st_mtime > sb2.st_mtime)
		return (TRUE);
	else if (id == B_ID_SQB_OT
		&& sb1.st_mtime < sb2.st_mtime)
		return (TRUE);
	return (FALSE);
}

unsigned char	b_sqb_double_file_tests(unsigned char id, char *argv[])
{
	struct stat	sb[2];

	if (access(argv[0], 0) == -1)
		return (1);
	if (access(argv[2], 0) == -1)
		return (1);
	stat(argv[0], &sb[0]);
	stat(argv[2], &sb[1]);
	if (b_sqb_file_comp_tests(id, sb[0], sb[1]) == TRUE)
		return (0);
	return (1);
}