"pear shaped"?
When creating directories, the 'x' permission have a different meaning than for files: They mean 'search', meaning if you don't set this permission, you'll not be able to 'search' through the directory... So, to create a file there you must set 'x' permission as well... Your mkdir() call should be:
Code:
... mkdir( path, 0777 ) ...
Notice mkdir() - as many libc functions - will set errno in case of error. One better code when creating a directory:
Code:
errno = 0;
if ( mkdir( path, 0777 ) )
{
perror( "mkdir" );
abort();
}
Second: Your call to creat() is wrong. Here's the prototype:
Code:
int creat( const char *pathname, mode_t mode );
You should use:
Code:
... creat( path, 0640 ) ...
if you want your file writable just to the owner and readable to the group.
Here's an example:
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
int main( void )
{
int fd;
if ( mkdir( "mydir", 0666 ) )
{
perror( "mkdir" );
return EXIT_FAILURE;
}
fd = creat( "mydir/file", 0640 );
if ( fd < 0 )
{
perror( "creat" );
return EXIT_FAILURE;
}
close( fd );
return EXIT_SUCCESS;
}
Compiling and running:
Code:
$ cc -o test test.c
$ ./test
creat: Permission denied
$ rmdir mydir
$ sed -i 's/0666/0777/' test.c
$ cc -o test test.c
$ ./test
$ tree -p .
.
├── [drwxr-xr-x] mydir
│ └── [-rw-r-----] file
├── [-rwxr-xr-x] test
└── [-rw-r--r--] test.c
BUT... Notice your process is bound to umask from your session. If you take a look at the directory and the file, will notice the 'group' and 'others' permission aren't what you expected, sometimes... Set the umask (with umask() call) beforehand if you want to make sure...