Browse Source

all: indent -kr -i4

Douglas Andreani 5 years ago
parent
commit
2b2ae5eafb

+ 70 - 71
libpng/img.c

@@ -3,86 +3,85 @@
 
 image initialize_png(char *title, char *filename, int width, int height)
 {
-	image ret;
-	FILE *fp = NULL;
-
-	fp = fopen(filename, "wb");
-	if (fp == NULL) {
-		fprintf(stderr, "could not create %s", filename);
-		exit(EXIT_FAILURE);
-	}
-
-	png_structp png_ptr =
-	    png_create_write_struct(PNG_LIBPNG_VER_STRING,
-				    NULL,
-				    NULL,
-				    NULL);
-
-	if (png_ptr == NULL) {
-		fprintf(stderr, "Could not create write struct. libpng\n");
-	}
-
-
-	png_infop info_ptr = png_create_info_struct(png_ptr);
-	if (info_ptr == NULL) {
-		fprintf(stderr, "Could not create info struct. libpng\n");
-	}
-
-	if (setjmp(png_jmpbuf(png_ptr))) {
-		fprintf(stderr, "Error during png creation\n");
-	}
-
-	png_init_io(png_ptr, fp);
-
-	//Write header
-	png_set_IHDR(png_ptr,
-		     info_ptr,
-		     width,
-		     height,
-		     8,
-		     PNG_COLOR_TYPE_RGB,
-		     PNG_INTERLACE_NONE,
-		     PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
-	if (title != NULL) {
-		png_text title_text;
-		title_text.compression = PNG_TEXT_COMPRESSION_NONE;
-		title_text.key = "Title";
-		title_text.text = title;
-		png_set_text(png_ptr, info_ptr, &title_text, 1);
-	}
-
-	png_write_info(png_ptr, info_ptr);
-
-	ret.png_ptr = png_ptr;
-	ret.info_ptr = info_ptr;
-	ret.width = width;
-	ret.height = height;
-	ret.fp = fp;
-	return ret;
+    image ret;
+    FILE *fp = NULL;
+
+    fp = fopen(filename, "wb");
+    if (fp == NULL) {
+	fprintf(stderr, "could not create %s", filename);
+	exit(EXIT_FAILURE);
+    }
+
+    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+						  NULL,
+						  NULL,
+						  NULL);
+
+    if (png_ptr == NULL) {
+	fprintf(stderr, "Could not create write struct. libpng\n");
+    }
+
+
+    png_infop info_ptr = png_create_info_struct(png_ptr);
+    if (info_ptr == NULL) {
+	fprintf(stderr, "Could not create info struct. libpng\n");
+    }
+
+    if (setjmp(png_jmpbuf(png_ptr))) {
+	fprintf(stderr, "Error during png creation\n");
+    }
+
+    png_init_io(png_ptr, fp);
+
+    //Write header
+    png_set_IHDR(png_ptr,
+		 info_ptr,
+		 width,
+		 height,
+		 8,
+		 PNG_COLOR_TYPE_RGB,
+		 PNG_INTERLACE_NONE,
+		 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
+    if (title != NULL) {
+	png_text title_text;
+	title_text.compression = PNG_TEXT_COMPRESSION_NONE;
+	title_text.key = "Title";
+	title_text.text = title;
+	png_set_text(png_ptr, info_ptr, &title_text, 1);
+    }
+
+    png_write_info(png_ptr, info_ptr);
+
+    ret.png_ptr = png_ptr;
+    ret.info_ptr = info_ptr;
+    ret.width = width;
+    ret.height = height;
+    ret.fp = fp;
+    return ret;
 }
 
 void write_image(image * img, pix_row * img_array)
 {
 
-	png_bytep row = NULL;
-	int x = 0, y = 0;
-	row = (png_bytep) malloc(3 * img->width * sizeof(png_byte));
-	for (y = 0; y < img->height; y++) {
-		for (x = 0; x < img->width; x++) {
-			row[x * 3 + 0] = img_array[x].p[y].r;	//red
-			row[x * 3 + 1] = img_array[x].p[y].g;	// green
-			row[x * 3 + 2] = img_array[x].p[y].b;	// blue
-		}
-		png_write_row(img->png_ptr, row);
+    png_bytep row = NULL;
+    int x = 0, y = 0;
+    row = (png_bytep) malloc(3 * img->width * sizeof(png_byte));
+    for (y = 0; y < img->height; y++) {
+	for (x = 0; x < img->width; x++) {
+	    row[x * 3 + 0] = img_array[x].p[y].r;	//red
+	    row[x * 3 + 1] = img_array[x].p[y].g;	// green
+	    row[x * 3 + 2] = img_array[x].p[y].b;	// blue
 	}
-	png_write_end(img->png_ptr, img->info_ptr);
-	free(row);
+	png_write_row(img->png_ptr, row);
+    }
+    png_write_end(img->png_ptr, img->info_ptr);
+    free(row);
 }
 
 void finish_image(image * img)
 {
-	fclose(img->fp);
-	png_free_data(img->png_ptr, img->info_ptr, PNG_FREE_ALL, -1);
-	png_destroy_write_struct(&img->png_ptr, NULL);
+    fclose(img->fp);
+    png_free_data(img->png_ptr, img->info_ptr, PNG_FREE_ALL, -1);
+    png_destroy_write_struct(&img->png_ptr, NULL);
 
 }

+ 14 - 14
libpng/img.h

@@ -5,28 +5,28 @@
 #include <stdio.h>
 
 typedef struct pix {
-	int r;
-	int g; 
-	int b;
-	int a;
-}pix;
+    int r;
+    int g;
+    int b;
+    int a;
+} pix;
 
 typedef struct pix_row {
-	int lenght;
-	pix* p;
-}pix_row;
+    int lenght;
+    pix *p;
+} pix_row;
 
 typedef struct image {
 
-	png_structp png_ptr;
-	png_infop info_ptr;
-	int width;
-	int height;
-	FILE *fp;
+    png_structp png_ptr;
+    png_infop info_ptr;
+    int width;
+    int height;
+    FILE *fp;
 } image;
 
 
-image initialize_png(char* title, char* filename, int width, int height);
+image initialize_png(char *title, char *filename, int width, int height);
 void write_image(image * img, pix_row * img_array);
 void finish_image(image * img);
 

+ 44 - 44
libpng/png.c

@@ -4,53 +4,53 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-int main(int argc, char** argv){
-
-	if (argc != 4)
-	{
-		fprintf(stderr, "\nusage: %s count width height", argv[0]);
-		exit(EXIT_FAILURE);
+int main(int argc, char **argv)
+{
+
+    if (argc != 4) {
+	fprintf(stderr, "\nusage: %s count width height", argv[0]);
+	exit(EXIT_FAILURE);
+    }
+
+    int width, height, c;
+
+    c = atoi(argv[1]);
+    width = atoi(argv[2]);
+    height = atoi(argv[3]);
+
+    pix_row rows[width];
+    pix p;
+    char filename[60];
+    int i, j, x, y, count;
+    image img;
+    p.r = 200;
+    p.g = 200;
+    p.b = 200;
+
+    srand48(1000);
+
+    for (count = 0; count < (c); count++) {
+
+	for (i = 0; i < width; i++) {
+	    rows[i].p = malloc(height * sizeof(pix));
+	    for (j = 0; j < height; j++) {
+		p.r = (int) (drand48() * 255);
+		p.g = (int) (drand48() * 255);
+		p.b = (int) (drand48() * 255);
+		rows[i].p[j] = p;
+	    }
 	}
+	sprintf(filename, "images/image%05d.png", count);
+	img = initialize_png("image010", filename, width, height);
+	write_image(&img, rows);
+	finish_image(&img);
+
+	for (i = 0; i < width; i++) {
 
-	int width, height, c;
-
-	c = atoi(argv[1]);
-	width = atoi(argv[2]);
-	height = atoi(argv[3]);
-
-	pix_row rows[width];
-	pix p;
-	char filename[60];
-	int i,j,x,y,count;
-	image img;
-	p.r = 200;
-	p.g = 200;
-	p.b = 200;
-
-	srand48(1000);
-	
-	for (count=0; count<(c);count++){
-
-		for (i=0;i<width;i++){
-			rows[i].p = malloc (height * sizeof(pix));
-			for (j=0 ;j<height; j++){
-				p.r = (int) (drand48() * 255);
-				p.g = (int) (drand48() * 255);
-				p.b = (int) (drand48() * 255);
-				rows[i].p[j] = p;
-			}
-		}
-		sprintf(filename, "images/image%05d.png", count);
-		img = initialize_png("image010", filename, width, height);
-		write_image(&img, rows);
-		finish_image(&img);
-
-		for (i=0; i<width;i++){
-
-			free(rows[i].p);
-		}
+	    free(rows[i].p);
 	}
+    }
 
-	return (0);
+    return (0);
 
 }

+ 41 - 43
pthread/example.c

@@ -3,57 +3,55 @@
 #include <pthread.h>
 
 struct fibo {
-	int limit;
-	int answer;
+    int limit;
+    int answer;
 };
 
-int 
-fib(int n)
+int fib(int n)
 {
-	int i=0;
-	if (n==1 || n==0) return 1;
-	for(i=0;i<n;i++){
-		return (fib(n-1) + fib(n-2));
-	}
-	return (-1);
+    int i = 0;
+    if (n == 1 || n == 0)
+	return 1;
+    for (i = 0; i < n; i++) {
+	return (fib(n - 1) + fib(n - 2));
+    }
+    return (-1);
 }
 
-void*
-runner1(void* arg)
+void *runner1(void *arg)
 {
-	struct fibo* fibl = (struct fibo*) arg;
-	fibl->answer = fib(fibl->limit);
-	pthread_exit(0);
+    struct fibo *fibl = (struct fibo *) arg;
+    fibl->answer = fib(fibl->limit);
+    pthread_exit(0);
 }
 
-int
-main (int argc, char** argv)
+int main(int argc, char **argv)
 {
-	
-	int i;
-	pthread_t id[argc];
-	pthread_attr_t attr;
-	struct fibo d[argc];
-
-	if (argc < 2) {
-		printf("usage: %s num1 num2 num3... numN", argv[0]);
-		return (1);
-	}
-
-	
-
-	pthread_attr_init(&attr);
-	for (i=1; i<argc;i++){
-		d[i-1].limit = atoi(argv[i]);
-		d[i-1].answer = 0;
-		pthread_create(&id[i-1], &attr, runner1, &d[i-1]);
-	}
-
-	for (i=0;i<argc-1;i++){
-		pthread_join(id[i], NULL);
-		printf("\n\nFib of limit: %d is %d", d[i].limit, d[i].answer);
-	}
-
-	return (0);
+
+    int i;
+    pthread_t id[argc];
+    pthread_attr_t attr;
+    struct fibo d[argc];
+
+    if (argc < 2) {
+	printf("usage: %s num1 num2 num3... numN", argv[0]);
+	return (1);
+    }
+
+
+
+    pthread_attr_init(&attr);
+    for (i = 1; i < argc; i++) {
+	d[i - 1].limit = atoi(argv[i]);
+	d[i - 1].answer = 0;
+	pthread_create(&id[i - 1], &attr, runner1, &d[i - 1]);
+    }
+
+    for (i = 0; i < argc - 1; i++) {
+	pthread_join(id[i], NULL);
+	printf("\n\nFib of limit: %d is %d", d[i].limit, d[i].answer);
+    }
+
+    return (0);
 
 }

+ 2 - 2
pthread/example.h

@@ -3,8 +3,8 @@
 #include <pthread.h>
 
 struct fibo {
-	int limit;
-	int answer;
+    int limit;
+    int answer;
 };
 
 int fib(int n);

+ 35 - 35
pthread/example2.c

@@ -2,59 +2,59 @@
 
 void *produce(void *buf)
 {
-	buffer *buff;
-	buff = (buffer *) buf;
-	printf("\nproducer started");
+    buffer *buff;
+    buff = (buffer *) buf;
+    printf("\nproducer started");
 
-	for (int i = 0; i < 20; i++) {
-		sem_wait(&buff->full);
-		pthread_mutex_lock(&buff->mutex);
+    for (int i = 0; i < 20; i++) {
+	sem_wait(&buff->full);
+	pthread_mutex_lock(&buff->mutex);
 
-		buff->data[buff->pos] = 0;
-		buff->pos = buff->pos + 1;
-		printf("\nproduced");
+	buff->data[buff->pos] = 0;
+	buff->pos = buff->pos + 1;
+	printf("\nproduced");
 
-		sem_post(&buff->empty);
-		pthread_mutex_unlock(&buff->mutex);
+	sem_post(&buff->empty);
+	pthread_mutex_unlock(&buff->mutex);
 
-		sleep(1);
-	}
+	sleep(1);
+    }
 
-	pthread_exit(0);
+    pthread_exit(0);
 }
 
 void *consume(void *buf)
 {
 
-	buffer *buff;
-	buff = (buffer *) buf;
-	printf("\nconsumer started");
-	for (int i = 0; i < 20; i++) {
-		sem_wait(&buff->empty);
-		pthread_mutex_lock(&buff->mutex);
+    buffer *buff;
+    buff = (buffer *) buf;
+    printf("\nconsumer started");
+    for (int i = 0; i < 20; i++) {
+	sem_wait(&buff->empty);
+	pthread_mutex_lock(&buff->mutex);
 
-		buff->pos = buff->pos - 1;
-		printf("\ngot value %d", buff->data[buff->pos]);
+	buff->pos = buff->pos - 1;
+	printf("\ngot value %d", buff->data[buff->pos]);
 
-		printf("\nconsumed");
+	printf("\nconsumed");
 
-		sem_post(&buff->full);
+	sem_post(&buff->full);
 
-		pthread_mutex_unlock(&buff->mutex);
-		sleep(2);
-	}
-	pthread_exit(0);
+	pthread_mutex_unlock(&buff->mutex);
+	sleep(2);
+    }
+    pthread_exit(0);
 }
 
 void init_buffer(buffer * buf)
 {
 
-	sem_init(&buf->empty, 0, 0);
-	sem_init(&buf->full, 0, BUFFER_SIZE);
-	pthread_mutex_init(&buf->mutex, NULL);
-	buf->pos = 0;
+    sem_init(&buf->empty, 0, 0);
+    sem_init(&buf->full, 0, BUFFER_SIZE);
+    pthread_mutex_init(&buf->mutex, NULL);
+    buf->pos = 0;
 
-	for (int i = 0; i < BUFFER_SIZE; i++) {
-		buf->data[i] = -1;
-	}
+    for (int i = 0; i < BUFFER_SIZE; i++) {
+	buf->data[i] = -1;
+    }
 }

+ 5 - 5
pthread/example2.h

@@ -8,11 +8,11 @@
 #define BUFFER_SIZE 10
 
 typedef struct buffer {
-	sem_t empty;
-	sem_t full;
-	pthread_mutex_t mutex;
-	int pos;
-	int data[BUFFER_SIZE];
+    sem_t empty;
+    sem_t full;
+    pthread_mutex_t mutex;
+    int pos;
+    int data[BUFFER_SIZE];
 
 } buffer;
 

+ 33 - 34
pthread/foo.c

@@ -8,34 +8,33 @@
 int example1(int argc, char **argv)
 {
 
-	int i;
-	pthread_t id[argc];
-	pthread_attr_t attr;
-	struct fibo d[argc];
+    int i;
+    pthread_t id[argc];
+    pthread_attr_t attr;
+    struct fibo d[argc];
 
-	if (argc < 2) {
-		printf("usage: %s num1 num2 num3... numN", argv[0]);
-		return (1);
-	}
+    if (argc < 2) {
+	printf("usage: %s num1 num2 num3... numN", argv[0]);
+	return (1);
+    }
 
 
 
-	pthread_attr_init(&attr);
-	for (i = 1; i < argc; i++) {
-		d[i - 1].limit = atoi(argv[i]);
-		d[i - 1].answer = 0;
-		pthread_create(&id[i - 1], &attr, runner1, &d[i - 1]);
-	}
+    pthread_attr_init(&attr);
+    for (i = 1; i < argc; i++) {
+	d[i - 1].limit = atoi(argv[i]);
+	d[i - 1].answer = 0;
+	pthread_create(&id[i - 1], &attr, runner1, &d[i - 1]);
+    }
 
-	for (i = 0; i < argc - 1; i++) {
+    for (i = 0; i < argc - 1; i++) {
 
-		pthread_join(id[i], NULL);
-		printf("\n\nFib of limit: %d is %d", d[i].limit,
-		       d[i].answer);
+	pthread_join(id[i], NULL);
+	printf("\n\nFib of limit: %d is %d", d[i].limit, d[i].answer);
 
-	}
+    }
 
-	return (0);
+    return (0);
 
 }
 
@@ -43,27 +42,27 @@ int example1(int argc, char **argv)
 int example2(int argc, char **argv)
 {
 
-	pthread_t id[2];
-	pthread_attr_t attr;
-	buffer buf;
+    pthread_t id[2];
+    pthread_attr_t attr;
+    buffer buf;
 
-	init_buffer(&buf);
+    init_buffer(&buf);
 
-	pthread_attr_init(&attr);
-	pthread_create(&id[0], &attr, produce, &buf);
-	pthread_create(&id[1], &attr, consume, &buf);
+    pthread_attr_init(&attr);
+    pthread_create(&id[0], &attr, produce, &buf);
+    pthread_create(&id[1], &attr, consume, &buf);
 
-	pthread_join(id[0], NULL);
-	pthread_join(id[1], NULL);
+    pthread_join(id[0], NULL);
+    pthread_join(id[1], NULL);
 
-	sem_destroy(&buf.empty);
-	pthread_mutex_destroy(&buf.mutex);
+    sem_destroy(&buf.empty);
+    pthread_mutex_destroy(&buf.mutex);
 
-	printf("\nProgram finished");
-	return (EXIT_SUCCESS);
+    printf("\nProgram finished");
+    return (EXIT_SUCCESS);
 }
 
 int main(int argc, char **argv)
 {
-	example2(argc, argv);
+    example2(argc, argv);
 }

+ 30 - 30
queue_thread_model/model.c

@@ -7,50 +7,50 @@
 void sum(int argc, char **values, char *buf_return)
 {
 
-	if (argc != 2) {
-		buf_return[0] = 0;
-		return;
-	} else {
-		long int a = strtol(values[0], NULL, 10);
-		long int b = strtol(values[1], NULL, 10);
-		long int sum = a + b;
-		sprintf(buf_return, "%d", sum);
-	}
+    if (argc != 2) {
+	buf_return[0] = 0;
+	return;
+    } else {
+	long int a = strtol(values[0], NULL, 10);
+	long int b = strtol(values[1], NULL, 10);
+	long int sum = a + b;
+	sprintf(buf_return, "%d", sum);
+    }
 }
 
 int main(int argc, char **argv)
 {
-	worker *w, *x;
+    worker *w, *x;
 
 
-	queue *q;
+    queue *q;
 
-	q = initialize_queue();
-	char *buf1, *buf2;
+    q = initialize_queue();
+    char *buf1, *buf2;
 
-	char **params1 = malloc(sizeof(char *) * 2);
-	params1[0] = "1";
-	params1[1] = "2";
+    char **params1 = malloc(sizeof(char *) * 2);
+    params1[0] = "1";
+    params1[1] = "2";
 
-	w = new_worker(sum, 2, params1, 10, buf1);
-	enqueue(q, w);
+    w = new_worker(sum, 2, params1, 10, buf1);
+    enqueue(q, w);
 
-	char **params2 = malloc(sizeof(char *) * 2);
-	params2[0] = "3";
-	params2[1] = "4";
+    char **params2 = malloc(sizeof(char *) * 2);
+    params2[0] = "3";
+    params2[1] = "4";
 
-	w = new_worker(sum, 2, params2, 10, buf2);
-	enqueue(q, w);
+    w = new_worker(sum, 2, params2, 10, buf2);
+    enqueue(q, w);
 
-	x = dequeue(q);
-	execute(x);
+    x = dequeue(q);
+    execute(x);
 
-	printf("\nValue 1: %s", x->buf_return);
+    printf("\nValue 1: %s", x->buf_return);
 
-	x = dequeue(q);
-	execute(x);
-	printf("\nValue 2: %s", x->buf_return);
+    x = dequeue(q);
+    execute(x);
+    printf("\nValue 2: %s", x->buf_return);
 
 
-	return (0);
+    return (0);
 }

+ 34 - 34
queue_thread_model/queue.c

@@ -4,54 +4,54 @@
 
 queue *initialize_queue()
 {
-	queue *q;
+    queue *q;
 
-	q = (queue *) malloc(sizeof(queue *));
+    q = (queue *) malloc(sizeof(queue *));
 
-	return q;
+    return q;
 }
 
 void enqueue(queue * q, worker * w)
 {
-	struct node *aux;
-	if (q->head == NULL) {
-		q->head = (struct node *) malloc(sizeof(struct node *));
-		q->head->next = NULL;
-		q->head->value = w;
-		q->tail = q->head;
-		return;
-	} else {
-		aux = (struct node *) malloc(sizeof(struct node *));
-		aux->next = NULL;
-		aux->value = w;
-		q->tail->next = aux;
-		q->tail = aux;
-	}
+    struct node *aux;
+    if (q->head == NULL) {
+	q->head = (struct node *) malloc(sizeof(struct node *));
+	q->head->next = NULL;
+	q->head->value = w;
+	q->tail = q->head;
+	return;
+    } else {
+	aux = (struct node *) malloc(sizeof(struct node *));
+	aux->next = NULL;
+	aux->value = w;
+	q->tail->next = aux;
+	q->tail = aux;
+    }
 }
 
 worker *dequeue(queue * q)
 {
-	struct node *aux;
-	worker *ret_val;
-
-	ret_val = q->head->value;
-	aux = q->head;
-	q->head = q->head->next;
-	free(aux);
-	return (ret_val);
+    struct node *aux;
+    worker *ret_val;
+
+    ret_val = q->head->value;
+    aux = q->head;
+    q->head = q->head->next;
+    free(aux);
+    return (ret_val);
 }
 
 void destroy_queue(queue * q)
 {
-	struct node *aux, *next;
+    struct node *aux, *next;
 
-	aux = q->head;
+    aux = q->head;
 
-	while (aux != NULL) {
-		next = aux->next;
-		free(aux);
-		if (next != NULL)
-			aux = next;
-	}
-	free(q);
+    while (aux != NULL) {
+	next = aux->next;
+	free(aux);
+	if (next != NULL)
+	    aux = next;
+    }
+    free(q);
 }

+ 4 - 4
queue_thread_model/queue.h

@@ -4,13 +4,13 @@
 #include "worker.h"
 
 struct node {
-	worker *value;
-	struct node *next;
+    worker *value;
+    struct node *next;
 };
 
 typedef struct queue {
-	struct node *head;
-	struct node *tail;
+    struct node *head;
+    struct node *tail;
 } queue;
 
 queue *initialize_queue();

+ 9 - 9
queue_thread_model/worker.c

@@ -4,24 +4,24 @@
 void execute(worker * w)
 {
 
-	w->fn_exec(w->argc, w->args, w->buf_return);
+    w->fn_exec(w->argc, w->args, w->buf_return);
 }
 
 worker *new_worker(exec fn, int argc, char **args, int retc,
 		   char *return_buf)
 {
-	worker *w = malloc(sizeof(worker));
-	w->fn_exec = fn;
-	w->argc = argc;
-	w->args = args;
-	w->buf_return = malloc(sizeof(char) * retc);
+    worker *w = malloc(sizeof(worker));
+    w->fn_exec = fn;
+    w->argc = argc;
+    w->args = args;
+    w->buf_return = malloc(sizeof(char) * retc);
 
 
-	return w;
+    return w;
 }
 
 void clear_worker(worker * w)
 {
-	free(w->buf_return);
-	free(w);
+    free(w->buf_return);
+    free(w);
 }

+ 4 - 4
queue_thread_model/worker.h

@@ -4,10 +4,10 @@
 typedef void (*exec)(int argc, char **values, char *buf_return);
 
 typedef struct worker {
-	exec fn_exec;
-	char **args;
-	int argc;
-	char *buf_return;
+    exec fn_exec;
+    char **args;
+    int argc;
+    char *buf_return;
 } worker;
 
 worker *new_worker(exec fn, int argc, char **args, int retc,

+ 53 - 51
tun/netw.c

@@ -13,38 +13,37 @@
 #define MTU 1500
 
 typedef struct ipv4 {
-    u_int16_t tun_flags; //tun specific 16 bits
-    u_int16_t tun_proto; //tun specific 16 bits
-    u_int8_t version; // 4 bits
-    u_int8_t header_length; // 4 bits
-    u_int8_t type_of_service; // 8 bits
-    u_int16_t total_length; //16 bits
-    u_int16_t identification; // 16 bits
-    u_int8_t flags; // 3 bits
+    u_int16_t tun_flags;	//tun specific 16 bits
+    u_int16_t tun_proto;	//tun specific 16 bits
+    u_int8_t version;		// 4 bits
+    u_int8_t header_length;	// 4 bits
+    u_int8_t type_of_service;	// 8 bits
+    u_int16_t total_length;	//16 bits
+    u_int16_t identification;	// 16 bits
+    u_int8_t flags;		// 3 bits
 
 } ipv4_t;
 
-int create_tun(char *dev_name, int flags){
+int create_tun(char *dev_name, int flags)
+{
     struct ifreq ifr;
     int fd, err;
     char *clonedev = "/dev/net/tun";
 
-    if ((fd = open(clonedev, O_RDWR)) < 0)
-    {
-        return fd;
+    if ((fd = open(clonedev, O_RDWR)) < 0) {
+	return fd;
     }
 
     memset(&ifr, 0, sizeof(ifr));
     ifr.ifr_ifru.ifru_flags = flags;
 
-    if(*dev_name) {
-        strncpy(ifr.ifr_ifrn.ifrn_name, dev_name, IFNAMSIZ);
+    if (*dev_name) {
+	strncpy(ifr.ifr_ifrn.ifrn_name, dev_name, IFNAMSIZ);
     }
 
-    if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0)
-    {
-        close(fd);
-        return err;
+    if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
+	close(fd);
+	return err;
     }
 
     strcpy(dev_name, ifr.ifr_ifrn.ifrn_name);
@@ -52,47 +51,50 @@ int create_tun(char *dev_name, int flags){
     return fd;
 }
 
-void print_packet(u_int8_t* buf, int buf_size) {
+void print_packet(u_int8_t * buf, int buf_size)
+{
     int i;
 
     printf("received %d bytes: ", buf_size);
 
-    for (i = 0; i < buf_size; i++)
-    {
-        if (i > 0) printf(":");
-        printf("%02X", buf[i]);
+    for (i = 0; i < buf_size; i++) {
+	if (i > 0)
+	    printf(":");
+	printf("%02X", buf[i]);
     }
     printf("\n");
 }
 
-void parse_packet(u_int8_t *buf, int buf_size) {
-    
-  
+void parse_packet(u_int8_t * buf, int buf_size)
+{
+
+
     //struct ethhdr * ethernet_header;
-    struct iphdr * ip_header;
-    struct udphdr * udp_header;
-    ip_header = (struct iphdr*)(buf);
-    
-    if (ip_header->protocol == IPPROTO_UDP)
-    {
-        printf("UDP packet found\n");
-        printf("TTL: %d \n",ip_header->ttl);
-        printf("Dest IP address: %s\n", inet_ntoa( *(struct in_addr*)&ip_header->daddr));
-        printf("Source IP address: %s\n", inet_ntoa( *(struct in_addr*)&ip_header->saddr));
-
-        udp_header =    (struct udphdr *) (buf + ip_header->ihl*4);
-        printf("Source Port: %d\n", ntohs(udp_header->source));
-        printf("Dest Port: %d\n", ntohs(udp_header->dest));
-        if (ntohs(udp_header->dest) == 53) 
-        {
-            printf("Found DNS packet\n");
-        }
+    struct iphdr *ip_header;
+    struct udphdr *udp_header;
+    ip_header = (struct iphdr *) (buf);
+
+    if (ip_header->protocol == IPPROTO_UDP) {
+	printf("UDP packet found\n");
+	printf("TTL: %d \n", ip_header->ttl);
+	printf("Dest IP address: %s\n",
+	       inet_ntoa(*(struct in_addr *) &ip_header->daddr));
+	printf("Source IP address: %s\n",
+	       inet_ntoa(*(struct in_addr *) &ip_header->saddr));
+
+	udp_header = (struct udphdr *) (buf + ip_header->ihl * 4);
+	printf("Source Port: %d\n", ntohs(udp_header->source));
+	printf("Dest Port: %d\n", ntohs(udp_header->dest));
+	if (ntohs(udp_header->dest) == 53) {
+	    printf("Found DNS packet\n");
+	}
     }
 
-    return;// NULL;
+    return;			// NULL;
 }
 
-int main (int argc, char** argv) {
+int main(int argc, char **argv)
+{
     char tun_name[IFNAMSIZ];
     u_int8_t buf[MTU];
     int ret;
@@ -101,14 +103,14 @@ int main (int argc, char** argv) {
     sprintf(tun_name, "tun01");
     int tunfd = create_tun(tun_name, (IFF_TUN | IFF_NO_PI));
     if (tunfd < 0) {
-        fprintf(stderr, "could not allocate device tun.\n");
-        return(EXIT_FAILURE);
+	fprintf(stderr, "could not allocate device tun.\n");
+	return (EXIT_FAILURE);
     }
 
     for (;;) {
-        ret = read(tunfd, buf, MTU);
-        parse_packet(buf, ret);
+	ret = read(tunfd, buf, MTU);
+	parse_packet(buf, ret);
     }
-   
+
     return (0);
 }

+ 103 - 102
x11-random/worley_noise.c

@@ -13,147 +13,148 @@
 static int seed_offset = 1;
 
 struct point {
-	int x;
-	int y;
+    int x;
+    int y;
 };
 
 
 double dist_p1_p2(int p1x, int p1y, int p2x, int p2y)
 {
-	double pA, pB;
-	pA = p1x - p2x;
-	pB = p1y - p2y;
+    double pA, pB;
+    pA = p1x - p2x;
+    pB = p1y - p2y;
 
-	return sqrt(pA * pA + pB * pB);
+    return sqrt(pA * pA + pB * pB);
 
 }
 
 void draw_pixel(Display * di, Window wi, GC gc, int x, int y, int color)
 {
-	XSetForeground(di, gc, color);
-	XDrawPoint(di, wi, gc, x, y);
+    XSetForeground(di, gc, color);
+    XDrawPoint(di, wi, gc, x, y);
 }
 
 float rand_0_1()
 {
-	return drand48();
+    return drand48();
 }
 
 void sort(double *arr)
 {
-	int arr_size = POINTS;
-	int i, j, aux;
-	for (i = 0; i < arr_size; i++) {
-		for (j = i + 1; j < arr_size; j++) {
-			if (arr[i] > arr[j]) {
-				aux = arr[j];
-				arr[j] = arr[i];
-				arr[i] = aux;
-			}
-		}
+    int arr_size = POINTS;
+    int i, j, aux;
+    for (i = 0; i < arr_size; i++) {
+	for (j = i + 1; j < arr_size; j++) {
+	    if (arr[i] > arr[j]) {
+		aux = arr[j];
+		arr[j] = arr[i];
+		arr[i] = aux;
+	    }
 	}
+    }
 
 }
 
 int map(int value, int min, int max, int map_min, int map_max)
 {
-	double R = (double) (map_max - map_min) / (double) (max - min);
-	double y = map_min + (value * R) + R;
-	return (int) y;
+    double R = (double) (map_max - map_min) / (double) (max - min);
+    double y = map_min + (value * R) + R;
+    return (int) y;
 }
 
 void worley_noise(Display * di, Window wi, GC gc)
 {
-	int x, y, i;
-	int c;
-	int noise_space[WIDTH][HEIGHT];
-	double dist[POINTS], sdist[POINTS];
-	struct point p[POINTS];
-
-
-	for (i = 0; i < POINTS; i++) {
-		p[i].x = (int) (rand_0_1() * WIDTH);
-		p[i].y = (int) (rand_0_1() * HEIGHT);
-	}
-
-	for (x = 0; x < WIDTH; x++) {
-		for (y = 0; y < HEIGHT; y++) {
-			for (i = 0; i < POINTS; i++) {
-				dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
-				sort(dist);
-			}
-			int d1 = map(dist[0], 0, WIDTH/2, 200, 0);
-			int d2 = map(dist[1], 0, WIDTH/2, 200, 0);
-			int d3 = map(dist[2], 0, WIDTH/2, 200, 0);
-			
-			/* 0x000000 rgb value where 
-			 * 16 r
-			 *  8 g
-			 *  4 b*/
-			c = (d1 << 16) + (d2 << 8) + d3;
-			noise_space[x][y] = c;
-		}
+    int x, y, i;
+    int c;
+    int noise_space[WIDTH][HEIGHT];
+    double dist[POINTS], sdist[POINTS];
+    struct point p[POINTS];
+
+
+    for (i = 0; i < POINTS; i++) {
+	p[i].x = (int) (rand_0_1() * WIDTH);
+	p[i].y = (int) (rand_0_1() * HEIGHT);
+    }
+
+    for (x = 0; x < WIDTH; x++) {
+	for (y = 0; y < HEIGHT; y++) {
+	    for (i = 0; i < POINTS; i++) {
+		dist[i] = dist_p1_p2(x, y, p[i].x, p[i].y);
+		sort(dist);
+	    }
+	    int d1 = map(dist[0], 0, WIDTH / 2, 200, 0);
+	    int d2 = map(dist[1], 0, WIDTH / 2, 200, 0);
+	    int d3 = map(dist[2], 0, WIDTH / 2, 200, 0);
+
+	    /* 0x000000 rgb value where 
+	     * 16 r
+	     *  8 g
+	     *  4 b*/
+	    c = (d1 << 16) + (d2 << 8) + d3;
+	    noise_space[x][y] = c;
 	}
+    }
 
-	for (x = 0; x < WIDTH; x++) {
-		for (y = 0; y < HEIGHT; y++) {
-			draw_pixel(di, wi, gc, x, y, noise_space[x][y]);
-		}
+    for (x = 0; x < WIDTH; x++) {
+	for (y = 0; y < HEIGHT; y++) {
+	    draw_pixel(di, wi, gc, x, y, noise_space[x][y]);
 	}
+    }
 }
 
-void redraw(Display * di, Window wi, GC gc) {
-	
-	XClearWindow(di, wi);
-	worley_noise(di, wi, gc);
-	XFlush(di);
+void redraw(Display * di, Window wi, GC gc)
+{
+
+    XClearWindow(di, wi);
+    worley_noise(di, wi, gc);
+    XFlush(di);
 }
 
 int main()
 {
-	Display *di = XOpenDisplay(getenv("DISPLAY"));
-	if (di == NULL) {
-		fprintf(stderr, "ERROR: No display");
-		exit(EXIT_FAILURE);
-	}
-	int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
-	int sc = DefaultScreen(di);
-	Window ro = DefaultRootWindow(di);
-	Window wi = XCreateSimpleWindow(di,
-					ro,
-					x,
-					y,
-					width,
-					height,
-					border_width,
-					BlackPixel(di, sc),
-					WhitePixel(di, sc));
-
-	XMapWindow(di, wi);
-	XStoreName(di, wi, "Default Window");
-
-	GC gc = XCreateGC(di, ro, 0, NULL);
-
-	XSelectInput(di, wi, KeyPressMask | ExposureMask);
-	XEvent ev;
-	int quit = 0;
-	while (!quit) {
-		if (XPending(di) > 0) {
-			int a = XNextEvent(di, &ev);
-			if (ev.type == KeyPress) {
-				printf("KeyPress %x\n", ev.xkey.keycode);
-				if (ev.xkey.keycode == 0x18)
-					quit = 1;
-			}
-			if (ev.type == Expose) {
-				redraw(di, wi, gc);
-			}
-		}
+    Display *di = XOpenDisplay(getenv("DISPLAY"));
+    if (di == NULL) {
+	fprintf(stderr, "ERROR: No display");
+	exit(EXIT_FAILURE);
+    }
+    int x = 0, y = 0, width = WIDTH, height = HEIGHT, border_width = 1;
+    int sc = DefaultScreen(di);
+    Window ro = DefaultRootWindow(di);
+    Window wi = XCreateSimpleWindow(di,
+				    ro,
+				    x,
+				    y,
+				    width,
+				    height,
+				    border_width,
+				    BlackPixel(di, sc),
+				    WhitePixel(di, sc));
+
+    XMapWindow(di, wi);
+    XStoreName(di, wi, "Default Window");
+
+    GC gc = XCreateGC(di, ro, 0, NULL);
+
+    XSelectInput(di, wi, KeyPressMask | ExposureMask);
+    XEvent ev;
+    int quit = 0;
+    while (!quit) {
+	if (XPending(di) > 0) {
+	    int a = XNextEvent(di, &ev);
+	    if (ev.type == KeyPress) {
+		printf("KeyPress %x\n", ev.xkey.keycode);
+		if (ev.xkey.keycode == 0x18)
+		    quit = 1;
+	    }
+	    if (ev.type == Expose) {
 		redraw(di, wi, gc);
+	    }
 	}
+	redraw(di, wi, gc);
+    }
 
-	XFreeGC(di, gc);
-	XDestroyWindow(di, wi);
-	XCloseDisplay(di);
-	return (EXIT_SUCCESS);
+    XFreeGC(di, gc);
+    XDestroyWindow(di, wi);
+    XCloseDisplay(di);
+    return (EXIT_SUCCESS);
 }