# HG changeset patch # User Markus Bröker # Date 1229169506 -3600 # Node ID af501b0c17160d9c75ffef044dcc233ff887f50e demos cvs copy committer: Markus Bröker diff --git a/Makefile b/Makefile new file mode 100644 --- /dev/null +++ b/Makefile @@ -0,0 +1,182 @@ +CC=gcc -g -ggdb +CFLAGS=-Wall -O2 -Iinclude -ansi +NASM=nasm -f elf -Iinclude/ +TARGET=ncurses \ + numerierung \ + xdemo \ + signals \ + tree \ + utf8 \ + file_demo \ + testcase \ + atoi_print \ + data_types \ + dnsresolve \ + nomalloc \ + urandom \ + threads \ + crypt \ + tokenpasting \ + hex2chars \ + floating \ + max \ + recording \ + endian \ + fak \ + blackhole \ + folge \ + counter \ + sudoku \ + cunit \ + md5 \ + varargs \ + concatenation \ + alpha_beta \ + life + +.SUFFIXES: .c .asm + +.c.o: + @echo Compiling $< ... + @$(CC) -c $(CFLAGS) -o $@ $< + +.asm.o: + @echo Assembling $< ... + @$(NASM) $< -o $@ + +all: $(TARGET) + +ncurses: ncurses.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -lncursesw -o $@ $< + +numerierung: numerierung.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +xdemo: xdemo.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -lX11 -o $@ $< + +signals: signals.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +tree: tree.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +utf8: utf8.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +file_demo: file_demo.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +testcase: testcase.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +atoi_print: atoi_print.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +data_types: data_types.c + @echo Linking $< ... + @$(CC) $(CFLAGS) -std=c99 -o $@ $< + +dnsresolve: dnsresolve.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +nomalloc: nomalloc.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +urandom: urandom.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +threads: threads.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< -lpthread + +crypt: crypt.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +tokenpasting: tokenpasting.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +hex2chars: hex2chars.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< -lm + +floating: floating.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< -lm + +max: max.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +recording: recording.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +endian: endian.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +fak: fak.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +blackhole: blackhole.c + @echo Compiling AND Linking $< WITH -O0 ... + @$(CC) $(CFLAGS) -O0 -o $@ $< + +folge: folge.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< -lm + +counter: counter.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +sudoku: sudoku.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< + +cunit: cunit.o + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $< -lcunit + +md5: md5.o + @echo Linking $< ... + @$(CC) -o $@ $< -llsf + +varargs: varargs.o + @echo Linking $< ... + @$(CC) -o $@ $< + +concatenation: concatenation.o + @echo Linking $< ... + @$(CC) -o $@ $< + +alpha_beta: alpha_beta.o + @echo Linking $< ... + @$(CC) -o $@ $< + +life: life.c + @echo Linking $< ... + @$(CC) -o $@ $< + +.PHONY: clean uninstall + +clean: + rm -f *.o *~ + rm -f $(TARGET) + diff --git a/alpha_beta.c b/alpha_beta.c new file mode 100644 --- /dev/null +++ b/alpha_beta.c @@ -0,0 +1,347 @@ +/* + * $Id: alpha_beta.c,v 1.5 2008-05-08 21:04:08 mbroeker Exp $ + * $Source: /development/c/demos/alpha_beta.c,v $ + */ + +/* TIC TAC TOE With Alpha Beta Search */ + +#include +#include +#include + +#define BOARD_SIZE 9 +#define EMPTY 32 + +struct Move { + int target; + int value; +}; + +typedef struct Move Move; + +struct Node { + Move *data; + struct Node *prev; +}; + +typedef struct Node Node; + +Move *bestMove; +Node *stack_end; + +char board[BOARD_SIZE] = { + EMPTY, EMPTY, EMPTY, + EMPTY, EMPTY, EMPTY, + EMPTY, EMPTY, EMPTY, +}; + +int desired_depth = -1; + +int estimateFunction (); +int max_alpha_beta (int, int, int); +int min_alpha_beta (int, int, int); +void print (void); + +/** + * push: push a move onto the stack + */ +void push (Move * move) +{ + Node *actual; + + if ((actual = malloc (sizeof (Node) + 1)) == NULL) { + perror ("MALLOC"); + exit (EXIT_FAILURE); + } + + actual->data = move; + actual->prev = stack_end; + + stack_end = actual; +} + +/** + * undo: revert the last move on the stack + */ +void undo () +{ + Node *actual; + + if (stack_end == NULL) { + printf ("Stack underrun\n"); + return; + } + + board[stack_end->data->target] = EMPTY; + + actual = stack_end->prev; + free (stack_end); + + stack_end = actual; +} + +/** + * validMove returns true(1) when the inspected field is EMPTY + */ +int validMove (int t) +{ + if (board[t] == EMPTY) + return 1; + return 0; +} + +/** + * Returns a Score of +-50 for 3 in a Row + */ +int estimateFunction () +{ + if (board[0] == 'X' && board[1] == 'X' && board[2] == 'X') + return -50; + if (board[0] == 'O' && board[1] == 'O' && board[2] == 'O') + return 50; + + if (board[3] == 'X' && board[4] == 'X' && board[5] == 'X') + return -50; + if (board[3] == 'O' && board[4] == 'O' && board[5] == 'O') + return 50; + + if (board[6] == 'X' && board[7] == 'X' && board[8] == 'X') + return -50; + if (board[6] == 'O' && board[7] == 'O' && board[8] == 'O') + return 50; + + if (board[0] == 'X' && board[3] == 'X' && board[6] == 'X') + return -50; + if (board[0] == 'O' && board[3] == 'O' && board[6] == 'O') + return 50; + + if (board[1] == 'X' && board[4] == 'X' && board[7] == 'X') + return -50; + if (board[1] == 'O' && board[4] == 'O' && board[7] == 'O') + return 50; + + if (board[2] == 'X' && board[5] == 'X' && board[8] == 'X') + return -50; + if (board[2] == 'O' && board[5] == 'O' && board[8] == 'O') + return 50; + + if (board[0] == 'X' && board[4] == 'X' && board[8] == 'X') + return -50; + if (board[0] == 'O' && board[4] == 'O' && board[8] == 'O') + return 50; + + if (board[2] == 'X' && board[4] == 'X' && board[6] == 'X') + return -50; + if (board[2] == 'O' && board[4] == 'O' && board[6] == 'O') + return 50; + + return 0; +} + +/** + * this function visualizes the board + */ +void print () +{ + int i; + + printf (" 1 2 3 \n +---+---+---+\n"); + for (i = 0; i < 3; i++) { + printf (" %d | ", i + 1); + printf ("%c", board[3 * i]); + printf (" | "); + printf ("%c", board[3 * i + 1]); + printf (" | "); + printf ("%c", board[3 * i + 2]); + printf (" | \n"); + if (i != 2) { + printf (" +---+---+---+\n"); + } else { + printf (" +---+---+---+\n"); + } + } + if (stack_end->data != NULL) + printf ("Score: %3d\n", stack_end->data->value); +} + +/** + * simulates and returns the next possible move for a Player or null, when no more moves are possible. + */ +Move *simulate (int t, int player) +{ + Move *move; + + while (t < BOARD_SIZE) { + if (!validMove (t)) { + t++; + continue; + } + + if ((move = malloc (sizeof (Move) + 1)) == NULL) { + perror ("MALLOC"); + exit (EXIT_FAILURE); + } + + if (player == 0) + board[t] = 'O'; + else + board[t] = 'X'; + + move->target = t; + move->value = estimateFunction (); + + push (move); + + return move; + } + return NULL; +} + +/** + * AlphaBeta-Algorithm: the Maximizer + */ +int max_alpha_beta (int depth, int alpha, int beta) +{ + int moveValue; + int t; + Move *move; + + if (desired_depth == -1) + desired_depth = depth; + + t = 0; + while ((move = simulate (t, 0)) != NULL) { + /* + * particular move from black + */ + t = move->target; + + if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) + moveValue = move->value; + else /* best next move */ + moveValue = min_alpha_beta (depth - 1, alpha, beta); + + print (); + undo (); + t++; + + if (moveValue >= beta) + return beta; + + if (moveValue > alpha) { + alpha = moveValue; + if (depth == desired_depth) + bestMove = move; + } + } + + return alpha; +} + +/** + * AlphaBeta-Algorithm: the Minimizer + */ +int min_alpha_beta (int depth, int alpha, int beta) +{ + int moveValue; + int t; + Move *move; + + t = 0; + while ((move = simulate (t, 1)) != NULL) { + /* + * particular move from white + */ + t = move->target; + + if (depth == 0 || (estimateFunction () * estimateFunction ()) == 2500) + moveValue = move->value; + else /* best next move */ + moveValue = max_alpha_beta (depth - 1, alpha, beta); + + undo (); + t++; + + if (moveValue <= alpha) + return alpha; + + if (moveValue < beta) + beta = moveValue; + } + + return beta; +} + +/** + * returns true when no more fields are empty + */ +int game_over () +{ + int i; + + for (i = 0; i < BOARD_SIZE; i++) { + if (board[i] == EMPTY) + return 0; + } + + return 1; +} + +int main (int argc, char **argv) +{ + Node *actual; + int i, t, value; + int depth; + + if (argc == 2) + depth = atoi (argv[1]); + else + depth = 1; + + if ((actual = malloc (sizeof (Node) + 1)) == NULL) { + perror ("MALLOC"); + return EXIT_FAILURE; + } + + actual->data = NULL; + actual->prev = NULL; + + stack_end = actual; + + printf ("\tTic Tac Toe\n"); + + for (i = 0; i < 5; i++) { + printf ("Enter a number: "); + while (scanf ("%d", &t) < 1) { + printf ("Enter a number: "); + t = getchar (); + } + + if (!validMove (--t)) { + i--; + continue; + } + + board[t] = 'X'; + + bestMove = NULL; + value = max_alpha_beta (depth, SHRT_MIN, SHRT_MAX); + + if (bestMove == NULL) { + break; + } + + board[bestMove->target] = 'O'; + print (); + + if ((estimateFunction () * estimateFunction ()) == 2500) { + return EXIT_SUCCESS; + } + + if (game_over ()) + break; + } + + print (); + return EXIT_SUCCESS; +} diff --git a/atoi_print.c b/atoi_print.c new file mode 100644 --- /dev/null +++ b/atoi_print.c @@ -0,0 +1,17 @@ +/** + * $Id: atoi_print.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/atoi_print.c,v $ + * + */ + +#include +#include + +int main (int argc, char **argv) +{ + int i; + + for (i = 1; i < argc; i++) + printf ("atoi(%s):=%d\n", argv[i], atoi (argv[i])); + return 0; +} diff --git a/blackhole.c b/blackhole.c new file mode 100644 --- /dev/null +++ b/blackhole.c @@ -0,0 +1,30 @@ +/* + * $Id: blackhole.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/blackhole.c,v $ + */ + +/* compile with -O0 or without optimizations or it won't crash */ + +#include +#include + +unsigned long blackhole (unsigned long i) +{ + /* + * blackhole stops after a datatype overrun + */ + if ((i + 1) < i) + return i; + + if (!(i % 10000)) + printf ("Recursion %lu\n", i); + + return blackhole (i + 1); +} + +int main (void) +{ + printf ("MAXIMUM RECURSION DEPTH ON YOUR MACHINE: %lu\n", blackhole (0)); + + return EXIT_SUCCESS; +} diff --git a/concatenation.c b/concatenation.c new file mode 100644 --- /dev/null +++ b/concatenation.c @@ -0,0 +1,62 @@ +/* + * $Id: concatenation.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/concatenation.c,v $ + */ + +#include +#include +#include + +struct P { + char *lastname; + char *firstname; + char *street; + int postal; + char *location; +}; + +typedef struct P Person; + +#define insert(a) { person->a = a; } +#define show(a) { printf("%9s:\t%s\n", #a, person.a); } +#define show_int(a) { printf("%9s:\t%d\n", #a, person.a); } + +void print_Person (Person person) +{ + show (lastname); + show (firstname); + show (street); + show_int (postal); + show (location); + printf ("\n"); +} + +void set_Person (Person * person, char *lastname, char *firstname, char *street, int postal, char *location) +{ + /* + * inserts into person-> + */ + insert (lastname); + insert (firstname); + insert (street); + insert (postal); + insert (location); +} + +int main (int argc, char **argv) +{ + Person person[5]; + int i; + + set_Person (&person[0], "Breitkopf", "Manuela", "Maxim-Gorki-Strasse 49", 18106, "Rostock"); + set_Person (&person[1], "Bröker", "Markus", "Maxim-Gorki-Strasse 25", 18106, "Rostock"); + set_Person (&person[2], "Fischer", "Tina", "Ehm-Welk Straße 11", 18106, "Rostock"); + set_Person (&person[3], "Jopp", "Marika", "Bleicherstraße 12", 18155, "Rostock"); + set_Person (&person[4], "Rennert", "Nicole", "Willi-Bredel-Strasse 20", 18106, "Rostock"); + + for (i = 0; i < 5; i++) { + print_Person (person[i]); + } + + return EXIT_SUCCESS; +} diff --git a/counter.c b/counter.c new file mode 100644 --- /dev/null +++ b/counter.c @@ -0,0 +1,36 @@ +/* + * $Id: counter.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/counter.c,v $ + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + time_t t; + struct tm *time_str; + int start, end; + + if (argc != 2) { + printf ("Usage: %s \"\"\n", argv[0]); + return EXIT_FAILURE; + } + + time (&t); + time_str = gmtime (&t); + + start = time_str->tm_hour * 60 * 60 + time_str->tm_min * 60 + time_str->tm_sec; + + system (argv[1]); + + time (&t); + time_str = gmtime (&t); + + end = time_str->tm_hour * 60 * 60 + time_str->tm_min * 60 + time_str->tm_sec; + + printf ("Command Execution Time: %8ds\n", end - start); + + return EXIT_SUCCESS; +} diff --git a/crypt.c b/crypt.c new file mode 100644 --- /dev/null +++ b/crypt.c @@ -0,0 +1,37 @@ +/** + * $Id: crypt.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/crypt.c,v $ + * + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + char *data; + int i; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + return EXIT_SUCCESS; + } + + /* + * Its already allocated + */ + data = argv[1]; + + for (i = 0; data[i]; i++) + data[i] = data[i] + 1; + + printf ("EnCrypted Data: %s\n", data); + + for (i = 0; data[i]; i++) + data[i] = data[i] - 1; + + printf ("DeCrypted Data: %s\n", data); + + return 0; +} diff --git a/cunit.c b/cunit.c new file mode 100644 --- /dev/null +++ b/cunit.c @@ -0,0 +1,135 @@ +/* + * Simple example of a CUnit unit test. + * + * This program (crudely) demonstrates a very simple "black box" + * test of the standard library functions fprintf() and fread(). + * It uses suite initialization and cleanup functions to open + * and close a common temporary file used by the test functions. + * The test functions then write to and read from the temporary + * file in the course of testing the library functions. + * + * The 2 test functions are added to a single CUnit suite, and + * then run using the CUnit Basic interface. The output of the + * program (on CUnit version 2.0-2) is: + * + * CUnit : A Unit testing framework for C. + * http://cunit.sourceforge.net/ + * + * Suite: Suite_1 + * Test: test of fprintf() ... passed + * Test: test of fread() ... passed + * + * --Run Summary: Type Total Ran Passed Failed + * suites 1 1 n/a 0 + * tests 2 2 2 0 + * asserts 5 5 5 0 + */ + +#include +#include +#include "CUnit/Basic.h" + +/* Pointer to the file used by the tests. */ +static FILE *temp_file = NULL; + +/* The suite initialization function. + * Opens the temporary file used by the tests. + * Returns zero on success, non-zero otherwise. + */ +int init_suite1 (void) +{ + if (NULL == (temp_file = fopen ("temp.txt", "w+"))) { + return -1; + } else { + return 0; + } +} + +/* The suite cleanup function. + * Closes the temporary file used by the tests. + * Returns zero on success, non-zero otherwise. + */ +int clean_suite1 (void) +{ + if (0 != fclose (temp_file)) { + return -1; + } else { + temp_file = NULL; + return 0; + } +} + +/* Simple test of fprintf(). + * Writes test data to the temporary file and checks + * whether the expected number of bytes were written. + */ +void testFPRINTF (void) +{ + int i1 = 10; + + if (NULL != temp_file) { + CU_ASSERT (0 == fprintf (temp_file, "")); + CU_ASSERT (2 == fprintf (temp_file, "Q\n")); + CU_ASSERT (7 == fprintf (temp_file, "i1 = %d", i1)); + } +} + +/* Simple test of fread(). + * Reads the data previously written by testFPRINTF() + * and checks whether the expected characters are present. + * Must be run after testFPRINTF(). + */ +void testFREAD (void) +{ + char buffer[20]; + + if (NULL != temp_file) { + rewind (temp_file); + CU_ASSERT (9 == fread (buffer, sizeof (unsigned char), 20, temp_file)); + CU_ASSERT (0 == strncmp (buffer, "Q\ni1 = 10", 9)); + } +} + +/* The main() function for setting up and running the tests. + * Returns a CUE_SUCCESS on successful running, another + * CUnit error code on failure. + */ +int main () +{ + CU_pSuite pSuite = NULL; + + /* + * initialize the CUnit test registry + */ + if (CUE_SUCCESS != CU_initialize_registry ()) + return CU_get_error (); + + /* + * add a suite to the registry + */ + pSuite = CU_add_suite ("Suite_1", init_suite1, clean_suite1); + if (NULL == pSuite) { + CU_cleanup_registry (); + return CU_get_error (); + } + + /* + * add the tests to the suite + */ + /* + * NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() + */ + if ((NULL == CU_add_test (pSuite, "test of fprintf()", testFPRINTF)) || + (NULL == CU_add_test (pSuite, "test of fread()", testFREAD))) { + CU_cleanup_registry (); + return CU_get_error (); + } + + /* + * Run all tests using the CUnit Basic interface + */ + CU_basic_set_mode (CU_BRM_VERBOSE); + CU_basic_run_tests (); + CU_cleanup_registry (); + return CU_get_error (); +} diff --git a/data_types.c b/data_types.c new file mode 100644 --- /dev/null +++ b/data_types.c @@ -0,0 +1,61 @@ +/** + * $Id: data_types.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/data_types.c,v $ + * + */ + +#include +#include +#include +#include +#include + +typedef unsigned char UCHAR; +typedef signed char SCHAR; + +typedef unsigned short USHORT; +typedef signed short SSHORT; + +typedef unsigned int UINT; +typedef signed int SINT; + +typedef unsigned long ULONG; +typedef signed long SLONG; + +#ifdef __USE_ISOC99 +typedef unsigned long long ULLONG; +typedef signed long long SLLONG; +#endif + +int main (int argc, char **argv) +{ + printf ("%20s %4s %20s %25s\n\n", "Type", "BITS", "MIN", "MAX"); + + printf ("%20s %4d %20d %25d\n", " signed char", 8 * sizeof (SCHAR), CHAR_MIN, CHAR_MAX); + printf ("%20s %4d %20d %25d\n", " signed short", 8 * sizeof (SSHORT), SHRT_MIN, SHRT_MAX); + printf ("%20s %4d %20d %25d\n", " signed int", 8 * sizeof (SINT), INT_MIN, INT_MAX); + printf ("%20s %4d %20ld %25ld\n", " signed long", 8 * sizeof (SLONG), LONG_MIN, LONG_MAX); + +#ifdef __USE_ISOC99 + printf ("%20s %4d %20lld %25lld\n\n", " signed llong", 8 * sizeof (SLLONG), (SLLONG) LLONG_MIN, LLONG_MAX); +#endif + + printf ("%20s %4d %20d %25u\n", " unsigned char", 8 * sizeof (UCHAR), 0, UCHAR_MAX); + printf ("%20s %4d %20d %25u\n", "unsigned short", 8 * sizeof (USHORT), 0, USHRT_MAX); + + printf ("%20s %4d %20d %25u\n", " unsigned int", 8 * sizeof (UINT), 0, UINT_MAX); + printf ("%20s %4d %20lu %25lu\n", " unsigned long", 8 * sizeof (ULONG), 0UL, ULONG_MAX); + +#ifdef __USE_ISOC99 + printf ("%20s %4d %20llu %25llu\n", "unsigned llong", 8 * sizeof (ULLONG), 0ULL, ULLONG_MAX); +#endif + +#ifdef __USE_ISOC99 + printf ("\n%20s %4d %20d %20d\n", "_Bool", 8 * sizeof (_Bool), 0, 1); +#endif + + printf ("\n%20s %4d \t\t %#1.2g \t\t %#1.2g\n", "float", 8 * sizeof (float), (double)FLT_MIN, (double)FLT_MAX); + printf ("%20s %4d \t\t %#1.2g \t\t %#1.2g\n", "double", 8 * sizeof (double), (double)DBL_MIN, (double)DBL_MAX); + + return 0; +} diff --git a/ddos/CVS/Entries b/ddos/CVS/Entries new file mode 100644 --- /dev/null +++ b/ddos/CVS/Entries @@ -0,0 +1,6 @@ +/Makefile/1.1.1.1/Mon Apr 28 17:32:53 2008// +/client.c/1.1.1.1/Mon Apr 28 17:32:53 2008// +/ddos.sh/1.1.1.1/Mon Apr 28 17:32:53 2008// +/server.c/1.1.1.1/Mon Apr 28 17:32:53 2008// +/set_limit.c/1.1.1.1/Mon Apr 28 17:32:53 2008// +D diff --git a/ddos/CVS/Repository b/ddos/CVS/Repository new file mode 100644 --- /dev/null +++ b/ddos/CVS/Repository @@ -0,0 +1,1 @@ +c/demos/ddos diff --git a/ddos/CVS/Root b/ddos/CVS/Root new file mode 100644 --- /dev/null +++ b/ddos/CVS/Root @@ -0,0 +1,1 @@ +:pserver:mbroeker@localhost:/development diff --git a/ddos/Makefile b/ddos/Makefile new file mode 100644 --- /dev/null +++ b/ddos/Makefile @@ -0,0 +1,28 @@ +CC=gcc -g -ggdb +CFLAGS=-Wall -O2 -Iinclude -ansi +NASM=nasm -f elf -Iinclude/ +TARGET=client server +SOBJECTS=server.o set_limit.o +COBJECTS=client.o + +.SUFFIXES: .c .asm + +.c.o: + @echo Compiling $< ... + @$(CC) -c $(CFLAGS) -o $@ $< + +all: $(TARGET) + +server: $(SOBJECTS) + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $(SOBJECTS) + +client: $(COBJECTS) + @echo Linking $< ... + @$(CC) $(CFLAGS) -o $@ $(COBJECTS) + +.PHONY: clean uninstall + +clean: + rm -f *.o *~ + rm -f $(TARGET) diff --git a/ddos/client.c b/ddos/client.c new file mode 100644 --- /dev/null +++ b/ddos/client.c @@ -0,0 +1,52 @@ +/** + * $Id: client.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/ddos/client.c,v $ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int main (int argc, char **argv) +{ + char message[81]; + int client_socket; + struct sockaddr_in ca; + socklen_t size; + int status; + + client_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (client_socket == -1) { + perror ("socket"); + return EXIT_FAILURE; + } + + ca.sin_family = AF_INET; + ca.sin_port = htons (4000); + ca.sin_addr.s_addr = inet_addr ("127.0.0.1"); + + size = sizeof (ca); + + status = connect (client_socket, (struct sockaddr *)&ca, size); + status = write (client_socket, "Hello\r\n", 8); + while (client_socket > 0) { + status = read (client_socket, message, 80); + if (status > 0) { + message[status] = 0; + printf ("SERVER RESPONS: %s", message); + } else { + perror ("READ"); + close (client_socket); + return 1; + } + } + + close (client_socket); + return 0; +} diff --git a/ddos/ddos.sh b/ddos/ddos.sh new file mode 100755 --- /dev/null +++ b/ddos/ddos.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +for i in {1..1000}; +do + ./client & + if [ $? == 0 ]; then + printf "Starting process\n"; + fi +done; + diff --git a/ddos/server.c b/ddos/server.c new file mode 100644 --- /dev/null +++ b/ddos/server.c @@ -0,0 +1,106 @@ +/** + * $Id: server.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/ddos/server.c,v $ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int set_limit (int); + +int main (int argc, char **argv) +{ + char message[81]; + int server_socket; + int client_socket; + struct sockaddr_in sa; + struct sockaddr_in ca; + socklen_t size; + int len; + int status; + pid_t pid; + pid_t parent_pid; + + server_socket = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (server_socket == -1) { + perror ("socket"); + return EXIT_FAILURE; + } + + sa.sin_family = AF_INET; + sa.sin_port = htons (4000); + sa.sin_addr.s_addr = INADDR_ANY; + + size = sizeof (sa); + + status = bind (server_socket, (struct sockaddr *)&sa, size); + if (status != 0) { + perror ("BIND"); + return EXIT_FAILURE; + } + + status = listen (server_socket, 10); + if (status != 0) { + perror ("LISTEN"); + return EXIT_FAILURE; + } + + int links = 0; + + parent_pid = getpid (); + + /* + * Child quits immediately, father mustn't wait + */ + signal (SIGCHLD, SIG_IGN); + + if (set_limit (500) != 0) { + printf ("Cannot limit the process limit\n"); + return EXIT_FAILURE; + } + + for (;;) { + size = sizeof (ca); + client_socket = accept (server_socket, (struct sockaddr *)&sa, (socklen_t *) & size); + + printf ("PARENT PID = %d\n", parent_pid); + + pid = fork (); + switch (pid) { + case 0: /* Child */ + close (server_socket); + if ((len = read (client_socket, message, 80)) == -1) + break; + message[len] = 0; + + len = write (client_socket, "Please wait...\r\n", 17); + sleep (10); + + printf ("DELETING LINK [%04d] %04d\n", getpid (), links--); + shutdown (client_socket, SHUT_RDWR); + close (client_socket); + return 1; + case -1: + perror ("Fork Error"); + close (client_socket); + /* + * server is still alive and will continue + * * when resources are available + */ + break; + default: /* PID > 0 */ + close (client_socket); + printf ("Starting LINK [%04d] %04d\n", pid, links++); + } + } + close (server_socket); + return 0; +} diff --git a/ddos/set_limit.c b/ddos/set_limit.c new file mode 100644 --- /dev/null +++ b/ddos/set_limit.c @@ -0,0 +1,15 @@ +/** + * $Id: set_limit.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/ddos/set_limit.c,v $ + */ + +#include + +int set_limit (int processes) +{ + struct rlimit rlim; + + rlim.rlim_cur = processes; + rlim.rlim_max = processes + 24; + return setrlimit (RLIMIT_NPROC, &rlim); +} diff --git a/dnsresolve.c b/dnsresolve.c new file mode 100644 --- /dev/null +++ b/dnsresolve.c @@ -0,0 +1,37 @@ +/** + * $Id: dnsresolve.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/dnsresolve.c,v $ + * + */ + +#include +#include +#include +#include +#include + +int main (int argc, char **argv) +{ + struct hostent *he; + char *ip; + + int i = 0; + + if (argc != 2) { + fprintf (stderr, "Usage: %s hostname\n", argv[0]); + exit (0); + } + + he = gethostbyname (argv[1]); + ip = NULL; + + if (he != NULL) { + while (he->h_addr_list[i] != NULL) { + ip = inet_ntoa (*((struct in_addr *)he->h_addr_list[i])); + printf ("%s: %s\n", argv[1], ip); + i++; + } + } + + return 0; +} diff --git a/endian.c b/endian.c new file mode 100644 --- /dev/null +++ b/endian.c @@ -0,0 +1,21 @@ +/* + * $Id: endian.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/endian.c,v $ + */ + +#include +#include + +int endian () +{ + unsigned short word = 0x1234; + unsigned char *p = (unsigned char *)&word; + + return ((p[0] == 0x34) ? 0 : 1); +} + +int main (int argc, char **argv) +{ + printf ("%s Endian System\n", (endian ())? "Big" : "Little"); + return EXIT_SUCCESS; +} diff --git a/fak.c b/fak.c new file mode 100644 --- /dev/null +++ b/fak.c @@ -0,0 +1,32 @@ +/* + * $Id: fak.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/fak.c,v $ + */ + +#include +#include + +long fak (int i) +{ + if (i == 0) + return 1; + else if (i > 0) + return (i * fak (i - 1)); + else + return (i * fak (i + 1)); +} + +int main (int argc, char **argv) +{ + int number; + + printf ("Enter a number: "); + if (scanf ("%d", &number) < 0) { + printf ("READ ERROR\n"); + return EXIT_FAILURE; + } + + printf ("The faktorial of %d is %ld\n", number, fak (number)); + + return EXIT_SUCCESS; +} diff --git a/file_demo.c b/file_demo.c new file mode 100644 --- /dev/null +++ b/file_demo.c @@ -0,0 +1,45 @@ +/** + * $Id: file_demo.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/file_demo.c,v $ + * + */ + +#include +#include +#include +#include +#include +#include + +int main (int argc, char **argv) +{ + int fd1; + int fd2; + int mode; + char *str = "Schreib mal was!"; + + if (argc != 2) + return -1; + mode = strtoul (argv[1], NULL, 8); + + system ("/bin/rm -f /home/mbroeker/Desktop/test1.txt"); + system ("/bin/rm -f /home/mbroeker/Desktop/test2.txt"); + + /* + * Opens ReadOnly + */ + fd1 = open ("/home/mbroeker/Desktop/test1.txt", O_CREAT, mode); + fd2 = open ("/home/mbroeker/Desktop/test2.txt", O_RDWR | O_CREAT, mode); + + if (fd1) + if (write (fd1, str, strlen (str)) == -1) + perror ("FD1"); + + if (fd2) + if (write (fd2, str, strlen (str) + 1) == -1) + perror ("FD2"); + + close (fd1); + close (fd2); + return 0; +} diff --git a/floating.c b/floating.c new file mode 100644 --- /dev/null +++ b/floating.c @@ -0,0 +1,38 @@ +/** + * $Id: floating.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/floating.c,v $ + * + */ + +#include +#include +#include + +double *getValues (int elements) +{ + double *values; + int i; + + if ((values = calloc (elements + 1, sizeof (double))) == NULL) + return NULL; + for (i = elements; i >= 0; i--) + values[i] = cos (i); + return values; +} + +#define MAX 40 + +int main (int argc, char **argv) +{ + double *values = getValues (MAX); + int i; + + for (i = 0; i < MAX; i++) { + if (fabs (cos (i) - values[i]) < 0.001) + printf ("COS(%d) = %3.2g = %3.2g\n", i, values[i], cos (i)); + } + + if (values != NULL) + free (values); + return EXIT_SUCCESS; +} diff --git a/folge.c b/folge.c new file mode 100644 --- /dev/null +++ b/folge.c @@ -0,0 +1,23 @@ +/* + * $Id: folge.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/folge.c,v $ + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + int N; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + for (N = 1; N <= atoi (argv[1]) + 1; N++) + printf ("[2^%d]: %.1f\n", N - 1, pow (2, N - 1)); + + return EXIT_SUCCESS; +} diff --git a/hex2chars.c b/hex2chars.c new file mode 100644 --- /dev/null +++ b/hex2chars.c @@ -0,0 +1,139 @@ +/** + * $Id: hex2chars.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/hex2chars.c,v $ + * + */ + +#include +#include +#include +#include + +#ifndef RUNS +#define RUNS 16 +#endif + +/* + * =708== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 13 from 1) + * ==708== malloc/free: in use at exit: 352 bytes in 32 blocks. + * ==708== malloc/free: 48 allocs, 16 frees, 3,680 bytes allocated. + * ==708== For counts of detected errors, rerun with: -v + * ==708== searching for pointers to 32 not-freed blocks. + * ==708== checked 68,684 bytes. + */ + +const char *characters = "xuzicjklmneopbdaqfrstvgwhy"; + +char *remap_works (char *str, int slen) +{ + int i; + int pos; + static char *s; + + if ((s = malloc (slen + 1)) == NULL) + return NULL; + + for (i = 0; i < slen; i++) { + if (str[i] >= 'a') { + pos = str[i] - 'a'; + s[i] = characters[pos]; + } else { + /* + * Let numbers asis + */ + s[i] = str[i]; + } + } + + s[i] = 0; + return s; +} + +char *remap_works_not (char *str, int slen) +{ + int i, j; + double value; + double *table; + static char *s; + + int clen = strlen (characters); + + if ((s = malloc (slen + 1)) == NULL) + return NULL; + + if ((table = calloc (clen, sizeof (double))) == NULL) + return NULL; + + for (i = 0; i < clen; i++) + table[i] = cos ('a' + i); + + for (i = 0; i < slen; i++) { + if (str[i] >= 'a') { + value = cos (str[i]); + for (j = 0; j < clen; j++) { + /* + * flip and expand characters + */ + s[i] = '*'; + if (value == table[j]) { + s[i] = characters[j]; + break; + } + + if ((fabs (value - table[j])) < 0.001) { + s[i] = characters[j]; + break; + } + } + } else { + /* + * Let numbers asis + */ + s[i] = str[i]; + } + } + + s[i] = 0; + + if (table != NULL) + free (table); + + return s; +} + +int main (int argc, char **argv) +{ + int i; + int len; + char *start; + char *end; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + exit (0); + } + + start = argv[1]; + len = strlen (start); + + printf ("\n --- REMAP_WORKS FINE ---\n\n"); + for (i = 0; i < RUNS; i++) { + if ((end = remap_works (start, len)) != NULL) + printf ("%s\t<==>\t%s\n", start, end); + start = end; + } + + /* + * reset state + */ + start = argv[1]; + + printf ("\n --- REMAP_WORKS_NOT because floating-point comparison is evil at all ---\n\n"); + for (i = 0; i < RUNS; i++) { + if ((end = remap_works_not (start, len)) != NULL) + printf ("%s\t<==>\t%s\n", start, end); + start = end; + } + + return 0; +} diff --git a/life.c b/life.c new file mode 100644 --- /dev/null +++ b/life.c @@ -0,0 +1,118 @@ +/** + * $Id$ + * $Source$ + * + */ + +#include +#include + +#define MAX 12 + +int read_file (char **); +int init_life (char **); +void free_life (char **); +void show_life (char **); + +int read_file (char **arena) +{ + int i, j; + char org; + + char file[80]; + FILE *inp = NULL; + + while (inp == NULL) { + printf ("Please enter the file name>> "); + if (scanf ("%s", file) < 1) + return -1; + + inp = fopen (file, "r"); + } + + for (i = 0; i < MAX; i++) { + printf ("Reading Line %3d: ", i); + for (j = 0; j < MAX; j++) { + if (feof (inp)) { + printf ("EOF detected\n"); + return fclose (inp); + } + + if ( (fscanf (inp, "%c ", &org)) < 1 ) + return -1; + + arena[i][j] = org; + printf ("%c ", arena[i][j]); + } + printf ("\n"); + } + + return fclose (inp); +} + +int init_life (char **arena) +{ + int i, j; + + for (i = 0; i < MAX; i++) { + if ((arena[i] = malloc (MAX + 1)) == NULL) + return -1; + for (j=0;j 2) + printf("Usage: %s\n", argv[0]); + + if (init_life (arena) == 0) + show_life (arena); + free_life (arena); + + return 0; +} diff --git a/max.c b/max.c new file mode 100644 --- /dev/null +++ b/max.c @@ -0,0 +1,29 @@ +/** + * $Id: max.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/max.c,v $ + * + */ +#include +#include + +unsigned int max (unsigned int a, unsigned int b) +{ + return (a * (a > b) + b * (b > a)); +} + +int main (int argc, char **argv) +{ + unsigned int a; + unsigned int b; + + if (argc != 3) { + printf ("Usage: %s value1 value2\n", argv[0]); + return EXIT_SUCCESS; + } + a = atoi (argv[1]); + b = atoi (argv[2]); + + printf ("%u\n", max (a, b)); + + return EXIT_SUCCESS; +} diff --git a/md5.c b/md5.c new file mode 100644 --- /dev/null +++ b/md5.c @@ -0,0 +1,20 @@ +/* + * $Id: md5.c,v 1.2 2008-04-29 07:26:06 mbroeker Exp $ + * $Source: /development/c/demos/md5.c,v $ + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + printf ("Report bugs to mbroeker@largo.homelinux.org\n"); + return EXIT_FAILURE; + } + + md5recursive (argv[1], 1); + return EXIT_SUCCESS; +} diff --git a/ncurses.c b/ncurses.c new file mode 100644 --- /dev/null +++ b/ncurses.c @@ -0,0 +1,91 @@ +/** + * $Id: ncurses.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/ncurses.c,v $ + * + */ + +/* + * Unicode != UTF-8 + * unicode with ncursesw ( wide char) + * gcc $< -lncursesw -o $@ + * + */ + +#include +#include +#include +#include +#include +#include +#include + +#ifndef u_char +typedef unsigned char u_char; +#endif + +#define ESCAPE 0x1B + +#ifndef get_wch +extern int get_wch (); +#endif + +int check_locale () +{ + if (!setlocale (LC_CTYPE, "")) { + perror ("SETLOCALE"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +wchar_t fucnt (WINDOW * win) +{ + wchar_t guess; + char dest[4]; + int maxx; + int maxy; + + while (guess != ESCAPE) { + while ((get_wch (&guess)) != OK) + /* + * waiting for multibyte character + */ + ; + + maxx = getmaxx (win); + maxy = getmaxy (win); + + dest[0] = 0; + dest[1] = 0; + dest[2] = 0; + dest[3] = 0; + if (wctomb (dest, guess) == -1) { + perror ("WCTOMB"); + return -1; + } + + mvprintw (maxy / 2, maxx / 2 - 12, + "UTF-8: (%2X:%2X:%2X:%2X)\t\t", (u_char) dest[0], + (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]); + + mvprintw (maxy - 1, 0, "KEY: %2lc\t", guess); + mvprintw (maxy - 1, maxx - 14, "UNICODE: %4X", guess); + mvprintw (maxy - 1, maxx / 2 - 10, "Press ESC to quit"); + } + return guess; +} + +int main () +{ + wchar_t x; + WINDOW *win; + + check_locale (); + win = initscr (); + noecho (); + x = fucnt (win); + endwin (); + + printf ("\n"); + return 0; +} diff --git a/nomalloc.c b/nomalloc.c new file mode 100644 --- /dev/null +++ b/nomalloc.c @@ -0,0 +1,34 @@ +/** + * $Id: nomalloc.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/nomalloc.c,v $ + * + */ + +#include + +struct Names { + char *name; + int age; +}; + +void setStruct (struct Names *names) +{ + names->name = "mbroeker"; + names->age = 32; +} + +void printStruct (struct Names *names) +{ + printf ("Name: %s\n", names->name); + printf (" Age: %d\n", (*names).age); +} + +int main (int argc, char **argv) +{ + struct Names names; + + setStruct (&names); + printStruct (&names); + + return 0; +} diff --git a/numerierung.c b/numerierung.c new file mode 100644 --- /dev/null +++ b/numerierung.c @@ -0,0 +1,62 @@ +/** + * $Id: numerierung.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/numerierung.c,v $ + * + */ + +#include +#include +#include +#include + +void usage (char *cmd) +{ + printf ("Usage: %s [-h] [-t] [-v] [file]\n", cmd); + exit (0); +} + +int main (int argc, char **argv) +{ + char buffer[81]; + int counter = 0; + FILE *f; + int c, pure = 0; + + while ((c = getopt (argc, argv, "hp")) != -1) { + printf ("Zeichen: %c\n", c); + switch (c) { + case 'h': + usage (argv[0]); + break; + case 'p': + pure = 1; + break; + default: + printf ("Unknown Parameter: %s\n", argv[optind]); + } + printf ("GETOPT: %d : %s\n", optind, argv[optind]); + } + + if (optind == argc) + f = stdin; + else + f = fopen (argv[optind], "r"); + + if (!f) { + perror ("Error opening file"); + return EXIT_FAILURE; + } + + *buffer = 0; + + while (fgets (buffer, 80, f) != NULL) { + if (!pure) + printf ("%4.4d: %s", counter++, buffer); + else + printf ("%s", buffer); + *buffer = 0; + } + + fclose (f); + return EXIT_SUCCESS; +} diff --git a/recording.c b/recording.c new file mode 100644 --- /dev/null +++ b/recording.c @@ -0,0 +1,90 @@ +/* + * $Id: recording.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/recording.c,v $ + * + * Program to illustrate /dev/dsp device + * Records several seconds of sound, then echoes it back. + * Runs until Control-C is pressed. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define LENGTH 3 /* how many seconds of speech to store */ +#define RATE 8000 /* the sampling rate */ +#define SIZE 8 /* sample size: 8 or 16 bits */ +#define CHANNELS 1 /* 1 = mono 2 = stereo */ + + /* + * this buffer holds the digitized audio + */ +unsigned char buf[LENGTH * RATE * SIZE * CHANNELS / 8]; + +int main (int argc, char **argv) +{ + int fd; /* sound device file descriptor */ + int arg; /* argument for ioctl calls */ + int status; /* return status of system calls */ + + /* + * open sound device + */ + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + printf ("Example: %s /dev/dsp\n", argv[0]); + printf ("Example: %s /dev/dsp1\n", argv[0]); + return EXIT_SUCCESS; + } + + fd = open (argv[1], O_RDWR); + if (fd < 0) { + perror ("open of sound device failed"); + return EXIT_FAILURE; + } + + /* + * set sampling parameters + */ + arg = SIZE; /* sample size */ + status = ioctl (fd, SOUND_PCM_WRITE_BITS, &arg); + if (status == -1) + perror ("SOUND_PCM_WRITE_BITS ioctl failed"); + if (arg != SIZE) + perror ("unable to set sample size"); + + arg = CHANNELS; /* mono or stereo */ + status = ioctl (fd, SOUND_PCM_WRITE_CHANNELS, &arg); + if (status == -1) + perror ("SOUND_PCM_WRITE_CHANNELS ioctl failed"); + if (arg != CHANNELS) + perror ("unable to set number of channels"); + + arg = RATE; /* sampling rate */ + status = ioctl (fd, SOUND_PCM_WRITE_RATE, &arg); + if (status == -1) + perror ("SOUND_PCM_WRITE_WRITE ioctl failed"); + + while (1) { /* loop until Control-C */ + printf ("Say something:\n"); + status = read (fd, buf, sizeof (buf)); /* record some sound */ + if (status != sizeof (buf)) + perror ("read wrong number of bytes"); + printf ("You said:\n"); + status = write (fd, buf, sizeof (buf)); /* play it back */ + if (status != sizeof (buf)) + perror ("wrote wrong number of bytes"); + /* + * wait for playback to complete before recording again + */ + status = ioctl (fd, SOUND_PCM_SYNC, 0); + if (status == -1) + perror ("SOUND_PCM_SYNC ioctl failed"); + } + return EXIT_SUCCESS; +} diff --git a/signals.c b/signals.c new file mode 100644 --- /dev/null +++ b/signals.c @@ -0,0 +1,43 @@ +/** + * $Id: signals.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/signals.c,v $ + * + */ + +#ifndef _XOPEN_SOURCE +#define _XOPEN_SOURCE 500 +#endif + +#include +#include +#include + +void sigproc () +{ + signal (SIGINT, sigproc); + printf ("I received the SIGINT Signal\n"); +} + +void sigquit () +{ + printf ("I received the SIGQUIT Signal\n"); + /* + * This kills the process and subprocesses + */ + kill (-1, SIGKILL); + /* + * ^^ can not be reached + */ + exit (0); +} + +int main (int argc, char **argv) +{ + signal (SIGINT, sigproc); + signal (SIGQUIT, sigquit); + + printf ("PRESS CTRL-\\ to quit\n"); + for (;;); + + return EXIT_SUCCESS; +} diff --git a/sudoku.c b/sudoku.c new file mode 100644 --- /dev/null +++ b/sudoku.c @@ -0,0 +1,246 @@ +/* + * $Id: sudoku.c,v 1.3 2008-04-29 15:18:53 mbroeker Exp $ + * $Source: /development/c/demos/sudoku.c,v $ + * + */ + +/* Sudoku - The Game */ + +#include +#include +#include +#include +#include +#include + +#ifndef DIM +#define DIM 9 +#endif + +#define COLORED "\e[31m" +#define FGCOLOR "\e[37m" +#define BGCOLOR "\e[40m\e[2J" +#define RESET "\e[0m" + +#define TRUE 1 +#define FALSE 0 + +#ifndef u_int +typedef unsigned int u_int; +#endif + +#ifndef MAX_ITERATIONS +#define MAX_ITERATIONS 10000000 +#endif + +struct Field { + u_int value; + int isConst; +}; + +/* the easiest way to get statistics */ +static unsigned long iterations = 0; + +typedef struct Field Field; +typedef Field **Board; + +/** + * read nxn fields from commandline and store it in the Board variable + */ +int readBoard (Board board) +{ + int i, j; + + u_int c; + + for (i = 0; i < DIM; i++) { + for (j = 0; j < DIM; j++) { + if (scanf ("%u", &c) < 1) { + fprintf (stderr, "readBoard: Not enough elements\n"); + fprintf (stderr, "readBoard: Got %d, needed %d\n", i * DIM + j, (DIM) * (DIM)); + return FALSE; + } + board[i][j].value = c; + board[i][j].isConst = (c == 0) ? 0 : 1; + } + } + return TRUE; +} + +/** + * Show the content of the Board in a human readable manner + */ +void showBoard (Board board, int colored) +{ + int i, j; + + for (i = 0; i < DIM; i++) { + if ((i % 3) == 0) + printf ("\n"); + + for (j = 0; j < DIM; j++) { + if ((board[i][j].isConst && colored)) + printf ("%s%2u%s ", COLORED, board[i][j].value, FGCOLOR); + else + printf ("%2u ", board[i][j].value); + if (((j + 1) % 3) == 0) + printf (" "); + } + printf ("\n"); + } +} + +/** + * checks for the possibility of value at board[row][col] + */ +int check (Board board, int row, int col, u_int value) +{ + int i, j; + + if (board[row][col].value > 0) + return FALSE; + + for (i = 0; i < DIM; i++) { + /* + * check vertically + */ + if (board[row][i].value == value) + return FALSE; + /* + * check horizontally + */ + if (board[i][col].value == value) + return FALSE; + } + + /* + * check block for unique values + */ + for (i = row - (row % 3); i < row - (row % 3) + 3; i++) + for (j = col - (col % 3); j < col - (col % 3) + 3; j++) + if (board[i][j].value == value) + return FALSE; + + return TRUE; +} + +/** + * go recursive from k to DIM^2 and try to solve the puzzle + */ +int solveBoard (Board board, int k) +{ + u_int i; + int q; + + if ((++iterations > MAX_ITERATIONS)) + return FALSE; + + while (k < (DIM) * (DIM) && (board[k / DIM][k % DIM].value != 0)) + k++; + + if (k == (DIM) * (DIM)) + return TRUE; + + for (q = FALSE, i = 1; q == FALSE && i <= 9; i++) { + if (check (board, k / DIM, k % DIM, i) == TRUE) { + board[k / DIM][k % DIM].value = i; + if (((DIM) * (DIM) - 1) == k) + q = TRUE; + else if ((q = solveBoard (board, k + 1)) == FALSE) { + board[k / DIM][k % DIM].value = 0; + } + } + } + + return q; +} + +/** + * Deletes every Field of the Board (CLEANUP) + */ +void freeBoard (const Board board) +{ + int i; + + for (i = 0; i < DIM; i++) { + if (board[i] != NULL) + free (board[i]); + } +} + +/** + * Show plain usage screen + */ +void usage (char *name) +{ + printf ("Usage: %s [-c] [-h]\n\n", name); + printf ("-c\t\tcolored output\n"); + printf ("-h\t\tshows this help\n"); + printf ("Report bugs to mbroeker@largo.homelinux.org\n"); +} + +int main (int argc, char **argv) +{ + Board b; + int i; + int ret; + int colored = 0; + + while ((i = getopt (argc, argv, "ch")) != -1) { + switch (i) { + case 'h': + usage (argv[0]); + return EXIT_SUCCESS; + case 'c': + printf ("%s%s", BGCOLOR, FGCOLOR); + colored = 1; + break; + default: + usage (argv[0]); + return EXIT_FAILURE; + } + } + + if ((DIM % 3) != 0) { + fprintf (stderr, "DIM must be a multiple of 3\n"); + fprintf (stderr, "RECOMPILE WITH -DDIM=9\n"); + return EXIT_FAILURE; + } + + if ((b = calloc (DIM, sizeof (Field *))) == NULL) { + perror ("calloc"); + return EXIT_FAILURE; + } + + for (i = 0; i < DIM; i++) { + if ((b[i] = malloc ((DIM * sizeof (Field)) + 1)) == NULL) { + perror ("malloc"); + return EXIT_FAILURE; + } + } + + printf ("Sudoku - Enter a %dx%d Matrix\n", DIM, DIM); + + if (readBoard (b) == FALSE) { + fprintf (stderr, "Input Error: Aborting\n"); + return EXIT_FAILURE; + } + + showBoard (b, colored); + + if ((ret = solveBoard (b, 0)) == TRUE) + printf ("\nPuzzle solved after %lu recursions\n", iterations); + else + printf ("\nCannot solve your puzzle after %lu recursions\n", iterations); + + showBoard (b, colored); + freeBoard (b); + + if (b != NULL) + free (b); + + if (colored) + printf ("%s", RESET); + + return EXIT_SUCCESS; +} diff --git a/testcase.c b/testcase.c new file mode 100644 --- /dev/null +++ b/testcase.c @@ -0,0 +1,55 @@ +/** + * $Id: testcase.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/testcase.c,v $ + * + */ + +#define _XOPEN_SOURCE 500 + +#include +#include +#include + +int main (int argc, char **argv) +{ + char line[83]; + char *token; + int i; + + if (argc != 4) { + printf ("Usage: %s (char*)string (char*)delim1 (char*)delim2\n", argv[0]); + return 0; + } + + snprintf (line, 80, "%s\r\n", argv[1]); + + token = strtok (line, argv[2]); + + while (token) { + printf ("TOKEN: %s\n", token); + token = strtok (NULL, argv[3]); + } + + i = 0; + printf ("while i++ <5: "); + while (i++ < 5) + printf ("%d ", i); + + i = 0; + printf ("\nwhile ++i <5: "); + while (++i < 5) + printf ("%d ", i); + + i = 0; + printf ("\nwhile i <5 : "); + while (i < 5) + printf ("%d ", i++); + + i = 0; + printf ("\nwhile i <5 : "); + while (i < 5) + printf ("%d ", ++i); + + printf ("\nTest finished\n"); + return 0; +} diff --git a/threads.c b/threads.c new file mode 100644 --- /dev/null +++ b/threads.c @@ -0,0 +1,49 @@ +/** + * $Id: threads.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/threads.c,v $ + */ + +#include +#include + +#include +#include + +void *thread_func (void *vptr_args); + +void do_some_work (void) +{ + time_t start_time = time (NULL); + + while (time (NULL) == start_time); /* busy-wait for 0-1 seconds */ +} + +int main (int argc, char **argv) +{ + int i; + pthread_t thread; + + pthread_create (&thread, NULL, &thread_func, NULL); + + for (i = 0; i < 20; ++i) { + fprintf (stdout, "a\n"); + + do_some_work (); + } + + pthread_join (thread, NULL); + + exit (EXIT_SUCCESS); +} + +void *thread_func (void *vptr_args) +{ + int i; + + for (i = 0; i < 20; ++i) { + fprintf (stderr, " b\n"); + + do_some_work (); + } + return NULL; +} diff --git a/tokenpasting.c b/tokenpasting.c new file mode 100644 --- /dev/null +++ b/tokenpasting.c @@ -0,0 +1,28 @@ +/** + * $Id: tokenpasting.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/tokenpasting.c,v $ + * + * Description: Demonstration of the secret token concatenation mechanism + */ + +#include +#include + +#define a(a,b) { tsb.fd##a = b; } + +struct { + int fd1; + int fd2; + int fd3; +} tsb; + +int main (int argc, char **argv) +{ + a (1, 10); + a (2, 20); + a (3, 30); + + printf ("1=[%d] 2=[%d] 3=[%d]\n", tsb.fd1, tsb.fd2, tsb.fd3); + + return EXIT_SUCCESS; +} diff --git a/tree.c b/tree.c new file mode 100644 --- /dev/null +++ b/tree.c @@ -0,0 +1,69 @@ +/** + * $Id: tree.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/tree.c,v $ + * + */ + +#include +#include +#include + +#define GETRANDOM(max) (1+(int)((float)max*rand()/RAND_MAX+1.0)) + +struct T { + int data; + struct T *next; +}; + +typedef struct T T; + +T *make_list (int elements, int rand_max) +{ + int i; + T *t; + T *actual; + T *first = NULL; + + srand (time (NULL)); + + if ((t = malloc (sizeof (T) + 1)) == NULL) { + perror ("MALLOC"); + return first; + } + + t->data = GETRANDOM (rand_max); + t->next = NULL; + + first = t; + + for (i = 1; i < elements; i++) { + if ((actual = malloc (sizeof (T) + 1)) == NULL) + break; + actual->data = GETRANDOM (rand_max); + actual->next = NULL; + t->next = actual; + t = actual; + } + return first; +} + +int main (int argc, char **argv) +{ + T *t, *actual; + + if (argc != 3) { + printf ("Usage: %s elements rand_max\n", argv[0]); + return EXIT_SUCCESS; + } + + t = make_list (atoi (argv[1]), atoi (argv[2])); + + while (t) { + printf ("%d\n", t->data); + actual = t->next; + free (t); + t = actual; + }; + + return EXIT_SUCCESS; +} diff --git a/urandom.c b/urandom.c new file mode 100644 --- /dev/null +++ b/urandom.c @@ -0,0 +1,41 @@ +/** + * $Id: urandom.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/urandom.c,v $ + * + */ + +#include +#include +#include +#include + +int compare (unsigned char *a, unsigned char *b) +{ + if (*a > *b) + return 1; + return -1; +} + +int main (int argc, char **argv) +{ + int i; + int elements; + int fd; + unsigned char numbers[256] = { 0 }; + + fd = open ("/dev/urandom", O_RDONLY); + if (fd == -1) + return EXIT_FAILURE; + + elements = read (fd, numbers, 255); + numbers[elements] = 0; + + qsort (numbers, elements, sizeof (unsigned char), (void *)&compare); + + for (i = 0; i < elements; i++) { + printf ("Number: %d\n", numbers[i]); + } + + close (fd); + return EXIT_SUCCESS; +} diff --git a/utf8.c b/utf8.c new file mode 100644 --- /dev/null +++ b/utf8.c @@ -0,0 +1,44 @@ +/** + * $Id: utf8.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/utf8.c,v $ + * + */ + +#include +#include +#include +#include + +#ifndef u_char +typedef unsigned char u_char; +#endif + +int main () +{ + char dest[4]; + + wchar_t *str = L"Unicode Example with german umlauts: ÄÖÜäöü߀\n"; + + if (!setlocale (LC_CTYPE, "")) { + perror ("SETLOCALE"); + return EXIT_FAILURE; + } + + printf ("%ls", str); + + while (*str != '\n') { + dest[0] = 0; + dest[1] = 0; + dest[2] = 0; + dest[3] = 0; + if (wctomb (dest, (*str)) == -1) + return -1; + + printf ("%lc -> [%4X] (%2X:%2X:%2X:%2X)\n", + *str, *str, (u_char) dest[0], (u_char) dest[1], (u_char) dest[2], (u_char) dest[3]); + str++; + } + + printf ("DIFF: %d : 0x%2X\n", 0x40, 0x40); + return 0; +} diff --git a/varargs.c b/varargs.c new file mode 100644 --- /dev/null +++ b/varargs.c @@ -0,0 +1,57 @@ +/* + * $Id: varargs.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/varargs.c,v $ + * + */ + +#include +#include +#include + +int params (char *fmt, ...) +{ + int i = 0; + va_list list; + char c; + char *s; + int value; + + va_start (list, fmt); + + while (*list) { + switch (*fmt++) { + case 'c': + c = va_arg (list, int); + + printf (" Char: %c\n", c); + i++; + break; + case 's': + s = va_arg (list, char *); + + printf ("String: %s\n", s); + i++; + break; + case 'd': + value = va_arg (list, int); + + printf (" Value: %d\n", value); + i++; + break; + default: + printf ("Unknown format character\n"); + } + } + + va_end (list); + + return i; +} + +int main (int argc, char **argv) +{ + + printf ("Number of Parameter: %d\n", params ("Xcssssd", 'I', "made", "this", "application", "in", 2008, NULL)); + + return EXIT_SUCCESS; +} diff --git a/xdemo.c b/xdemo.c new file mode 100644 --- /dev/null +++ b/xdemo.c @@ -0,0 +1,53 @@ +/** + * $Id: xdemo.c,v 1.1.1.1 2008-04-28 17:32:53 mbroeker Exp $ + * $Source: /development/c/demos/xdemo.c,v $ + * + */ + +#include +#include +#include + +int main (int argc, char **argv) +{ + Window w = 0; + Display *dpy = XOpenDisplay (NULL); + XEvent xev; + int active; + + if (dpy == NULL) { + printf ("Error opening localhost:0\n"); + return EXIT_SUCCESS; + } + + XSynchronize (dpy, 1); + w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy), 0, 0, 640, 480, 1, 1, 1); + + if (w < 0) + return EXIT_SUCCESS; + + printf ("WINDOW-ID: %ld\n", w); + + XRaiseWindow (dpy, w); + XMapWindow (dpy, w); + + printf ("Press any key to quit\n"); + + active = 1; + XSelectInput (dpy, w, KeyPressMask); + + while (active) { + XNextEvent (dpy, &xev); + switch (xev.type) { + case KeyPress: + active = 0; + break; + default: + printf ("Unknown Event: %d\n", xev.type); + } + } + + XDestroyWindow (dpy, w); + XCloseDisplay (dpy); + return 0; +}