coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/chmod.c (142 lines, 3.8 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
// chmod.c, version 1.0.2
// OMKOV coreutils implementation of POSIX chmod
// Copyright (C) 2020, Jakob Wakeling
// MIT Licence

/*
	TODO Don't recursively change symlink permissions
	TODO Handle copying permission
*/

#include "util/error.h"
#include "util/mode.h"
#include "util/optget.h"

#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define VERSION "1.0.2"

static struct lop lops[] = {
	{ "help",    ARG_NUL, 256 },
	{ "version", ARG_NUL, 257 },
	{ NULL, 0, 0 }
};

static bool Rflag;

static mode_t mask;

static inline int   setmode(const char *path, chmod_t *m);
static inline char *mkpath(const char *path, const char *file);

static void hlp(void);
static void ver(void);

int main(int ac, char *av[]) { A0 = av[0];
	struct opt opt = OPTGET_INIT; opt.str = "R"; opt.lops = lops;
	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
	case 'R': { Rflag = true; break; }
	case 256: { hlp(); return 0; }
	case 257: { ver(); return 0; }
	default: { return 1; }
	}
	
	if (opt.ind >= ac - 1) { error(1, "missing operand"); }
	
	mask = umask(0); chmod_t *m = strmode(av[opt.ind]);
	if (!m) { error(1, "%s: invalid mode", av[opt.ind]); }
	
	for (char **p = &av[opt.ind + 1]; *p; ++p) {
		if (setmode(*p, m)) { warn("%s: %s", *p, serr()); warned = true; }
	}
	
	free(m); return warned;
}

/* Set the mode of the specified file */
static inline int setmode(const char *path, chmod_t *m) {
	struct stat st; register bool ln = false;
	
	// Get current permissions of file
	if (lstat(path, &st)) { return 1; };
	if (S_ISLNK(st.st_mode)) { if (stat(path, &st)) { return 1; } ln = true; }
	mode_t mode = st.st_mode;
	
	// Handle each chmod_t
	for (int i = 0;; ++i) switch (m[i].flag) {
	case MF_NORM: case MF_XIFX: {
		// If ref is null, use the file mode creation mask
		if (!m[i].ref) { m[i].ref = M_ALL & ~mask; }
		
		switch (m[i].op) {
		case '+': { mode |= m[i].ref & m[i].mod; break; }
		case '-': { mode &= (~(m[i].ref & m[i].mod) & 07777); break; }
		case '=': { mode &= ~m[i].ref; mode |= m[i].ref & m[i].mod; break; }
		}
		
		// If the XIFX flag is set, set execute permission accordingly
		if (m[i].flag == MF_XIFX) {
			if (S_ISDIR(mode) || mode & M_EX) { mode |= m[i].ref & M_EX; }
		} break;
	}
	case MF_COPY: { /* TODO */ }
	case MF_NULL: { goto end; }
	} end:;
	
	// Set permissions of the file to the calculated mode
	if (chmod(path, mode)) { return 1; }
	
	// Recurse into directories if R flag is set
	if (Rflag && !ln && S_ISDIR(st.st_mode)) {
		DIR *di; if (!(di = opendir(path))) { return 1; }
		
		// For each entry in directory, recursively call setmode
		for (struct dirent *de; (de = readdir(di));) {
			if (de->d_name[0] == '.' && (de->d_name[1] == 0 ||
				(de->d_name[1] == '.' && de->d_name[2] == 0))) { continue; }
			
			char *s = mkpath(path, de->d_name);
			if (setmode(s, m)) { warn("%s: %s", s, serr()); warned = true; }
			
			free(s);
		}
		
		closedir(di);
	}
	
	return 0;
}

/* Allocate and make path from two components */
static inline char *mkpath(const char *path, const char *file) {
	register size_t pl = strlen(path); char *s;
	s = (char *)malloc((pl + strlen(file) + 2) * sizeof (*s));
	strcpy(s, path); if (s[pl - 1] != '/') { s[pl] = '/'; s[++pl] = 0; }
	strcat(s, file); return s;
}

/* Print help information */
static void hlp(void) {
	puts("chmod - change the file modes\n");
	puts("usage: chmod [-R] mode file...\n");
	puts("options:");
	puts("  -R         Recursively change file mode bits");
	puts("  --help     Display help information");
	puts("  --version  Display version information");
}

/* Print version information */
static void ver(void) {
	puts("OMKOV coreutils chmod, version " VERSION);
	puts("Copyright (C) 2020, Jakob Wakeling");
	puts("MIT Licence (https://opensource.org/licenses/MIT)");
}