-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_rm.c
48 lines (45 loc) · 1.69 KB
/
ft_rm.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rm.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edelangh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/01/31 17:15:33 by edelangh #+# #+# */
/* Updated: 2015/01/31 17:53:53 by edelangh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "ft_printf.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
int ft_rm(char *name)
{
DIR *directory;
struct dirent *entry;
struct stat file_stat;
char buffer[10024];
if (stat(name, &file_stat) == -1)
return (-1);
if (S_ISREG(file_stat.st_mode))
return (remove(name));
ft_bzero(buffer, sizeof(buffer));
if (!(directory = opendir(name)))
return (-1);
while ((entry = readdir(directory)) != NULL)
{
if (!ft_strcmp(entry->d_name, ".") || !ft_strcmp(entry->d_name, ".."))
continue ;
ft_sprintf(buffer, "%s/%s", name, entry->d_name);
stat(buffer, &file_stat);
if (S_ISREG(file_stat.st_mode))
remove(buffer);
else if (S_ISDIR(file_stat.st_mode))
ft_rm(buffer);
}
closedir(directory);
return (remove(name));
}