Commit d4f39dcf9904c18f585ab6e2207146d2d86933d2
- Diff rendering mode:
- inline
- side by side
Makefile
(1 / 1)
|   | |||
| 1 | 1 | CC = gcc | |
| 2 | 2 | CFLAGS = -Wall -Wextra -std=c99 -pedantic | |
| 3 | 3 | LDFLAGS = -lcurl -lssl -lcrypto -lrt -lssl -lcrypto -ldl -lz -lz | |
| 4 | OBJECTS = main.o check_auth.o update_status.o get_friends_timeline.o write_preferences.o get_credentials.o access_url.o | ||
| 4 | OBJECTS = main.o check_auth.o update_status.o get_friends_timeline.o write_preferences.o get_credentials.o access_url.o url_encode.o | ||
| 5 | 5 | ||
| 6 | 6 | timeline: | |
| 7 | 7 | $(CC) $(CFLAGS) -o get_friends_timeline $(LDFLAGS) get_friends_timeline.c |
tests/url_encode_test.c
(11 / 0)
|   | |||
| 1 | #include <stdio.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include "../url_encode.h" | ||
| 4 | |||
| 5 | int main(void) { | ||
| 6 | char *test_thingy = "Hello, Senõrita."; | ||
| 7 | char *result = url_encode(test_thingy); | ||
| 8 | puts(result); | ||
| 9 | free(result); | ||
| 10 | return 0; | ||
| 11 | } |
update_status.c
(6 / 4)
|   | |||
| 4 | 4 | #include "update_status.h" | |
| 5 | 5 | #include "access_url.h" | |
| 6 | 6 | #include "get_credentials.h" | |
| 7 | #include "url_encode.h" | ||
| 7 | 8 | #include "constants.h" | |
| 8 | 9 | ||
| 9 | 10 | int update_status(char *message) { | |
| 10 | char *status_string; | ||
| 11 | char *status_prefix = STATUS_PREFIX; | ||
| 12 | status_string = (char*) calloc(strlen(message) + strlen(status_prefix), sizeof(char)); | ||
| 11 | const char *status_prefix = STATUS_PREFIX; | ||
| 12 | char *encoded = url_encode(message); | ||
| 13 | char *status_string = calloc(strlen(encoded) + strlen(status_prefix) + 1, sizeof(char)); | ||
| 13 | 14 | strncat(status_string, status_prefix, STATUS_PREFIX_LENGTH); | |
| 14 | strncat(status_string, message, 300); | ||
| 15 | strncat(status_string, encoded, 420 - STATUS_PREFIX_LENGTH); | ||
| 15 | 16 | char username_password[USER_PASSWORD_SIZE]; | |
| 16 | 17 | get_credentials(username_password, USER_PASSWORD_SIZE + 1); | |
| 17 | 18 | access_url(username_password, "http://twitter.com/statuses/update.xml", status_string, DO_POST); | |
| 18 | 19 | free(status_string); | |
| 20 | free (encoded); | ||
| 19 | 21 | printf("Updating status...\n"); | |
| 20 | 22 | ||
| 21 | 23 | return 0; |
url_encode.c
(50 / 0)
|   | |||
| 1 | #include <string.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include "url_encode.h" | ||
| 5 | |||
| 6 | /* Should be correct by RFC 3986, | ||
| 7 | * http://www.ietf.org/rfc/rfc3986.txt | ||
| 8 | * section 2.3 and 2.4. | ||
| 9 | */ | ||
| 10 | static int needs_replace(char c) { | ||
| 11 | if ( | ||
| 12 | ('a' <= c && c <= 'z') | ||
| 13 | || ('A' <= c && c <= 'z') | ||
| 14 | || ('0' <= c && c <= '9') | ||
| 15 | || ( c == '-') | ||
| 16 | || (c == '.') | ||
| 17 | || (c == '_') | ||
| 18 | || (c == '~') | ||
| 19 | ) { | ||
| 20 | return 0; | ||
| 21 | } | ||
| 22 | else { | ||
| 23 | return 1; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | static char zero_to_f(char c) { | ||
| 28 | const char *hex = "0123456789abcdef"; | ||
| 29 | return hex[c & 15]; /* Needed for safety */ | ||
| 30 | } | ||
| 31 | |||
| 32 | char *url_encode(char *not_replaced) { | ||
| 33 | char *replaced = malloc(strlen(not_replaced) * 3 + 1); | ||
| 34 | char *working_buf = replaced; | ||
| 35 | |||
| 36 | while (*not_replaced) { | ||
| 37 | if (needs_replace(*not_replaced)) { | ||
| 38 | *working_buf++ = '%'; | ||
| 39 | *working_buf++ = zero_to_f(*not_replaced >> 4); | ||
| 40 | *working_buf++ = zero_to_f(*not_replaced & 15); | ||
| 41 | } | ||
| 42 | else { | ||
| 43 | *working_buf++ = *not_replaced; | ||
| 44 | } | ||
| 45 | |||
| 46 | not_replaced++; | ||
| 47 | } | ||
| 48 | *working_buf++ = '\0'; | ||
| 49 | return replaced; | ||
| 50 | } |
url_encode.h
(4 / 0)
|   | |||
| 1 | #ifndef URL_ENCODE_h | ||
| 2 | #define URL_ENCODE_h | ||
| 3 | char *url_encode(char *); | ||
| 4 | #endif |

