i-bash/builtins/umask.def

305 lines
6.8 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
This file is umask.def, from which is created umask.c.
It implements the builtin "umask" in Bash.
Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any later
version.
Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
$PRODUCES umask.c
$BUILTIN umask
$FUNCTION umask_builtin
1998-04-17 19:52:44 +00:00
$SHORT_DOC umask [-p] [-S] [mode]
1996-08-26 18:22:31 +00:00
The user file-creation mask is set to MODE. If MODE is omitted, or if
`-S' is supplied, the current value of the mask is printed. The `-S'
option makes the output symbolic; otherwise an octal number is output.
1998-04-17 19:52:44 +00:00
If `-p' is supplied, and MODE is omitted, the output is in a form
that may be used as input. If MODE begins with a digit, it is
interpreted as an octal number, otherwise it is a symbolic mode string
like that accepted by chmod(1).
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
#include "../bashtypes.h"
#include "../filecntl.h"
1998-04-17 19:52:44 +00:00
#ifndef _MINIX
# include <sys/file.h>
#endif
1996-12-23 17:02:34 +00:00
#if defined (HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include <stdio.h>
1996-08-26 18:22:31 +00:00
#include "../shell.h"
#include "../posixstat.h"
#include "common.h"
1996-12-23 17:02:34 +00:00
#include "bashgetopt.h"
1996-08-26 18:22:31 +00:00
/* **************************************************************** */
/* */
/* UMASK Builtin and Helpers */
/* */
/* **************************************************************** */
static void print_symbolic_umask ();
static int symbolic_umask ();
/* Set or display the mask used by the system when creating files. Flag
of -S means display the umask in a symbolic mode. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
umask_builtin (list)
WORD_LIST *list;
{
1998-04-17 19:52:44 +00:00
int print_symbolically, opt, umask_value, pflag;
1997-06-05 14:59:13 +00:00
mode_t umask_arg;
1996-08-26 18:22:31 +00:00
1998-04-17 19:52:44 +00:00
print_symbolically = pflag = 0;
1996-12-23 17:02:34 +00:00
reset_internal_getopt ();
1998-04-17 19:52:44 +00:00
while ((opt = internal_getopt (list, "Sp")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
case 'S':
1996-08-26 18:22:31 +00:00
print_symbolically++;
break;
1998-04-17 19:52:44 +00:00
case 'p':
pflag++;
break;
1996-12-23 17:02:34 +00:00
default:
builtin_usage ();
1996-08-26 18:22:31 +00:00
return (EX_USAGE);
}
}
1996-12-23 17:02:34 +00:00
list = loptend;
1996-08-26 18:22:31 +00:00
if (list)
{
if (digit (*list->word->word))
{
1996-12-23 17:02:34 +00:00
umask_value = read_octal (list->word->word);
1996-08-26 18:22:31 +00:00
/* Note that other shells just let you set the umask to zero
by specifying a number out of range. This is a problem
with those shells. We don't change the umask if the input
is lousy. */
1996-12-23 17:02:34 +00:00
if (umask_value == -1)
1996-08-26 18:22:31 +00:00
{
builtin_error ("`%s' is not an octal number from 000 to 777",
list->word->word);
return (EXECUTION_FAILURE);
}
}
else
{
1996-12-23 17:02:34 +00:00
umask_value = symbolic_umask (list);
if (umask_value == -1)
1996-08-26 18:22:31 +00:00
return (EXECUTION_FAILURE);
}
1997-06-05 14:59:13 +00:00
umask_arg = (mode_t)umask_value;
umask (umask_arg);
1996-08-26 18:22:31 +00:00
if (print_symbolically)
1997-06-05 14:59:13 +00:00
print_symbolic_umask (umask_arg);
1996-08-26 18:22:31 +00:00
}
else /* Display the UMASK for this user. */
{
1997-06-05 14:59:13 +00:00
umask_arg = umask (022);
umask (umask_arg);
1996-08-26 18:22:31 +00:00
1998-04-17 19:52:44 +00:00
if (pflag)
printf ("umask%s ", (print_symbolically ? " -S" : ""));
1996-08-26 18:22:31 +00:00
if (print_symbolically)
1997-06-05 14:59:13 +00:00
print_symbolic_umask (umask_arg);
1996-08-26 18:22:31 +00:00
else
1997-06-05 14:59:13 +00:00
printf ("%03o\n", umask_arg);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
1996-08-26 18:22:31 +00:00
fflush (stdout);
return (EXECUTION_SUCCESS);
}
/* Print the umask in a symbolic form. In the output, a letter is
printed if the corresponding bit is clear in the umask. */
static void
print_symbolic_umask (um)
1997-06-05 14:59:13 +00:00
mode_t um;
1996-08-26 18:22:31 +00:00
{
char ubits[4], gbits[4], obits[4]; /* u=rwx,g=rwx,o=rwx */
int i;
i = 0;
if ((um & S_IRUSR) == 0)
ubits[i++] = 'r';
if ((um & S_IWUSR) == 0)
ubits[i++] = 'w';
if ((um & S_IXUSR) == 0)
ubits[i++] = 'x';
ubits[i] = '\0';
i = 0;
if ((um & S_IRGRP) == 0)
gbits[i++] = 'r';
if ((um & S_IWGRP) == 0)
gbits[i++] = 'w';
if ((um & S_IXGRP) == 0)
gbits[i++] = 'x';
gbits[i] = '\0';
i = 0;
if ((um & S_IROTH) == 0)
obits[i++] = 'r';
if ((um & S_IWOTH) == 0)
obits[i++] = 'w';
if ((um & S_IXOTH) == 0)
obits[i++] = 'x';
obits[i] = '\0';
printf ("u=%s,g=%s,o=%s\n", ubits, gbits, obits);
}
/* Set the umask from a symbolic mode string similar to that accepted
by chmod. If the -S argument is given, then print the umask in a
symbolic form. */
static int
symbolic_umask (list)
WORD_LIST *list;
{
int um, umc, c;
int who, op, perm, mask;
char *s;
/* Get the initial umask. Don't change it yet. */
um = umask (022);
umask (um);
1996-12-23 17:02:34 +00:00
/* All work below is done with the complement of the umask -- it's
1996-08-26 18:22:31 +00:00
more intuitive and easier to deal with. It is complemented
again before being returned. */
umc = ~um;
s = list->word->word;
for (;;)
{
who = op = perm = mask = 0;
/* Parse the `who' portion of the symbolic mode clause. */
while (member (*s, "agou"))
{
switch (c = *s++)
{
case 'u':
who |= S_IRWXU;
continue;
case 'g':
who |= S_IRWXG;
continue;
case 'o':
who |= S_IRWXO;
continue;
case 'a':
who |= S_IRWXU | S_IRWXG | S_IRWXO;
continue;
default:
break;
}
}
/* The operation is now sitting in *s. */
op = *s++;
switch (op)
{
case '+':
case '-':
case '=':
break;
default:
builtin_error ("bad symbolic mode operator: %c", op);
return (-1);
}
/* Parse out the `perm' section of the symbolic mode clause. */
while (member (*s, "rwx"))
{
c = *s++;
switch (c)
{
case 'r':
perm |= S_IRUGO;
break;
case 'w':
perm |= S_IWUGO;
break;
case 'x':
perm |= S_IXUGO;
break;
}
}
/* Now perform the operation or return an error for a
bad permission string. */
if (!*s || *s == ',')
{
if (who)
perm &= who;
switch (op)
{
case '+':
umc |= perm;
break;
case '-':
umc &= ~perm;
break;
case '=':
umc &= ~who;
umc |= perm;
break;
1997-06-05 14:59:13 +00:00
#if 0
/* No other values are possible. */
1996-08-26 18:22:31 +00:00
default:
1996-12-23 17:02:34 +00:00
builtin_error ("bad symbolic mode operator: %c", op);
1996-08-26 18:22:31 +00:00
return (-1);
1997-06-05 14:59:13 +00:00
#endif
1996-08-26 18:22:31 +00:00
}
if (!*s)
{
um = ~umc & 0777;
break;
}
else
s++; /* skip past ',' */
}
else
{
builtin_error ("bad character in symbolic mode: %c", *s);
return (-1);
}
}
return (um);
}