View Full Version : [SOLVED] stringification
conradin
February 15th, 2012, 06:36 PM
Hi all,
I am trying to make a define macro that is a string of flags for the mode of the function open(). What I have, (bellow) seems to set m775 to the correct set of flags, but when the file is written, the permissions are all screwed up. Can someone show me what I'm doing wrong? I want the permissions to be set to 775 when Im done. And yes, my umask is set to allow for this to work.
#include <sys/types.h> //Specified in man 2 open
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h> //Allows use of error numbers
#include <fcntl.h> //Specified in man 2 open
#include <stdio.h>
#define m775 "S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH"
int main(int argc, char *argv[]){
char modez[] = m775;
int fd;
printf("%s", modez);
fd = open(argv[1], O_CREAT, modez);
return 0;
}
conradin
February 15th, 2012, 06:54 PM
By comparision, (this): totaly works but looks stupid:
#include <sys/types.h> //Specified in man 2 open
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h> //Allows use of error numbers
#include <fcntl.h> //Specified in man 2 open
#include <stdio.h>
#define m775 "S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH"
#define m700 S_IRWXU
#define m070 S_IRWXG
#define m004 S_IROTH
#define m001 S_IXOTH
int main(int argc, char *argv[]){
int fd;
fd = open(argv[1], O_CREAT, m700 | m070 | m004 | m001 );
return 0;
}
McNils
February 15th, 2012, 06:54 PM
You are sending the address of modez to open instead of what you really mean.
Here is a simple correct version.
#include <sys/types.h> //Specified in man 2 open
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h> //Allows use of error numbers
#include <fcntl.h> //Specified in man 2 open
#include <stdio.h>
#define m775 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
int main(int argc, char *argv[]){
int fd;
fd = open(argv[1], O_CREAT, m775);
return 0;
}
Bachstelze
February 15th, 2012, 06:58 PM
Well, you are giving open() a string where it expects an integer... What you want is
#include <sys/types.h> //Specified in man 2 open
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h> //Allows use of error numbers
#include <fcntl.h> //Specified in man 2 open
#include <stdio.h>
#define m775 ((S_IRWXU) | (S_IRWXG) | (S_IROTH) | (S_IXOTH))
int main(int argc, char *argv[]){
mode_t modez = m775;
int fd;
printf("%d", m775);
fd = open(argv[1], O_CREAT, modez);
return 0;
}
You can also just do this...
fd = open(argv[1], O_CREAT, 0775);
Arndt
February 15th, 2012, 07:00 PM
#define m775 S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
This works, but it is always a good idea to let parentheses be a part of the macro definition:
#define m775 (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
dwhitney67
February 15th, 2012, 07:04 PM
By comparision, (this): totaly works but looks stupid:
#define m775 "S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH"
Congratulations. The macro above defines m775 to be a string. If you had removed the quotes, you would have had better luck.
conradin
February 15th, 2012, 07:16 PM
!! Aw man, I tried something like that the other day, but didnt have the 0 in 775! That does look much better!
Alas, I really should get into using, and understanding macros anyway.
Thank you Everyone!
You can also just do this...
fd = open(argv[1], O_CREAT, 0775);[/QUOTE]
dwhitney67
February 15th, 2012, 07:27 PM
!! Aw man, I tried something like that the other day, but didnt have the 0 in 775! That does look much better!
Alas, I really should get into using, and understanding macros anyway.
Thank you Everyone!
You can also just do this...
fd = open(argv[1], O_CREAT, 0775);
When you prepend a 0 (zero) before the numeral 775, that number is treated as octal. Thus:
0775 = 7*64 + 7*8 + 5 = 509 (base 10)
Obviously 509 is different from 775.
ofnuts
February 15th, 2012, 10:54 PM
Am I alone in thinking that defining
#define m775 (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
isn't terribly helpful for the hoi polloi who don't know their access flags by heart, and that calling this macro "NO_WRITE_RIGHTS_FOR_OTHERS" would be a bit more explicit?
Arndt
February 16th, 2012, 04:12 AM
Am I alone in thinking that defining
#define m775 (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
isn't terribly helpful for the hoi polloi who don't know their access flags by heart, and that calling this macro "NO_WRITE_RIGHTS_FOR_OTHERS" would be a bit more explicit?
No, I agree. If you know the number is 775 and expect the readers to know it too, why not write that in the first place.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.