time and seq added
This commit is contained in:
parent
f9050b632e
commit
dfb806be4e
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
echo ./*.c $CFLAGS $OUTPUT | xargs $CC
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
echo ./*.c $CFLAGS $OUTPUT | xargs $CC
|
|
@ -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;
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
#!/bin/sh
|
||||
echo ./*.c $CFLAGS $OUTPUT | xargs $CC
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue