time and seq added

This commit is contained in:
Your Name 2023-12-22 22:16:26 +03:00
parent f9050b632e
commit dfb806be4e
7 changed files with 135 additions and 0 deletions

1
TODO
View File

@ -34,6 +34,7 @@ Other:
free
swapon
swapoff
hexdump
Loginutils:
su

2
src/coreutils/seq/build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
echo ./*.c $CFLAGS $OUTPUT | xargs $CC

58
src/coreutils/seq/seq.c Normal file
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
double parse_double(const char *str) {
char *ptr;
double res = strtod(str, &ptr);
if (*ptr) {
fprintf(stderr, "seq: %s: cant parse\n", ptr);
exit(1);
}
return res;
}
int main(int argc, char **argv) {
double start = 1;
double last = 0;
double n = 1;
argv++;
argc--;
switch (argc) {
case 1:
last = parse_double(argv[0]);
break;
case 2:
start = parse_double(argv[0]);
last = parse_double(argv[1]);
break;
case 3:
n = parse_double(argv[1]);
start = parse_double(argv[0]);
last = parse_double(argv[2]);
break;
default:
fprintf(stderr, "seq: missing operands\n");
return 1;
}
if (start <= last && n >= 0) {
for (double i = start; i <= last; i += n)
printf("%.*f\n", i);
}
else if (n <= 0)
for (double i = start; i >= last; i += n)
printf("%.*f\n", i);
return 0;
}

2
src/coreutils/time/build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/sh
echo ./*.c $CFLAGS $OUTPUT | xargs $CC

51
src/coreutils/time/time.c Normal file
View File

@ -0,0 +1,51 @@
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/times.h>
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "time: missing operand\n");
return 1;
}
/* Get time */
struct tms t1, t2;
long ticks = sysconf(_SC_CLK_TCK);
if (ticks <= 0) {
fprintf(stderr, "time: %s\n", strerror(errno));
return 1;
}
clock_t r1 = times(&t1);
if (r1 == (clock_t)-1) {
fprintf(stderr, "time: %s\n", strerror(errno));
return 1;
}
/* Run */
pid_t pid;
if ((pid = fork()) == 0)
execvp(argv[1], argv + 1);
int status = 0;
waitpid(pid, &status, 0);
/* Get time */
clock_t r2 = times(&t1);
if (r2 == (clock_t)-1) {
fprintf(stderr, "time: %s\n", strerror(errno));
return 1;
}
/* Print */
fprintf(stderr, "real %.4f\nuser %.4f\nsys %.4f\n", (r2 - r1) / (double)ticks, t1.tms_cutime / (double)ticks, t2.tms_cstime / (double)ticks);
if (status != 0)
printf("Proccess returned %d\n", status);
return 0;
}

View File

@ -0,0 +1,2 @@
#!/bin/sh
echo ./*.c $CFLAGS $OUTPUT | xargs $CC

View File

@ -0,0 +1,19 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/syscall.h>
int main(int argc, char **argv) {
if (argc < 2) {
printf("pivot_root [new-root] [put-old]\n");
return 0;
}
if (syscall(SYS_pivot_root, argv[0], argv[1]) < 0) {
fprintf(stderr, "pivot_root: %s\n", strerror(errno));
return 1;
}
return 0;
}