diff options
| author | EliteTK <tomasz.kramkowski@gmail.com> | 2014-07-10 22:35:35 +0100 | 
|---|---|---|
| committer | EliteTK <tomasz.kramkowski@gmail.com> | 2014-07-10 22:35:35 +0100 | 
| commit | a8609ccd901b1942e862c14205026d841e640add (patch) | |
| tree | 5eaf1d04d454bc45acfdf4c698b2860b91f48a00 | |
| parent | 922fe2f68c39a765896d274356c7c9dc4fb9cd73 (diff) | |
| download | c-stuff-a8609ccd901b1942e862c14205026d841e640add.tar.gz c-stuff-a8609ccd901b1942e862c14205026d841e640add.tar.xz c-stuff-a8609ccd901b1942e862c14205026d841e640add.zip  | |
More stuff.
| -rw-r--r-- | ack.c | 21 | ||||
| -rw-r--r-- | bftext.c | 257 | ||||
| -rw-r--r-- | crazy-swap.c | 17 | ||||
| -rw-r--r-- | fgen.c | 30 | ||||
| -rw-r--r-- | glfw.c | 242 | ||||
| -rw-r--r-- | glut-time.c | 310 | ||||
| -rw-r--r-- | hertz.c | 88 | ||||
| -rw-r--r-- | hostnames.c | 27 | ||||
| -rw-r--r-- | imlib2.c | 230 | ||||
| -rw-r--r-- | intfloatprint.c | 7 | ||||
| -rw-r--r-- | lighthouse3dglex.c | 221 | ||||
| -rw-r--r-- | luaing.c | 25 | ||||
| -rw-r--r-- | mousecurses.c | 107 | ||||
| -rw-r--r-- | ncurses.c | 32 | ||||
| -rw-r--r-- | ncursor.c | 30 | ||||
| -rw-r--r-- | overcomplicated_loop.c | 27 | ||||
| -rw-r--r-- | rewrite.c | 8 | ||||
| -rw-r--r-- | sockets.c | 90 | ||||
| -rw-r--r-- | swap.c | 17 | ||||
| -rw-r--r-- | timer.c | 58 | ||||
| -rw-r--r-- | vectors-testing.c | 47 | ||||
| -rw-r--r-- | xcb.c | 30 | 
22 files changed, 1876 insertions, 45 deletions
@@ -0,0 +1,21 @@ +#include <stdio.h> + +int ack(m, n) +int m, n; +{ +        int ans; +        if (m == 0) ans = n + 1; +        else if (n == 0) ans = ack(m - 1, 1); +        else ans = ack(m - 1, ack(m, n - 1)); +        return (ans); +} + +int main(argc, argv) +int argc; char **argv; +{ +        int i, j; +        for (j = 0; i <= 5; i++) +                for (j = 0; j <= 5; j++) +                        printf("ackerman(%d, %d) is: %d\n", i, j, ack(i, j)); +        return 0; +} diff --git a/bftext.c b/bftext.c new file mode 100644 index 0000000..c572d23 --- /dev/null +++ b/bftext.c @@ -0,0 +1,257 @@ +// Generated with TK's "fuck" brainfuck to C parser. +#include<stdio.h> +#include<stdlib.h> + +int main() { +    char *ptrt; +    ptrt = calloc(1000, sizeof(char)); +    char *ptr; +    ptr = ptrt; +    // The following is directly parsed +    --*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    putchar(*ptr); +    --*ptr; +    while (*ptr) { +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    while (*ptr) { +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    while (*ptr) { +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    --*ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    --*ptr; +    while (*ptr) { +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    putchar(*ptr); +    --*ptr; +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    --*ptr; +    putchar(*ptr); +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    while (*ptr) { +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        --*ptr; +        ++ptr; +        ++*ptr; +        ++*ptr; +        --ptr; +    } +    ++ptr; +    ++*ptr; +    ++*ptr; +    putchar(*ptr); +    // Closing cleanup +    free(ptrt); +    putchar('\n'); +    return 0; +} diff --git a/crazy-swap.c b/crazy-swap.c new file mode 100644 index 0000000..e06136a --- /dev/null +++ b/crazy-swap.c @@ -0,0 +1,17 @@ +static inline void swap(int *a, int *b) +{ +        *a=*a^*b; +        *b=*b^*a; +        *a=*a^*b; +} + +int main(int argc, char **argv) +{ +        int i; +        for (i = 0; i < 999999999; i++) { +                int a = 2; +                int b = 123; +                swap(&a, &b); +        } +        return 0; +} @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <stdlib.h> +#include <math.h> + +int main(int argc, char **argv) +{ +        if (argc != 4) { +                fprintf(stderr, "Usage: %s <sampling-frequency> <frequency> <duration> [amplitude]\n", argv[0]); +                exit(1); +        } + +        long int sfreq = strtol(argv[1], NULL, 10); +        long double freq = strtod(argv[2], NULL); +        if (freq <= 0) freq = 1; +        long double duration = strtod(argv[3], NULL); +        long double amplitude = 127; + +        if (argc == 5) { +                amplitude = strtol(argv[4], NULL, 10); +                if (amplitude > 127) amplitude = 127; +                else if (amplitude < 0) amplitude = 0; +        } + +        long double wave_length = sfreq / freq; + +        for (long int i = 0; i < duration / 1000 * sfreq; i++) +                putchar((char)((sinf(i / wave_length * M_PI_2 * 4) + 1) * amplitude)); + +        return 0; +} @@ -0,0 +1,242 @@ +#define GLFW_INCLUDE_GLU +#include <GLFW/glfw3.h> +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include <stdbool.h> + +#define WIDTH 640 +#define HEIGHT 480 + +#define FOV 70 + +#define MOUSE_SPEED 2 + +#define BOOST_MUL 2 + +#define DEG_RAD(d) ((float)(d) * M_PI / 180) + +static float yaw = 0, pitch = 0; +static float x = 0, y = 0, z = 0; +static int forward = 0, up = 0, right = 0; +static bool boost = false; +static bool mouse_grabbed = false; + +void error_callback(int, const char*); + +void mouse_button_callback(GLFWwindow *, int, int, int); +void key_callback(GLFWwindow *, int, int, int, int); +void mouse_resolve(GLFWwindow *); + +void framebuffer_size_callback(GLFWwindow *, int, int); + +void display(void); +double get_delta(); + +int main(int argc, char **argv) +{ +    GLFWwindow *window; + +    if (!glfwInit()) +        return 1; + +    if (!(window = glfwCreateWindow(640, 480, "Test.", NULL, NULL))) { +        glfwTerminate(); +        return 1; +    } + +    glfwMakeContextCurrent(window); + +    glfwSetErrorCallback(error_callback); + +    glEnable(GL_DEPTH_TEST); + +    glfwSetMouseButtonCallback(window, mouse_button_callback); +    glfwSetKeyCallback(window, key_callback); + +    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + +    while (!glfwWindowShouldClose(window)) { +        glfwPollEvents(); + +        if (mouse_grabbed) +            mouse_resolve(window); + +        display(); + +        glfwSwapBuffers(window); +    } + +    glfwTerminate(); +    return 0; +} + +void error_callback(int error, const char* description) +{ +    fputs(description, stderr); +    exit(error); +} + +void display(void) +{ +    glClearColor(0.5, 0.5, 0.5, 0.0); +    glClear(GL_COLOR_BUFFER_BIT); + +    glClearColor(0.0, 0.0, 0.0, 0.0); +    glClear(GL_DEPTH_BUFFER_BIT); + +    double delta = get_delta(); + +    float cos_pitch = cosf(DEG_RAD(pitch)); + +    float lx = -cos_pitch * sinf(DEG_RAD(yaw)); +    float ly = sinf(DEG_RAD(pitch)); +    float lz = -cos_pitch * cosf(DEG_RAD(yaw)); + +    x += lx * forward * delta * 10; +    y += ly * forward * delta * 10; +    z += lz * forward * delta * 10; + +    y += 1.0 * up * delta * 10; + +    x -= lz * right * delta * 10; +    z += lx * right * delta * 10; + +    glLoadIdentity(); + +    gluLookAt(x, y, z, x + lx, y + ly, z + lz, 0, 1, 0); + +    glTranslatef(0, 0, -5); + +    glBegin(GL_QUADS); +        glColor3f(1, 0, 0); +        glVertex3f(-1, -1, 0); +        glColor3f(1, 1, 0); +        glVertex3f(1, -1, 0); +        glColor3f(0, 1, 1); +        glVertex3f(1, 1, 0); +        glColor3f(1, 0, 1); +        glVertex3f(-1, 1, 0); +    glEnd(); + +    glTranslatef(0, -3, 5); + +    glBegin(GL_QUADS); +        glColor3f(0.5, 0, 0); +        glVertex3f(-100, 0, -100); +        glVertex3f(100, 0, -100); +        glColor3f(0, 0.5, 0); +        glVertex3f(100, 0, 100); +        glVertex3f(-100, 0, 100); +    glEnd(); +} + +double get_delta() +{ +    static double last_time = 0; + +    double time = glfwGetTime(); + +    double delta = time - last_time; + +    last_time = time; + +    return delta; +} + +void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) +{ +    if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) +        mouse_grabbed = !mouse_grabbed; +    int cmode = mouse_grabbed ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL; + +    glfwSetInputMode(window, GLFW_CURSOR, cmode); +} + +void cursor_enter_callback(GLFWwindow *window, int entered) +{ +    if (entered == GL_TRUE) { +        glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); +    } else { +        glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); +    } +} + +void mouse_resolve(GLFWwindow *window) +{ +    double xpos, ypos; +    glfwGetCursorPos(window, &xpos, &ypos); + +    int width, height; +    glfwGetWindowSize(window, &width, &height); + +    glfwSetCursorPos(window, width / 2, height / 2); + +    float dpp = (float)FOV / (float)width; + +    int dx = xpos - width / 2; +    int dy = ypos - height / 2; + +    yaw -= dx * dpp * MOUSE_SPEED; +    pitch -= dy * dpp * MOUSE_SPEED; + +    if (yaw > 180) yaw -= 360; +    if (yaw <= -180) yaw += 360; +    if (pitch > 89.9) pitch = 89.9; +    if (pitch < -89.9) pitch = -89.9; +} + +void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) +{ +    if (action == GLFW_PRESS) { +        action = 1; +    } else if (action == GLFW_RELEASE) { +        action = -1; +    } + +    switch (key) { +        case GLFW_KEY_W: forward += action; +                         break; +        case GLFW_KEY_S: forward -= action; +                         break; + +        case GLFW_KEY_Z: up += action; +                         break; +        case GLFW_KEY_X: up -= action; +                         break; + +        case GLFW_KEY_D: right += action; +                         break; +        case GLFW_KEY_A: right -= action; +                         break; +        case GLFW_KEY_LEFT_SHIFT: ; +                                  break; +    } + +    if (forward > 1) forward = 1; +    if (forward < -1) forward = -1; + +    if (up > 1) up = 1; +    if (up < -1) up = -1; + +    if (right > 1) right = 1; +    if (right < -1) right = -1; +} + +void framebuffer_size_callback(GLFWwindow *window, int width, int height) +{ +    if (height == 0) +        height = 1; + +    float ratio = (float)width / (float)height; + +    glMatrixMode(GL_PROJECTION); + +    glLoadIdentity(); + +    glViewport(0, 0, width, height); + +    gluPerspective(FOV, ratio, 1, 100); + +    glMatrixMode(GL_MODELVIEW); +} diff --git a/glut-time.c b/glut-time.c new file mode 100644 index 0000000..358d0dc --- /dev/null +++ b/glut-time.c @@ -0,0 +1,310 @@ +#include <GL/glew.h> +#include <GL/glut.h> +#include <ctype.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +#define WWIDTH 640 +#define WHEIGHT 480 + +void display_callback(void); +void reshape_callback(int, int); +void keyboard_callback(unsigned char, int, int); +void special_callback(int, int, int); +void setShaders(void); +char *textFileRead(char *); + +static float red = 0; +static float green = 0; +static float blue = 0; + +int main(int argc, char **argv) +{ +    glutInit(&argc, argv); + +    int swidth, sheight; +    swidth = glutGet(GLUT_SCREEN_WIDTH); +    sheight = glutGet(GLUT_SCREEN_HEIGHT); + +    glutInitWindowPosition((swidth - WWIDTH) / 2, (sheight - WHEIGHT) / 2); +    glutInitWindowSize(WWIDTH, WHEIGHT); +    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); +    int w = glutCreateWindow("Win! Dough."); + +    glewInit(); + +    setShaders(); + +    glEnable(GL_DEPTH_TEST); +    glDepthFunc(GL_LEQUAL); +    glDepthMask(GL_TRUE); + +    /*glutDisplayFunc(display_callback);*/ +    glutReshapeFunc(reshape_callback); +    glutIdleFunc(display_callback); +    glutKeyboardFunc(keyboard_callback); +    glutSpecialFunc(special_callback); + +    glutMainLoop(); + +    return 0; +} + +void display_callback(void) +{ +    static float angle = 0; +    static int elapsed_old = 0; + +    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + +    glLoadIdentity(); + +    gluLookAt(0, 0, -1, +              0, 0,  0, +              0, 1,  0); + +    glPushMatrix(); + +    glTranslatef(0, 0, 5); + +    glRotatef(angle, 0.6, 0.9, 0.3); + +    glPushMatrix(); + +    glTranslatef(-0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(red, green, blue); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(red, green, blue); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(red, green, blue); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(red, green, blue); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); +    glPushMatrix(); + +    glTranslatef(0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(green, red, blue); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(green, red, blue); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(green, red, blue); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(green, red, blue); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); + +    glPushMatrix(); +    glRotatef(90, 0, 1, 0); + +    glPushMatrix(); + +    glTranslatef(-0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(blue, green, red); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(blue, green, red); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(blue, green, red); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(blue, green, red); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); +    glPushMatrix(); + +    glTranslatef(0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(red, blue, green); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(red, blue, green); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(red, blue, green); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(red, blue, green); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); + +    glPopMatrix(); + +    glPushMatrix(); + +    glRotatef(90, 0, 0, 1); + +    glPushMatrix(); + +    glTranslatef(-0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(blue, blue, red); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(blue, blue, red); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(blue, blue, red); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(blue, blue, red); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); +    glPushMatrix(); + +    glTranslatef(0.5, 0, 0); + +    glBegin(GL_QUADS); +        glColor3f(red, blue, red); +        glVertex3f(0, 0.5, 0.5); +        glColor3f(red, blue, red); +        glVertex3f(0, 0.5, -0.5); +        glColor3f(red, blue, red); +        glVertex3f(0, -0.5, -0.5); +        glColor3f(red, blue, red); +        glVertex3f(0, -0.5, 0.5); +    glEnd(); + +    glPopMatrix(); + +    glPopMatrix(); + +    glPopMatrix(); + +    int elapsed = glutGet(GLUT_ELAPSED_TIME); +    int delta = elapsed - elapsed_old; +    elapsed_old = elapsed; +    angle += 90 * (float)delta / 1000; + +    glutSwapBuffers(); +} + +void reshape_callback(int width, int height) +{ +    if (height == 0) +        height = 1; + +    float ratio = 1.0 * width / height; + +    glMatrixMode(GL_PROJECTION); + +    glLoadIdentity(); + +    glViewport(0, 0, width, height); + +    gluPerspective(45, ratio, 1, 1000); + +    glMatrixMode(GL_MODELVIEW); +} + +void keyboard_callback(unsigned char key, int x, int y) +{ +    if (isgraph(key) || isblank(key)) { +        printf("\"%c\" was pressed. (Value: %d)\n", key, key); +    } else { +        printf("Key of value %d was pressed.\n", key); +    } +} + +void special_callback(int key, int x, int y) +{ +    switch (key) { +        case GLUT_KEY_F1: +            red = 1.0; +            green = 0.0; +            blue = 0.0; +            break; +        case GLUT_KEY_F2: +            red = 0.0; +            green = 1.0; +            blue = 0.0; +            break; +        case GLUT_KEY_F3: +            red = 0.0; +            green = 0.0; +            blue = 1.0; +            break; +        default: +            red = (float)rand() / (float)RAND_MAX; +            green = (float)rand() / (float)RAND_MAX; +            blue = (float)rand() / (float)RAND_MAX; +    } + +    printf("Special key value %d pressed.\n", key); +} + +void setShaders() { +    GLuint v, f, p; +    char *fs, *vs; + +	v = glCreateShader(GL_VERTEX_SHADER); +	f = glCreateShader(GL_FRAGMENT_SHADER);	 + +	vs = textFileRead("gluttest.vert"); +	fs = textFileRead("gluttest.frag"); + +	const char * vv = vs; +	const char * ff = fs; + +	glShaderSource(v, 1, &vv,NULL); +	glShaderSource(f, 1, &ff,NULL); + +	free(vs);free(fs); + +	glCompileShader(v); +	glCompileShader(f); + +	p = glCreateProgram(); + +	glAttachShader(p,v); +	glAttachShader(p,f); + +	glLinkProgram(p); +	glUseProgram(p); +} + + +char *textFileRead(char *fn) +{ +	FILE *fp; +	char *content = NULL; + +	int count=0; + +	if (fn != NULL) { +		fp = fopen(fn,"rt"); + +		if (fp != NULL) { +       +      fseek(fp, 0, SEEK_END); +      count = ftell(fp); +      rewind(fp); + +			if (count > 0) { +				content = (char *)malloc(sizeof(char) * (count+1)); +				count = fread(content,sizeof(char),count,fp); +				content[count] = '\0'; +			} +			fclose(fp); +		} +	} +	 +	if (content == NULL) +	   { +	   fprintf(stderr, "ERROR: could not load in file %s\n", fn); +	   exit(1); +	   } +	return content; +} @@ -0,0 +1,88 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <math.h> + +static long int sfreq; + +void playtone(long int, int, long int, float); +void playtone_pwm(long int, long int, int, long int); +void sinetone(float, int, float); + +int main(int argc, char **argv) +{ +        if (argc != 2) { +                fprintf(stderr, "Usage: %s <frequency>\n", argv[0]); +                exit(1); +        } + +        sfreq = strtol(argv[1], NULL, 10); + +        /*for (int i = 1; i < 3000; i++)*/ +                /*sinetone(i, 127, 5);*/ + +        for (int i = 1; i < 3000; i++) +                sinetone(i, 127, 1.0/i * 1000.0); + +        /*for (int i = 0; i < 1000; i++) {*/ +                /*long int pulse_width = 441;*/ + +                /*float duty_cycle = (sinf(i / 5.0 * M_PI) + 1.0) / 2.0;*/ + +                /*for (int ii = 0; ii < ceilf(duty_cycle * pulse_width); ii++)*/ +                        /*putchar(0);*/ +                /*for (int ii = 0; ii < floorf((1.0 - duty_cycle) * pulse_width); ii++)*/ +                        /*putchar(255);*/ +        /*}*/ + +        /*playtone_pwm(160, 80, 127, 10000);*/ +        /*for (float i = 0; i < 1; i+=0.01)*/ +                /*playtone(100, 127, 10, i);*/ +        return 0; +} + +void playtone(long int freq, int ampl, long int time, float duty_cycle) +{ +        if (ampl > 127) +                ampl = 127; +        long int pulse_width = sfreq / freq; + +        long int total_waves = time * freq / 1000; + +        for (long int wi = 0; wi < total_waves; wi++) { +                int i; +                for (i = 0; i < (int)((float)pulse_width * duty_cycle); i++) +                        putchar(128-ampl); +                for (i = 0; i < (int)((float)pulse_width * (1.0 - duty_cycle)); i++) +                        putchar(255+ampl); +        } +} + +void playtone_pwm(long int freq, long int freq_pwm, int ampl, long int time) +{ +        if (ampl > 127) +                ampl = 127; + +        long int pulse_width = sfreq / freq; + +        long int total_waves = time * freq / 1000; + +        for (long int wi = 0; wi < total_waves; wi++) { +                int i; +                float pulse_proportion = (sinf((float)wi / (float)total_waves * time / freq_pwm * 2 * M_PI) + 1) / 2; +                for (i = 0; i < (int)((float)pulse_width * pulse_proportion); i++) +                        putchar(128-ampl); +                for (i = 0; i < (int)((float)pulse_width * (1.0 - pulse_proportion)); i++) +                        putchar(255+ampl); +        } +} + +void sinetone(float freq, int ampl, float time) +{ +        if (ampl > 127) ampl = 127; + +        float wave_length = sfreq / freq; + +        for (long int wi = 0; wi < time * sfreq / 1000; wi++) +                putchar((char)((sinf((float)wi / wave_length * M_PI_2 * 4) + 1) * ampl)); +} diff --git a/hostnames.c b/hostnames.c new file mode 100644 index 0000000..33729e0 --- /dev/null +++ b/hostnames.c @@ -0,0 +1,27 @@ +#include <stdio.h> +#include <netdb.h> +#include <arpa/inet.h> +#include <stdlib.h> + +int main(int argc, char **argv) +{ +        if (argc != 2) { +                fprintf(stderr, "Usage: %s <domain-name>\n", argv[0]); +                exit(1); +        } + +        struct hostent* he; + +        he = gethostbyname(argv[1]); + +        if (!he) { +                fputs("Error, could not resolve.\n", stderr); +                exit(1); +        } + +        char str[INET_ADDRSTRLEN]; +        inet_ntop(AF_INET, he->h_addr, str, INET_ADDRSTRLEN); + +        printf("%s\n", str); +        return 0; +} diff --git a/imlib2.c b/imlib2.c new file mode 100644 index 0000000..6345c5b --- /dev/null +++ b/imlib2.c @@ -0,0 +1,230 @@ +#include <Imlib2.h> +#include <X11/Xlib.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +Display *disp; +Window win; +Visual *vis; +Colormap cm; +int depth; + +int main(int argc, char **argv) +{ +        XEvent ev; + +        Imlib_Updates updates, current_update; +        Imlib_Image buffer; +        Imlib_Font font; +        Imlib_Color_Range range; +        int mouse_x = 0, mouse_y = 0; + +        disp = XOpenDisplay(NULL); + +        vis = DefaultVisual(disp, DefaultScreen(disp)); +        depth = DefaultDepth(disp, DefaultScreen(disp)); +        cm = DefaultColormap(disp, DefaultScreen(disp)); + +        win = XCreateSimpleWindow(disp, DefaultRootWindow(disp), +                        0, 0, 640, 480, 0, 0, 0); + +        XSelectInput(disp, win, ButtonPressMask | ButtonReleaseMask | +                        PointerMotionMask | ExposureMask); + +        XMapWindow(disp, win); + +        imlib_set_cache_size(2048 * 1024); + +        imlib_set_font_cache_size(512 * 1024); + +        imlib_add_path_to_font_path("./ttfonts"); + +        imlib_set_color_usage(128); + +        imlib_context_set_dither(1); + +        imlib_context_set_display(disp); +        imlib_context_set_visual(vis); +        imlib_context_set_colormap(cm); +        imlib_context_set_drawable(win); + +        while(true) { +                Imlib_Image image; +                int w, h, text_w, text_h; + +                updates = imlib_updates_init(); + +                do { +                        XNextEvent(disp, &ev); +                        switch (ev.type) { +                                case Expose: +                                        updates = imlib_update_append_rect(updates, +                                                        ev.xexpose.x, ev.xexpose.y, +                                                        ev.xexpose.width, ev.xexpose.height); +                                        break; +                                case ButtonPress: +                                        exit(0); +                                        break; +                                case MotionNotify: +                                        image = imlib_load_image("./test_images/mush.png"); +                                        imlib_context_set_image(image); +                                        w = imlib_image_get_width(); +                                        h = imlib_image_get_height(); + +                                        imlib_context_set_image(image); +                                        imlib_free_image(); + +                                        updates = imlib_update_append_rect(updates, +                                                        mouse_x - (w / 2), mouse_y - (h / 2), +                                                        w, h); +                                        font = imlib_load_font("notepad/30"); +                                        if (font) { +                                                char text[4096]; +                                                imlib_context_set_font(font); +                                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y); +                                                imlib_get_text_size(text, &text_w, &text_h); +                                                imlib_free_font(); + +                                                updates = imlib_update_append_rect(updates, +                                                                320 - text_w / 2, 240 - text_h / 2, +                                                                text_w, text_h); +                                        } + +                                        mouse_x = ev.xmotion.x; +                                        mouse_y = ev.xmotion.y; + +                                        updates = imlib_update_append_rect(updates, +                                                        mouse_x - w / 2, mouse_y - h / 2, +                                                        w, h); +                                        font = imlib_load_font("notepad/30"); +                                        if (font) { +                                                char text[4096]; + +                                                imlib_context_set_font(font); +                                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y); +                                                imlib_get_text_size(text, &text_w, &text_h); +                                                imlib_free_font(); +                                                updates = imlib_update_append_rect(updates, +                                                                320 - text_w / 2, 240 - text_h / 2, +                                                                text_w, text_h); +                                        } +                                default: +                                        break; +                        } +                } while (XPending(disp)); + + +                updates = imlib_updates_merge_for_rendering(updates, 640, 480); +                for (current_update = updates; +                                current_update; +                                current_update = imlib_updates_get_next(current_update)) { +                        int up_x, up_y, up_w, up_h; + +                        imlib_updates_get_coordinates(current_update, &up_x, &up_y, &up_w, &up_h); + +                        buffer = imlib_create_image(up_w, up_h); + +                        imlib_context_set_blend(1); + +                        image = imlib_load_image("./test_images/bg.png"); + +                        imlib_context_set_image(image); + +                        w = imlib_image_get_width(); +                        h = imlib_image_get_height(); + +                        imlib_context_set_image(buffer); + +                        if (image) { +                                imlib_blend_image_onto_image(image, 0, 0, 0, +                                                w, h, -up_x, -up_y, 640, 480); + +                                imlib_context_set_image(image); + +                                imlib_free_image(); +                        } + +                        image = imlib_load_image("./test_images/mush.png"); +                        imlib_context_set_image(image); +                        w = imlib_image_get_width(); +                        h = imlib_image_get_height(); +                        imlib_context_set_image(image); +                        if (image) { +                                imlib_blend_image_onto_image(image, 0, 0, 0, w, h, +                                                mouse_x - w / 2 - up_x, +                                                mouse_y - h / 2 - up_y, +                                                w, h); +                                imlib_context_set_image(image); +                                imlib_free_image(); +                        } + +                        range = imlib_create_color_range(); +                        imlib_context_set_color_range(range); + +                        imlib_context_set_color(255, 255, 255, 255); +                        imlib_add_color_to_color_range(0); + +                        imlib_context_set_color(255, 200, 10, 100); +                        imlib_add_color_to_color_range(10); + +                        imlib_context_set_color(0, 0, 0, 0); +                        imlib_add_color_to_color_range(20); + +                        imlib_context_set_image(buffer); +                        imlib_image_fill_color_range_rectangle(-up_x, -up_y, 128, 128, -45); + +                        imlib_free_color_range(); + +                        font = imlib_load_font("notepad/30"); +                        if (font) { +                                char text[4096]; + +                                imlib_context_set_font(font); +                                imlib_context_set_image(buffer); +                                imlib_context_set_color(0, 0, 0, 255); +                                sprintf(text, "Mouse is at %i, %i", mouse_x, mouse_y); +                                imlib_get_text_size(text, &text_w, &text_h); +                                imlib_text_draw(320 - text_w / 2 - up_x, +                                                240 - text_h / 2 - up_y, +                                                text); +                                imlib_free_font(); +                        } + +                        imlib_context_set_blend(0); +                        imlib_context_set_image(buffer); +                        imlib_render_image_on_drawable(up_x, up_y); +                        imlib_free_image(); +                } +                if (updates) +                        imlib_updates_free(updates); +        } +        return 0; +} + +        /*Imlib_Image image = imlib_create_image(640, 480);*/ + +        /*imlib_context_set_image(image);*/ + +        /*imlib_context_set_color(255, 255, 255, 255);*/ + +        /*imlib_image_fill_rectangle(0, 0, 640, 480);*/ + +        /*imlib_context_set_color(255, 128, 0, 255);*/ + +        /*ImlibPolygon poly = imlib_polygon_new();*/ + +        /*imlib_polygon_add_point(poly, 0, 0);*/ +        /*imlib_polygon_add_point(poly, 10, 440);*/ +        /*imlib_polygon_add_point(poly, 600, 10);*/ + +        /*imlib_image_fill_polygon(poly);*/ + +        /*imlib_polygon_free(poly);*/ + +        /*imlib_save_image("imlibtest.png");*/ + +        /*imlib_free_image();*/ + +        /*return 0;*/ +/*}*/ diff --git a/intfloatprint.c b/intfloatprint.c new file mode 100644 index 0000000..0e04427 --- /dev/null +++ b/intfloatprint.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ +        printf("Int for float: %f, Float for float: %f\n", 1.0, 1.0); +        return 0; +} diff --git a/lighthouse3dglex.c b/lighthouse3dglex.c new file mode 100644 index 0000000..08cff1a --- /dev/null +++ b/lighthouse3dglex.c @@ -0,0 +1,221 @@ + +/* +  Simple Demo for GLSL +  www.lighthouse3d.com +   +  tweaked a bit by andy +*/ + +#include "GL/glew.h" + +#ifdef __linux__ +#include <GL/glut.h> +#endif + +#ifdef __APPLE__ +#include <glut.h> +#endif + +#ifdef _WIN32 +#include "glut.h" +#endif + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +/////////////////////////////////////////////////////////////////////// + +char *textFileRead(char *fn) +{ +	FILE *fp; +	char *content = NULL; + +	int count=0; + +	if (fn != NULL) { +		fp = fopen(fn,"rt"); + +		if (fp != NULL) { +       +      fseek(fp, 0, SEEK_END); +      count = ftell(fp); +      rewind(fp); + +			if (count > 0) { +				content = (char *)malloc(sizeof(char) * (count+1)); +				count = fread(content,sizeof(char),count,fp); +				content[count] = '\0'; +			} +			fclose(fp); +		} +	} +	 +	if (content == NULL) +	   { +	   fprintf(stderr, "ERROR: could not load in file %s\n", fn); +	   exit(1); +	   } +	return content; +} + +///////////////////////////////////////////////////////////////////////            + +int textFileWrite(char *fn, char *s) +{ +        FILE *fp; +        int status = 0; + +        if (fn != NULL) { +                fp = fopen(fn,"w"); + +                if (fp != NULL) { +                         +                        if (fwrite(s,sizeof(char),strlen(s),fp) == strlen(s)) +                                status = 1; +                        fclose(fp); +                } +        } +        return(status); +}    + +/////////////////////////////////////////////////////////////////////// + +GLuint v,f,p; +float lpos[4] = {1.0, 0.5, 1.0, 0.0}; + +/////////////////////////////////////////////////////////////////////// + +void changeSize(int w, int h) +{ +	// Prevent a divide by zero, when window is too short +	// (you cant make a window of zero width). +	if(h == 0) +		h = 1; + +	float ratio = 1.0* w / h; + +	// Reset the coordinate system before modifying +	glMatrixMode(GL_PROJECTION); +	glLoadIdentity(); +	 +	// Set the viewport to be the entire window +    glViewport(0, 0, w, h); + +	// Set the correct perspective. +	gluPerspective(45, ratio, 1, 1000); +	glMatrixMode(GL_MODELVIEW); +} + +/////////////////////////////////////////////////////////////////////// + +void renderScene(void) +{ +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + +	glLoadIdentity(); +	gluLookAt(0.0,0.0,5.0,  +		      0.0,0.0,-1.0, +			  0.0f,1.0f,0.0f); + +	glLightfv(GL_LIGHT0, GL_POSITION, lpos); +	glutSolidTeapot(1); + +	glutSwapBuffers(); +} + +/////////////////////////////////////////////////////////////////////// + +void processNormalKeys(unsigned char key, int x, int y) { + +	if (key == 27)  +		exit(0); +} + +/////////////////////////////////////////////////////////////////////// + +void setShaders() +{ +	char *vs,*fs; + +	v = glCreateShader(GL_VERTEX_SHADER); +	f = glCreateShader(GL_FRAGMENT_SHADER); + +    // older syntax +	//v = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); +	//f = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); + +	vs = textFileRead("./toon.vert"); +	fs = textFileRead("./toon.frag"); + +	const char * ff = fs; +	const char * vv = vs; + +	glShaderSource(v, 1, &vv,NULL); +	glShaderSource(f, 1, &ff,NULL); + +    // older syntax +	//glShaderSourceARB(v, 1, &vv,NULL); +	//glShaderSourceARB(f, 1, &ff,NULL); + +	free(vs);free(fs); + +	glCompileShader(v); +	glCompileShader(f); + +    // older syntax +	//glCompileShaderARB(v); +	//glCompileShaderARB(f); + +	p = glCreateProgram(); + +    // older syntax +	//p = glCreateProgramObjectARB(); + +	glAttachShader(p,f); +	glAttachShader(p,v); +	 +    // older syntax +	//glAttachObjectARB(p,f); +	//glAttachObjectARB(p,v); + +	glLinkProgram(p); +	 +	// comment out this line to not use the shader +	glUseProgram(p); + +    // older syntax +	//glLinkProgramARB(p); +	//glUseProgramObjectARB(p); +} + +/////////////////////////////////////////////////////////////////////// + +int main(int argc, char **argv) { +	glutInit(&argc, argv); +	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); +	glutInitWindowPosition(100,100); +	glutInitWindowSize(320,320); +	glutCreateWindow("GLSL"); + +	glutDisplayFunc(renderScene); +	glutIdleFunc(renderScene); +	glutReshapeFunc(changeSize); +	glutKeyboardFunc(processNormalKeys); + +	glEnable(GL_DEPTH_TEST); +	glClearColor(0.0,0.0,0.0,1.0); + +	glewInit(); +	if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) +		printf("Ready for GLSL\n"); +	else { +		printf("No GLSL support\n"); +		exit(1); +	} + +	setShaders(); + +	glutMainLoop(); +	return 0; +} diff --git a/luaing.c b/luaing.c new file mode 100644 index 0000000..ac08668 --- /dev/null +++ b/luaing.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <string.h> +#include <lua.h> +#include <lauxlib.h> +#include <lualib.h> + +int main(int argc, char **argv) +{ +    char buffer[256]; +    int error; +    lua_State *L = luaL_newstate(); +    luaL_openlibs(L); + +    while (fgets(buffer, sizeof(buffer), stdin) != NULL) { +        error = luaL_loadbuffer(L, buffer, strlen(buffer), "line") +            || lua_pcall(L, 0, 0, 0); +        if (error) { +            fprintf(stderr, "%s", lua_tostring(L, -1)); +            lua_pop(L, 1); +        } +    } + +    lua_close(L); +    return 0; +} diff --git a/mousecurses.c b/mousecurses.c new file mode 100644 index 0000000..212064b --- /dev/null +++ b/mousecurses.c @@ -0,0 +1,107 @@ +#include <ncurses.h> +#include <string.h> + +#define WIDTH 30 +#define HEIGHT 10  + +int startx = 0; +int starty = 0; + +char *choices[] = { 	"Choice 1", +    "Choice 2", +    "Choice 3", +    "Choice 4", +    "Exit", +}; + +int n_choices = sizeof(choices) / sizeof(char *); + +void print_menu(WINDOW *menu_win, int highlight); +void report_choice(int mouse_x, int mouse_y, int *p_choice); + +int main() +{	int c, choice = 0; +    WINDOW *menu_win; +    MEVENT event; + +    /* Initialize curses */ +    initscr(); +    clear(); +    noecho(); +    cbreak();	//Line buffering disabled. pass on everything + +    /* Try to put the window in the middle of screen */ +    startx = (80 - WIDTH) / 2; +    starty = (24 - HEIGHT) / 2; + +    attron(A_REVERSE); +    mvprintw(23, 1, "Click on Exit to quit (Works best in a virtual console)"); +    refresh(); +    attroff(A_REVERSE); + +    /* Print the menu for the first time */ +    menu_win = newwin(HEIGHT, WIDTH, starty, startx); +    print_menu(menu_win, 1); +    /* Get all the mouse events */ +    mousemask(ALL_MOUSE_EVENTS, NULL); + +    while(1) +    {	c = wgetch(menu_win); +        switch(c) +        {	case KEY_MOUSE: +            if(getmouse(&event) == OK) +            {	/* When the user clicks left mouse button */ +                if(event.bstate & BUTTON1_PRESSED) +                {	report_choice(event.x + 1, event.y + 1, &choice); +                    if(choice == -1) //Exit chosen +                        goto end; +                    mvprintw(22, 1, "Choice made is : %d String Chosen is \"%10s\"", choice, choices[choice - 1]); +                    refresh();  +                } +            } +            print_menu(menu_win, choice); +            break; +        } +    }		 +end: +    endwin(); +    return 0; +} + + +void print_menu(WINDOW *menu_win, int highlight) +{ +    int x, y, i;	 + +    x = 2; +    y = 2; +    box(menu_win, 0, 0); +    for(i = 0; i < n_choices; ++i) +    {	if(highlight == i + 1) +        {	wattron(menu_win, A_REVERSE);  +            mvwprintw(menu_win, y, x, "%s", choices[i]); +            wattroff(menu_win, A_REVERSE); +        } +        else +            mvwprintw(menu_win, y, x, "%s", choices[i]); +        ++y; +    } +    wrefresh(menu_win); +} + +/* Report the choice according to mouse position */ +void report_choice(int mouse_x, int mouse_y, int *p_choice) +{	int i,j, choice; + +    i = startx + 2; +    j = starty + 3; + +    for(choice = 0; choice < n_choices; ++choice) +        if(mouse_y == j + choice && mouse_x >= i && mouse_x <= i + strlen(choices[choice])) +        {	if(choice == n_choices - 1) +            *p_choice = -1;		 +            else +                *p_choice = choice + 1;	 +            break; +        } +} diff --git a/ncurses.c b/ncurses.c new file mode 100644 index 0000000..ad7dced --- /dev/null +++ b/ncurses.c @@ -0,0 +1,32 @@ +#include <ncurses.h> + +int main() +{ +    int ch; + +    initscr();            /* Start curses mode         */ +    raw();                /* Line buffering disabled    */ +    keypad(stdscr, TRUE);        /* We get F1, F2 etc..        */ +    noecho();            /* Don't echo() while we do getch */ + +    printw("Type any character to see it in bold\n"); +    ch = getch();            /* If raw() hadn't been called +                              * we have to press enter before it +                              * gets to the program         */ +    if(ch == KEY_F(1))        /* Without keypad enabled this will */ +        printw("F1 Key pressed");/*  not get to us either    */ +    /* Without noecho() some ugly escape +     * charachters might have been printed +     * on screen            */ +    else +    {    printw("The pressed key is "); +        attron(A_BOLD); +        printw("%c", ch); +        attroff(A_BOLD); +    } +    refresh();            /* Print it on to the real screen */ +    getch();            /* Wait for user input */ +    endwin();            /* End curses mode          */ + +    return 0; +} diff --git a/ncursor.c b/ncursor.c new file mode 100644 index 0000000..93c3f07 --- /dev/null +++ b/ncursor.c @@ -0,0 +1,30 @@ +#include <stdlib.h> +#include <ncurses.h> +#include <stdbool.h> + +int main(int argc, char **argv) +{ +    initscr(); +    raw(); +    noecho(); + +    int row, col; +    getmaxyx(stdscr, row, col); + +    int x = 0, y = 0; + +    bool run = true; +    while (run) { +        char c = getch(); +        switch (c) { +            case 'w': if (x > 0) x--; break; +            case 's': if (x < (row - 1)) x++; break; +            case 'a': if (y > 0) y--; break; +            case 'd': if (y < (col - 1)) y++; break; +            case 'x': run = false; break; +        } +        move(x, y); +    } + +    endwin(); +} diff --git a/overcomplicated_loop.c b/overcomplicated_loop.c new file mode 100644 index 0000000..a843a8a --- /dev/null +++ b/overcomplicated_loop.c @@ -0,0 +1,27 @@ +#include <setjmp.h> +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +static jmp_buf env; + +int main (void) +{ +    srand(time(NULL)); +    int a = 0; +    while (a == 0) { +        a += rand() * time(NULL); +        float c = 1 / a; + +        for (int b = 20; !setjmp(env); b -= 21) { +            if ((a / b) < 20) +                c *= c; + +            if (c == a) +                longjmp(env, 0); +            /* +             * Your code here. +             */ +        } +    } +} diff --git a/rewrite.c b/rewrite.c new file mode 100644 index 0000000..0f91219 --- /dev/null +++ b/rewrite.c @@ -0,0 +1,8 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ +    printf("0123456789012345678901234567890123456789"); +    printf("\rTesting.\n"); +    return 0; +} @@ -8,58 +8,58 @@  #define messagelength 1000000  int main(int argc, char *argv[]) { -    char *hostname = "feeds.feedburner.com"; -    char ip[100]; -    struct hostent *he; -    struct in_addr **addr_list; -    int i; +        char *hostname = "feeds.feedburner.com"; +        char ip[100]; +        struct hostent *he; +        struct in_addr **addr_list; +        int i; -    if ( (he = gethostbyname( hostname ) ) == NULL) { -        //gethostbyname failed -        herror("gethostbyname"); -        return 1; -    } +        if ( (he = gethostbyname( hostname ) ) == NULL) { +                //gethostbyname failed +                herror("gethostbyname"); +                return 1; +        } -    addr_list = (struct in_addr **) he->h_addr_list; +        addr_list = (struct in_addr **) he->h_addr_list; -    for(i = 0; addr_list[i] != NULL; i++) { -        strcpy(ip, inet_ntoa(*addr_list[i]) ); -    } +        for(i = 0; addr_list[i] != NULL; i++) { +                strcpy(ip, inet_ntoa(*addr_list[i]) ); +        } -    printf("%s resolved to : %s\n", hostname, ip); +        printf("%s resolved to : %s\n", hostname, ip); -	int socket_desc; -	struct sockaddr_in server; -	char *message, server_reply[messagelength]; +        int socket_desc; +        struct sockaddr_in server; +        char *message, server_reply[messagelength]; -	socket_desc = socket(AF_INET, SOCK_STREAM, 0); -	if (socket_desc == -1) { -		printf("Could not create socket"); -	} -	 -	server.sin_addr.s_addr = inet_addr(ip); -	server.sin_family = AF_INET; -	server.sin_port = htons( 80 ); +        socket_desc = socket(AF_INET, SOCK_STREAM, 0); +        if (socket_desc == -1) { +                printf("Could not create socket"); +        } -	if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) { -		puts("connect error"); -		return 1; -	} -	 -	puts("Connected\n"); +        server.sin_addr.s_addr = inet_addr(ip); +        server.sin_family = AF_INET; +        server.sin_port = htons( 80 ); -	message = "HEAD /Explosm HTTP/1.1\r\nHost: feeds.feedburner.com\r\n\r\n"; -	if( send(socket_desc , message , strlen(message) , 0) < 0) { -		puts("Send failed"); -		return 1; -	} -	puts("Data Send\n"); -	 -	if( recv(socket_desc, server_reply , messagelength, 0) < 0) { -		puts("recv failed"); -	} -	puts("Reply received\n"); -	puts(server_reply); +        if (connect(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0) { +                puts("connect error"); +                return 1; +        } -	return 0; +        puts("Connected\n"); + +        message = "HEAD /Explosm HTTP/1.1\r\nHost: feeds.feedburner.com\r\n\r\n"; +        if( send(socket_desc , message , strlen(message) , 0) < 0) { +                puts("Send failed"); +                return 1; +        } +        puts("Data Send\n"); + +        if( recv(socket_desc, server_reply , messagelength, 0) < 0) { +                puts("recv failed"); +        } +        puts("Reply received\n"); +        puts(server_reply); + +        return 0;  } @@ -0,0 +1,17 @@ +static inline void swap(int *a, int *b) +{ +        int tmp = *a; +        *a = *b; +        *b = tmp; +} + +int main(int argc, char **argv) +{ +        int i; +        for (i = 0; i < 999999999; i++) { +                int a = 2; +                int b = 123; +                swap(&a, &b); +        } +        return 0; +} @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +unsigned long long int get_seconds(char *); + +void usage(char *cmd) +{ +    printf("Usage:\n" +           "    %s <n>{s,m,h,D,M,Y} ...\n", cmd); +} + +int main(int argc, char **argv) +{ +    if (argc < 2) { +        printf("Not enough arguments.\n"); +        usage(argv[0]); +        exit(1); +    } + +    unsigned long long int total_seconds = 0; + +    int i; +    for (i = 1; i < argc; i++) +        total_seconds += get_seconds(argv[i]); + +    printf("Total time: %llu second(s).\n", total_seconds); + +    return 0; +} + +unsigned long long int get_seconds(char *code) +{ +    int length = strlen(code); +    if (length < 2) { +        return 0; +    } + +    int multiplier = 0; +    char suffix = code[length - 1]; +    switch (suffix) { +        case 's': multiplier = 1; break; // 1 second +        case 'm': multiplier = 60; break; // 1 minute +        case 'h': multiplier = 3600; break; // 1 hour +        case 'D': multiplier = 86400; break; // 1 day +        case 'M': multiplier = 2419200; break; // 28 days +        case 'Y': multiplier = 31536000; break; // 365 days +        default : return 0; +    } + +    char value[length + 1]; +    strcpy(value, code); + +    value[length - 1] = '\0'; + +    return strtoull(value, NULL, 10) * multiplier; +} diff --git a/vectors-testing.c b/vectors-testing.c new file mode 100644 index 0000000..dba96b2 --- /dev/null +++ b/vectors-testing.c @@ -0,0 +1,47 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define V_NEW(l) v_new(l, (double[]) + +typedef struct { +        int length; +        double *raw; +} Vec; + +double v_dot(Vec *, Vec *); +Vec *v_new(int, double *); + +int main(int argc, char **argv) +{ +        Vec *v1 = v_new(3, (double[]){1, 2, 2}); +        Vec *v2 = V_NEW(3){1, 2, 3}); +        printf("Dot: %f\n", v_dot(v1, v2)); +        printf("%d: %f, %f, %f.\n", v1->length, v1->raw[0], v1->raw[1], v1->raw[2]); +        printf("%d: %f, %f, %f.\n", v2->length, v2->raw[0], v2->raw[1], v2->raw[2]); + +        free(v1); +        free(v2); +} + +Vec *v_new(int length, double *raw) +{ +        Vec *vec = malloc(sizeof(Vec)); +        vec->raw = malloc(length * sizeof(double)); +        vec->length = length; +        memcpy(vec->raw, raw, length * sizeof(double)); +        return vec; +} + +double v_dot(Vec *v1, Vec *v2) +{ +        int shortest = MIN(v1->length, v2->length); + +        double dot = 0; + +        for (int i = 0; i < shortest; i++) +                dot += v1->raw[i] * v2->raw[i]; + +        return dot; +} @@ -0,0 +1,30 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <xcb/xcb.h> +#include <inttypes.h> + +int main(int argc, char **argv) +{ +        int i, screen_num; +        xcb_connection_t *connection = xcb_connect(NULL, &screen_num); + +        const xcb_setup_t *setup = xcb_get_setup(connection); +        xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup); + +        for (i = 0; i < screen_num; i++) { +                xcb_screen_next(&iter); +        } + +        xcb_screen_t *screen = iter.data; + +        printf("\n"); +        printf("Information about screen %"PRIu32":\n", screen->root); +        printf("  width         : %"PRIu16"\n", screen->width_in_pixels); +        printf("  height        : %"PRIu16"\n", screen->height_in_pixels); +        printf("  white pixel   : %"PRIu32"\n", screen->white_pixel); +        printf("  black pixel   : %"PRIu32"\n", screen->black_pixel); + +        xcb_disconnect(connection); +        return 0; +}  | 
