Skip to content

Commit

Permalink
Code cleanups and API documentation updates
Browse files Browse the repository at this point in the history
darcs-hash:20051023121429-ac50b-6ff72171b5a90b6e398bd84e748388c1dba831d9.gz
  • Loading branch information
liljencrantz committed Oct 23, 2005
1 parent ba6ad50 commit f8de9de
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 67 deletions.
2 changes: 2 additions & 0 deletions doc_src/doc.hdr
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ certain environment variables.
- \c CDPATH, which is an array of directories in which to search for the new directory for the \c cd builtin.
- \c fish_color_normal, \c fish_color_command, \c fish_color_substitution, \c fish_color_redirection, \c fish_color_end, \c fish_color_error, \c fish_color_param, \c fish_color_comment, \c fish_color_match, \c fish_color_search_match, \c fish_color_cwd, \c fish_pager_color_prefix, \c fish_pager_color_completion, \c fish_pager_color_description and \c fish_pager_color_progress are used to change the color of various elements in \c fish. These variables are universal, i.e. when changing them, their new value will be used by all running fish sessions. The new value will also be retained when restarting fish.
- \c PATH, which is an array of directories in which to search for commands
- \c umask, which is the current file creation mask. The preffered way to change the umask variable is through the <a href="commands.html#umask">umask shellscript function</a>. An attempt to set umask to an invalid value will always fail.

\c fish also sends additional information to the user through the
values of certain environment variables. The user can not change the values of these variables. They are:
Expand Down Expand Up @@ -635,6 +636,7 @@ builtins or shellscript functions, and can only be used inside fish.
- <a href="builtins.html#switch">switch</a>, conditionally execute a block of commands
- <a href="commands.html#tokenize">tokenize</a>, split a string up into multiple tokens
- <a href="builtins.html#ulimit">ulimit</a>, set or get the shells resurce usage limits
- <a href="commandss.html#umask">umask</a>, set or get the file creation mask
- <a href="builtins.html#while">while</a>, perform a block of commands while a condition is met

For more information about these commands, use the <tt>--help</tt>
Expand Down
27 changes: 24 additions & 3 deletions doc_src/umask.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,30 @@ With no argument, the current file-creation mask is printed, if an
argument is specified, it is the new file creation mask. The mask may
be specified as an octal number, in which case it is interpreted as
the rights that should be masked away, i.e. it is the inverse of the
file permissions any new files will have. If a synbolic mask is
specified, the actual file permission bits, and not the inverse, are
specified.
file permissions any new files will have.

If a symbolic mask is specified, the actual file permission bits, and
not the inverse, should be specified. A symbolic mask is a comma
separated list of rights. Each right consists of three parts:

- The first part specifies to whom this set of right applies, and can
be one of \c u, \c g, \c o or \c a, where \c u specifies the user who
owns the file, \c g specifies the group owner of the file, \c o
specifiec other users rights and \c a specifies all three should be
changed.
- The second part of a right specifies the mode, and can be one of \c
=, \c + or \c -, where \c = specifies that the rights should be set to
the new value, \c + specifies that the specified right should be added
to those previously specified and \c - specifies that the specified
rights should be removed from those previously specified.
- The third part of a right specifies what rights should be changed
and can be any compination of \c r, \c w and \c x, representing
read, write and execute rights.

If the first and second parts are skipped, they are assumed to be \c a
and \c =, respectively. As an example, <code>r,u+w</code> means all
users should have read access and the file owner should also have
write access.

- <code>-h</code> or <code>--help</code> print this message
- <code>-S</code> or <code>--symbolic</code> prints the file-creation mask in symbolic form instead of octal form. Use <code>man chmod</code> for more information.
Expand Down
60 changes: 53 additions & 7 deletions env.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ static hash_table_t *global;
*/
static hash_table_t env_read_only;

/**
Table of variables whose value is dynamically calculated, such as umask, status, etc
*/
static hash_table_t env_electric;

/**
Exported variable array used by execv
*/
Expand Down Expand Up @@ -194,6 +199,21 @@ static void start_fishd()
sb_destroy( &cmd );
}

/**
Return the current umask value.
*/
static mode_t get_umask()
{
mode_t res;
res = umask( 0 );
umask( res );
return res;
}

/**
Universal variable callback function. This function makes sure the
proper events are triggered when an event occurs.
*/
static void universal_callback( int type,
const wchar_t *name,
const wchar_t *val )
Expand Down Expand Up @@ -254,6 +274,14 @@ void env_init()
hash_put( &env_read_only, L"COLUMNS", L"" );
hash_put( &env_read_only, L"PWD", L"" );

/*
Names of all dynamically calculated variables
*/
hash_init( &env_electric, &hash_wcs_func, &hash_wcs_cmp );
hash_put( &env_electric, L"history", L"" );
hash_put( &env_electric, L"status", L"" );
hash_put( &env_electric, L"umask", L"" );

/*
HOME and USER should be writeable by root, since this can be a
convenient way to install software.
Expand Down Expand Up @@ -398,17 +426,24 @@ void env_set( const wchar_t *key,
{
wchar_t *end;
int mask;


/*
Set the new umask
*/
if( val && wcslen(val) )
{
errno=0;
mask = wcstol( val, &end, 8 );

if( !errno && !*end )
if( !errno && (!*end) && (mask <= 0777) && (mask >= 0) )
{
umask( mask );
}
}
/*
Do not actually create a umask variable, on env_get, it will be calculated dynamically
*/
return;
}


Expand Down Expand Up @@ -665,6 +700,12 @@ wchar_t *env_get( const wchar_t *key )
sb_printf( &dyn_var, L"%d", proc_get_last_status() );
return (wchar_t *)dyn_var.buff;
}
else if( wcscmp( key, L"umask" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"0%0.3o", get_umask() );
return (wchar_t *)dyn_var.buff;
}

while( env != 0 )
{
Expand Down Expand Up @@ -703,7 +744,7 @@ int env_exist( const wchar_t *key )
env_node_t *env = top;
wchar_t *item;

if( hash_get( &env_read_only, key ) )
if( hash_get( &env_read_only, key ) || hash_get( &env_electric, key ) )
{
return 1;
}
Expand Down Expand Up @@ -807,6 +848,13 @@ static void add_to_hash( const void *k, void *aux )
0 );
}

static void add_key_to_list( const void * key,
const void * val,
void *aux )
{
al_push( (array_list_t *)aux, key );
}


void env_get_names( array_list_t *l, int flags )
{
Expand Down Expand Up @@ -853,11 +901,9 @@ void env_get_names( array_list_t *l, int flags )
hash_foreach2( &global_env->env,
add_key_to_hash,
&names );

if( get_names_show_unexported )
{
al_push( l, L"history" );
al_push( l, L"status" );
}
hash_foreach2( &env_electric, &add_key_to_list, l );

if( get_names_show_exported )
{
Expand Down
58 changes: 1 addition & 57 deletions init/fish_function.fish
Original file line number Diff line number Diff line change
Expand Up @@ -724,36 +724,6 @@ function type -d "Print the type of a command"
return $status
end
function __fish_umask_help
set bullet \*
if count $LANG >/dev/null
if test (expr match $LANG ".*UTF") -gt 0
set bullet \u2022
end
end
echo \tumask - Set or get the user file-creation mask
echo
echo (__bold Synopsis)
echo
echo \t(set_color $fish_color_command)umask(set_color normal) [OPTIONS] [mask]
echo
echo (__bold Description)
echo
echo \tWith no argument, the current file-creation mask is printed, if an\n\targument is specified, it is the new file creation mask.
echo
echo \t$bullet (__bold -h) or (__bold --help) print this message
echo \t$bullet (__bold -S) or (__bold --symbolic) prints the file-creation mask in symbolic\n\t\ \ form instead of octal form. Use \'(set_color $fish_color_command)man(set_color $fish_color_normal) chmod\' for more information.
echo \t$bullet (__bold -p) or (__bold --as-command) prints any output in a form that may be reused\n\t\ \ as input
echo
echo (__bold Example)
echo
echo \t\'(set_color $fish_color_command)umask(set_color normal) 600\' sets the file creation mask to read and write for the\n\towner and no permissions at all for any other users.
echo
end
function __fish_umask_parse -d "Parses a file permission specification as into an octal version"
# Test if already a valid octal mask, and pad it with zeros
if echo $argv | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
Expand All @@ -768,19 +738,6 @@ function __fish_umask_parse -d "Parses a file permission specification as into a
set -e implicit_all
# Make sure the current umask is defined
if not set -q umask
set umask 0000
end
# If umask is invalid, reset it
if not echo $umask | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
set umask 0000
end
# Pad umask with zeros
for i in (seq (echo 5-(echo $umask|wc -c)|bc)); set -- argv 0$umask; end
# Insert inverted umask into res variable
set tmp $umask
Expand Down Expand Up @@ -875,19 +832,6 @@ function __fish_umask_print_symbolic
set -l res ""
set -l letter a u g o
# Make sure the current umask is defined
if not set -q umask
set umask 0000
end
# If umask is invalid, reset it
if not echo $umask | grep -E '^(0|)[0-7]{1,3}$' >/dev/null
set umask 0000
end
# Pad umask with zeros
for i in (seq (echo 5-(echo $umask|wc -c)|bc)); set -- argv 0$umask; end
for i in 2 3 4
set res $res,$letter[$i]=
set val (echo $umask|cut -c $i)
Expand Down Expand Up @@ -933,7 +877,7 @@ function umask -d "Set default file permission mask"
switch $opt[1]
case -h --help
__fish_umask_help
help umask
return 0
case -p --as-command
Expand Down

0 comments on commit f8de9de

Please sign in to comment.