LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-17-2002, 06:15 AM   #1
udayan
Member
 
Registered: Apr 2002
Location: India
Distribution: Linux Redhat 7.0
Posts: 62

Rep: Reputation: 16
programm.. open call used..


i made the following program in C with Red Hat 7.0 as the Operating system.
#include<fcntl.h>
#include<stdio.h>
main()
{
int fd;
fd = open("/home/mydir/testfile",O_CREAT,777);
return o;
}

but i got a file with the following mode
-r----x--t
Why is this so?
thanx in advance
 
Old 05-17-2002, 06:40 AM   #2
vfs
Member
 
Registered: Apr 2002
Location: Brazil
Distribution: Slackware
Posts: 184

Rep: Reputation: 30
Uh, try using "0777" as permission.

I'm not a C expert (in fact, I "segfault" all the time ), but i think you have to declare modes this way...

'man chmod' may help...

HTH,

vfs
 
Old 05-17-2002, 09:38 AM   #3
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
vfs is correct. You need the leading zero on the mode. You should use the macros that are defined in the headers. To get 777 permissions you could use:
Code:
(S_IRWXU | S_IRWXG | S_IRWXO)
Remember that the shells umask value may cancel out some permissions when the file is created, so may get something that you don't expect.

Also you can use creat( "", mode) instead of open( "", O_CREAT|O_WRONLY|O_TRUNC, mode ) when creating files.

Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FILEMODE (S_IRWXU | S_IRWXG | S_IRWXO)
main()
{
   int fd = 0;
   ssize_t iWrite = 0;
   char szBuf[] = "Hello there";

   fd = open( "tempfile",
              O_CREAT|O_WRONLY|O_TRUNC,
              FILEMODE );
   iWrite = write ( fd, szBuf, sizeof( szBuf ) );
   close ( fd );
}
 
Old 05-17-2002, 02:26 PM   #4
udayan
Member
 
Registered: Apr 2002
Location: India
Distribution: Linux Redhat 7.0
Posts: 62

Original Poster
Rep: Reputation: 16
yeah i was havin trouble with the unmask thing. i suppose when we give a value of mode =777 as mode and the mask value is 022 then from the !mask and mode we get the real value of the file.
thanx for your help
 
  


Reply



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
Good open source programm for learning noteventime Programming 1 04-06-2005 02:13 AM
What do you call your remote control? I call mine... RHLinuxGUY General 19 10-18-2004 06:23 AM
how to call c programm from perl scripts akaash Programming 4 03-18-2004 07:41 PM
one more on open system call udayan Programming 2 05-17-2002 02:32 PM
open system call in linux udayan Linux - Newbie 1 05-06-2002 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration