LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-27-2004, 03:49 PM   #1
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
open() file creation weirdness


I think I'm losing my mind. Someone please tell me why "testfile" is being created with such an insane mode. If I specifically pass something like 0644 as the mode to open() it creates the file as expected, but shouldn't that be the default? And the same thing happens on my other linux box too!

(This is a little test program I wrote since I was having a similar problem in a different program that was creating files with mode -------r-- )
Code:
itsme@itsme:~/C/testdir$ cat opentest.c
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(void)
{
  int fd;

  fd = open("testfile", O_WRONLY|O_TRUNC|O_CREAT);
  close(fd);

  return 0;
}
Code:
itsme@itsme:~/C/testdir$ gcc -Wall opentest.c -o opentest
itsme@itsme:~/C/testdir$ ls -al
total 24
drwxr-xr-x  2 itsme users  4096 2004-09-27 13:20 ./
drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
-rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
-rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
itsme@itsme:~/C/testdir$ ./opentest
itsme@itsme:~/C/testdir$ ls -al
total 24
drwxr-xr-x  2 itsme users  4096 2004-09-27 13:20 ./
drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
-rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
-rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
-r-x--s--T  1 itsme users     0 2004-09-27 13:20 testfile*
itsme@itsme:~/C/testdir$ umask
0022
itsme@itsme:~/C/testdir$ touch foo
itsme@itsme:~/C/testdir$ ls -al
total 24
drwxr-xr-x  2 itsme users  4096 2004-09-27 13:22 ./
drwxr-xr-x  5 itsme users  4096 2004-09-27 13:19 ../
-rw-r--r--  1 itsme users     0 2004-09-27 13:22 foo
-rwxr-xr-x  1 itsme users 10690 2004-09-27 13:20 opentest*
-rw-------  1 itsme users   211 2004-09-27 13:19 opentest.c
-r-x--s--T  1 itsme users     0 2004-09-27 13:20 testfile*
itsme@itsme:~/C/testdir$
This is what's making me think it should have a sane mode if I leave it unspecified in my program:
Code:
      O_CREAT
              If the file does not exist it will be created.  The
              owner (user ID) of the file is set to the effective
              user  ID of the process. The group ownership (group
              ID) is set either to the effective group ID of  the
              process  or to the group ID of the parent directory
              (depending on filesystem type  and  mount  options,
              and  the  mode  of the parent directory, see, e.g.,
              the mount options bsdgroups and sysvgroups  of  the
              ext2 filesystem, as described in mount(8)).
 
Old 09-27-2004, 04:41 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Original Poster
Rep: Reputation: 58
Nevermind, problem found. Thanks.

The mode parameter is not optional like the man page leads you to believe.
 
Old 09-28-2004, 03:44 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,506

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
The manual page is not that confusing on this subject:
Quote:
mode must be specified when O_CREAT is in the flags, and is ignored otherwise.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open source flow chart creation gauntalus Linux - Software 5 11-12-2012 11:32 PM
host file weirdness EdR Red Hat 1 09-27-2005 06:18 AM
Open office write table weirdness rammstein Linux - Software 1 09-08-2005 08:59 PM
RPM Spec file creation: %file section question davidas Linux - Newbie 0 03-16-2004 10:36 PM
file creation in C ratheesh Programming 1 01-08-2004 05:39 AM


All times are GMT -5. The time now is 03:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration