refactor and add su

This commit is contained in:
Freya Murphy 2023-05-14 21:43:02 -04:00
parent c68d7136fe
commit e735ad6710
40 changed files with 570 additions and 293 deletions

View file

@ -15,6 +15,7 @@ CCFLAGS += -DMAJOR=$(MAJOR) -DMINOR=$(MINOR) -DPATCH=$(PATCH) -DCHECK_LINK
CCFLAGS += $(INCFLAGS) CCFLAGS += $(INCFLAGS)
LDFLAGS = -s LDFLAGS = -s
LDFLAGS = -lcrypt
LDFLAGS += $(INCFLAGS) LDFLAGS += $(INCFLAGS)
UNAME := $(shell uname) UNAME := $(shell uname)
@ -53,6 +54,8 @@ clean:
install: install:
cp $(APP) /usr/local/bin/lazysphere cp $(APP) /usr/local/bin/lazysphere
chown root:root /usr/local/bin/lazysphere
chmod 6711 /usr/local/bin/lazysphere
uninstall: uninstall:
rm /usr/local/bin/lazysphere rm /usr/local/bin/lazysphere

View file

@ -1,4 +1,3 @@
#include "args.h"
#include "command.h" #include "command.h"
#include "lslib.h" #include "lslib.h"
@ -7,7 +6,6 @@
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <string.h> #include <string.h>
enum method { enum method {
@ -27,7 +25,7 @@ static struct {
static void help (void) { static void help (void) {
printf("Usage: chmod [-Rcvf] MODE[,MODE]... FILE...\n\n"); printf("Usage: chmod [-Rcvf] MODE[,MODE]... FILE...\n\n");
printf("MODE is octal number (bit pattern sstrwxrwxrwx) or [ugoa]{+|-|=}[rwxXst]\n\n"); printf("MODE is octal number (bit pattern sstrwxrwxrwx) or {+|-|=}[rwxXst]\n\n");
printf("\t-R\tRecurse\n"); printf("\t-R\tRecurse\n");
printf("\t-c\tList changed files\n"); printf("\t-c\tList changed files\n");
printf("\t-v\tVerbose\n"); printf("\t-v\tVerbose\n");
@ -73,7 +71,7 @@ static void chmod_file(char* path) {
if (lstat(get_path_buffer(), &s) < 0) { if (lstat(get_path_buffer(), &s) < 0) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot stat '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;
@ -89,7 +87,7 @@ static void chmod_file(char* path) {
if (chmod(get_path_buffer(), mode) < 0) { if (chmod(get_path_buffer(), mode) < 0) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot chmod '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot chmod '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;
@ -110,7 +108,7 @@ static void chmod_file(char* path) {
d = opendir(get_path_buffer()); d = opendir(get_path_buffer());
if (d == NULL) { if (d == NULL) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot open dir '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot open dir '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;

View file

@ -6,7 +6,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
@ -60,7 +59,7 @@ static void chown_file(char* path) {
if (chown(get_path_buffer(), flags.uid, flags.gid) < 0) { if (chown(get_path_buffer(), flags.uid, flags.gid) < 0) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot chown '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot chown '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;
@ -75,7 +74,7 @@ static void chown_file(char* path) {
if (lstat(get_path_buffer(), &s) < 0) { if (lstat(get_path_buffer(), &s) < 0) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot stat '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;
@ -89,7 +88,7 @@ static void chown_file(char* path) {
d = opendir(get_path_buffer()); d = opendir(get_path_buffer());
if (d == NULL) { if (d == NULL) {
if (!flags.quiet) { if (!flags.quiet) {
error_s("cannot open dir '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot open dir '%s'", get_path_buffer());
} }
pop_path_buffer(save); pop_path_buffer(save);
return; return;

View file

@ -2,12 +2,12 @@
#include "lslib.h" #include "lslib.h"
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h>
#include <unistd.h> #include <unistd.h>
static struct { static struct {
@ -81,7 +81,7 @@ static bool copy_file(char* from, char* to) {
static bool symlink_file(char* from, char* to) { static bool symlink_file(char* from, char* to) {
if (symlink(from, to) < 0) { if (symlink(from, to) < 0) {
error_s("failed to symlink '%s': %s", from, strerror(errno)); error_s("failed to symlink '%s'", from);
return false; return false;
} else if (flags.verbose) { } else if (flags.verbose) {
output("symlinked '%s'", from); output("symlinked '%s'", from);
@ -91,7 +91,7 @@ static bool symlink_file(char* from, char* to) {
static bool hardlink_file(char* from, char* to) { static bool hardlink_file(char* from, char* to) {
if (link(from, to) < 0) { if (link(from, to) < 0) {
error_s("failed to hardlink '%s': %s", from, strerror(errno)); error_s("failed to hardlink '%s'", from);
return false; return false;
} else if (flags.verbose) { } else if (flags.verbose) {
output("hardlinked '%s'", from); output("hardlinked '%s'", from);
@ -117,12 +117,12 @@ static void run_copy(struct stat* s) {
if (!flags.preserve) return; if (!flags.preserve) return;
if (chmod(to, s->st_mode) < 0) { if (chmod(to, s->st_mode) < 0) {
error_s("cannot chmod '%s': %s", to, strerror(errno)); error_s("cannot chmod '%s'", to);
return; return;
} }
if (chown(to, s->st_uid, s->st_gid) < 0) { if (chown(to, s->st_uid, s->st_gid) < 0) {
error_s("cannot chown '%s': %s", to, strerror(errno)); error_s("cannot chown '%s'", to);
return; return;
} }
} }
@ -140,14 +140,14 @@ static void cp_directory(struct stat* s) {
} }
if (mkdir(get_path_buffer_2(), s->st_mode) < 0 && errno != EEXIST) { if (mkdir(get_path_buffer_2(), s->st_mode) < 0 && errno != EEXIST) {
error_s("cannot create directory '%s': %s", get_path_buffer_2(), strerror(errno)); error_s("cannot create directory '%s'", get_path_buffer_2());
return; return;
} }
d = opendir(get_path_buffer()); d = opendir(get_path_buffer());
if (d == NULL) { if (d == NULL) {
error_s("cannot open directory '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot open directory '%s'", get_path_buffer());
return; return;
} }
@ -183,7 +183,7 @@ static void cp_file(char* path) {
struct stat s; struct stat s;
if (lstat(get_path_buffer(), &s) < 0) { if (lstat(get_path_buffer(), &s) < 0) {
pop_path_buffer(save); pop_path_buffer(save);
error_s("cannot stat '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot stat '%s'", get_path_buffer());
return; return;
} }
@ -218,7 +218,7 @@ COMMAND(cp) {
if (argc - start == 2) { if (argc - start == 2) {
struct stat s; struct stat s;
if (lstat(argv[start], &s) < 0) { if (lstat(argv[start], &s) < 0) {
error("cannot stat '%s': %s", argv[start], strerror(errno)); error("cannot stat '%s'", argv[start]);
} }
push_path_buffer(argv[argc-2]); push_path_buffer(argv[argc-2]);
push_path_buffer_2(argv[argc-1]); push_path_buffer_2(argv[argc-1]);
@ -234,7 +234,7 @@ COMMAND(cp) {
push_path_buffer_2(argv[argc-1]); push_path_buffer_2(argv[argc-1]);
if (lstat(get_path_buffer_2(), &s) < 0) { if (lstat(get_path_buffer_2(), &s) < 0) {
error("target: '%s': %s", get_path_buffer_2(), strerror(errno)); error("target: '%s'", get_path_buffer_2());
} }
for (i = start; i < argc - 1; i++) { for (i = start; i < argc - 1; i++) {

View file

@ -48,7 +48,7 @@ COMMAND(dd) {
} }
} }
buffer = malloc(bs); buffer = xalloc(bs);
while ((read = fread(buffer, 1, bs, in_file)) != 0) { while ((read = fread(buffer, 1, bs, in_file)) != 0) {
fwrite(buffer, 1, read, out_file); fwrite(buffer, 1, read, out_file);

View file

@ -76,7 +76,7 @@ static bool parse_regex(char** end, struct LineAddress* address, enum RegexDirec
cap = 8; cap = 8;
siz = 0; siz = 0;
buf = malloc(cap * sizeof(long int)); buf = xalloc(cap * sizeof(long int));
i = (dir == ALL ? 0 : line_current); i = (dir == ALL ? 0 : line_current);
until = (dir == BEFORE ? 0 : line_count - 1); until = (dir == BEFORE ? 0 : line_count - 1);
@ -88,7 +88,7 @@ static bool parse_regex(char** end, struct LineAddress* address, enum RegexDirec
} }
if (cap == siz) { if (cap == siz) {
cap *= 2; cap *= 2;
buf = realloc(buf, cap * sizeof(long int)); buf = xrealloc(buf, cap * sizeof(long int));
} }
buf[siz] = i; buf[siz] = i;
siz++; siz++;
@ -112,7 +112,7 @@ static void free_address (struct LineAddress address) {
static void expand_buffer(long int** buf, unsigned long* cap, unsigned long* size) { static void expand_buffer(long int** buf, unsigned long* cap, unsigned long* size) {
if (*cap == *size) { if (*cap == *size) {
*cap *= 2; *cap *= 2;
*buf = realloc(*buf, sizeof(long int) * *cap); *buf = xrealloc(*buf, sizeof(long int) * *cap);
} }
} }
@ -127,7 +127,7 @@ static bool parse_regex_lines(char** end, struct LineAddress* address) {
cap = 8; cap = 8;
siz = 0; siz = 0;
buf = malloc(cap * sizeof(long int)); buf = xalloc(cap * sizeof(long int));
addr = *address; addr = *address;
if (addr.type == INDEX) { if (addr.type == INDEX) {
@ -310,7 +310,7 @@ static void load_empty(void) {
free_data(false); free_data(false);
line_capacity = 8; line_capacity = 8;
lines = malloc(sizeof(char*) * line_capacity); lines = xalloc(sizeof(char*) * line_capacity);
line_count = 0; line_count = 0;
line_current = 0; line_current = 0;
@ -320,7 +320,7 @@ static void load_empty(void) {
static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsigned long* size) { static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsigned long* size) {
unsigned long cap = 8; unsigned long cap = 8;
unsigned long siz = 0; unsigned long siz = 0;
char** buf = malloc(sizeof(char*) * cap); char** buf = xalloc(sizeof(char*) * cap);
char* line = NULL; char* line = NULL;
size_t offset = 0; size_t offset = 0;
@ -329,7 +329,7 @@ static void get_input(FILE* file, char*** buffer, unsigned long* capacity, unsig
while (getline(&line, &offset, file) != -1) { while (getline(&line, &offset, file) != -1) {
if (cap == siz) { if (cap == siz) {
cap *= 2; cap *= 2;
buf = realloc(buf, sizeof(char*) * cap); buf = xrealloc(buf, sizeof(char*) * cap);
} }
buf[siz] = line; buf[siz] = line;
siz++; siz++;
@ -400,7 +400,7 @@ static void expand(unsigned long count) {
if (count < line_capacity) return; if (count < line_capacity) return;
line_capacity *= 2; line_capacity *= 2;
if (count > line_capacity) line_capacity = count; if (count > line_capacity) line_capacity = count;
lines = realloc(lines, line_capacity * sizeof(char*)); lines = xrealloc(lines, line_capacity * sizeof(char*));
} }
static void append_lines(unsigned long index, char** new, unsigned long new_len) { static void append_lines(unsigned long index, char** new, unsigned long new_len) {
@ -495,9 +495,9 @@ static bool get_file_name(char** filename) {
len--; len--;
} }
if (default_filename != NULL) { if (default_filename != NULL) {
default_filename = realloc(default_filename, len + 1); default_filename = xrealloc(default_filename, len + 1);
} else { } else {
default_filename = malloc(len + 1); default_filename = xalloc(len + 1);
} }
memcpy(default_filename, *filename, len + 1); memcpy(default_filename, *filename, len + 1);
*filename = default_filename; *filename = default_filename;
@ -561,7 +561,7 @@ static void read_file(char* filename) {
capacity = 8; capacity = 8;
size = 0; size = 0;
buf = malloc(capacity * sizeof(char*)); buf = xalloc(capacity * sizeof(char*));
get_input(file, &buf, &capacity, &size); get_input(file, &buf, &capacity, &size);
if (size < 1) { if (size < 1) {
@ -584,7 +584,7 @@ static void expand_string(char** buf, int* capacity, int* size, char* text, int
if (*size + len >= *capacity) { if (*size + len >= *capacity) {
*capacity *= 2; *capacity *= 2;
if (*capacity < *size + len) *capacity = *size + len; if (*capacity < *size + len) *capacity = *size + len;
*buf = realloc(*buf, *capacity); *buf = xrealloc(*buf, *capacity);
} }
memcpy(*buf + *size, text, len); memcpy(*buf + *size, text, len);
*size += len; *size += len;
@ -593,7 +593,7 @@ static void expand_string(char** buf, int* capacity, int* size, char* text, int
static int substute_string(long int index, long int matches, re_t regex, char* sub, int sub_len) { static int substute_string(long int index, long int matches, re_t regex, char* sub, int sub_len) {
int capacity = 8; int capacity = 8;
int size = 0; int size = 0;
char* buf = malloc(sizeof(char) * capacity); char* buf = xalloc(sizeof(char) * capacity);
long int left; long int left;
int offset = 0; int offset = 0;

View file

@ -1,13 +1,12 @@
#include "args.h"
#include "command.h" #include "command.h"
#include "lslib.h" #include "lslib.h"
#include <errno.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
static void help (void) { static void help (void) {
printf("Usage: groups [USER]\n\n"); printf("Usage: groups [USER]\n\n");
@ -34,14 +33,14 @@ COMMAND(groups) {
if (errno == 0) { if (errno == 0) {
error("user not found"); error("user not found");
} else { } else {
error("failed to fetch groups: %s", strerror(errno)); error("failed to fetch groups");
} }
} }
ngroups = 0; ngroups = 0;
getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups);
groups = malloc(sizeof(gid_t) * ngroups); groups = xalloc(sizeof(gid_t) * ngroups);
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
for (i = 0; i < ngroups; i++){ for (i = 0; i < ngroups; i++){
@ -49,7 +48,7 @@ COMMAND(groups) {
if(gr == NULL) { if(gr == NULL) {
free(groups); free(groups);
error("failed to fetch groups: %s", strerror(errno)); error("failed to fetch groups");
} }
printf("%s ",gr->gr_name); printf("%s ",gr->gr_name);

View file

@ -1,13 +1,12 @@
#include "args.h"
#include "command.h" #include "command.h"
#include "lslib.h" #include "lslib.h"
#include <errno.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
static void help (void) { static void help (void) {
printf("Usage: id [USER]\n\n"); printf("Usage: id [USER]\n\n");
@ -35,7 +34,7 @@ COMMAND(user_id) {
if (errno == 0) { if (errno == 0) {
error("user not found"); error("user not found");
} else { } else {
error("failed to fetch groups: %s", strerror(errno)); error("failed to fetch groups");
} }
} }
@ -44,7 +43,7 @@ COMMAND(user_id) {
ngroups = 0; ngroups = 0;
getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups); getgrouplist(pw->pw_name, pw->pw_gid, NULL, &ngroups);
groups = malloc(sizeof(gid_t) * ngroups); groups = xalloc(sizeof(gid_t) * ngroups);
getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups);
gid = pw->pw_gid; gid = pw->pw_gid;
@ -60,7 +59,7 @@ COMMAND(user_id) {
struct group* gr = getgrgid(groups[i]); struct group* gr = getgrgid(groups[i]);
if(gr == NULL) { if(gr == NULL) {
free(groups); free(groups);
error("failed to fetch groups: %s", strerror(errno)); error("failed to fetch groups");
} }
printf("%d(%s)", gr->gr_gid, gr->gr_name); printf("%d(%s)", gr->gr_gid, gr->gr_name);

View file

@ -66,9 +66,9 @@ static DIR* get_directory(char* path) {
DIR* d = opendir(path); DIR* d = opendir(path);
if (d == NULL) { if (d == NULL) {
if (errno == ENOTDIR) { if (errno == ENOTDIR) {
error_s("`%s` is a a file\n", path); error_s("`%s` is a a file", path);
} else { } else {
error_s("failed to open directory '%s': %s\n", path, strerror(errno)); error_s("failed to open directory '%s'", path);
} }
} }
return d; return d;
@ -90,7 +90,7 @@ static bool get_file_info(const char* file_name, struct FileInfo* info) {
save = push_path_buffer(file_name); save = push_path_buffer(file_name);
if (lstat(get_path_buffer(), &s) < 0) { if (lstat(get_path_buffer(), &s) < 0) {
error_s("failed to read file '%s': %s\n", get_path_buffer(), strerror(errno)); error_s("failed to read file '%s'", get_path_buffer());
pop_path_buffer(save); pop_path_buffer(save);
return false; return false;
} }
@ -199,7 +199,7 @@ static bool get_file_info(const char* file_name, struct FileInfo* info) {
info->type = ty; info->type = ty;
file_len = strlen(file_name) + 1; file_len = strlen(file_name) + 1;
info->name = malloc(file_len); info->name = xalloc(file_len);
memcpy(info->name, file_name, file_len); memcpy(info->name, file_name, file_len);
print_file_size(s.st_size, info->size); print_file_size(s.st_size, info->size);
@ -340,7 +340,7 @@ static void push_file(
if (*size == *capacity) { if (*size == *capacity) {
*capacity *= 2; *capacity *= 2;
*files = realloc(*files, sizeof(struct FileInfo) * *capacity); *files = xrealloc(*files, sizeof(struct FileInfo) * *capacity);
} }
user_len = strlen(finfo.usr->pw_name); user_len = strlen(finfo.usr->pw_name);
@ -382,7 +382,7 @@ static void recurse_directory(char* dir_name) {
capacity = 8; capacity = 8;
size = 0; size = 0;
files = malloc(sizeof(struct FileInfo) * capacity); files = xalloc(sizeof(struct FileInfo) * capacity);
memset(&info, 0, sizeof(struct FileListInfo)); memset(&info, 0, sizeof(struct FileListInfo));
while((file = readdir(d)) != NULL) { while((file = readdir(d)) != NULL) {
@ -434,7 +434,7 @@ static void list_directory(char* path) {
capacity = 8; capacity = 8;
size = 0; size = 0;
files = malloc(sizeof(struct FileInfo) * capacity); files = xalloc(sizeof(struct FileInfo) * capacity);
memset(&info, 0, sizeof(struct FileListInfo)); memset(&info, 0, sizeof(struct FileListInfo));
while ((file = readdir(d)) != NULL) { while ((file = readdir(d)) != NULL) {
@ -466,7 +466,7 @@ static void list_file_args(int start, int argc, char** argv) {
capacity = 8; capacity = 8;
size = 0; size = 0;
files = malloc(sizeof(struct FileInfo) * capacity); files = xalloc(sizeof(struct FileInfo) * capacity);
memset(&info, 0, sizeof(struct FileListInfo)); memset(&info, 0, sizeof(struct FileListInfo));
for (i = start; i < argc; i++) { for (i = start; i < argc; i++) {

View file

@ -38,7 +38,7 @@ static bool mkdir_parents(char* path) {
if (path[i] != '/') continue; if (path[i] != '/') continue;
path[i] = '\0'; path[i] = '\0';
if (mkdir(path, flags.mode) < 0 && errno != EEXIST) { if (mkdir(path, flags.mode) < 0 && errno != EEXIST) {
error_s("failed to create directory '%s': %s", path, strerror(errno)); error_s("failed to create directory '%s'", path);
return false; return false;
}; };
path[i] = '/'; path[i] = '/';
@ -62,7 +62,7 @@ COMMAND(makedir) {
continue; continue;
} }
if (mkdir(argv[i], flags.mode) < 0) { if (mkdir(argv[i], flags.mode) < 0) {
error_s("failed to create directory '%s': %s", argv[i], strerror(errno)); error_s("failed to create directory '%s'", argv[i]);
} }
} }

View file

@ -64,7 +64,7 @@ static void mv_dir(bool exists) {
} }
if (rename(get_path_buffer(), get_path_buffer_2()) < 0) { if (rename(get_path_buffer(), get_path_buffer_2()) < 0) {
error_s("cannot move '%s': %s", get_path_buffer(), strerror(errno)); error_s("cannot move '%s'", get_path_buffer());
} else if (flags.verbose) { } else if (flags.verbose) {
output("moved '%s'", get_path_buffer()); output("moved '%s'", get_path_buffer());
} }
@ -91,7 +91,7 @@ COMMAND(mv) {
dest = true; dest = true;
if (lstat(get_path_buffer_2(), &s) < 0 && argc - start > 2) { if (lstat(get_path_buffer_2(), &s) < 0 && argc - start > 2) {
dest = false; dest = false;
error("cannot stat '%s': %s", get_path_buffer_2(), strerror(errno)); error("cannot stat '%s'", get_path_buffer_2());
} }
if (dest && flags.refuse_if_dir) { if (dest && flags.refuse_if_dir) {

View file

@ -52,7 +52,7 @@ static bool rm_dir (void) {
d = opendir(get_path_buffer()); d = opendir(get_path_buffer());
if (d == NULL) { if (d == NULL) {
error_s("failed to stat '%s': %s\n", get_path_buffer(), strerror(errno)); error_s("failed to stat '%s'", get_path_buffer());
return false; return false;
} }
@ -71,7 +71,7 @@ static void rm_file(char* path) {
struct stat s; struct stat s;
if (lstat(get_path_buffer(), &s) < 0) { if (lstat(get_path_buffer(), &s) < 0) {
pop_path_buffer(save); pop_path_buffer(save);
error_s("failed to stat '%s': %s\n", get_path_buffer(), strerror(errno)); error_s("failed to stat '%s'", get_path_buffer());
return; return;
} }
@ -102,7 +102,7 @@ static void rm_file(char* path) {
} }
if (remove(get_path_buffer()) < 0) { if (remove(get_path_buffer()) < 0) {
error_s("failed to delete '%s': %s\n", get_path_buffer(), strerror(errno)); error_s("failed to delete '%s'", get_path_buffer());
} else if (flags.verbose) { } else if (flags.verbose) {
output("deleted '%s'\n", get_path_buffer()); output("deleted '%s'\n", get_path_buffer());
} }

104
command/su.c Normal file
View file

@ -0,0 +1,104 @@
#include "command.h"
#include "lslib.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static struct {
bool clear_env;
bool update_env;
char* shell;
char* user;
} flags;
static bool restricted_shell(const char *shell) {
char *line;
int result = true;
while ((line = getusershell()) != NULL) {
if (strcmp(line, shell) == 0) {
result = false;
break;
}
}
endusershell();
return result;
}
static void help (void) {
printf("Usage: su [-lmp] [-s SH] [-] [USER [FILE ARGS]] \n\n");
printf("Run shell under USER (by default, root)\n\n");
printf("\t-,-l\tClear environment, go to home dir, run shell as login shell\n");
printf("\t-p,-m\tDo not set new $HOME, $SHELL, $USER, $LOGNAME\n");
printf("\t-s SH Shell\tto use instead of user's default\n");
}
static int short_arg (char c, char* next) {
switch (c) {
case 'l':
flags.clear_env = true;
break;
case 'p':
case 'm':
flags.update_env = false;
break;
case 's':
check_arg(next);
flags.shell = next;
return ARG_USED;
default:
return ARG_INVALID;
}
return ARG_UNUSED;
}
COMMAND (su) {
int start, res;
uid_t cur_uid;
struct passwd* pw;
flags.clear_env = false;
flags.update_env = true;
flags.shell = NULL;
flags.user = "root";
start = parse_args(argc, argv, help, short_arg, NULL);
if (start < argc && streql(argv[start], "-")) {
flags.clear_env = true;
start++;
}
if (start < argc)
flags.user = argv[start++];
pw = xgetpwnam(flags.user);
cur_uid = getuid();
if (cur_uid != 0) {
res = prompt_password(pw, "Password: ");
} else {
res = PASSWORD_VALID;
}
if (res != PASSWORD_VALID) {
error("invalid password");
}
if (flags.shell && cur_uid != 0 && pw->pw_shell && restricted_shell(pw->pw_shell)) {
error_s("using restricted shell");
flags.shell = NULL;
}
if (!flags.shell)
flags.shell = pw->pw_shell;
change_identity(pw);
setup_environment(flags.shell, flags.update_env, flags.clear_env, pw);
exec_shell(flags.shell, flags.clear_env, (const char**) &argv[start]);
return EXIT_FAILURE; /* */
}

View file

@ -1,12 +1,9 @@
#include "args.h"
#include "command.h" #include "command.h"
#include "lslib.h" #include "lslib.h"
#define _X_OPEN_SOURCE 500
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#include <string.h> #include <string.h>
static struct { static struct {
@ -58,7 +55,7 @@ static bool sync_file(char* path) {
} else { } else {
ret = flags.sync_data ? fdatasync(fd) : fsync(fd); ret = flags.sync_data ? fdatasync(fd) : fsync(fd);
if (ret < 0) { if (ret < 0) {
error_s("failed to sync '%s': %s", path, strerror(errno)); error_s("failed to sync '%s'", path);
} }
} }

View file

@ -23,10 +23,10 @@ static size_t tail_file_lines(FILE* file, unsigned int count, size_t skip) {
size_t len; size_t len;
char* line; char* line;
ring = malloc(sizeof(char*) * count); ring = xalloc(sizeof(char*) * count);
memset(ring, 0, sizeof(char*) * count); memset(ring, 0, sizeof(char*) * count);
ring_len = malloc(sizeof(int) * count); ring_len = xalloc(sizeof(int) * count);
index = 0; index = 0;
size = 0; size = 0;
@ -74,7 +74,7 @@ static size_t tail_file_chars(FILE* file, unsigned int count, size_t skip) {
unsigned int size, i; unsigned int size, i;
int read, c; int read, c;
ring = malloc(sizeof(char) * count); ring = xalloc(sizeof(char) * count);
memset(ring, 0, count); memset(ring, 0, count);
index = 0; index = 0;

View file

@ -71,7 +71,7 @@ COMMAND(tee_cmd) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
files = malloc(sizeof(FILE*) * (argc - start)); files = xalloc(sizeof(FILE*) * (argc - start));
for (i = start; i < argc; i++) { for (i = start; i < argc; i++) {
FILE* file = get_file(argv[i], flags.append ? "a" : "w"); FILE* file = get_file(argv[i], flags.append ? "a" : "w");

View file

@ -1,7 +1,6 @@
#include "command.h" #include "command.h"
#include "lslib.h" #include "lslib.h"
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -76,7 +75,7 @@ char* read_next(FILE* file, int arg_count) {
size = 0; size = 0;
capacity = 8; capacity = 8;
buf = malloc(sizeof(char) * capacity); buf = xalloc(sizeof(char) * capacity);
while(c = getc(file), true) { while(c = getc(file), true) {
if (c == EOF && size == 0) { if (c == EOF && size == 0) {
@ -86,7 +85,7 @@ char* read_next(FILE* file, int arg_count) {
if (size == capacity) { if (size == capacity) {
capacity *= 2; capacity *= 2;
buf = realloc(buf, sizeof(char) * capacity); buf = xrealloc(buf, sizeof(char) * capacity);
} }
if (c == '\0' || c == EOF || (!flags.null_seperated && c == '\n')) { if (c == '\0' || c == EOF || (!flags.null_seperated && c == '\n')) {
@ -104,7 +103,7 @@ void read_args(FILE* file, char*** args, int* size, int* capacity) {
while (arg = read_next(file, read), true) { while (arg = read_next(file, read), true) {
if (*size == *capacity) { if (*size == *capacity) {
*capacity *= 2; *capacity *= 2;
*args = realloc(*args, sizeof(char*) * *capacity); *args = xrealloc(*args, sizeof(char*) * *capacity);
} }
(*args)[(*size)++] = arg; (*args)[(*size)++] = arg;
read++; read++;
@ -146,7 +145,7 @@ COMMAND(xargs) {
size = arg_on_stack_count + 1; size = arg_on_stack_count + 1;
capacity = size + 8; capacity = size + 8;
args = malloc(sizeof(char*) * capacity); args = xalloc(sizeof(char*) * capacity);
args[0] = command; args[0] = command;
memcpy(&args[1], &argv[arg_start], arg_on_stack_count * sizeof(char*)); memcpy(&args[1], &argv[arg_start], arg_on_stack_count * sizeof(char*));
read_args(flags.file, &args, &size, &capacity); read_args(flags.file, &args, &size, &capacity);
@ -178,7 +177,7 @@ COMMAND(xargs) {
} }
if (execvp(command, args) == -1) { if (execvp(command, args) == -1) {
error("error: failed to execute command: %s", strerror(errno)); error("failed to execute command");
} }
cleanup: cleanup:

View file

@ -1,6 +1,4 @@
#include "args.h" #include "lslib.h"
#include "error.h"
#include "convert.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View file

@ -1,18 +0,0 @@
#ifndef ARGS_H
#define ARGS_H
#define UNUSED(x) (void)(x)
#define ARG_UNUSED 0
#define ARG_USED 1
#define ARG_IGNORE 2
#define ARG_INVALID 3
void check_arg (char* arg);
void global_help(void (*help)(void));
void parse_help (int argc, char** argv, void (*help)(void));
int parse_args (int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*));
#endif

View file

@ -1,5 +1,4 @@
#include "buffer.h" #include "lslib.h"
#include "convert.h"
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
@ -58,3 +57,21 @@ void pop_path_buffer_2(int i) {
bool is_dot_dir(const char* path) { bool is_dot_dir(const char* path) {
return streql(path, ".") || streql(path, ".."); return streql(path, ".") || streql(path, "..");
} }
const char* get_last_component(const char* path) {
const char* last;
char c;
last = NULL;
while (c = *path, true) {
if (c == '\\') {
last = path;
} else if (c == '\0') {
break;
}
path++;
}
return last;
}

View file

@ -1,16 +0,0 @@
#ifndef BUFFER_H
#define BUFFER_H
#include "def.h"
char* get_path_buffer(void);
int push_path_buffer(const char* string);
void pop_path_buffer(int i);
char* get_path_buffer_2(void);
int push_path_buffer_2(const char* string);
void pop_path_buffer_2(int i);
bool is_dot_dir(const char* path);
#endif

View file

@ -1,5 +1,4 @@
#include "convert.h" #include "lslib.h"
#include "error.h"
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
@ -124,3 +123,10 @@ void print_file_path(char* path) {
printf("%s", path); printf("%s", path);
} }
} }
void nuke_str(char* type) {
*type = 0;
while(*(++type)) {
*type = 0;
}
}

View file

@ -1,20 +0,0 @@
#ifndef CONVERT_H
#define CONVERT_H
#include "def.h"
#include <sys/stat.h>
#include <stddef.h>
long int get_number(const char* text);
long int get_blkm(const char* text);
mode_t get_mode(const char* text);
bool streql(const char* a, const char* b);
bool prefix(const char* pre, const char* str);
void print_file_size(size_t bytes, char buf[5]);
void print_date_time(time_t mills, char buf[13]);
void print_file_path(char* path);
#endif

View file

@ -1,31 +0,0 @@
#ifndef COLOR_H
#define COLOR_H
#include <stdint.h>
#define ANSCII "\x1b["
#define NEXT ";"
#define RESET "0"
#define BOLD "1"
#define NORMAL "3"
#define BACKGROUND "4"
#define HIGHLIGHT "9"
#define BLACK "0"
#define RED "1"
#define GREEN "2"
#define YELLOW "3"
#define BLUE "4"
#define MAGENTA "5"
#define TURQUOISE "6"
#define WHITE "7"
#define COLOR "m"
typedef uint8_t bool;
#define true 1
#define false 0
#endif

View file

@ -1,17 +1,29 @@
#include "error.h" #include "lslib.h"
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include <string.h>
extern char* cmd; extern char* cmd;
void die (void) {
exit (EXIT_FAILURE);
}
void error_s(const char *format, ...) { void error_s(const char *format, ...) {
va_list list; va_list list;
va_start(list, format); va_start(list, format);
fprintf(stderr, "%s: ", cmd); fprintf(stderr, "%s: ", cmd);
vfprintf(stderr, format, list); vfprintf(stderr, format, list);
if (errno != 0) {
fprintf(stderr, ": %s", strerror(errno));
errno = 0;
}
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
@ -21,8 +33,15 @@ void error(const char *format, ...) {
fprintf(stderr, "%s: ", cmd); fprintf(stderr, "%s: ", cmd);
vfprintf(stderr, format, list); vfprintf(stderr, format, list);
if (errno != 0) {
fprintf(stderr, ": %s", strerror(errno));
errno = 0;
}
fprintf(stderr, "\n"); fprintf(stderr, "\n");
exit(EXIT_FAILURE);
die();
} }
void output(const char *format, ...) { void output(const char *format, ...) {

View file

@ -1,13 +0,0 @@
#ifndef ERROR_H
#define ERROR_H
__attribute__ ((__format__(printf, 1, 2)))
void error_s(const char* format, ...);
__attribute__ ((__format__(printf, 1, 2)))
void error(const char* format, ...);
__attribute__ ((__format__(printf, 1, 2)))
void output(const char* format, ...);
#endif

View file

@ -1,6 +1,4 @@
#include "file.h" #include "lslib.h"
#include "error.h"
#include "convert.h"
#include <paths.h> #include <paths.h>
#include <errno.h> #include <errno.h>
@ -21,7 +19,7 @@ FILE* get_file_s(const char* path, const char* type) {
if (lstat(path, &s) < 0) { if (lstat(path, &s) < 0) {
if (type[0] != 'r') goto read; if (type[0] != 'r') goto read;
error_s("failed to read %s: %s", path, strerror(errno)); error_s("failed to read %s", path);
return NULL; return NULL;
} }
@ -35,7 +33,7 @@ read:
file = fopen(path, type); file = fopen(path, type);
if (file == NULL) { if (file == NULL) {
error_s("failed to %s file %s: %s", type[0] == 'r' ? "read" : "write", path, strerror(errno)); error_s("failed to %s file %s", type[0] == 'r' ? "read" : "write", path);
} }
return file; return file;
@ -48,8 +46,8 @@ FILE* get_file(const char* path, const char* type) {
} }
int get_tty (void) { int get_tty (void) {
int fd = open(_PATH_TTY, O_RDONLY); int fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY | O_SYNC);
if (fd < 0) error("failed to get tty: %s", strerror(errno)); if (fd < 0) error("failed to get tty");
return fd; return fd;
} }
@ -57,7 +55,7 @@ FILE* get_tty_stream(char* type) {
int fd = get_tty(); int fd = get_tty();
FILE* file = fdopen(fd, type); FILE* file = fdopen(fd, type);
if (file == NULL) { if (file == NULL) {
error("failed to open tty stream: %s", strerror(errno)); error("failed to open tty stream");
} }
return file; return file;
} }

View file

@ -1,14 +0,0 @@
#ifndef FILE_H
#define FILE_H
#include <stdio.h>
FILE* get_file_s(const char* path, const char* type);
FILE* get_file(const char* path, const char* type);
int get_tty(void);
FILE* get_tty_stream(char* type);
#endif

24
lib/identity.c Normal file
View file

@ -0,0 +1,24 @@
#include "lslib.h"
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <errno.h>
void change_identity(const struct passwd* pw) {
int res;
res = initgroups(pw->pw_name, pw->pw_gid);
endgrent();
if (res != 0) {
if (errno == ENOSYS && pw->pw_uid == getuid()) {
return;
}
error("can't set groups");
}
xsetgid(pw->pw_gid);
xsetuid(pw->pw_uid);
}

View file

@ -3,14 +3,138 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <sys/stat.h>
#include <pwd.h>
#define ANSCII "\x1b["
#define NEXT ";"
#define RESET "0"
#define BOLD "1"
#define NORMAL "3"
#define BACKGROUND "4"
#define HIGHLIGHT "9"
#define BLACK "0"
#define RED "1"
#define GREEN "2"
#define YELLOW "3"
#define BLUE "4"
#define MAGENTA "5"
#define TURQUOISE "6"
#define WHITE "7"
#define COLOR "m"
typedef uint8_t bool;
#define true 1
#define false 0
#define UNUSED(x) (void)(x)
#define ARG_UNUSED 0
#define ARG_USED 1
#define ARG_IGNORE 2
#define ARG_INVALID 3
void check_arg (char* arg);
void global_help(void (*help)(void));
void parse_help (int argc, char** argv, void (*help)(void));
int parse_args (int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*));
FILE* get_file_s(const char* path, const char* type);
FILE* get_file(const char* path, const char* type);
int get_tty(void);
FILE* get_tty_stream(char* type);
void nuke_str(char* type);
char* get_path_buffer(void);
int push_path_buffer(const char* string);
void pop_path_buffer(int i);
char* get_path_buffer_2(void);
int push_path_buffer_2(const char* string);
void pop_path_buffer_2(int i);
long int get_number(const char* text);
long int get_blkm(const char* text);
mode_t get_mode(const char* text);
bool streql(const char* a, const char* b);
bool prefix(const char* pre, const char* str);
void print_file_size(size_t bytes, char buf[5]);
void print_date_time(time_t mills, char buf[13]);
void print_file_path(char* path);
bool is_dot_dir(const char* path);
const char* get_last_component(const char* path);
void die (void);
__attribute__ ((__format__(printf, 1, 2)))
void error_s(const char* format, ...);
__attribute__ ((__format__(printf, 1, 2)))
void error(const char* format, ...);
__attribute__ ((__format__(printf, 1, 2)))
void output(const char* format, ...);
typedef struct regex_t* re_t;
re_t re_compile(const char* pattern);
int re_matchp(re_t pattern, const char* text, int* matchlength);
int re_match(const char* pattern, const char* text, int* matchlength);
struct Stack {
size_t size;
size_t capacity;
void* data;
};
void stack_init(struct Stack* stack, size_t size);
void stack_push(struct Stack* stack, void* data, size_t len);
void* stack_pop(struct Stack* stack, size_t len);
void stack_free(struct Stack* stack);
void stack_push_int(struct Stack* stack, int value);
bool stack_pop_int(struct Stack* stack, int* value);
#define DEFAULT_SHELL "/bin/sh"
#define DEFAULT_PATH "/sbin:/usr/sbin:/bin:/usr/bin"
void setup_environment(const char *shell, bool new_env, bool clear_env, const struct passwd *pw);
void exec_shell(const char *shell, bool loginshell, const char **additional_args);
int check_password(const struct passwd *pw, char* plaintext);
int prompt_password(const struct passwd *pw, char* prompt);
void change_identity(const struct passwd* pw);
void* xalloc(size_t size);
void* xrealloc(void* ptr, size_t size);
void* xzalloc(size_t size);
void xsetenv(const char *name, const char *value);
void xsetuid(uid_t uid);
void xsetgid(gid_t gid);
struct passwd* xgetpwnam(char* name);
#define PASSWORD_INVALID 0
#define PASSWORD_VALID 1
#define PASSWORD_EMPTY 2
#include "args.h"
#include "buffer.h"
#include "def.h"
#include "convert.h"
#include "error.h"
#include "file.h"
#include "regex.h"
#include "stack.h"
#endif #endif

70
lib/password.c Normal file
View file

@ -0,0 +1,70 @@
#include "lslib.h"
#include <errno.h>
#include <pwd.h>
#include <shadow.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SHADOW_BUFSIZE 256
static const char* get_passwd(const struct passwd* pw) {
const char *pass;
if (!pw)
return "aa"; /* "aa" will never match */
pass = pw->pw_passwd;
if ((pass[0] == 'x' || pass[0] == '*') && !pass[1]) {
struct spwd* result;
errno = 0;
result = getspnam(pw->pw_name);
if (errno == EACCES) {
errno = 0;
error("binary not properly installed");
}
pass = (result == NULL) ? "aa" : result->sp_pwdp;
}
return pass;
}
int check_password(const struct passwd *pw, char* plaintext) {
char* encrypted;
char* salt;
char* p;
const char *pw_pass;
int r;
pw_pass = get_passwd(pw);
if (!pw_pass[0]) {
return PASSWORD_EMPTY;
}
salt = strdup(pw_pass);
if (salt == NULL) die();
p = strchr(salt + 1, '$');
if (p == NULL) die();
p = strchr(p + 1, '$');
if (p == NULL) die();
p[1] = 0;
encrypted = crypt(plaintext, salt);
r = (strcmp(encrypted, pw_pass) == 0);
nuke_str(plaintext);
free(salt);
return r;
}
int prompt_password(const struct passwd *pw, char* prompt) {
char* plaintext = getpass(prompt);
return check_password(pw, plaintext);
}

View file

@ -29,7 +29,8 @@
#include "regex.h" #include "lslib.h"
#include <stdio.h> #include <stdio.h>
#include <ctype.h> #include <ctype.h>

View file

@ -1,54 +0,0 @@
/*
*
* Mini regex-module inspired by Rob Pike's regex code described in:
*
* http://www.cs.princeton.edu/courses/archive/spr09/cos333/beautiful.html
*
*
*
* Supports:
* ---------
* '.' Dot, matches any character
* '^' Start anchor, matches beginning of string
* '$' End anchor, matches end of string
* '*' Asterisk, match zero or more (greedy)
* '+' Plus, match one or more (greedy)
* '?' Question, match zero or one (non-greedy)
* '[abc]' Character class, match if one of {'a', 'b', 'c'}
* '[^abc]' Inverted class, match if NOT one of {'a', 'b', 'c'} -- NOTE: feature is currently broken!
* '[a-zA-Z]' Character ranges, the character set of the ranges { a-z | A-Z }
* '\s' Whitespace, \t \f \r \n \v and spaces
* '\S' Non-whitespace
* '\w' Alphanumeric, [a-zA-Z0-9_]
* '\W' Non-alphanumeric
* '\d' Digits, [0-9]
* '\D' Non-digits
*
*
*/
#ifndef _TINY_REGEX_C
#define _TINY_REGEX_C
#ifndef RE_DOT_MATCHES_NEWLINE
/* Define to 0 if you DON'T want '.' to match '\r' + '\n' */
#define RE_DOT_MATCHES_NEWLINE 1
#endif
/* Typedef'd pointer to get abstract datatype. */
typedef struct regex_t* re_t;
/* Compile regex string pattern to a regex_t-array. */
re_t re_compile(const char* pattern);
/* Find matches of the compiled pattern inside text. */
int re_matchp(re_t pattern, const char* text, int* matchlength);
/* Find matches of the txt pattern inside text (will compile automatically first). */
int re_match(const char* pattern, const char* text, int* matchlength);
#endif /* ifndef _TINY_REGEX_C */

59
lib/shell.c Normal file
View file

@ -0,0 +1,59 @@
#include "lslib.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void setup_environment(const char *shell, bool new_env, bool clear_env, const struct passwd *pw) {
if (!shell || !shell[0])
shell = DEFAULT_SHELL;
if (clear_env) {
const char* term;
term = getenv("TERM");
clearenv();
if (term)
xsetenv("TERM", term);
xsetenv("PATH", DEFAULT_PATH);
}
if (new_env) {
xsetenv("USER", pw->pw_name);
xsetenv("LOGNAME", pw->pw_name);
xsetenv("HOME", pw->pw_dir);
xsetenv("SHELL", shell);
}
}
void exec_shell(const char *shell, bool loginshell, const char **additional_args) {
const char** args;
args = additional_args;
while (args && *args)
args++;
args = xzalloc(sizeof(args[0]) * (2 + (args - additional_args)));
if (!shell || !shell[0])
shell = DEFAULT_SHELL;
args[0] = get_last_component(shell);
if (loginshell) {
size_t len = strlen(shell) + 2;
char* arg = xalloc(len);
arg[0] = '-';
memcpy(&arg[1], shell, len - 1);
args[0] = arg;
}
if (additional_args) {
int count = 0;
while (*additional_args)
args[++count] = *additional_args++;
}
execv(shell, (char**) args);
error("cannot execute '%s'", shell);
}

View file

@ -1,4 +1,4 @@
#include "stack.h" #include "lslib.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -8,14 +8,14 @@
void stack_init(struct Stack* stack, size_t size) { void stack_init(struct Stack* stack, size_t size) {
stack->size = 0; stack->size = 0;
stack->capacity = size; stack->capacity = size;
stack->data = malloc(size); stack->data = xalloc(size);
} }
void stack_push(struct Stack* stack, void* data, size_t len) { void stack_push(struct Stack* stack, void* data, size_t len) {
size_t new_size = stack->size + len; size_t new_size = stack->size + len;
if (new_size >= stack->capacity) { if (new_size >= stack->capacity) {
stack->capacity = new_size * 2; stack->capacity = new_size * 2;
stack->data = realloc(stack->data, stack->capacity); stack->data = xrealloc(stack->data, stack->capacity);
} }
memcpy((uint8_t*)stack->data + stack->size, data, len); memcpy((uint8_t*)stack->data + stack->size, data, len);
stack->size += len; stack->size += len;

View file

@ -1,22 +0,0 @@
#ifndef STACK_H
#define STACK_H
#include "def.h"
#include <stddef.h>
struct Stack {
size_t size;
size_t capacity;
void* data;
};
void stack_init(struct Stack* stack, size_t size);
void stack_push(struct Stack* stack, void* data, size_t len);
void* stack_pop(struct Stack* stack, size_t len);
void stack_free(struct Stack* stack);
void stack_push_int(struct Stack* stack, int value);
bool stack_pop_int(struct Stack* stack, int* value);
#endif

49
lib/xfuncs.c Normal file
View file

@ -0,0 +1,49 @@
#include "lslib.h"
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
void* xalloc(size_t size) {
void *ptr = malloc(size);
if (ptr == NULL && size != 0) die();
return ptr;
}
void* xrealloc(void* ptr, size_t size) {
ptr = realloc(ptr, size);
if (ptr == NULL && size != 0) die();
return ptr;
}
void* xzalloc(size_t size) {
void* ptr = xalloc(size);
memset(ptr, 0, size);
return ptr;
}
void xsetenv(const char *name, const char *value) {
if (setenv(name, value, 1))
error("cannot set env");
}
void xsetuid(uid_t uid) {
if (setuid(uid))
error("cannot set uid");
}
void xsetgid(gid_t gid) {
if (setgid(gid))
error("cannot set gid");
}
struct passwd* xgetpwnam(char* name) {
struct passwd* pw;
pw = getpwnam(name);
if (!pw)
error("cannot get user: '%s'", name);
return pw;
}

View file

@ -5,7 +5,7 @@ A terrible busybox/gnu coreutils clone.
Currently the only supported commands are: Currently the only supported commands are:
`dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups`, `ls`, `tail`, `head`, `ed`, `tee`, `true`, `false`, `tee`, `whoami`, `dd`, `cat`, `yes`, `echo`, `printf`, `id`, `groups`, `ls`, `tail`, `head`, `ed`, `tee`, `true`, `false`, `tee`, `whoami`,
`wc`, `xargs`, `tac`, `rm`, `cp`, `mkdir`, `mv`, `grep`, `chown`, `chmod`, `sync` `wc`, `xargs`, `tac`, `rm`, `cp`, `mkdir`, `mv`, `grep`, `chown`, `chmod`, `sync`, `su`
## How to ## How to

View file

@ -29,5 +29,6 @@ COMMAND(grep);
COMMAND(chown_cmd); COMMAND(chown_cmd);
COMMAND(chmod_cmd); COMMAND(chmod_cmd);
COMMAND(sync_cmd); COMMAND(sync_cmd);
COMMAND(su);
#endif #endif

View file

@ -1,5 +1,4 @@
#include "command.h" #include "command.h"
#include "convert.h"
#include "lslib.h" #include "lslib.h"
#include <stdlib.h> #include <stdlib.h>
@ -23,7 +22,7 @@ int main (ARGUMENTS) {
if (argc < 2) { if (argc < 2) {
printf("usage: lazysphere [function [arguments]...]\n\n"); printf("usage: lazysphere [function [arguments]...]\n\n");
printf("currently defined functions:\n"); printf("currently defined functions:\n");
printf("\tdd, cat, yes, echo, printf, id, groups, ls, tail, head, ed, tee, true, false, tee, whoami, wc, xargs, tac, rm, cp, mkdir, mv, grep, chown, chmod, sync\n"); printf("\tdd, cat, yes, echo, printf, id, groups, ls, tail, head, ed, tee, true, false, tee, whoami, wc, xargs, tac, rm, cp, mkdir, mv, grep, chown, chmod, sync, su\n");
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
argc--; argc--;
@ -91,6 +90,8 @@ int main (ARGUMENTS) {
return chmod_cmd(NEXT_ARGS); return chmod_cmd(NEXT_ARGS);
} else if (streql(cmd, "sync")) { } else if (streql(cmd, "sync")) {
return sync_cmd(NEXT_ARGS); return sync_cmd(NEXT_ARGS);
} else if (streql(cmd, "su")) {
return su(NEXT_ARGS);
} else { } else {
fprintf(stderr, "lazysphere: invalid command %s\n", cmd); fprintf(stderr, "lazysphere: invalid command %s\n", cmd);
return EXIT_FAILURE; return EXIT_FAILURE;