-1

I am trying to create a program where the user can add different path regex-s so that a specific set of operations on the files that match the regex.

I tried using opendir() of the dirent.h header file but soon realized that it does not use the concept of regex.

The dir command I am trying to emulate is

dir [regex] /b

I need the output in a (char) buffer**. Piping the output could be a solution but I am looking for a more efficient way to do it.

Is there any predefined function in the standard (C90) library or will we have to create our own implementation?

2 Answers2

1

C does not know about directories. They are operating system specific, usually provided by your OS kernel (look however inside GNU Hurd as an exception, and into unikernels). Read the C11 standard n1570 and forget, in 2020, about the obsolete C89 standard and TurboC. Consider trying some Linux distribution (such as Ubuntu or Debian or others). Most of them provide GCC or Clang (or the non-optimizing TinyCC compiler) and are very developer-friendly. My recommendation: use GCC as gcc -Wall -Wextra -g. Choose a good enough built automation tool (maybe GNU make) with an appropriate source-code editor (such as GNU emacs or vim or geany or others). Learn how to debug small programs and use the GDB debugger and the git version control tool.

POSIX does know about directories (it is an API specification written in English, also defining regex(3)). See here, and read the Linux man pages. And also the WinAPI.

On Linux, see mkdir(2), chdir(2), readdir(3), getcwd(3), unlink(2), stat(2), open(2), nftw(3), path_resolution(7) etc etc; you could want to study the source code of a Linux kernel and of some common C library for it, such as GNU glibc or musl-libc. Budget for that several months full time of your efforts. They are open source, so with some conditions you are allowed to study, improve and reuse their source code. See also http://linuxfromscratch.org/

Notice also popen. You probably don't want to use it and would prefer using more primitive system calls (see syscalls(2) for their list on Linux). You could use a library like Glib (from GTK).

Remember that C programs (of the freestanding kind) could run on the bare metal (e.g. Arduino). In those cases, speaking of directories does not make any sense. See also osdev.org for more, and observe that the Linux kernel is written in C (with a tiny amount of assembler code).

GrassHopper was an OS written mostly in C without any files or directories. See also old discussions archived on tunes.org and tccboot.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Could you share the download link for POSIX C90? – Hello World Feb 17 '20 at 08:29
  • 1
    @HelloWorld: I don't think that POSIX C90 exist. The POSIX standard is an OS API above C. – Basile Starynkevitch Feb 17 '20 at 08:37
  • Any regex library for C90? – Hello World Feb 17 '20 at 09:49
  • @Hello World: POSIX has one. C90 does not – Basile Starynkevitch Feb 17 '20 at 12:53
  • @Hello World: Download then install some Linux distribution such as [Debian](https://debian.org/). It is free (both libre and gratis) and runs on most PCs. Backup your precious data before installing any Linux distribution. Ask on https://unix.stackexchange.com/ for help if so needed – Basile Starynkevitch Feb 17 '20 at 13:09
  • 1
    Actually I was looking for something like a library in Turbo C++. The same goes for the other answer you had posted yesterday. But I am pretty sure that it would help someone else at some point in the future. So please don't delete it. – Hello World Feb 19 '20 at 07:51
  • OP is programming on Turbo C. None of the advice you give is in any way relevant to that. – fuz Feb 23 '20 at 10:28
  • The main advice is to give up TurboC – Basile Starynkevitch Feb 23 '20 at 17:43
  • @BasileStarynkevitch Your main advice is to use both a different toolchain and a different operating system. That's not really an answer to OPs question; would be fine as a comment though. OP probably has a reason for using the toolchain he uses. – fuz Feb 24 '20 at 08:06
0

Use the function findfirst to start querying a directory and then findnext to iterate. The functions find all files in one directory matching a given pattern, so you possibly need to append \*.* to your directory name to list all files in that directory.

Refer to the Turbo C documentation for details.

fuz
  • 88,405
  • 25
  • 200
  • 352