Create Directory Permissions - c

I would like to know something about directory creation in C.
I know in unix based systems, you can create a directory with this function:
mkdir (const char* directory_name,mode_t mode);
but, in windows, mkdir function, only accept one argument, the name of the directory.
you cannot specify mode bits for access permissions.
in windows the function for creating the directory is:
_mkdir (const char* directory_name);
so, a portable way to creating a directory is like:
#ifdef WIN32
_mkdir (directory_name);
#else
mkdir (directory_name,mode);
#endif
my question is, is there a way to specify permissions like mkdir in unix, but in windows?

You can use CreateDirectory and supply the appropriate SECURITY_ATTRIBUTES (the linked example uses the security descriptor and attributes for a registry key, but fundamentally this is no different than for a file).

You can set permissions with CreateDirectory, from the API Windows.

On windows there also is the chmod() analogon _chmod(): http://msdn.microsoft.com/en-us/library/1z319a54%28v=vs.100%29.aspx

Related

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

Creating a new directory without mkdir()

In C I'm using 'open()' to create and open files:
fd = open(fileName, O_CREAT | O_RDWR, 0644);
if 'fileName' includes a directory the file is created only if the directory exists and the application has permission to write to this directory.
If the directory does not exists 'open()' returns an error and I would like to create the directory.
With makedir (see below) I am able to create the directory.
int mkdir(const char *path, mode_t mode);
I was wondering, are there other ways to create a directory? I believe we can also use ioctl()? Are there more alternatives?
I'm asking this because in our code we are not allowed to use mkdir.
I was wondering, are there other ways to create a directory? I believe we can also use ioctl()?
At least on Linux and HP-UX, mkdir is a system call on its own.
So, No, there is no other way to create a directory, at least not on Linux - other systems might implement directory creation differently. But on any reasonable system, I would expect directory creation to be a privileged operation, so you need to go through the kernel.
The only slightly different approach would be to execute the system call directly, by bypassing the C runtime library. In assembly nasm syntax on an i386 architecture, this would look something like
mkdir:
mov eax, 39 ; System-call "sys_mkdir"
mov ebx, dirName ; directory name to create
mov ecx, 0x755 ; creation mode
int 80H ; call sys_mkdir
However, this is not something you would normally do since it is completely system dependant. Switching to a different CPU would require different register setup and/or different instructions - that is what the C runtime library encapsulates.
See also
http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
http://asm.sourceforge.net/syscall.html
http://en.wikipedia.org/wiki/System_call
A kernel driver can create a folder using
struct file *fp = (struct file *) NULL;
fp = filp_open("/home/testdir", O_DIRECTORY|O_CREAT, S_IRUSR);
I can imagine some situations when a "make_dir" driver which creates large number of folders will be faster than calling mkdir for every folder.

Function to check file exist with specific extention or pattern in C

Is there any function to check file(s) exist with specific extension or pattern in a directory(s) in Windows and Linux?
For example, to check for files with bbram extension in nvmdir directory.
file_exists(nvmdir .. "\\*.bbram")
A somewhat less elegant solution would be to use popen() or even lesser elegant system() to issue an OS specific "shell" command.
If you want linux/windows compatibility I guess you'll need to do it yourself (look at #ifdef etc to define witch os you're using).
Then you'll need opendir (http://man7.org/linux/man-pages/man3/opendir.3.html), readdir (http://man7.org/linux/man-pages/man3/readdir.3.html) functions.
I let you dig into it.

find directory in a C program

How can I find if a directory exists or not in a C program? I know that getcwd() gives you the current directory but I want to find ANY directory. Is there a function for that or how do I do it? I am using Ubuntu
opendir, readdir and closedir are POSIX functions, so they should be available in Linux, MacOS, Windows as well as any Unix type system.
you can use access() function , like for example:
access(path, F_OK);
It returns 0 if found. -1 if not found.
int mkdir (const char *filename, mode_t mode)
you need to include the header file sys/stat.h to use this function.
The mkdir function creates a new, empty directory with name filename. The argument mode specifies the file permissions for the new directory file.A return value of 0 indicates successful completion, and -1 indicates failure.
In case of failure and if your Directory already exists errno value will equal to EEXIST.

Creating a temporary folder in tmp folder on OS X

How can i create a temporary folder in tmp folder using C++ language.
I have 3 volumes. Leopard, Development and 10.6 (in Mac OS X) and I want to create a temp directory in current home directory.
Here is my code. I am confused about this line char* tempdir = "/Volumes/Development/NewFolder.XXXXXX":
if (!mkdtemp(tempdir))
fprintf(stderr, "Not able to create directory");
Under POSIX, you can use mkdtemp to create a directory with a unique name. On Windows, use GetTempPath to retrieve the name of the temp directory, then create a directory with a random name there.
You can use the boost::Filesystem library function: create_directory( "temp" );
This is very portable and will work under most operating systems.
Boost can be downloaded here.
Boost is an excellent choice, but one of the problems with boost is that you would probably be downloading a huge amount of sources - if all you need is the filesystem functionality this is a bit of an overkill. Try http://stlplus.sourceforge.net/stlplus3/docs/file_system.html
Also why don't you just use the good old system() function? The string argument to system of course would be platform dependent.
Arpan

Resources