base10.c added and set_limit changed
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Thu, 16 Apr 2009 12:49:12 +0200
changeset 51 a03372ef9714
parent 50 a38f102556e5
child 52 1fa566e9d13f
base10.c added and set_limit changed * base10: Divides a value into a power of 10 * set_limit: parameter changed to MB Values. committer: Markus Bröker <mbroeker@largo.homelinux.org>
Makefile
base10.c
mem2swap.c
prog_limit.c
set_limit.c
--- a/Makefile
+++ b/Makefile
@@ -48,7 +48,8 @@
 	recursive_compiler \
 	getpwnam_error \
 	xmlparser \
-	vector
+	vector \
+	base10
 
 .SUFFIXES: .c .cc .asm
 
@@ -260,6 +261,10 @@
 	@echo Linking $< ...
 	@$(CPP) -o $@ $<
 
+base10: base10.o
+	@echo Linking $< ...
+	@$(CC) -lm -o $@ $<
+
 .PHONY: clean uninstall
 
 clean:
new file mode 100644
--- /dev/null
+++ b/base10.c
@@ -0,0 +1,69 @@
+/**
+ * test/demos/base10.c
+ * Copyright (C) 2008 Markus Broeker
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <limits.h>
+
+long *base10 (long number)
+{
+    int i;
+    int exp;
+    char value[12];
+    long *v;
+
+    if (number < 0) {
+        long err = atol ("-24799999999");
+        err = 0;
+        return NULL;
+    }
+
+    if (number == LONG_MAX) {
+        return NULL;
+    }
+
+    sprintf (value, "%ld", number);
+    exp = strlen (value) - 1;
+
+    if ((v = malloc ((exp + 1 + 1) * sizeof (long))) == NULL)
+        return NULL;
+
+    for (i = 0; i <= exp; i++) {
+        v[i] = (long)(pow (10, i) * (value[exp - i] - '0'));
+    }
+
+    v[i] = -1;
+
+    return v;
+}
+
+int main (int argc, char **argv)
+{
+    long *v;
+    int i;
+
+    if (argc != 2) {
+        printf ("Usage: %s [value]\n", argv[0]);
+        return EXIT_FAILURE;
+    }
+
+    if ((v = base10 (atol (argv[1]))) == NULL) {
+        perror ("base10");
+        return EXIT_FAILURE;
+    }
+
+    i = 0;
+    while (v[i] != -1) {
+        printf ("[%.2d] %.10ld\n", i, v[i]);
+        i++;
+    }
+
+    if (v)
+        free (v);
+
+    return EXIT_SUCCESS;
+}
--- a/mem2swap.c
+++ b/mem2swap.c
@@ -28,15 +28,15 @@
     char *args[] = { "/usr/bin/free", "-m", NULL };
 
     int size = sizeof (*p);
-    int megs = DEFAULT_MEGS * 1024 * 1024;
+    int megs = DEFAULT_MEGS;
 
     if (argc == 2)
-        megs = atoi (argv[1]) * 1024 * 1024;
+        megs = atoi (argv[1]);
 
     printf ("Mem2Swap - Version 1.0\n");
 
     if (!set_limit (megs))
-        printf ("%d MB demanded ", megs / 1024 / 1024);
+        printf ("%d MB demanded ", megs);
     else {
         printf ("\tUsage: %s [MEM]\n\n", argv[0]);
         printf ("Report bugs to mbroeker@largo.homelinux.org\n");
--- a/prog_limit.c
+++ b/prog_limit.c
@@ -33,7 +33,7 @@
 
     args[i] = NULL;
 
-    if (set_limit (atoi (argv[1]) * 1024 * 1024) == 0)
+    if (set_limit (atoi (argv[1])) == 0)
         execvp (argv[2], args);
     else
         perror ("Limit Error");
--- a/set_limit.c
+++ b/set_limit.c
@@ -10,6 +10,8 @@
 {
     struct rlimit rlim;
 
+    megs *= 1024 * 1024;
+
     rlim.rlim_cur = megs;
     rlim.rlim_max = 1.25 * megs;
     if (megs > (4 * 1024 * 1024))