summaryrefslogtreecommitdiff
path: root/userland/minibox/utilities/ls.c
blob: 5a8f8ea7bd6f6d48da849b19af15ab415c44d879 (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
#include "include.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int ls_main(int argc, char **argv) {
  int list_hidden = 0;
  int newline = 0;
  /*
          for (int ch;-1 != (ch = getopt(argc, argv, "a1"));)
                  switch ((char)ch)
                  {
                  case 'a':
                          list_hidden = 1;
                          break;
                  case '1':
                          newline = 1;
                          break;
                  }*/
  char *path = argv[1];
  char path_buffer[256];

  if (!path) {
    (void)getcwd(path_buffer, 256);
    path = path_buffer;
  }

  struct dirent **namelist;
  int n;
  COND_PERROR_EXP(-1 == (n = scandir(path, &namelist, 0, 0)), "scandir",
                  return 1);

  int prev = 0;
  for (int i = 0; i < n; i++) {
    if (!list_hidden)
      if ('.' == *namelist[i]->d_name)
        continue;

    if (prev)
      putchar(newline ? '\n' : ' ');
    else
      prev = 1;
    printf("%s", namelist[i]->d_name);
  }
  putchar('\n');
  scandir_sane_free(namelist);
  return 0;
}