~emersion/mrsh

44efa8a8a7969bbb4341b9d87ac8ab7e7ef92d4f — Ben Brown 1 year, 2 months ago 29001c4
Very basic umask implementation

Doesn't yet support the -S flag, or symbolic modes, but does allow for
getting and setting the umask.
4 files changed, 39 insertions(+), 0 deletions(-)

M builtin/builtin.c
A builtin/umask.c
M include/builtin.h
M meson.build
M builtin/builtin.c => builtin/builtin.c +1 -0
@@ 31,6 31,7 @@ static const struct builtin builtins[] = {
	{ "times", builtin_times, true },
	{ "true", builtin_true, false },
	{ "type", builtin_type, false },
	{ "umask", builtin_umask, false },
	{ "unalias", builtin_unalias, false },
	{ "unset", builtin_unset, true },
};

A builtin/umask.c => builtin/umask.c +36 -0
@@ 0,0 1,36 @@
#define _POSIX_C_SOURCE 200809L
#include <mrsh/builtin.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "builtin.h"

/*
 * TODO: Implement -S flag and symbolic mode
 */

static const char umask_usage[] = "usage: umask [mode]\n";

int builtin_umask(struct mrsh_state *state, int argc, char *argv[]) {
	mode_t mode;
	mode_t default_mode = 0022;

	if (argc < 2) {
		mode = umask(default_mode);
		printf("%04o\n", mode);
		umask(mode);
		return EXIT_SUCCESS;
	}

	char *endptr;
	mode = strtol(argv[1], &endptr, 8);
	if (*endptr != '\0') {
		fprintf(stderr, "Invalid mode %s\n", argv[1]);
		fprintf(stderr, umask_usage);
		return EXIT_FAILURE;
	}

	umask(mode);
	return EXIT_SUCCESS;
}

M include/builtin.h => include/builtin.h +1 -0
@@ 24,6 24,7 @@ int builtin_shift(struct mrsh_state *state, int argc, char *argv[]);
int builtin_times(struct mrsh_state *state, int argc, char *argv[]);
int builtin_true(struct mrsh_state *state, int argc, char *argv[]);
int builtin_type(struct mrsh_state *state, int argc, char *argv[]);
int builtin_umask(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unalias(struct mrsh_state *state, int argc, char *argv[]);
int builtin_unset(struct mrsh_state *state, int argc, char *argv[]);


M meson.build => meson.build +1 -0
@@ 64,6 64,7 @@ lib_mrsh = library(
		'builtin/times.c',
		'builtin/true.c',
		'builtin/type.c',
		'builtin/umask.c',
		'builtin/unalias.c',
		'builtin/unset.c',
		'hashtable.c',