Where is readdir in linux? - c

I'm editing linux v2.18.8 (CentOS5) to include file attributes for a project for school. One of the things we have to implement as a group is to list all attribute names (Attributes are text files stored in hidden directories at the same level as the file).
Just an example:
this.jpg is a file in /Desktop/Testing. When you add a custom user attribute a folder is created that holds the attributes. So if I add the attribute "Creator" and have it's value as "bob" you get a dir /Desktop/Testing/.this.jpg_attr/Creator (and if you open Creator, it's contents will be "bob").
What I'm interested in doing is listing all of the user created attributes in the attributes folder (or from the example all the files in /Desktop/Testing/.this.jpg_attr/) I know that I want to use the readdir call, but I don't know where to find it in the linux setup. If anyone could give me any guidance as to where to look for readdir, I think I can figure out how to manipulate it.
I'M NOT ASKING FOR ANY CODE. JUST A FILE PATH FOR WHERE READDIR IS, OR THE SYSTEM CALL THAT CALLS READDIR.
Thanks in advanced.

It's not exactly clear what you mean. The readdir function you want to use is part of the standard library and will be linked in automatically.
Perhaps you're asking how to include the header file that declares readdir. In that case:
#include <dirent.h>
You can get more information about the readdir function using this command:
man 3 readdir

The system call is getdents(2), however you should continue to use the readdir wrapper for portability.

Related

Is there a simple way to check if a file exists in C (Visual Studio)?

I know this is a frequently asked question, but most solutions I have found are from 6-10 years ago and don't seem to work.
As a part of the C program I am writing in Visual Studio, I need to find a function that is able to return a boolean value - whether or not a file with a given name exists (the file in question is located in the debug directory, which is why I am saying file name and not file path).
I need to implement it using a library I am able to include in VS, hence using access() from the unistd.h library will not work.
Also, it has to be a safe function.
If there's a function in the WINAPI that does all of that - that would be best.
Thanks in advance for the help.
PathFileExistsW should do the job. It takes the path of the file or directory, which you want to check the existence of, as the first argument. It returns BOOL (TRUE, if the file or directory exists, and FALSE, if it doesn't. You have to include shlwapi.h as header and link against Shlwapi.lib in order to use this function.

Obtaining absolute path of files in C

I need a method to obtain the absolute path of a file in C programming language for the implementation of 'cp' UNIX's command. The objective is show an error when the source path and destination path are the same.
There are multiple possibilities, for example:
cp file . // show error
cp ../file .
cp file file // show error
I haven't found a good method to solve this problem.
Converting comments into an answer.
Lookup realpath() to get the 'real name' of a path, but it really isn't necessary. You can use stat() to see if the device and inode number are the same for two names.
Also note that if you have two files linked (for example, /home/user1/name1 and /home/user2/name2), the names might be different but still refer to the same file (and the links could be 'hard' or symbolic). You can detect their equivalence with stat() but not with realpath() — at least, not with realpath() if the link is a hard link.

Is there any command to find which header file needs to be included for certain type (not function)?

For library functions I use man <function> to find which header file I need to include. But for types like size_t, uint32_t, etc. the man approach doesn't work.
Is there any command to find the header file required?
Another possible solution is to search locally using grep.
Something like grep -r 'typedef\s.*\suint32_t;' /usr/include should work for you.
A possible solution is open http://en.cppreference.com/w/c/types/<type> which relies on an external web page.

Reading Directory for all files of a specified file type

I want to know how to do something like the following...
I have a directory, let's call this directory "D:\Folder\" and it has some file types like .json, .lua, etc and I need to be able to put the appropriate files in a table based off their file type. How do I do this via Lua without external libraries? Also, how can I get other information on the files, like size, date modified, etc via lua and store that info?
As Yu Hao said in the comment, Lua by itself doesn't have any methods to get the list of files in a folder or access attributes of those files. In terms of external libraries, you can use Lua Filesystem module that has everything you need or winapi if you are looking for Windows-specific solution. Both are small libraries that can be compiled quite easily using mingw.
If you are looking for Windows-only-no-external-library solution, you should be able to run "dir" command and process its results using io.popen. You can parse the captured output and get file names, sizes, and dates based on that. You can also get the file size by using file:seek, but since you may be parsing anyway, you can get it all from the output. I don't think there is anything much simpler than that.
how about searching for a pattern that represents any and all characters a file could posses and then .file_type...and then run that through io.open for example...possible?
You won't be able to "guess" filenames by enumerating possible symbol combinations simply because this .... will .... take .... a .... very .... long .... time.

How to find execute files in Linux?

I wants to get the names of execute files in some directory in Linux.
How can I do it?
I tried to use opendir like this:
dir = opendir(directoryName);
I need to get only the names of the execute files.
I programming in C.
thanks :)
You should define what you mean by executable files.
That could be any file with its execute bit (it is the owner, group, or other) set. Then test with access(2) & X_OK and/or use stat(2).
That could also be only ELF executables. See elf(5); then the issue might be to check that a file could indeed be executed, which might be difficult (what about missing library dependencies? or ill-formed ELF files?). Maybe use libelf (and/or libmagic to do the equivalent of file(1) command).
To scan recursively a file tree, use nftw(3); to scan just a directory use opendir(3) & readdir(3) (don't forget the closedir!), then you'll probably need to build the complete file path from each directory entry (perhaps using snprintf(3) or asprintf(3))
See also Advanced Linux Programming

Resources