Thread: umask

  1. #1
    Stewdent
    Guest

    umask

    i am working on my own shell and am having trouble working with some of the built-in commands

    does anyone have any suggestions on what would be the best way to implement the umask function

    thanks

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    well, the man page gives you
    SYNOPSIS
    #include <sys/types.h>
    #include <sys/stat.h>

    mode_t umask(mode_t mask);
    so play around with those headers and that struct.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    102
    Well you need to understand permission bits.
    There are 3 general permission bytes.
    user,group,world

    Each has a different setting
    with chmod you add the buts up to get what you want
    like bit 1 == execute
    bit 2 == write
    bit 4 == read

    So if you want to use make a file world readable and user writable,readable, and executable, with the group having no permisisons you would to this

    chmod 701 $FILENAME

    Now umask (or usermask) will actually change the default permissions, opposite of what you want.

    a umake of 077 will make a new file have the chmod of 700.

    It masks off the bits with new file.

    The only difficulty is to rembemer that umask works opposite of chmod.

    I would recomend that you read the chmod manpage, as it will explain how to use chmod to modify the permissions on a file that already exists. umask works on new files.

    To demo, in a shell do this
    #umask 057 && touch test1
    #umask 077 && touch test2
    # umake 500 && touch test3
    # ls -l test*

    and you can see the differences in the permission of each file and how the umake will change it.

    HTH

    Squid

  4. #4
    Stewdent
    Guest
    okay..let me be a little more clear

    below is the code for a function I am trying to write

    the input will be from the command line

    a string of the form "umask 123"

    needless to say it is not working. If someone could point out the error in my ways i would appreciate it.

    Code:
    void my_umask( int argc, char * argv[] )
    {
        char num[4];
        long len, n1,n2,n3, flag = 0,n, old_mask;
        
        if(argc > 2)
            my_error("too many arguments.");
        else if(argc == 2)
        {
            len = strlen(argv[1]);
            
            if( len > 3 )
            {
               my_error("unrecognized argument for umask.");
               return;
            }
            else
            {
               strcpy( num, (argv[1]) );
               n=atol(num);
                         
               n1 = n/100;
               n2 = ( n - (n1*100) )/ 10;
               n3 = ( n - ((n1*100) + (n2*10)) );
              
               if( n1>=0 && n1<=7)
                  if( n2>=0 && n2<=7)
                     if( n3>=0 && n3<=7)  
                     {
                        flag = 1;
                        mask = n;
                        umask(n);
                     }//end if
               
               if(flag != 1)
               {
                  my_error("illegal argument for umask.");
                  return;
               }
            }
        }
        else  //argc==1  print the value of mask
            umask();    //not doing anything?????
        return;
    }

Popular pages Recent additions subscribe to a feed