Compatibility: We support Linux and BSD
authorMarkus Brökers <mbroeker@largo.homelinux.org>
Tue, 17 Aug 2010 14:47:41 +0200
changeset 138 dff18d1ac2af
parent 137 d8a0984b72fc
child 139 cb1d3f4cf18e
Compatibility: We support Linux and BSD
Makefile
daemon.c
file_demo.c
fork.c
mem2swap.c
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
      CC = gcc -g -ggdb $(PROF)
     CPP = g++ -g -ggdb $(PROF)
  CFLAGS = -Wall -O2 -Iinclude $(EXTRA)
-  EXTRA =
-LDFLAGS = -L/usr/lib
+  EXTRA = -I/usr/local/include
+LDFLAGS = -L/usr/lib -L/usr/local/lib
      RM = rm -f
    FIND = find
    MAKE = make
@@ -13,9 +13,7 @@
    PROF = -fprofile-arcs -ftest-coverage -pg
 endif
 
-TARGET  = ncurses
-TARGET += numerierung
-TARGET += xdemo
+TARGET  = numerierung
 TARGET += signals
 TARGET += tree
 TARGET += utf8
@@ -32,23 +30,18 @@
 TARGET += hex2chars
 TARGET += floating
 TARGET += max
-TARGET += recording
 TARGET += endian
 TARGET += fak
 TARGET += blackhole
 TARGET += folge
 TARGET += counter
 TARGET += sudoku
-TARGET += cunit
-TARGET += md5
-TARGET += md5rec
 TARGET += myprintf
 TARGET += concatenation
 TARGET += alpha_beta
 TARGET += life
 TARGET += bad_alloc
 TARGET += lotto
-TARGET += database
 TARGET += gauss
 TARGET += mem2swap
 TARGET += prog_limit
@@ -59,7 +52,6 @@
 TARGET += min2time
 TARGET += recursive_compiler
 TARGET += getpwnam_error
-TARGET += xmlparser
 TARGET += vector
 TARGET += base10
 TARGET += files
@@ -77,6 +69,28 @@
 TARGET += getbits
 TARGET += fun
 
+# needs ncursesw
+TARGET  += ncurses
+
+#needs xpat
+TARGET += xmlparser
+
+# needs CUnit
+TARGET += cunit
+
+# needs postgresql
+TARGET += database
+
+# needs lsflib/
+TARGET += md5
+TARGET += md5rec
+
+# needs x11
+TARGET += xdemo
+
+# needs linux
+TARGET += recording
+
 .SUFFIXES: .c .cc .asm
 
 .c.o:
--- a/daemon.c
+++ b/daemon.c
@@ -16,6 +16,7 @@
 
 #define      MAXLEN 80
 #define    TIME_OUT 60
+
 #define  INPUT_FILE "/proc/meminfo"
 #define OUTPUT_FILE "/tmp/meminfo.log"
 #define   LOCK_FILE "/tmp/.daemon.lock"
@@ -48,6 +49,11 @@
     struct stat st;
     int fd;
 
+    if (stat (INPUT_FILE, &st) != 0) {
+        perror (INPUT_FILE);
+        exit (EXIT_FAILURE);
+    }
+
     if (argc == 2) {
         if (!strcmp (argv[1], "-h")) {
             printf ("Usage: %s [-f|-h]\n", argv[0]);
@@ -63,7 +69,10 @@
     }
 
     if (stat (LOCK_FILE, &st) < 0) {
-        fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0);
+        if ((fd = open (LOCK_FILE, O_WRONLY | O_CREAT, 0)) < 0) {
+            perror (LOCK_FILE);
+            exit (EXIT_FAILURE);
+        }
         close (fd);
     } else {
         fprintf (stderr, "%s is already running\n", argv[0]);
--- a/file_demo.c
+++ b/file_demo.c
@@ -10,6 +10,9 @@
 #include <string.h>
 #include <fcntl.h>
 
+#define TESTFILE1 "/tmp/test1.txt"
+#define TESTFILE2 "/tmp/test2.txt"
+
 int main (int argc, char **argv)
 {
     int fd1;
@@ -18,20 +21,20 @@
 
     char *str = "Schreib mal was!";
 
-    if (argc != 2)
+    if (argc != 2) {
+        printf ("Usage: %s <MODE>\n", argv[0]);
         return -1;
+    }
     mode = strtoul (argv[1], NULL, 8);
 
-    if (system ("/bin/rm -f /home/mbroeker/Desktop/test1.txt") != 0)
-        return EXIT_FAILURE;
-    if (system ("/bin/rm -f /home/mbroeker/Desktop/test2.txt") != 0)
-        return EXIT_FAILURE;
+    unlink (TESTFILE1);
+    unlink (TESTFILE2);
 
     /*
      * Opens ReadOnly
      */
-    fd1 = open ("/home/mbroeker/Desktop/test1.txt", O_CREAT, mode);
-    fd2 = open ("/home/mbroeker/Desktop/test2.txt", O_RDWR | O_CREAT, mode);
+    fd1 = open (TESTFILE1, O_CREAT, mode);
+    fd2 = open (TESTFILE2, O_RDWR | O_CREAT, mode);
 
     if (fd1)
         if (write (fd1, str, strlen (str)) == -1)
--- a/fork.c
+++ b/fork.c
@@ -18,7 +18,7 @@
     switch (pid) {
     case 0:
         printf ("Starting new Process\n");
-        execve (cmd, argv, argp);
+        return execve (cmd, argv, argp);
     case -1:
         perror ("FORK");
         return errno;
@@ -26,8 +26,12 @@
         printf ("Pid started as %d\n", pid);
         wait (&pid);
         printf ("Exit-Status = %d\n", pid);
+
         return pid;
     }
+
+    // should never happen
+    return -1;
 }
 
 int main (int argc, char **argv)
--- a/mem2swap.c
+++ b/mem2swap.c
@@ -17,6 +17,14 @@
 #define DEFAULT_MEGS 64
 #endif
 
+#ifdef __linux__
+#define CMD_FREE "/usr/bin/free"
+#define CMD_ARGS "-m"
+#elif __unix__
+#define CMD_FREE "/sbin/sysctl"
+#define CMD_ARGS "hw.usermem"
+#endif
+
 int set_limit (int);
 
 int main (int argc, char **argv)
@@ -26,7 +34,7 @@
     long int *p;
     long int *p_new;
 
-    char *args[] = { "/usr/bin/free", "-m", NULL };
+    char *args[] = { CMD_FREE, CMD_ARGS, NULL };
 
     int size = sizeof (*p);
     int megs = DEFAULT_MEGS;
@@ -69,7 +77,7 @@
     pid = fork ();
     switch (pid) {
     case 0:
-        execve ("/usr/bin/free", args, NULL);
+        execve (CMD_FREE, args, NULL);
     case -1:
         perror ("Fork Error");
         return EXIT_FAILURE;