fixed. Support buulding in 1 binary

This commit is contained in:
Your Name 2024-07-09 15:43:55 +03:00
parent f8dd7660ae
commit 7d0207ace2
58 changed files with 251 additions and 434 deletions

View File

@ -1,6 +1,8 @@
#!/bin/sh
CFLAGS=-s -Os -Wextra -Wall -pedantic
CC=cc
for i in $(ls src); do
echo "CC $i"
cc -s -Os -Wextra -Wall -pedantic -I. -Iinclude src/$i -o bin/$(basename $i .c)
$CC $CFLAGS -Iconfigs -Iinclude src/$i -o bin/$(basename $i .c)
done

View File

@ -1,8 +1,8 @@
#ifndef _CONFIG_H
#ifndef _CONFIG_H
#define _CONFIG_H
/* (cat tee wc xargs rev split cp) text buffer */
#define BUF_SIZE 512 * 512
#define BUF_SIZE 32000
#ifdef _SHRED_C
/* source of random (shred) */

View File

@ -1,4 +0,0 @@
NAME="Plain Os"
ID=plainos
VERSION_ID=0.1
PRETTY_NAME="PlainOs/Linux 0.1 "

View File

@ -1,9 +0,0 @@
#!/bin/sh
echo "[*] Mounting..."
mount -a
echo "[*] Hostname..."
hostname -c /etc/hostname
cat /etc/motd
env TERM="linux" login

View File

@ -1,14 +0,0 @@
#!/bin/sh
#FIXME!!!
echo "[*] Umounting..."
umount -a
echo "[*] Syncing..."
sync
echo "[*] Closing processes..."
kill -a -s TERM
sleep 5
echo "[*] Poweroff..."

View File

@ -3,7 +3,7 @@
#include <string.h>
unsigned long long parse_uint(const char *str) {
static unsigned long long parse_uint(const char *str) {
char *p = NULL;
unsigned long long res = strtoull(str, &p, 0);
@ -32,7 +32,7 @@ unsigned long long parse_uint(const char *str) {
return res;
}
unsigned long long mu_parse_duration(const char *arg) {
static unsigned long long mu_parse_duration(const char *arg) {
if (strchr(arg, '.')) {
/* TODO */
}

View File

@ -6,7 +6,7 @@
#include <string.h>
#include <sys/stat.h>
int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) {
static int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path) {
if (stat(path, stat_path)) {
if (prog_name != NULL)
fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno));
@ -17,7 +17,7 @@ int mu_get_stat(const char *prog_name, const char *path, struct stat *stat_path)
return 0;
}
int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) {
static int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path) {
if (lstat(path, stat_path)) {
if (prog_name != NULL)
fprintf(stderr, "%s: %s: %s\n", prog_name, path, strerror(errno));
@ -28,7 +28,7 @@ int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path
return 0;
}
int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) {
static int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) {
if (flag)
return mu_get_lstat(prog_name, path, stat_path);

View File

@ -6,7 +6,7 @@
#define MU_HUMAN_BUF_SIZE 16
char *mu_humansize(off_t n, off_t block) {
static char *mu_humansize(off_t n, off_t block) {
static char mu_hs_buf[MU_HUMAN_BUF_SIZE + 1];
memset(mu_hs_buf, '\0', sizeof(mu_hs_buf));

View File

@ -5,7 +5,7 @@
#include <string.h>
#include <stdlib.h>
char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
static char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
int flag = 0;
if (src == NULL) {

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#include <sys/stat.h>
char *mu_mode_2_str(mode_t file_mode) {
static char *mu_mode_2_str(mode_t file_mode) {
static char mode[11];
snprintf(mode, sizeof(mode), "----------");

View File

@ -1,7 +1,7 @@
#ifndef _UTF8_STRLEN
#define _UTF8_STRLEN
size_t mu_strlen(const char *s) {
static size_t mu_strlen(const char *s) {
size_t i = 0;
while (*s++)

View File

@ -15,7 +15,7 @@
#define SU_PERM (S_ISUID | S_ISGID | S_ISVTX)
#define FULL_PERM (WR_PERM | EX_PERM | RD_PERM)
mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
static mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
char *p = NULL;
mode_t mode = (mode_t)strtol(s, &p, 8);

View File

@ -9,7 +9,7 @@ typedef struct {
unsigned int val;
} MU_MOUNT_OPTS;
MU_MOUNT_OPTS mu_options[] = {
static MU_MOUNT_OPTS mu_options[] = {
{"defaults", NULL, NULL, 0},
{"ro", "rw", "Read only / Read and write", MS_RDONLY},
{"remount", NULL, "Remount a mounted filesystem", MS_REMOUNT},
@ -20,7 +20,7 @@ MU_MOUNT_OPTS mu_options[] = {
{"noatime", "atime", "Disable/enable updates to inode access times", MS_NOATIME}
};
unsigned long mu_parse_opts(char *str, char *data, const size_t data_size) {
static unsigned long mu_parse_opts(char *str, char *data, const size_t data_size) {
memset(data, '\0', data_size);
unsigned long opt = 0;
size_t data_len = 0;

View File

@ -46,7 +46,7 @@ struct mu_proc {
long rsslim;
};
int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
static int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
proc_s->uid = -1;
proc_s->gid = -1;
@ -98,7 +98,7 @@ int mu_proc_status(const char *prog_name, const pid_t pid, struct mu_proc *proc_
return 0;
}
int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
static int mu_proc_stat(const char *prog_name, const pid_t pid, struct mu_proc *proc_s) {
char path[PATH_MAX + 1];
snprintf(path, sizeof(path), "/proc/%d/stat", pid);

View File

@ -8,7 +8,7 @@
#include <errno.h>
#include "config.h"
void dec_salt(void) {
static void dec_salt(void) {
size_t i;
for (i = 0; i < sizeof(MU_SALT_ENC) / sizeof(int); i++)
MU_SALT_BUF[i] = (char)MU_SALT_ENC[i];
@ -16,7 +16,7 @@ void dec_salt(void) {
MU_SALT_BUF[i + 1] = '\0';
}
char *enc_password(const char *prog_name, const char *pass, const char *salt) {
static char *enc_password(const char *prog_name, const char *pass, const char *salt) {
if (salt == NULL)
dec_salt();
@ -31,7 +31,7 @@ char *enc_password(const char *prog_name, const char *pass, const char *salt) {
return cpass;
}
int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
static int pw_check(const char *prog_name, const struct passwd *pw, const char *pass) {
if (pw->pw_passwd[0] == '\0' && pass[0] == '\0')
return 0;

View File

@ -7,7 +7,7 @@
#include "make_path.h"
#include "get_stat.h"
int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) {
static int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) {
struct stat sb;
if (mu_get_stats(prog_name, link_flag, path, &sb))
return 1;

View File

@ -4,7 +4,7 @@
#include <string.h>
#include <libgen.h>
char *bname(char *str, const char *suffix) {
static char *bname(char *str, const char *suffix) {
char *base = basename(str);
if (suffix) {

View File

@ -5,7 +5,7 @@
#include <unistd.h>
#include "config.h"
int cat(const char *path) {
static int cat(const char *path) {
int fd = STDIN_FILENO;
if (strcmp(path, "-"))

View File

@ -9,12 +9,12 @@
#include "get_stat.h"
#include "recurse.h"
char r_flag;
char *f_flag = "chgrp";
char H_flag;
char v_flag;
static char r_flag;
static char *f_flag = "chgrp";
static char H_flag;
static char v_flag;
int change(const char *path, void *p) {
static int change(const char *path, void *p) {
struct group *grp = (struct group *)p;
struct stat stat_path;

View File

@ -10,12 +10,12 @@
#include "parse_mode.h"
#include "recurse.h"
char r_flag;
char *f_flag = "chmod";
char H_flag;
char v_flag;
static char r_flag;
static char *f_flag = "chmod";
static char H_flag;
static char v_flag;
int change(const char *file, void *p) {
static int change(const char *file, void *p) {
char *mode_arg = (char *)p;
struct stat sb;

View File

@ -11,15 +11,15 @@
#include "recurse.h"
#include "unused.h"
char r_flag;
char *f_flag = "chown";
char H_flag;
char v_flag;
int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
long gid;
long uid;
static char r_flag;
static char *f_flag = "chown";
static char H_flag;
static char v_flag;
static int (*chown_func)(const char *pathname, uid_t owner, gid_t group);
static long gid;
static long uid;
int change(const char *path, void *p) {
static int change(const char *path, void *p) {
char *name = (char *)p;
if (chown_func(path, uid, gid)) {
@ -35,7 +35,7 @@ int change(const char *path, void *p) {
return 0;
}
void get_owner(const char *arg) {
static void get_owner(const char *arg) {
char *group = strchr(arg, ':');
char g_flag = 1;

View File

@ -3,9 +3,9 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
char s_flag;
static char s_flag;
int compare(FILE *fp1, FILE *fp2, const char *s1, const char *s2) {
static int compare(FILE *fp1, FILE *fp2, const char *s1, const char *s2) {
if (fp1 == fp2)
return 0;
@ -44,7 +44,7 @@ int compare(FILE *fp1, FILE *fp2, const char *s1, const char *s2) {
return 0;
}
long parse_int(const char *str) {
static long parse_int(const char *str) {
char *ptr;
long val = strtol(str, &ptr, 0);
if (*ptr || val < 0) {
@ -55,7 +55,7 @@ long parse_int(const char *str) {
return val;
}
FILE *file_open(const char *path) {
static FILE *file_open(const char *path) {
if (!strcmp(path, "-"))
return stdin;

View File

@ -12,12 +12,12 @@
#include "get_stat.h"
#include "config.h"
char *f_flag = "cp";
char r_flag;
char v_flag;
char L_flag;
static char *f_flag = "cp";
static char r_flag;
static char v_flag;
static char L_flag;
char copy_reg(int mode, const char *src, const char *dst) {
static char copy_reg(int mode, const char *src, const char *dst) {
int ret = 1;
int ifd = open(src, O_RDONLY);
@ -52,7 +52,7 @@ CLOSE_OFD:
return ret;
}
char copy_lnk(const char *src, const char *dst) {
static char copy_lnk(const char *src, const char *dst) {
char path[PATH_MAX + 1];
ssize_t ret = readlink(src, path, sizeof(path));
if (ret < 0)
@ -65,7 +65,7 @@ char copy_lnk(const char *src, const char *dst) {
return 0;
}
char copy(struct stat st, const char *src, const char *dst) {
static char copy(struct stat st, const char *src, const char *dst) {
if (v_flag)
printf("Copying '%s' to '%s'\n", src, dst);
@ -83,7 +83,7 @@ char copy(struct stat st, const char *src, const char *dst) {
return 0;
}
int cptree(const char *src, const char *dst) {
static int cptree(const char *src, const char *dst) {
struct stat src_stat;
if (mu_get_stats(f_flag, !L_flag, src, &src_stat))
return 1;

View File

@ -6,7 +6,7 @@
#include <errno.h>
#include <time.h>
const char *fmts[] = {
static const char *fmts[] = {
"%R",
"%T",
"%d-%m",
@ -19,7 +19,7 @@ const char *fmts[] = {
"%d-%m-%Y %T"
};
time_t parse_date(char *str) {
static time_t parse_date(char *str) {
time_t local = time(NULL);
struct tm *tm = localtime(&local);

View File

@ -7,18 +7,18 @@
#include <errno.h>
#include "human.h"
off_t infull, inpart;
off_t outfull, outpart;
off_t tbytes;
static off_t infull, inpart;
static off_t outfull, outpart;
static off_t tbytes;
void summary(void) {
static void summary(void) {
fprintf(stderr, "%jd+%jd records in\n", infull, inpart);
fprintf(stderr, "%jd+%jd records out\n", outfull, outpart);
fprintf(stderr, "%s total bytes copied\n", mu_humansize(tbytes, 1024));
}
int openfile(int flag, char *path, int mode) {
static int openfile(int flag, char *path, int mode) {
if (!strcmp(path, "-")) {
if (flag)
return STDOUT_FILENO;
@ -35,7 +35,7 @@ int openfile(int flag, char *path, int mode) {
return fd;
}
off_t strtonum(char *str) {
static off_t strtonum(char *str) {
char *p = NULL;
off_t res = strtoll(str, &p, 0);
if (str != p) {
@ -67,7 +67,7 @@ off_t strtonum(char *str) {
return res;
}
int copy(int fd, void *buf, off_t len, off_t max) {
static int copy(int fd, void *buf, off_t len, off_t max) {
off_t n = write(fd, buf, len);
if (n < 0) {
fprintf(stderr, "dd: %s\n", strerror(errno));

View File

@ -9,12 +9,12 @@
#include "human.h"
#include "config.h"
char a_flag;
char h_flag;
char *t_flag;
off_t block = 1024;
static char a_flag;
static char h_flag;
static char *t_flag;
static off_t block = 1024;
int print(const struct mntent *me) {
static int print(const struct mntent *me) {
struct statvfs disk;
if (!strcmp(me->mnt_fsname, "none"))

View File

@ -9,12 +9,12 @@
#include "make_path.h"
#include "human.h"
char h_flag;
char s_flag;
char c_flag;
off_t total;
static char h_flag;
static char s_flag;
static char c_flag;
static off_t total;
void print(off_t size, const char *filename) {
static void print(off_t size, const char *filename) {
if (h_flag)
printf("%s\t%s\n", mu_humansize(size * 512, 1024), filename);
@ -22,7 +22,7 @@ void print(off_t size, const char *filename) {
printf("%jd\t%s\n", (intmax_t)size / 2, filename);
}
off_t du(const char *path, int recurs_flag) {
static off_t du(const char *path, int recurs_flag) {
struct stat sb;
if (mu_get_lstat("du", path, &sb))
return 0;

View File

@ -4,10 +4,10 @@
#include <stdlib.h>
#include <string.h>
char n_flag = 0;
char e_flag = 0;
static char n_flag = 0;
static char e_flag = 0;
void format(char *str) {
static void format(char *str) {
for (size_t i = 0; i < strlen(str); i++) {
unsigned int c = str[i];
if (c == '\\') {

View File

@ -8,10 +8,10 @@ typedef struct {
char *pattern;
regex_t rgex;
} PATTERN;
PATTERN *regexs;
size_t r_size;
static PATTERN *regexs;
static size_t r_size;
int addpattern(char *str) {
static int addpattern(char *str) {
if (regexs == NULL) {
regexs = malloc(sizeof(PATTERN));
if (regexs == NULL) {

View File

@ -8,11 +8,11 @@
#include <stdlib.h>
#include "config.h"
char v_flag;
char c_flag;
char n_flag;
static char v_flag;
static char c_flag;
static char n_flag;
long parse_long(const char *str) {
static long parse_long(const char *str) {
char *ptr;
long ret = strtol(str, &ptr, 0);
if (*ptr) {
@ -23,7 +23,7 @@ long parse_long(const char *str) {
return ret;
}
int print(const char *file, FILE *fp, long lines, long bytes) {
static int print(const char *file, FILE *fp, long lines, long bytes) {
if (v_flag)
printf(HEAD_FMT, file);

View File

@ -5,7 +5,7 @@
#include <limits.h>
#include <unistd.h>
void readcfg(const char *cfg, char *buf, size_t len) {
static void readcfg(const char *cfg, char *buf, size_t len) {
FILE *fp = fopen(cfg, "r");
if (fp == NULL) {
fprintf(stderr, "hostname: %s\n", strerror(errno));

View File

@ -9,13 +9,13 @@
#include <limits.h>
#include <sys/types.h>
char g_flag;
char G_flag;
char n_flag;
char r_flag;
char u_flag;
static char g_flag;
static char G_flag;
static char n_flag;
static char r_flag;
static char u_flag;
int print_groups(const struct passwd *pwd, const char *fmt, const int flag) {
static int print_groups(const struct passwd *pwd, const char *fmt, const int flag) {
gid_t groups[NGROUPS_MAX];
int ngroups = NGROUPS_MAX;
if (getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups) < 0)
@ -38,7 +38,7 @@ int print_groups(const struct passwd *pwd, const char *fmt, const int flag) {
return 0;
}
int ids(uid_t uid, struct passwd *pwd) {
static int ids(uid_t uid, struct passwd *pwd) {
if (u_flag) {
if (n_flag)
printf("%s", pwd->pw_name);
@ -54,7 +54,7 @@ int ids(uid_t uid, struct passwd *pwd) {
return 0;
}
int def_ids(uid_t uid, struct passwd *pwd) {
static int def_ids(uid_t uid, struct passwd *pwd) {
printf("uid=%d(%s) gid=%d", uid, pwd->pw_name, pwd->pw_gid);
struct group *grp = getgrgid(pwd->pw_gid);
if (grp)
@ -67,7 +67,7 @@ int def_ids(uid_t uid, struct passwd *pwd) {
return 0;
}
void usage(int sig) {
static void usage(int sig) {
puts("id [gGurn] [user]\n\t-u User ID\n\t-g Group ID\n\t-G Supplementary group IDs\n\t-n Print names instead of numbers\n\t-r Print real ID instead of effective ID");
exit(sig);
}

View File

@ -1,67 +0,0 @@
#define INIT
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/reboot.h>
#include <sys/wait.h>
#include "config.h"
void poweroff(int sig) {
printf("Syncing...\n");
sync();
sleep(5);
if (sig == SIGUSR1)
reboot(RB_POWER_OFF);
else if (sig == SIGUSR2)
reboot(RB_AUTOBOOT);
}
void execute(char *argv[]) {
if (fork() == 0) {
setsid();
execvp(argv[0], argv);
fprintf(stderr, "init: Failed to run %s\nRebooting...\n", argv[0]);
poweroff(SIGUSR2);
}
}
int main(void) {
if (getpid() != 1) {
fprintf(stderr, "init: PID Must be 1\n");
return 1;
}
chdir("/");
printf("%s\n", INIT_MSG);
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGUSR2);
signal(SIGTERM, SIG_IGN);
signal(SIGINT, SIG_IGN);
/* Start main service */
sigprocmask(SIG_BLOCK, &set, NULL);
execute(INIT_START);
/* Sig handler */
int sig = 0;
while (1) {
if (sigwait(&set, &sig) == 0)
if (sig == SIGUSR2 || sig == SIGUSR1) {
execute(INIT_POWEROFF);
poweroff(sig);
}
}
return 0;
}

View File

@ -12,7 +12,7 @@ typedef struct {
int signal;
} SIG;
SIG signals[] = {
static SIG signals[] = {
{"HUP", SIGHUP},
{"INT", SIGINT},
{"QUIT", SIGQUIT},
@ -34,7 +34,7 @@ SIG signals[] = {
};
int parse_sig(char *arg) {
static int parse_sig(char *arg) {
int sig = atoi(arg);
if (sig > 0 && sig <= NSIG)
return sig;
@ -49,7 +49,7 @@ int parse_sig(char *arg) {
return SIGTERM;
}
int my_kill(pid_t pid, int signal) {
static int my_kill(pid_t pid, int signal) {
if (kill(pid, signal)) {
fprintf(stderr, "kill: %d: %s\n", pid, strerror(errno));
return 1;

View File

@ -4,11 +4,11 @@
#include <unistd.h>
#include "make_path.h"
char s_flag;
char f_flag;
char v_flag;
static char s_flag;
static char f_flag;
static char v_flag;
int ln(const char *src, const char *dst) {
static int ln(const char *src, const char *dst) {
if (f_flag)
if (unlink(dst) && v_flag)
fprintf(stderr, "ln: removed %s\n", dst);

View File

@ -11,7 +11,7 @@
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
int hide_input(int fd, int flag) {
static int hide_input(int fd, int flag) {
struct termios term;
if (tcgetattr(fd, &term) < 0)
return 1;
@ -28,7 +28,7 @@ int hide_input(int fd, int flag) {
return 0;
}
void login(const struct passwd *pw) {
static void login(const struct passwd *pw) {
char *shell = (pw->pw_shell[0] == '\0') ? "/bin/sh" : pw->pw_shell;
setenv("HOME", pw->pw_dir, 1);
@ -45,7 +45,7 @@ void login(const struct passwd *pw) {
execlp(shell, shell, "-l", NULL);
}
struct passwd *proccess_input(char *hostname) {
static struct passwd *proccess_input(char *hostname) {
static char user[512];
static char psswd[512];

View File

@ -20,22 +20,22 @@
#include "config.h"
#include "human.h"
char O_flag;
char a_flag;
char l_flag;
char F_flag;
char c_flag;
char R_flag;
char d_flag;
char L_flag;
char h_flag;
char s_flag;
char i_flag;
char p_flag;
char nul_flag;
static char O_flag;
static char a_flag;
static char l_flag;
static char F_flag;
static char c_flag;
static char R_flag;
static char d_flag;
static char L_flag;
static char h_flag;
static char s_flag;
static char i_flag;
static char p_flag;
static char nul_flag;
int sortd(const void *p1, const void *p2);
int (*sorter)(const void *p1, const void *p2) = sortd;
static int sortd(const void *p1, const void *p2);
static int (*sorter)(const void *p1, const void *p2) = sortd;
struct d_node {
/* basename */
@ -49,7 +49,7 @@ struct d_node {
};
/* Work with dir */
struct d_node *stat_file(char *filename, const int lfile) {
static struct d_node *stat_file(char *filename, const int lfile) {
/* lfile its flag. 1 if passed file from list_one() */
struct d_node *file = malloc(sizeof(struct d_node));
if (file == NULL) {
@ -73,7 +73,7 @@ struct d_node *stat_file(char *filename, const int lfile) {
return file;
}
void dfree(struct d_node **dir, const size_t files) {
static void dfree(struct d_node **dir, const size_t files) {
for (size_t i = 0; i < files; i++) {
if (dir[i] != NULL && dir[i]->full_name != NULL)
free(dir[i]->full_name);
@ -85,7 +85,7 @@ void dfree(struct d_node **dir, const size_t files) {
free(dir);
}
struct d_node **list(const char *path, off_t *total_size, size_t *nfiles, int *ret) {
static struct d_node **list(const char *path, off_t *total_size, size_t *nfiles, int *ret) {
DIR *dp = opendir(path);
if (dp == NULL) {
fprintf(stderr, "ls: %s: %s\n", path, strerror(errno));
@ -134,7 +134,7 @@ struct d_node **list(const char *path, off_t *total_size, size_t *nfiles, int *r
return dn;
}
char *get_date(const time_t mtime) {
static char *get_date(const time_t mtime) {
static char date[100];
strftime(date, sizeof(date), "%b %d %H:%M", localtime(&mtime));
@ -143,7 +143,7 @@ char *get_date(const time_t mtime) {
}
/* Print */
int print(const struct d_node *node) {
static int print(const struct d_node *node) {
char suf = ' ';
char *color = "";
@ -229,7 +229,7 @@ int print(const struct d_node *node) {
return ret;
}
int col_print(struct d_node **node, const size_t files, const struct winsize w) {
static int col_print(struct d_node **node, const size_t files, const struct winsize w) {
/* Get max len */
size_t maxlen = 0;
for (size_t i = 0; i < files; i++) {
@ -288,7 +288,7 @@ int col_print(struct d_node **node, const size_t files, const struct winsize w)
return 0;
}
int struct_print(struct d_node **dir, const size_t files, const struct winsize w) {
static int struct_print(struct d_node **dir, const size_t files, const struct winsize w) {
int ret = 0;
/* pipe print */
@ -309,20 +309,20 @@ int struct_print(struct d_node **dir, const size_t files, const struct winsize w
}
/* Sort */
int sortt(const void *p1, const void *p2) {
static int sortt(const void *p1, const void *p2) {
return (*(struct d_node **)p2)->stats.st_mtime - (*(struct d_node **)p1)->stats.st_mtime;
}
int sorts(const void *p1, const void *p2) {
static int sorts(const void *p1, const void *p2) {
return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size;
}
int sortd(const void *p1, const void *p2) {
static int sortd(const void *p1, const void *p2) {
return strcmp((*(struct d_node **)p1)->full_name, (*(struct d_node **)p2)->full_name);
}
/* Ls */
int ls_dir(const char *dir_name, const int label, const struct winsize w) {
static int ls_dir(const char *dir_name, const int label, const struct winsize w) {
if (dir_name == NULL)
return 0;
@ -360,7 +360,7 @@ int ls_dir(const char *dir_name, const int label, const struct winsize w) {
return ret;
}
int ls_files(int argc, char **argv, const struct winsize w) {
static int ls_files(int argc, char **argv, const struct winsize w) {
size_t files = 0;
struct d_node **file = malloc(sizeof(struct d_node *));
if (file == NULL) {

View File

@ -7,11 +7,11 @@
#include <errno.h>
#include "parse_mode.h"
char v_flag;
char p_flag;
mode_t mode = 0777;
static char v_flag;
static char p_flag;
static mode_t mode = 0777;
int do_mkdir(const char *path) {
static int do_mkdir(const char *path) {
if (mkdir(path, mode)) {
if (p_flag)
return 0;
@ -26,7 +26,7 @@ int do_mkdir(const char *path) {
return 0;
}
int do_parents(const char *path) {
static int do_parents(const char *path) {
if (path[0] == '.' || path[0] == '/')
return 0;

View File

@ -10,7 +10,7 @@
#include <stdio.h>
#include "parse_mode.h"
long parse_long(const char *str) {
static long parse_long(const char *str) {
char *ptr = NULL;
long value = strtol(str, &ptr, 10);

View File

@ -5,7 +5,7 @@
#include <errno.h>
#include "make_path.h"
int make_temp_dir(char *tmp) {
static int make_temp_dir(char *tmp) {
if (!mkdtemp(tmp)) {
if (errno == EINVAL)
fprintf(stderr, "mktemp: template does not end in exactly 'XXXXX': %s\n", tmp);
@ -19,7 +19,7 @@ int make_temp_dir(char *tmp) {
return 0;
}
int get_suffix(const char *str) {
static int get_suffix(const char *str) {
size_t len = strlen(str);
for (size_t i = len - 1; i > 0; i--)
if (str[i] == 'X')
@ -28,7 +28,7 @@ int get_suffix(const char *str) {
return 0;
}
int make_temp_file(char *tmp) {
static int make_temp_file(char *tmp) {
if (!strstr(tmp, "XXXXXX")) {
fprintf(stderr, "mktemp: too few X's in template: %s\n", tmp);
return 1;

View File

@ -10,7 +10,7 @@
#include "parse_mount.h"
#include "config.h"
int do_mount(const char *src, const char *dst, const char *fs_type, unsigned long opt, char *data) {
static int do_mount(const char *src, const char *dst, const char *fs_type, unsigned long opt, char *data) {
if (mount(src, dst, fs_type, opt, data) < 0) {
fprintf(stderr, "mount: %s: %s\n", src, strerror(errno));
return 1;
@ -19,7 +19,7 @@ int do_mount(const char *src, const char *dst, const char *fs_type, unsigned lon
return 0;
}
int parse_fstab(void) {
static int parse_fstab(void) {
FILE *fp = setmntent(MOUNT_CFG, "r");
if (fp == NULL) {
fprintf(stderr, "mount: %s\n", strerror(errno));
@ -39,7 +39,7 @@ int parse_fstab(void) {
return 0;
}
int print_mounts(void) {
static int print_mounts(void) {
FILE *fp = fopen(MOUNT_LIST, "r");
if (fp == NULL) {
fprintf(stderr, "mount: %s\n", strerror(errno));
@ -56,7 +56,7 @@ int print_mounts(void) {
return 0;
}
void print_opts(void) {
static void print_opts(void) {
for (size_t i = 0; i < sizeof(mu_options) / sizeof(mu_options[0]); i++) {
printf("\t%s", mu_options[i].opt);
if (mu_options[i].notopt != NULL)

View File

@ -6,9 +6,9 @@
#include <stdlib.h>
#include <unistd.h>
#include "make_path.h"
char *f_flag = "mv";
static char *f_flag = "mv";
int move(const char *src, const char *dst) {
static int move(const char *src, const char *dst) {
char *copy = strdup(src);
if (!copy)
return 1;

View File

@ -3,7 +3,7 @@
#include <string.h>
#include <unistd.h>
int printvars(int len, char **names) {
static int printvars(int len, char **names) {
int ret = 0;
for (int i = 0; i < len; i++) {

View File

@ -9,7 +9,7 @@
#include <ctype.h>
#include <pwd.h>
int pscan(const char *pid) {
static int pscan(const char *pid) {
char path[PATH_MAX + 1];
/* Arguments */

View File

@ -1,5 +1,4 @@
/* Safe variant of kilo editor */
/* Based on the web-tutorial and contain many edits */
#include <fcntl.h>
#include <stdio.h>
@ -14,11 +13,11 @@
#define CTRL_KEY(k) ((k) & 0x1f)
#define TAB_SIZE 8
struct termios orig_termios;
struct winsize ws;
static struct termios orig_termios;
static struct winsize ws;
char *scr_buf;
size_t scr_buf_size;
static char *scr_buf;
static size_t scr_buf_size;
struct row {
char *buf;
@ -28,34 +27,29 @@ struct row {
size_t rlen;
};
struct row *row_s;
size_t row_size;
static struct row *row_s;
static size_t row_size;
/* Cursors */
unsigned int curx;
unsigned int cury;
unsigned int renx;
static unsigned int curx;
static unsigned int cury;
static unsigned int renx;
unsigned int rowoff;
unsigned int coloff;
static unsigned int rowoff;
static unsigned int coloff;
char *file_path;
static char *file_path;
enum {
FAILED_SAVE = 1,
SUCCESS_SAVE,
MARK
};
char status_type;
char modified_flag;
/* for mark */
char mark_flag;
unsigned int curx2;
unsigned int cury2;
static char status_type;
static char modified_flag;
/* Cleaners */
void bufFree(void) {
static void bufFree(void) {
if (scr_buf != NULL)
free(scr_buf);
@ -63,7 +57,7 @@ void bufFree(void) {
scr_buf_size = 0;
}
void fileBufFree(void) {
static void fileBufFree(void) {
if (row_s != NULL) {
if (row_size > 0)
for (size_t i = 0; i < row_size; i++) {
@ -81,7 +75,7 @@ void fileBufFree(void) {
row_size = 0;
}
void die(int ret, char *msg) {
static void die(int ret, char *msg) {
write(STDOUT_FILENO, "\033[H\033[J", 6);
bufFree();
@ -94,7 +88,7 @@ void die(int ret, char *msg) {
}
/* Save */
char *buf2str(size_t *len) {
static char *buf2str(size_t *len) {
size_t totalen = 0;
for (size_t i = 0; i < row_size; i++)
totalen += row_s[i].len + 1;
@ -117,7 +111,7 @@ char *buf2str(size_t *len) {
return buf;
}
void save(void) {
static void save(void) {
size_t len = 0;
char *buf = buf2str(&len);
@ -148,7 +142,7 @@ BAD_SAVE:
}
/* Funcs */
void bufAppend(const char *str, const size_t len) {
static void bufAppend(const char *str, const size_t len) {
if (len == 0)
return;
@ -173,7 +167,7 @@ void bufAppend(const char *str, const size_t len) {
scr_buf_size += len;
}
int updateRenderStr(const unsigned int row) {
static int updateRenderStr(const unsigned int row) {
int tabs = 0;
for (size_t i = 0; i < row_s[row].len; i++)
if (row_s[row].buf[i] == '\t')
@ -205,7 +199,7 @@ int updateRenderStr(const unsigned int row) {
return 0;
}
int addRow(const unsigned int at, const char *buf, const size_t len) {
static int addRow(const unsigned int at, const char *buf, const size_t len) {
struct row *row_s2 = realloc(row_s, sizeof(struct row) * (row_size + 1));
if (row_s2 == NULL)
return 1;
@ -230,7 +224,7 @@ int addRow(const unsigned int at, const char *buf, const size_t len) {
return 0;
}
void readFile(const char *path) {
static void readFile(const char *path) {
row_s = malloc(sizeof(struct row));
if (row_s == NULL)
die(1, "malloc in readFIle");
@ -259,7 +253,7 @@ void readFile(const char *path) {
fclose(fp);
}
void insertChar(char c) {
static void insertChar(char c) {
if ((size_t)cury == row_size) {
if (addRow(row_size, "", 0)) {
save();
@ -290,7 +284,7 @@ void insertChar(char c) {
curx++;
}
void insertNewline(void) {
static void insertNewline(void) {
if (curx == 0) {
if (addRow(cury, "", 0)) {
save();
@ -327,7 +321,7 @@ void insertNewline(void) {
curx = 0;
}
void delChar(void) {
static void delChar(void) {
if ((curx == 0 && cury == 0) || cury == (unsigned int)row_size)
return;
@ -379,17 +373,13 @@ void delChar(void) {
}
}
void cmd(void) {
/* Execute internal cmds */
}
/* Terminal */
void DRawMode(void) {
static void DRawMode(void) {
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &orig_termios) < 0)
die(1, "tcsetattr");
}
void ERawMode(void) {
static void ERawMode(void) {
if (tcgetattr(STDIN_FILENO, &orig_termios) < 0)
die(1, "tcgetattr");
@ -406,7 +396,7 @@ void ERawMode(void) {
die(1, "tcsetattr");
}
void winsize(void) {
static void winsize(void) {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0)
die(1, "ioctl");
@ -416,7 +406,7 @@ void winsize(void) {
ws.ws_row--;
}
void statusBar(const char *path) {
static void statusBar(const char *path) {
char info[124];
int ret = 0;
@ -437,7 +427,7 @@ void statusBar(const char *path) {
}
/* Keyboard */
char readkey(void) {
static char readkey(void) {
char c;
int ret;
@ -451,7 +441,7 @@ char readkey(void) {
return c;
}
void moveCursor(char c) {
static void moveCursor(char c) {
switch (c) {
case 'A':
if (cury != 0)
@ -527,7 +517,7 @@ void moveCursor(char c) {
curx = row_s[cury].len;
}
void keyboard(void) {
static void keyboard(void) {
char key = readkey();
switch (key) {
case '\033':
@ -542,10 +532,6 @@ void keyboard(void) {
insertNewline();
break;
case CTRL_KEY('f'):
cmd();
break;
case CTRL_KEY('q'):
die(0, NULL);
break;

View File

@ -6,7 +6,7 @@
#include <stdlib.h>
#include <sys/resource.h>
int renice(int which, int who, int adj) {
static int renice(int which, int who, int adj) {
adj += getpriority(which, who);
if (setpriority(which, who, adj)) {

View File

@ -10,11 +10,11 @@
#include "recurse.h"
#include "unused.h"
char *f_flag = "rm";
char r_flag;
char v_flag;
static char *f_flag = "rm";
static char r_flag;
static char v_flag;
int verbose(const char *path) {
static int verbose(const char *path) {
if (v_flag) {
fprintf(stderr, "rm: remove %s? [y/n] ", path);
fflush(stderr);
@ -33,12 +33,12 @@ int verbose(const char *path) {
return 1;
}
void handle_error(const char *path) {
static void handle_error(const char *path) {
if (f_flag)
fprintf(stderr, "rm: %s: %s\n", path, strerror(errno));
}
int rm(const char *path, void *p) {
static int rm(const char *path, void *p) {
UNUSED(p);
if (verbose(path) && remove(path) < 0) {
@ -49,7 +49,7 @@ int rm(const char *path, void *p) {
return 0;
}
int rmd(const char *path, void *p) {
static int rmd(const char *path, void *p) {
UNUSED(p);
if (verbose(path) && rmdir(path) < 0) {

View File

@ -4,7 +4,7 @@
#include <unistd.h>
#include <stdlib.h>
double parse_double(const char *str) {
static double parse_double(const char *str) {
char *ptr;
double res = strtod(str, &ptr);

View File

@ -18,4 +18,5 @@ int main(int argc, char **argv) {
setsid();
execvp(argv[1], argv + 1);
return 1;
}

View File

@ -10,12 +10,12 @@
#include <sys/stat.h>
#include "config.h"
char f_flag;
char u_flag;
char z_flag;
int n_loops = 3;
static char f_flag;
static char u_flag;
static char z_flag;
static int n_loops = 3;
int shred(int rand_fd, int fd) {
static int shred(int rand_fd, int fd) {
/* Get size */
off_t size = lseek(fd, 0, SEEK_END);

View File

@ -1,80 +0,0 @@
#include <stdio.h>
#include <float.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int parse_double(const char *str, double *res) {
char *ptr;
*res = strtod(str, &ptr);
if (*ptr)
return 0;
return 1;
}
void print(double diff, char *str, double min) {
double val = 0;
if (!parse_double(str, &val))
return;
putchar('\xe2');
putchar('\x96');
putchar('\x81' + (int)round((val - min + 1) / diff * 7));
}
int main(int argc, char **argv) {
if (argc == 1) {
printf("spark: missing operands\nspark [num1] [num2]...\n");
return 0;
}
unsigned int r_flag = 0;
char ret = '\n';
double min = DBL_MAX;
double max = DBL_MIN;
/* Remove programm name */
argv++;
argc--;
for (int i = 0; i < argc; i++) {
double val = 0;
if (strstr(argv[i], "r"))
r_flag = 1;
else if (strstr(argv[i], "n"))
ret = '\r';
else if (parse_double(argv[i], &val)) {
if (val > max)
max = val;
if (val < min)
min = val;
}
else {
printf("spark [rn] [num1 num2...]\n\t-n Use \\r\n\t-r Reverse]\n");
return 0;
}
}
double diff = max - min + 1;
if (diff < 1)
diff = 1;
if (r_flag)
for (int i = argc - 1; i >= 0; i--)
print(diff, argv[i], min);
else
for (int i = 0; i < argc; i++)
print(diff, argv[i], min);
putchar(ret);
return 0;
}

View File

@ -6,7 +6,7 @@
#include <errno.h>
#include "config.h"
FILE *next_file(FILE *old, int x, int slen, char *prefix) {
static FILE *next_file(FILE *old, int x, int slen, char *prefix) {
if (old != NULL)
fclose(old);

View File

@ -12,11 +12,11 @@
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)
char *c_flag = NULL;
char *s_flag = NULL;
char p_flag;
static char *c_flag = NULL;
static char *s_flag = NULL;
static char p_flag;
int hide_input(int fd, int flag) {
static int hide_input(int fd, int flag) {
struct termios term;
if (tcgetattr(fd, &term) < 0)
return 1;
@ -33,7 +33,7 @@ int hide_input(int fd, int flag) {
return 0;
}
void su(const struct passwd *pw) {
static void su(const struct passwd *pw) {
char *shell = s_flag;
if (s_flag == NULL)
shell = (pw->pw_shell[0] == '\0') ? "/bin/sh" : pw->pw_shell;
@ -53,7 +53,7 @@ void su(const struct passwd *pw) {
execlp(shell, shell, "-l", NULL);
}
int password(const struct passwd *pw) {
static int password(const struct passwd *pw) {
static char psswd[512];
printf("Password: ");
@ -81,7 +81,7 @@ int password(const struct passwd *pw) {
return 0;
}
void endwin(int sig) {
static void endwin(int sig) {
UNUSED(sig);
hide_input(STDIN_FILENO, 0);

View File

@ -1,3 +1,4 @@
#define _XOPEN_SOURCE
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
@ -7,11 +8,11 @@
#include <string.h>
#include <time.h>
char c_flag;
char m_flag;
char a_flag;
static char c_flag;
static char m_flag;
static char a_flag;
int setdate(const char *path, const time_t date, char flag) {
static int setdate(const char *path, const time_t date, char flag) {
struct utimbuf new_time;
if (flag || a_flag)
@ -28,7 +29,7 @@ int setdate(const char *path, const time_t date, char flag) {
return 0;
}
int touch(const char *path, const time_t date) {
static int touch(const char *path, const time_t date) {
if (!c_flag) {
int fd = open(path, O_CREAT | O_RDONLY, 0666);
@ -55,7 +56,7 @@ int touch(const char *path, const time_t date) {
return 0;
}
time_t parse_date(char *str) {
static time_t parse_date(char *str) {
char *fmt = NULL;
switch (strlen(str)) {
case 8:

View File

@ -9,7 +9,7 @@
#include <sys/mount.h>
#include "config.h"
int parse_fstab(unsigned long flag) {
static int parse_fstab(unsigned long flag) {
FILE *fp = setmntent(MOUNT_CFG, "r");
if (fp == NULL) {
fprintf(stderr, "umount: %s\n", strerror(errno));

View File

@ -40,10 +40,10 @@ enum {
COUNT
};
char flags[COUNT];
unsigned int counter = 0;
static char flags[COUNT];
static unsigned int counter = 0;
void print(const char *msg) {
static void print(const char *msg) {
if (counter > 0 && !flags[ALL])
putchar(' ');
@ -129,4 +129,5 @@ int main(int argc, char **argv) {
print("unknow");
putchar('\n');
return 0;
}

View File

@ -7,21 +7,21 @@
#include "config.h"
/* cmd arguments l - lines c - bytes w - words */
unsigned int l_flag;
unsigned int c_flag;
unsigned int w_flag;
unsigned int t_flag;
static size_t l_flag;
static size_t c_flag;
static size_t w_flag;
static size_t t_flag;
unsigned int words;
unsigned int bytes;
unsigned int lines;
static size_t words;
static size_t bytes;
static size_t lines;
/* Total */
unsigned int twords;
unsigned int tbytes;
unsigned int tlines;
static size_t twords;
static size_t tbytes;
static size_t tlines;
void count(const int fd) {
static void count(const int fd) {
char buf[BUF_SIZE + 1];
off_t n = 0;
@ -45,20 +45,20 @@ void count(const int fd) {
}
}
tbytes += bytes;
twords += words;
tlines += lines;
tbytes += (size_t)bytes;
twords += (size_t)words;
tlines += (size_t)lines;
}
void print_count(const char *path, unsigned int plines, unsigned int pwords, unsigned int pbytes) {
static void print_count(const char *path, size_t plines, size_t pwords, size_t pbytes) {
if (l_flag)
printf("%7u ", plines);
printf("%7zu ", plines);
if (w_flag)
printf(" %7u", pwords);
printf(" %7zu", pwords);
if (c_flag)
printf(" %7u", pbytes);
printf(" %7zu", pbytes);
printf(" %s\n", path);
}

View File

@ -12,15 +12,15 @@
#define ARG_MAX 10000
#endif
int args;
char *cmd[ARG_MAX + 1];
static int args;
static char *cmd[ARG_MAX + 1];
char *I_flag;
char t_flag;
char r_flag;
char nl_flag;
int n_flag;
size_t s_flag;
static char *I_flag;
static char t_flag;
static char r_flag;
static char nl_flag;
static int n_flag;
static size_t s_flag;
enum {
NORMAL,
@ -34,7 +34,7 @@ enum {
QUOTE
};
void clear_cmd(void) {
static void clear_cmd(void) {
for (int i = 0; i < args; i++) {
if (cmd[i] != NULL) {
free(cmd[i]);
@ -45,7 +45,7 @@ void clear_cmd(void) {
args = 0;
}
int Iflag_push(const char *str, char **arg) {
static int Iflag_push(const char *str, char **arg) {
size_t arg_len = strlen(*arg);
size_t str_len = strlen(str);
size_t Iflag_len = strlen(I_flag);
@ -68,7 +68,7 @@ int Iflag_push(const char *str, char **arg) {
return 0;
}
int add_arg(const char *str, size_t chars, int flag) {
static int add_arg(const char *str, size_t chars, int flag) {
if (args >= ARG_MAX)
return ERROR;
@ -100,7 +100,7 @@ int add_arg(const char *str, size_t chars, int flag) {
return NORMAL;
}
int is_correct(char c) {
static int is_correct(char c) {
if (nl_flag)
return c == '\0';
@ -108,7 +108,7 @@ int is_correct(char c) {
return isspace(c);
}
int xargs(void) {
static int xargs(void) {
size_t arg_size = 0;
char *arg = malloc(1);
if (arg == NULL) {
@ -197,7 +197,7 @@ int xargs(void) {
return ret;
}
int spawn(void) {
static int spawn(void) {
if (t_flag) {
for (int i = 0; i < args; i++)
fprintf(stderr, "%s ", cmd[i]);