fixes, 1/2 dd writted

This commit is contained in:
Your Name 2024-02-01 21:09:23 +03:00
parent 2aca0e1900
commit 3636254cb5
5 changed files with 174 additions and 22 deletions

View File

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

View File

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

3
src/coreutils/dd/build.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
project_dir=$(pwd)
echo ./*.c $CFLAGS -o $OUTPUT$(basename $project_dir) | xargs $CC

162
src/coreutils/dd/dd.c Normal file
View File

@ -0,0 +1,162 @@
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "unused.h"
int openfile(int flag, char *path, int mode) {
if (!strcmp(path, "-")) {
if (flag)
return STDOUT_FILENO;
return STDIN_FILENO;
}
int fd = open(path, mode, 0666);
if (fd < 0) {
fprintf(stderr, "dd: %s: %s\n", path, strerror(errno));
exit(1);
}
return fd;
}
size_t strtonum(char *str) {
char *p = NULL;
size_t res = strtoll(str, &p, 10);
if (str != p) {
if (!strcmp(p, "b"))
res *= 512;
else if (!strcmp(p, "MB"))
res *= 1000000;
else if (!strcmp(p, "M"))
res *= 1048576;
else if (!strcmp(p, "KB"))
res *= 1024;
else if (!strcmp(p, "kB"))
res *= 1000;
else if (!strcmp(p, "G"))
res *= 1000000000;
else if (!strcmp(p, "GB"))
res *= 1073741824;
}
if (res <= 1)
res = 0;
return res;
}
int main(int argc, char **argv) {
char *ifp = "-";
char *ofp = "-";
off_t skip = 0;
off_t seek = 0;
off_t count = -1;
off_t bs = 0;
off_t ibs = 512;
off_t obs = 512;
int ofd = STDOUT_FILENO;
int ifd = STDIN_FILENO;
/* Return value */
int ret = 1;
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
char *val = strchr(arg, '=');
if (val == NULL) {
printf("dd\n\t[if=InputFile]\n\t[of=OutputFile]\n\nN and BYTES may be followed by the following multiplicative\nsuffixes: w=2, b=512, kB=1000, K=1024, MB=1000*1000,\nM=1024*1024, GB=1000*1000*1000, G=1024*1024*1024\n");
return 1;
}
*val = '\0';
val++;
/* Get value */
if (!strcmp(arg, "if"))
ifp = val;
else if (!strcmp(arg, "of"))
ofp = val;
else if (!strcmp(arg, "seek"))
seek = strtonum(val);
else if (!strcmp(arg, "skip"))
skip = strtonum(val);
else if (!strcmp(arg, "count"))
count = strtonum(val);
else if (!strcmp(arg, "bs"))
bs = strtonum(val);
else if (!strcmp(arg, "ibs"))
ibs = strtonum(val);
else if (!strcmp(arg, "obs"))
obs = strtonum(val);
}
if (bs) {
ibs = bs;
obs = bs;
}
/* Make input buffer */
char *buf = malloc(ibs);
if (buf == NULL) {
fprintf(stderr, "dd: %s\n", strerror(errno));
return 1;
}
/* Open files. Input */
ifd = openfile(0, ifp, O_RDONLY);
if (skip) {
if (lseek(ifd, skip * ibs, SEEK_CUR) < 0)
goto CLOSE;
}
/* Output */
int oflag = O_WRONLY | O_CREAT;
if (seek)
oflag |= O_TRUNC;
ofd = openfile(1, ofp, oflag);
if (seek) {
if (ftruncate(ofd, seek * ibs) < 0 || lseek(ofd, seek * ibs, SEEK_SET) < 0) {
fprintf(stderr, "dd: %s\n", strerror(errno));
goto CLOSE;
}
}
/* dd */
if (fsync(ofd) < 0)
goto CLOSE;
ret = 0;
CLOSE:
free(buf);
close(ifd);
close(ofd);
/* tmp */
UNUSED(obs);
UNUSED(count);
return ret;
}

View File

@ -21,11 +21,8 @@ void print(off_t size, const char *filename) {
if (h_flag)
printf("%s\t%s%c", mu_humansize(size * BLK_SIZE), filename, n_flag);
else if (b_flag)
printf("%ju\t%s%c", (intmax_t)size, filename, n_flag);
else
printf("%ju\t%s%c", ((intmax_t)size * BLK_SIZE) / 1024, filename, n_flag);
printf("%jd\t%s%c", (intmax_t)size * BLK_SIZE / 1024, filename, n_flag);
}
off_t du(const char *path, int recurs_flag) {
@ -70,11 +67,7 @@ off_t du(const char *path, int recurs_flag) {
/* Get file size */
else {
if (b_flag)
sum = sb.st_size;
else
sum = (512 * sb.st_blocks + BLK_SIZE - 1) / BLK_SIZE;
sum = (512 * sb.st_blocks + BLK_SIZE - 1) / BLK_SIZE;
if (c_flag)
total += sum;
@ -99,11 +92,6 @@ int main(int argc, char **argv) {
s_flag = 1;
break;
case 'b':
b_flag = 1;
h_flag = 0;
break;
case 'c':
c_flag = 1;
break;
@ -113,7 +101,7 @@ int main(int argc, char **argv) {
break;
default:
printf("du [src1 src2...]\n\t[-h Sizes in human readable format]\n\t[-s Display only a total for each argument]\n\t[-b Size in bytes] [-c produce a grand total]\n\t[-0 end each output line with NUL, not newline]\n");
printf("du [src1 src2...]\n\t[-h Sizes in human readable format]\n\t[-s Display only a total for each argument]\n\t[-c produce a grand total]\n\t[-0 end each output line with NUL, not newline]\n");
return 0;
}
}