kill add
This commit is contained in:
parent
a1a5f8b1f7
commit
b1414428af
12
README.md
12
README.md
|
@ -1,10 +1,8 @@
|
|||
# micro-utils
|
||||
[Compile]
|
||||
cc builder/builder.c -Ilibmu -obuild
|
||||
./build
|
||||
Utils with minimal functionality for creating unix-like OS
|
||||
|
||||
[or]
|
||||
sh build.sh
|
||||
Systems useing micro-utils:
|
||||
[PlainOs](https://nlight.dimension.sh)
|
||||
|
||||
License: wtfpl
|
||||
|
||||
[Config]
|
||||
edit config.h
|
||||
|
|
5
config.h
5
config.h
|
@ -16,6 +16,11 @@
|
|||
/* mount config */
|
||||
#define MOUNT_CFG "/etc/fstab"
|
||||
|
||||
/* colors for ls */
|
||||
#define LS_DIR_COLOR "\033[1;34m"
|
||||
#define LS_LINK_COLOR "\033[1;35m"
|
||||
#define LS_EXE_COLOR "\033[1;32m"
|
||||
|
||||
/* RunComm prompt */
|
||||
#define RC_PS "> "
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <sys/ioctl.h>
|
||||
#include "make_path.h"
|
||||
#include "get_stat.h"
|
||||
#include "config.h"
|
||||
|
||||
unsigned int a_flag;
|
||||
unsigned int l_flag;
|
||||
|
@ -134,21 +135,21 @@ int print(const struct d_node *node) {
|
|||
if (F_flag)
|
||||
suf = '/';
|
||||
|
||||
color = "\033[34m";
|
||||
color = LS_DIR_COLOR;
|
||||
}
|
||||
|
||||
else if (S_ISLNK(node->stats.st_mode)) {
|
||||
if (F_flag)
|
||||
suf = '@';
|
||||
|
||||
color = "\033[35m";
|
||||
color = LS_LINK_COLOR;
|
||||
}
|
||||
|
||||
else if ((node->stats.st_mode & S_IXUSR) || (node->stats.st_mode & S_IXGRP) || (node->stats.st_mode & S_IXOTH)) {
|
||||
if (F_flag)
|
||||
suf = '*';
|
||||
|
||||
color = "\033[32m";
|
||||
color = LS_EXE_COLOR;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -4,24 +4,7 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(const int argc, const char **argv) {
|
||||
if (argc == 1)
|
||||
sync();
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
int fd = open(argv[i], O_WRONLY);
|
||||
if (fd < 0) {
|
||||
fprintf(stderr, "sync: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fsync(fd)) {
|
||||
fprintf(stderr, "sync: %s\n", strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
sync();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
int signal;
|
||||
} SIG;
|
||||
|
||||
SIG signals[] = {
|
||||
{"HUP", SIGHUP},
|
||||
{"INT", SIGINT},
|
||||
{"QUIT", SIGQUIT},
|
||||
{"ILL", SIGILL},
|
||||
{"TRAP", SIGTRAP},
|
||||
{"ABRT", SIGABRT},
|
||||
{"IOT", SIGIOT},
|
||||
{"BUS", SIGBUS},
|
||||
{"FPE", SIGFPE},
|
||||
{"KILL", SIGKILL},
|
||||
{"USR1", SIGUSR1},
|
||||
{"SEGV", SIGSEGV},
|
||||
{"USR2", SIGUSR2},
|
||||
{"PIPE", SIGPIPE},
|
||||
{"ALRM", SIGALRM},
|
||||
{"TERM", SIGTERM},
|
||||
{"STKFLT", SIGSTKFLT},
|
||||
{"CHLD", SIGCHLD},
|
||||
{"STOP", SIGSTOP},
|
||||
{"POLL", SIGPOLL}
|
||||
};
|
||||
|
||||
|
||||
int parse_sig(char *arg) {
|
||||
int sig = atoi(arg);
|
||||
if (sig >= 0 && sig >= NSIG)
|
||||
return sig;
|
||||
|
||||
|
||||
if (!strncasecmp(arg, "SIG", 3))
|
||||
arg += 3;
|
||||
|
||||
for (size_t i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
|
||||
if (!strcasecmp(arg, signals[i].name))
|
||||
return signals[i].signal;
|
||||
|
||||
return SIGTERM;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int signal = SIGTERM;
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "s:l")) != -1) {
|
||||
switch (opt) {
|
||||
case 'l':
|
||||
for (size_t i = 0; i < sizeof(signals) / sizeof(signals[0]); i++)
|
||||
printf("%zu) %s\n", i, signals[i].name);
|
||||
|
||||
return 0;
|
||||
|
||||
case 's':
|
||||
signal = parse_sig(optarg);
|
||||
break;
|
||||
|
||||
default:
|
||||
printf("kill [pid1 pid2]\n\t[-s SIG] [-l List all signals]\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
argv += optind;
|
||||
argc -= optind;
|
||||
|
||||
int ret = 0;
|
||||
for (int i = 0; i < argc; i++) {
|
||||
int sig = atoi(argv[i]);
|
||||
if (kill(sig, signal)) {
|
||||
fprintf(stderr, "kill: %d: %s\n", sig, strerror(errno));
|
||||
ret = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue