fixed
This commit is contained in:
parent
e33ad2a512
commit
2a914caec3
4
config.h
4
config.h
|
@ -47,10 +47,8 @@ char *INIT_START[] = {"/etc/rc.init", NULL};
|
||||||
/* #define OS_NAME "unknow" */
|
/* #define OS_NAME "unknow" */
|
||||||
|
|
||||||
/* Options: To disable, comment line */
|
/* Options: To disable, comment line */
|
||||||
/* Add escape-char support in echo */
|
|
||||||
#define ECHO_FANCY
|
|
||||||
|
|
||||||
/* getloadavg() unavailable in android os */
|
/* getloadavg() unavailable in android */
|
||||||
#ifndef __ANDROID__
|
#ifndef __ANDROID__
|
||||||
#define UPTIME_LOADAVG
|
#define UPTIME_LOADAVG
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,78 +2,85 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "config.h"
|
|
||||||
|
char n_flag = 0;
|
||||||
|
char e_flag = 0;
|
||||||
|
|
||||||
void format(char *str) {
|
void format(char *str) {
|
||||||
#ifdef ECHO_FANCY
|
if (e_flag) {
|
||||||
for (size_t i = 0; i < strlen(str); i++) {
|
for (size_t i = 0; i < strlen(str); i++) {
|
||||||
unsigned int c = str[i];
|
unsigned int c = str[i];
|
||||||
if (c == '\\') {
|
if (c == '\\') {
|
||||||
switch (str[i + 1]) {
|
switch (str[i + 1]) {
|
||||||
case 'a':
|
case 'a':
|
||||||
c = '\a';
|
c = '\a';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
c = '\n';
|
c = '\n';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 't':
|
case 't':
|
||||||
c = '\t';
|
c = '\t';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'c':
|
case 'c':
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
case 'v':
|
case 'v':
|
||||||
c = '\v';
|
c = '\v';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'r':
|
case 'r':
|
||||||
c = '\r';
|
c = '\r';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'f':
|
case 'f':
|
||||||
c = '\f';
|
c = '\f';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'e':
|
case 'e':
|
||||||
c = '\033';
|
c = '\033';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'b':
|
case 'b':
|
||||||
c = '\b';
|
c = '\b';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
c = '\\';
|
c = '\\';
|
||||||
|
}
|
||||||
|
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
i++;
|
putchar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
putchar(c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
else
|
||||||
fputs(str, stdout);
|
fputs(str, stdout);
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
unsigned int n_flag = 0;
|
|
||||||
|
|
||||||
int i;
|
argv++;
|
||||||
for (i = 1; i < argc; i++) {
|
argc--;
|
||||||
if (argv[i][0] != '-')
|
|
||||||
break;
|
|
||||||
|
|
||||||
else if (!strcmp(argv[i], "-n"))
|
for (int i = 0; i < argc; i++) {
|
||||||
n_flag = 1;
|
if (argv[i][0] == '-') {
|
||||||
|
if (strstr(argv[i] + 1, "n"))
|
||||||
|
n_flag = 1;
|
||||||
|
|
||||||
|
else if (strstr(argv[i] + 1, "e"))
|
||||||
|
e_flag = 1;
|
||||||
|
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
format(argv[i]);
|
format(argv[i]);
|
||||||
if (i < argc - 1)
|
if (i < argc - 1)
|
||||||
putchar(' ');
|
putchar(' ');
|
||||||
|
|
|
@ -129,22 +129,24 @@ struct d_node **list_one(const char *path, int *ret) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char *new_path = strdup(path);
|
char *new_path = strdup(path);
|
||||||
if (new_path == NULL) {
|
if (new_path == NULL)
|
||||||
free(dir);
|
goto ERROR;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
dir[0] = stat_file(new_path, 1);
|
dir[0] = stat_file(new_path, 1);
|
||||||
if (dir[0] == NULL) {
|
if (dir[0] == NULL)
|
||||||
free(dir);
|
goto ERROR_PATH;
|
||||||
free(new_path);
|
|
||||||
*ret = 1;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
dir[1] = NULL;
|
dir[1] = NULL;
|
||||||
return dir;
|
return dir;
|
||||||
|
|
||||||
|
ERROR_PATH:
|
||||||
|
*ret = 1;
|
||||||
|
free(new_path);
|
||||||
|
|
||||||
|
ERROR:
|
||||||
|
free(dir);
|
||||||
|
return NULL;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void dfree(struct d_node **dir, size_t files) {
|
void dfree(struct d_node **dir, size_t files) {
|
||||||
|
@ -158,7 +160,9 @@ void dfree(struct d_node **dir, size_t files) {
|
||||||
|
|
||||||
char *get_date(time_t mtime) {
|
char *get_date(time_t mtime) {
|
||||||
static char time[100];
|
static char time[100];
|
||||||
|
|
||||||
strftime(time, sizeof(time), "%b %d %H:%M", localtime(&mtime));
|
strftime(time, sizeof(time), "%b %d %H:%M", localtime(&mtime));
|
||||||
|
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,11 +304,11 @@ int sortt(const void *p1, const void *p2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int sorts(const void *p1, const void *p2) {
|
int sorts(const void *p1, const void *p2) {
|
||||||
return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size;
|
return (*(struct d_node **)p2)->stats.st_size - (*(struct d_node **)p1)->stats.st_size <= 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
int sortd(const void *p1, const void *p2) {
|
int sortd(const void *p1, const void *p2) {
|
||||||
return (*(struct d_node **)p1)->stats.st_nlink - (*(struct d_node **)p2)->stats.st_nlink;
|
return strcmp((*(struct d_node **)p1)->full_name, (*(struct d_node **)p2)->full_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ls(const char *dir_name, int label, struct winsize w) {
|
int ls(const char *dir_name, int label, struct winsize w) {
|
||||||
|
@ -328,8 +332,7 @@ int ls(const char *dir_name, int label, struct winsize w) {
|
||||||
if (dir == NULL)
|
if (dir == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (sorter != NULL)
|
qsort(dir, files, sizeof(struct d_node *), sorter);
|
||||||
qsort(dir, files, sizeof(struct d_node *), sorter);
|
|
||||||
|
|
||||||
if ((label || R_flag) && !d_flag && !its_file)
|
if ((label || R_flag) && !d_flag && !its_file)
|
||||||
printf("%s:\n", dir_name);
|
printf("%s:\n", dir_name);
|
||||||
|
|
|
@ -84,14 +84,14 @@ int main(int argc, char **argv) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argv[optind] == NULL) {
|
argc -= optind;
|
||||||
|
argv += optind;
|
||||||
|
|
||||||
|
if (argc == 0) {
|
||||||
fprintf(stderr, "rm: missing operand\n");
|
fprintf(stderr, "rm: missing operand\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
argv += optind;
|
|
||||||
argc -= optind;
|
|
||||||
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
for (int i = 0; i < argc; i++) {
|
for (int i = 0; i < argc; i++) {
|
||||||
if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){
|
if (!strcmp(argv[i], ".") || !strcmp(argv[i], "..")){
|
||||||
|
|
|
@ -12,15 +12,15 @@ typedef struct {
|
||||||
} SIG;
|
} SIG;
|
||||||
|
|
||||||
SIG signals[] = {
|
SIG signals[] = {
|
||||||
{"HUP", SIGHUP},
|
{"HUP", SIGHUP},
|
||||||
{"INT", SIGINT},
|
{"INT", SIGINT},
|
||||||
{"QUIT", SIGQUIT},
|
{"QUIT", SIGQUIT},
|
||||||
{"ILL", SIGILL},
|
{"ILL", SIGILL},
|
||||||
{"TRAP", SIGTRAP},
|
{"TRAP", SIGTRAP},
|
||||||
{"ABRT", SIGABRT},
|
{"ABRT", SIGABRT},
|
||||||
{"IOT", SIGIOT},
|
{"IOT", SIGIOT},
|
||||||
{"BUS", SIGBUS},
|
{"BUS", SIGBUS},
|
||||||
{"FPE", SIGFPE},
|
{"FPE", SIGFPE},
|
||||||
{"KILL", SIGKILL},
|
{"KILL", SIGKILL},
|
||||||
{"USR1", SIGUSR1},
|
{"USR1", SIGUSR1},
|
||||||
{"SEGV", SIGSEGV},
|
{"SEGV", SIGSEGV},
|
||||||
|
@ -35,10 +35,9 @@ SIG signals[] = {
|
||||||
|
|
||||||
int parse_sig(char *arg) {
|
int parse_sig(char *arg) {
|
||||||
int sig = atoi(arg);
|
int sig = atoi(arg);
|
||||||
if (sig >= 0 && sig >= NSIG)
|
if (sig >= 0 && sig <= NSIG)
|
||||||
return sig;
|
return sig;
|
||||||
|
|
||||||
|
|
||||||
if (!strncasecmp(arg, "SIG", 3))
|
if (!strncasecmp(arg, "SIG", 3))
|
||||||
arg += 3;
|
arg += 3;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
#!/bin/sh
|
||||||
|
project_dir=$(pwd)
|
||||||
|
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC
|
|
@ -0,0 +1,66 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
int open_file(const char *path) {
|
||||||
|
int fd = open(path, O_RDONLY);
|
||||||
|
if (fd < 0) {
|
||||||
|
fprintf(stderr, "ps: %s: %s\n", path, strerror(errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int pscan(const char *pid) {
|
||||||
|
char path[PATH_MAX + 1];
|
||||||
|
char cmdline[PATH_MAX + 1];
|
||||||
|
|
||||||
|
/* Arguments */
|
||||||
|
snprintf(path, sizeof(path), "/proc/%s/cmdline", pid);
|
||||||
|
int fd = open_file(path);
|
||||||
|
if (fd == -1)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
read(fd, cmdline, sizeof(cmdline));
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
printf("%5s %5s\n", pid, cmdline);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
int opt;
|
||||||
|
while ((opt = getopt(argc, argv, "o:")) != -1) {
|
||||||
|
switch (opt) {
|
||||||
|
case 'o':
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("ps [o] [PID]\n\t-o Options (nice, tty, cmd, pid, nice)\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DIR *dp = opendir("/proc");
|
||||||
|
if (dp == NULL) {
|
||||||
|
fprintf(stderr, "ps: /proc: %s\n", strerror(errno));
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("PID CMD\n");
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
struct dirent *ep;
|
||||||
|
while ((ep = readdir(dp)) != NULL)
|
||||||
|
if (atoi(ep->d_name))
|
||||||
|
if (pscan(ep->d_name))
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
|
closedir(dp);
|
||||||
|
return ret;
|
||||||
|
}
|
Loading…
Reference in New Issue