/
my_ls.c
80 lines (72 loc) · 1.36 KB
/
my_ls.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
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
/*
** my_ls.c for my_ls in /home/rahmeh_d/github/lib-c
**
** Made by Rahmeh Daniel
** Login <rahmeh_d@epitech.net>
**
** Started on Tue Feb 9 11:06:28 2016 Rahmeh Daniel
** Last update Tue Feb 9 11:07:52 2016 Rahmeh Daniel
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include "my.h"
int my_ls1()
{
struct dirent *d;
DIR *dir;
char *name;
name = ".";
dir = opendir(name);
if (dir == NULL)
{
perror("ls");
return (-1);
}
else
{
while ((d = readdir(dir)) != NULL)
{
if (d->d_name[0] != '.')
{
my_putstr(d->d_name);
my_putchar('\n');
}
}
}
return (0);
}
int my_ls2(char *name)
{
struct dirent *d;
DIR *dir;
dir = opendir(name);
if (dir == NULL)
{
perror("ls");
return (-1);
}
else
{
while ((d = readdir(dir)) != NULL)
{
if (d->d_name[0] != '.')
{
my_putstr(d->d_name);
my_putchar('\n');
}
}
}
return (0);
}
int main(int ac, char **av)
{
if (ac == 1)
my_ls1();
else if (ac == 2)
my_ls2(av[1]);
return (0);
}