Implementing the ls -l command in C

maxence57590 Posted messages 4 Status Membre -  
maxence57590 Posted messages 4 Status Membre -
Hello,
As part of a project, I would like to display the different types of files in a directory (executable, text, etc.).
I can display the files using "opendir," but I keep searching and can't find anything on how to display the types of files... If anyone has a suggestion, thank you in advance.

4 réponses

jisisv Posted messages 3678 Status Modérateur 936
 
A regular file is quite different from a symbolic link, a named pipe, or a device entry (character, block).

<ital>struct dirent {
ino_t d_ino; /* numéro d'inode */
off_t d_off; /* pas un décalage; voir NOTES */
unsigned short d_reclen; /* longueur de cet enregistrement */
unsigned char d_type; /* type de fichier; non pris en charge
par tous les types de systèmes de fichiers */
char d_name[256]; /* nom de fichier */
}</ital>

L'entrée d_type contient le type d'entrée.

See
man 3 readdir


To determine file types, look towards libmagic

--
Gates a donné ^W vous a vendu les fenêtres.
GNU nous a donné toute la maison. (Alexandrin)
1
maxence57590 Posted messages 4 Status Membre
 
A big THANK YOU, it really helps me, I feel like I'm not far from my desired goal!
0
KX Posted messages 19031 Status Modérateur 3 020
 
Hello,

The "type of a file" does not exist. All files are a series of bytes, there is no difference.

Depending on the operating system, you may have concepts of extensions or execution rights that allow you to add additional information. But nothing can be generalized.
--
Trust does not exclude control.
0
maxence57590 Posted messages 4 Status Membre
 
In this case, how to display the extensions?
0
KX Posted messages 19031 Status Modérateur 3 020
 
The file extension is in its name.

In itself, a file does not have an extension; it's just the end of its name that introduces this notion, but it's only an indication, it has no value. You can rename an image file to .exe, it doesn't make it an executable file.
0
fiddy Posted messages 441 Registration date   Status Contributeur Last intervention   1 847
 
Extensions only make sense on Windows.
If you're talking about "ls -l", you're probably on GNU/Linux. So forget about this extension story.

ls -l does not provide the file type. It will only show if it is a directory or not... However, it will also display the file permissions (read, write, etc.).

Is it really ls -l that you want to reproduce?
--

Google is your friend
0
maxence57590 Posted messages 4 Status Membre
 
I need to do both, check if the files in a directory are executables, text files, or directories, and also know the file permissions.
0