fixed du ls chmod
This commit is contained in:
parent
1a14f8e484
commit
9f12d068fc
|
@ -14,6 +14,7 @@ char *mu_humansize(off_t n, off_t block) {
|
|||
|
||||
double size = n;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < strlen(postfixes) && size >= block; i++)
|
||||
size /= block;
|
||||
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
#ifndef _MODE_TO_STR_H
|
||||
#define _MODE_TO_STR_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
char *mu_mode_2_str(mode_t file_mode) {
|
||||
static char mode[11];
|
||||
snprintf(mode, sizeof(mode), "----------");
|
||||
|
||||
if (file_mode & S_IRUSR)
|
||||
mode[1] = 'r';
|
||||
|
||||
if (file_mode & S_IRGRP)
|
||||
mode[4] = 'r';
|
||||
|
||||
if (file_mode & S_IROTH)
|
||||
mode[7] = 'r';
|
||||
|
||||
if (file_mode & S_IWUSR)
|
||||
mode[2] = 'w';
|
||||
|
||||
if (file_mode & S_IWGRP)
|
||||
mode[5] = 'w';
|
||||
|
||||
if (file_mode & S_IWOTH)
|
||||
mode[8] = 'w';
|
||||
|
||||
if (file_mode & S_IXUSR)
|
||||
mode[3] = 'x';
|
||||
|
||||
if (file_mode & S_IXGRP)
|
||||
mode[6] = 'x';
|
||||
|
||||
if (file_mode & S_IXOTH)
|
||||
mode[9] = 'x';
|
||||
|
||||
if (file_mode & S_ISUID) {
|
||||
if (file_mode & S_IXUSR)
|
||||
mode[3] = 's';
|
||||
|
||||
else
|
||||
mode[3] = 'S';
|
||||
}
|
||||
|
||||
if (file_mode & S_ISGID) {
|
||||
if (file_mode & S_IRGRP)
|
||||
mode[6] = 's';
|
||||
|
||||
else
|
||||
mode[6] = 'S';
|
||||
}
|
||||
|
||||
return mode;
|
||||
}
|
||||
|
||||
#endif
|
|
@ -12,6 +12,7 @@
|
|||
#define WR_PERM (S_IWUSR | S_IWGRP | S_IWOTH)
|
||||
#define EX_PERM (S_IXUSR | S_IXGRP | S_IXOTH)
|
||||
#define RD_PERM (S_IRUSR | S_IRGRP | S_IROTH)
|
||||
#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) {
|
||||
|
@ -27,7 +28,7 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
|
|||
mode = 0;
|
||||
|
||||
/* Default + */
|
||||
int append = 1;
|
||||
char append = 1;
|
||||
mode_t mask = 0;
|
||||
|
||||
for (size_t i = 0; i < strlen(s); i++) {
|
||||
|
@ -44,6 +45,10 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
|
|||
mode |= EX_PERM;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
mode |= SU_PERM;
|
||||
break;
|
||||
|
||||
case '+':
|
||||
append = 1;
|
||||
break;
|
||||
|
@ -52,6 +57,10 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
|
|||
append = 0;
|
||||
break;
|
||||
|
||||
case '=':
|
||||
append = 2;
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
mask |= G;
|
||||
break;
|
||||
|
@ -77,10 +86,14 @@ mode_t mu_parse_mode(const char *s, mode_t cur_mode) {
|
|||
mask = U;
|
||||
|
||||
mask &= mode;
|
||||
if (!append)
|
||||
if (append == 0)
|
||||
mode = ~mode;
|
||||
|
||||
return (cur_mode & ~mask) | (mode & mask);
|
||||
if (append == 2)
|
||||
return mode & mask;
|
||||
|
||||
else
|
||||
return (cur_mode & ~mask) | (mode & mask);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include "mode_to_str.h"
|
||||
#include "make_path.h"
|
||||
#include "get_stat.h"
|
||||
#include "parse_mode.h"
|
||||
|
@ -30,7 +31,7 @@ int change(const char *file, void *p) {
|
|||
}
|
||||
|
||||
if (v_flag)
|
||||
printf("chmod: %s: changed mode to %d\n", file, mode);
|
||||
printf("chmod: %s: changed mode to %s\n", file, mu_mode_2_str(mode));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,19 +16,21 @@ off_t total;
|
|||
|
||||
void print(off_t size, const char *filename) {
|
||||
if (h_flag)
|
||||
printf("%s\t%s\n", mu_humansize(size, 1024), filename);
|
||||
printf("%s\t%s\n", mu_humansize(size * 512, 1024), filename);
|
||||
|
||||
else
|
||||
printf("%jd\t%s\n", (intmax_t)size / 1024, filename);
|
||||
printf("%jd\t%s\n", (intmax_t)size / 2, filename);
|
||||
}
|
||||
|
||||
off_t du(const char *path, int recurs_flag) {
|
||||
off_t sum = 0;
|
||||
|
||||
struct stat sb;
|
||||
if (mu_get_lstat("du", path, &sb))
|
||||
return 0;
|
||||
|
||||
off_t sum = sb.st_blocks;
|
||||
if (c_flag)
|
||||
total += sum;
|
||||
|
||||
if (S_ISDIR(sb.st_mode)) {
|
||||
DIR *dp = opendir(path);
|
||||
if (!dp) {
|
||||
|
@ -62,15 +64,8 @@ off_t du(const char *path, int recurs_flag) {
|
|||
print(sum, path);
|
||||
}
|
||||
|
||||
/* Get file size */
|
||||
else {
|
||||
sum = sb.st_blocks * 512;
|
||||
if (c_flag)
|
||||
total += sum;
|
||||
|
||||
if (!recurs_flag)
|
||||
print(sum, path);
|
||||
}
|
||||
else if (!recurs_flag)
|
||||
print(sum, path);
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include "mode_to_str.h"
|
||||
#include "make_path.h"
|
||||
#include "get_stat.h"
|
||||
#include "config.h"
|
||||
|
@ -165,60 +166,8 @@ char *get_date(time_t mtime) {
|
|||
int print(const struct d_node *node) {
|
||||
char suf = ' ';
|
||||
char *color = "";
|
||||
char mode[] = "----------";
|
||||
|
||||
if (node->stats.st_mode & S_IRUSR)
|
||||
mode[1] = 'r';
|
||||
|
||||
if (node->stats.st_mode & S_IRGRP)
|
||||
mode[4] = 'r';
|
||||
|
||||
if (node->stats.st_mode & S_IROTH)
|
||||
mode[7] = 'r';
|
||||
|
||||
if (node->stats.st_mode & S_IWUSR)
|
||||
mode[2] = 'w';
|
||||
|
||||
if (node->stats.st_mode & S_IWGRP)
|
||||
mode[5] = 'w';
|
||||
|
||||
if (node->stats.st_mode & S_IWOTH)
|
||||
mode[8] = 'w';
|
||||
|
||||
if (node->stats.st_mode & S_IXUSR)
|
||||
mode[3] = 'x';
|
||||
|
||||
if (node->stats.st_mode & S_IXGRP)
|
||||
mode[6] = 'x';
|
||||
|
||||
if (node->stats.st_mode & S_IXOTH)
|
||||
mode[9] = 'x';
|
||||
|
||||
if (node->stats.st_mode & S_ISUID) {
|
||||
if (node->stats.st_mode & S_IXUSR)
|
||||
mode[3] = 's';
|
||||
|
||||
else
|
||||
mode[3] = 'S';
|
||||
}
|
||||
|
||||
if (node->stats.st_mode & S_ISGID) {
|
||||
if (node->stats.st_mode & S_IRGRP)
|
||||
mode[6] = 's';
|
||||
|
||||
else
|
||||
mode[6] = 'S';
|
||||
}
|
||||
|
||||
if (node->stats.st_mode & S_ISVTX) {
|
||||
if (node->stats.st_mode & S_IROTH)
|
||||
mode[9] = 't';
|
||||
|
||||
else
|
||||
mode[9] = 'T';
|
||||
}
|
||||
|
||||
|
||||
char *mode = mu_mode_2_str(node->stats.st_mode);
|
||||
if (S_ISDIR(node->stats.st_mode)) {
|
||||
if (node->name[strlen(node->name) - 1] != '/')
|
||||
suf = '/';
|
||||
|
|
|
@ -29,7 +29,7 @@ void execute(char *argv[]) {
|
|||
execvp(argv[0], argv);
|
||||
|
||||
fprintf(stderr, "init: Failed to run %s\nRebooting...\n", argv[0]);
|
||||
poweroff(SIGKILL);
|
||||
poweroff(SIGUSR2);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue