Commit d4f39dcf9904c18f585ab6e2207146d2d86933d2

URL encoding seems to be working.
Makefile
(1 / 1)
  
11CC = gcc
22CFLAGS = -Wall -Wextra -std=c99 -pedantic
33LDFLAGS = -lcurl -lssl -lcrypto -lrt -lssl -lcrypto -ldl -lz -lz
4OBJECTS = main.o check_auth.o update_status.o get_friends_timeline.o write_preferences.o get_credentials.o access_url.o
4OBJECTS = main.o check_auth.o update_status.o get_friends_timeline.o write_preferences.o get_credentials.o access_url.o url_encode.o
55
66timeline:
77 $(CC) $(CFLAGS) -o get_friends_timeline $(LDFLAGS) get_friends_timeline.c
  
1#include <stdio.h>
2#include <stdlib.h>
3#include "../url_encode.h"
4
5int 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}
  
44#include "update_status.h"
55#include "access_url.h"
66#include "get_credentials.h"
7#include "url_encode.h"
78#include "constants.h"
89
910int 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));
1314 strncat(status_string, status_prefix, STATUS_PREFIX_LENGTH);
14 strncat(status_string, message, 300);
15 strncat(status_string, encoded, 420 - STATUS_PREFIX_LENGTH);
1516 char username_password[USER_PASSWORD_SIZE];
1617 get_credentials(username_password, USER_PASSWORD_SIZE + 1);
1718 access_url(username_password, "http://twitter.com/statuses/update.xml", status_string, DO_POST);
1819 free(status_string);
20 free (encoded);
1921 printf("Updating status...\n");
2022
2123 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 */
10static 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
27static char zero_to_f(char c) {
28 const char *hex = "0123456789abcdef";
29 return hex[c & 15]; /* Needed for safety */
30}
31
32char *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}
  
1#ifndef URL_ENCODE_h
2#define URL_ENCODE_h
3char *url_encode(char *);
4#endif