Posted: . At: 9:22 AM. This was 1 year ago. Post ID: 17821
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


A very useful program to list files in a directory.


This is a useful program to list all files in a directory.

ds.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
/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
/********************************************************************
* Description: A program to list files in a directory.
* Author: John Cartwright <>
* Created at: Mon Apr  3 08:53:50 AEST 2023
* Computer: localhost.localdomain
* System: Linux 5.14.0-162.18.1.el9_1.x86_64 on x86_64
*
* Copyright (c) 2023 John Cartwright  All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/syscall.h>
 
#define BUF_SIZE 4096
 
int
main(int argc, char **argv)
{
  int fd, nread;
  char buf[BUF_SIZE];
  struct dirent *d;
  int bpos;
  char d_type;
 
  fd = open(argc > 1 ? argv[1] : ".", O_RDONLY | O_DIRECTORY);
  if (fd == -1)
    perror("open");
 
  for ( ; ; )
  {
    nread = syscall(SYS_getdents, fd, buf, BUF_SIZE);
    if (nread == -1)
    { perror("getdents"); }
 
    if (nread == 0)
    { break; }
 
    for (bpos = 0; bpos < nread;)
    {
      d = (struct dirent *) (buf + bpos);
      d_type = *(buf + bpos + d->d_reclen - 1);
      if( d->d_ino != 0 && d_type == DT_REG )
      { printf("|- %s\n", (char *)d->d_name-1 ); }
      bpos += d->d_reclen;
    }
  }
 
  exit(EXIT_SUCCESS);
}

Compile this program very easily.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ gcc -Wall -Os ds.c -o list

This is what the output looks like.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ ./list Books/
Chaos Daemons 6th Edition Codex - 2013.pdf
DF30-TheVillageWithNoName.pdf
Doctor Who Annual 1966.pdf
Doctor Who Annual 1967.pdf
Doctor Who Annual 1968.pdf
Doctor Who Annual 1969.pdf
Doctor Who Annual 1970.pdf
Doctor Who Annual 1971.pdf
Doctor Who Annual 1973.pdf
Doctor Who Annual 1974.pdf
Doctor Who Annual 1975.pdf
Doctor Who Annual 1976.pdf
Doctor Who Annual 1977.pdf
Doctor Who Annual 1978.pdf
Doctor Who Annual 1979.pdf
Doctor Who Annual 1980.pdf
Doctor Who Annual 1981.pdf
Doctor Who Annual 1982.pdf
Doctor Who Annual 1983.pdf
Doctor Who Annual 1984.pdf
Doctor Who Annual 1985.pdf
Doctor Who Annual 1986.pdf
Warhammer 40k - Codex - Chaos Daemons 6th.pdf
Bond 10 - The Spy Who Loved Me - Fleming_ Ian.epub
Bond 11 - On Her Majesty's Secret Servic - Fleming_ Ian.epub
Bond 12 - You Only Live Twice - Fleming_ Ian.epub
Bond 13 - The Man With The Golden Gun - Fleming_ Ian.epub
Bond 1 - Casino Royale - Fleming_ Ian.epub
Bond 2 - Live and Let Die - Fleming_ Ian.epub
Bond 3 - Moonraker - Fleming_ Ian.epub
Bond 4 - Diamonds Are Forever - Fleming_ Ian.epub
Bond 5 - From Russia with Love - Fleming_ Ian.epub
Bond 6 - Dr. No - Fleming_ Ian.epub
Bond 7 - Goldfinger - Fleming_ Ian.epub
Bond 8 - For Your Eyes Only - Fleming_ Ian.epub
Bond 9 - Thunderball - Fleming_ Ian.epub
Diamond Age - Stephenson_ Neal.epub
Harry Potter 7 - Harry Potter and the De - Rowling_ J.K_.epub
The Fountains of Paradise - Clarke_ Arthur C_.epub
Zodiac - Stephenson_ Neal.epub

This is how easy it is to list files using a C program. This one lists all files.

┗━━━━━━━━━━┓ john@localhost ~/Documents
           ┗━━━━━━━━━━━━━╾ ╍▷ ./list /home/john/
|- .bash_logout
|- .bash_profile
|- .zshrc
|- .bash_history
|- .python_history
|- .xscreensaver
|- Firefox_wallpaper.png
|- .lesshst
|- .Xauthority
|- .nvidia-settings-rc
|- gpu.sh
|- .bashrc
|- .viminfo

This program is one that will print a listing of files in a directory. This has output just like the ls -la command.

https://gist.github.com/john302/85da5593b180e84d12f75b10471b830f


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.