Revision 15928a3d

View differences:

mainbox/Makefile
1
SRC=main.c tool.c query.c event.c
2
HDR=tool.h query.h event.h ../tooltron_mb.h
1
SRC=main.c tool.c query.c event.c util.c
2
HDR=tool.h query.h event.h util.h ../tooltron_mb.h
3 3

  
4 4
FLAGS=-O2 -g -Wall -I.. `pkg-config --cflags --libs libmodbus libcurl`
5 5

  
mainbox/query.c
1 1
#include "query.h"
2 2
#include "event.h"
3
#include "util.h"
4
#include <stdlib.h>
3 5
#include <stdio.h>
6
#include <string.h>
4 7
#include <curl/curl.h>
5 8

  
6 9
const char *server;
10
char *tooltron_password;
7 11

  
8 12
int query_init(const char *server_name) {
9 13
  CURLcode error_code;
14
  int len;
10 15

  
11 16
  server = server_name;
12 17

  
13 18
  error_code = curl_global_init(CURL_GLOBAL_SSL);
14
  if (error_code)
19
  if (error_code) {
15 20
    fprintf(stderr, "curl_global_init: %s\n", curl_easy_strerror(error_code));
21
    return error_code;
22
  }
16 23

  
17
  return error_code;
24
  tooltron_password = read_file("tooltron_password");
25
  if (!tooltron_password)
26
    return 1;
27
  len = strlen(tooltron_password);
28
  while (len > 0 && tooltron_password[len-1] == '\n')
29
    tooltron_password[--len] = '\0';
30

  
31
  return 0;
18 32
}
19 33

  
20 34
void query_cleanup() {
21 35
  curl_global_cleanup();
36
  if (tooltron_password)
37
    free(tooltron_password);
22 38
}
23 39

  
24 40
static size_t write_bool(void *buffer, size_t size, size_t nmemb, void *userp) {
......
110 126
    return 1;
111 127

  
112 128
  curl_formadd(&formpost, &lastptr,
129
      CURLFORM_COPYNAME, "username",
130
      CURLFORM_COPYCONTENTS, "tooltron",
131
      CURLFORM_END);
132

  
133
  curl_formadd(&formpost, &lastptr,
134
      CURLFORM_COPYNAME, "password",
135
      CURLFORM_COPYCONTENTS, tooltron_password,
136
      CURLFORM_END);
137

  
138
  curl_formadd(&formpost, &lastptr,
113 139
      CURLFORM_COPYNAME, "type",
114 140
      CURLFORM_COPYCONTENTS, "usage",
115 141
      CURLFORM_END);
mainbox/util.c
1
#include "util.h"
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <unistd.h>
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
#include <fcntl.h>
8

  
9
char *read_file(const char *filename) {
10
  int fd, len, size, nread;
11
  char *str;
12
  
13
  fd = open(filename, O_RDONLY);
14
  if (fd < 0) {
15
    perror("open");
16
    return NULL;
17
  }
18

  
19
  len = 0;
20
  size = 64;
21
  str = malloc(size);
22
  if (!str) {
23
    close(fd);
24
    return NULL;
25
  }
26

  
27
  while (1) {
28
    nread = read(fd, str+len, size-len);
29
    len += nread;
30
    if (len == size) {
31
      size *= 2;
32
      str = realloc(str, size);
33
      if (!str)
34
        return NULL;
35
    }
36
    if (nread == 0) {
37
      str[len] = '\0';
38
      close(fd);
39
      return str;
40
    } else if (nread < 0) {
41
      perror("read");
42
      free(str);
43
      close(fd);
44
      return NULL;
45
    }
46
  }
47
}
mainbox/util.h
1
#ifndef UTIL_H
2
#define UTIL_H
3

  
4
/* Reads in an entire file. Returns NULL on error, or a malloc'd pointer to a
5
 * string which should later be freed. */
6
char *read_file(const char *filename);
7

  
8
#endif

Also available in: Unified diff