Results 1 to 10 of 10

Thread: stringification

  1. #1
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question stringification

    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.


    Code:
    #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~

  2. #2
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question Re: stringification

    By comparision, (this): totaly works but looks stupid:
    Code:
    #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;
    }
    Last edited by conradin; February 15th, 2012 at 06:55 PM. Reason: code block looks stupid
    ~Conradin~

  3. #3
    Join Date
    Dec 2006
    Location
    Linköping, Sweden
    Beans
    195
    Distro
    Kubuntu 9.10 Karmic Koala

    Re: stringification

    You are sending the address of modez to open instead of what you really mean.

    Here is a simple correct version.
    Code:
    #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;
    }

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: stringification

    Well, you are giving open() a string where it expects an integer... What you want is

    Code:
    #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...

    Code:
    fd = open(argv[1], O_CREAT, 0775);
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    May 2006
    Beans
    1,787

    Re: stringification

    Quote Originally Posted by McNils View Post
    Code:
    #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)

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: stringification

    Quote Originally Posted by conradin View Post
    By comparision, (this): totaly works but looks stupid:
    Code:
    #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.

  7. #7
    Join Date
    Nov 2008
    Location
    Maine
    Beans
    1,125
    Distro
    Ubuntu 10.04 Lucid Lynx

    Talking Re: stringification

    !! 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...

    Code:
    fd = open(argv[1], O_CREAT, 0775);
    [/QUOTE]
    ~Conradin~

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,274
    Distro
    Kubuntu

    Re: stringification

    Quote Originally Posted by conradin View Post
    !! 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...

    Code:
    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.

  9. #9
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: stringification

    Am I alone in thinking that defining
    Code:
    #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?

  10. #10
    Join Date
    May 2006
    Beans
    1,787

    Re: stringification

    Quote Originally Posted by ofnuts View Post
    Am I alone in thinking that defining
    Code:
    #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.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •