Browse Source

removed all warning and added the original xdg source file instead

master
Lennart Weller 6 years ago
parent
commit
7aec50621a
9 changed files with 255 additions and 128 deletions
  1. 2
    1
      src/CMakeLists.txt
  2. 2
    0
      src/api.c
  3. 4
    108
      src/config.c
  4. 10
    15
      src/gog.h
  5. 8
    0
      src/http.c
  6. 3
    3
      src/main.c
  7. 3
    1
      src/serialization.c
  8. 4
    0
      src/util.c
  9. 219
    0
      src/xdg-user-dir-lookup.c

+ 2
- 1
src/CMakeLists.txt View File

@@ -6,7 +6,7 @@ find_package(OAuth REQUIRED)
include_directories(${PROTOBUFC_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${LIBJSON_INCLUDE_DIRS} ${LIBOAUTH_INCLUDE_DIRS})
set(LIBS ${LIBS} ${PROTOBUFC_LIBRARIES} ${LIBJSON_LIBRARIES} ${LIBOAUTH_LIBRARIES} ${CURL_LIBRARIES})

add_definitions(-std=c99 -g -O0)
add_definitions(-g -O0)

set(GOG_SRC gog.h
main.c
@@ -15,6 +15,7 @@ set(GOG_SRC gog.h
util.c
serialization.c
config.c
xdg-user-dir-lookup.c
)
protoc(STATE_SRC state.proto)
protoc(CONFIG_SRC config.proto)

+ 2
- 0
src/api.c View File

@@ -1,3 +1,5 @@
#include <string.h>
#include <json/json.h>
#include "gog.h"

int gog_request_token(struct oauth_t *oauth) {

+ 4
- 108
src/config.c View File

@@ -1,3 +1,7 @@
#include <string.h>
#include <sys/stat.h>
#include <errno.h>

#include "gog.h"
#include "config.pb-c.h"

@@ -122,111 +126,3 @@ char *config_file_path() {

return config_file;
}
char *xdg_user_dir_lookup(char *type) {
FILE *file;
char *home_dir, *config_home, *config_file;
char buffer[512];
char *user_dir;
char *p, *d;
int len;
int relative;

home_dir = getenv("HOME");

if(home_dir == NULL)
return strdup("/tmp");

config_home = getenv("XDG_CONFIG_HOME");
if(config_home == NULL || config_home[0] == 0) {
config_file = malloc(strlen(home_dir) + strlen("/.config/user-dirs.dirs") + 1);
strcpy(config_file, home_dir);
strcat(config_file, "/.config/user-dirs.dirs");
}
else {
config_file = malloc(strlen(config_home) + strlen("/user-dirs.dirs") + 1);
strcpy(config_file, config_home);
strcat(config_file, "/user-dirs.dirs");
}

file = fopen(config_file, "r");
free(config_file);
if(file == NULL)
goto error;

user_dir = NULL;
while(fgets(buffer, sizeof(buffer), file)) {
/* Remove newline at end */
len = strlen(buffer);
if(len > 0 && buffer[len-1] == '\n')
buffer[len-1] = 0;

p = buffer;
while(*p == ' ' || *p == '\t')
p++;

if(strncmp(p, "XDG_", 4) != 0)
continue;
p += 4;
if(strncmp(p, type, strlen(type)) != 0)
continue;
p += strlen(type);
if(strncmp(p, "_DIR", 4) != 0)
continue;
p += 4;

while(*p == ' ' || *p == '\t')
p++;

if(*p != '=')
continue;
p++;

while(*p == ' ' || *p == '\t')
p++;

if(*p != '"')
continue;
p++;

relative = 0;
if(strncmp(p, "$HOME/", 6) == 0) {
p += 6;
relative = 1;
}
else if(*p != '/')
continue;

if(relative) {
user_dir = malloc(strlen(home_dir) + 1 + strlen(p) + 1);
strcpy(user_dir, home_dir);
strcat(user_dir, "/");
}
else {
user_dir = malloc(strlen(p) + 1);
*user_dir = 0;
}

d = user_dir + strlen(user_dir);
while(*p && *p != '"') {
if((*p == '\\') &&(*(p+1) != 0))
p++;
*d++ = *p++;
}
*d = 0;
}
fclose(file);

if(user_dir)
return user_dir;

error:
/* Special case desktop for historical compatibility */
if(strcmp(type, "DESKTOP") == 0) {
user_dir = malloc(strlen(home_dir) + strlen("/Desktop") + 1);
strcpy(user_dir, home_dir);
strcat(user_dir, "/Desktop");
return user_dir;
}
else
return strdup(home_dir);
}

+ 10
- 15
src/gog.h View File

@@ -1,16 +1,12 @@
#ifndef __GOG__
#define __GOG__

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<oauth.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include <stdlib.h>
#include <oauth.h>
#include <sys/types.h>

#include<curl/curl.h>
#include<json/json.h>
#include <curl/curl.h>
#include <json/json.h>

#define CONSUMER_KEY "1f444d14ea8ec776585524a33f6ecc1c413ed4a5"
#define CONSUMER_SECRET "20d175147f9db9a10fc0584aa128090217b9cf88"
@@ -215,7 +211,6 @@ size_t get_string(char *buf, size_t max_len);
* http://ubuntuforums.org/showthread.php?t=464934
*/
size_t get_password(char *buf, size_t max_len);

int file_exists(char *path);
void print_error(struct oauth_t *oauth);

@@ -228,14 +223,14 @@ int deserialize_file(char *file, struct download_t **out);
/* config.c */
int load_config();
int save_config();
/**
* \copyright Alexander Larsson <alexl@redhat.com>
*/
char *xdg_config_home();
char *xdg_user_dir_lookup(char *type);
char *config_dir_path();
char *config_file_path();
void create_config_dirs();
char *xdg_config_home();

/* xdg-user-dir-lookup.c */
char *xdg_user_dir_lookup_with_fallback(const char *type, const char *fallback);
char *xdg_user_dir_lookup(const char *type);

/* api.c */
/** \brief Downloads the gog api configuration containing all urls

+ 8
- 0
src/http.c View File

@@ -1,4 +1,12 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <curl/curl.h>
#include <json/json.h>

#include "gog.h"

size_t static write_callback(void *buffer, size_t size, size_t nmemb, void *userp) {
char **response_ptr = (char**)userp;
*response_ptr = strndup(buffer, (size_t)(size *nmemb));

+ 3
- 3
src/main.c View File

@@ -28,10 +28,10 @@ int main() {
int i;

printf("E-Mail: ");
get_string(&email, sizeof(email));
get_string(email, sizeof(email));

printf("Password: ");
get_password(&password, sizeof(password));
get_password(password, sizeof(password));

if(gog_login(oauth, email, password)) {
config.token = oauth->token;
@@ -45,7 +45,7 @@ int main() {
char title[512];

printf("Download: ");
get_string(&title, sizeof(title));
get_string(title, sizeof(title));

if(gog_game_details(oauth, title)) {
game = oauth->msg->game;

+ 3
- 1
src/serialization.c View File

@@ -1,6 +1,8 @@
#include <string.h>
#include <google/protobuf-c/protobuf-c.h>

#include "gog.h"
#include "state.pb-c.h"
#include <google/protobuf-c/protobuf-c.h>

int serialize_download(struct download_t *dl, void **out) {
DState dstate = DSTATE__INIT;

+ 4
- 0
src/util.c View File

@@ -3,6 +3,10 @@
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include <curl/curl.h>
#include <json/json.h>

struct message_t *setup_handler(struct oauth_t *oauth, char *reply) {
struct json_object *answer;

+ 219
- 0
src/xdg-user-dir-lookup.c View File

@@ -0,0 +1,219 @@
/*
This file is not licenced under the GPL like the rest of the code.
Its is under the MIT license, to encourage reuse by cut-and-paste.

Copyright (c) 2007 Red Hat, Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/**
* xdg_user_dir_lookup_with_fallback:
* @type: a string specifying the type of directory
* @fallback: value to use if the directory isn't specified by the user
* @returns: a newly allocated absolute pathname
*
* Looks up a XDG user directory of the specified type.
* Example of types are "DESKTOP" and "DOWNLOAD".
*
* In case the user hasn't specified any directory for the specified
* type the value returned is @fallback.
*
* The return value is newly allocated and must be freed with
* free(). The return value is never NULL if @fallback != NULL, unless
* out of memory.
**/
char *
xdg_user_dir_lookup_with_fallback (const char *type, const char *fallback)
{
FILE *file;
char *home_dir, *config_home, *config_file;
char buffer[512];
char *user_dir;
char *p, *d;
int len;
int relative;
home_dir = getenv ("HOME");

if (home_dir == NULL)
goto error;

config_home = getenv ("XDG_CONFIG_HOME");
if (config_home == NULL || config_home[0] == 0)
{
config_file = (char*) malloc (strlen (home_dir) + strlen ("/.config/user-dirs.dirs") + 1);
if (config_file == NULL)
goto error;

strcpy (config_file, home_dir);
strcat (config_file, "/.config/user-dirs.dirs");
}
else
{
config_file = (char*) malloc (strlen (config_home) + strlen ("/user-dirs.dirs") + 1);
if (config_file == NULL)
goto error;

strcpy (config_file, config_home);
strcat (config_file, "/user-dirs.dirs");
}

file = fopen (config_file, "r");
free (config_file);
if (file == NULL)
goto error;

user_dir = NULL;
while (fgets (buffer, sizeof (buffer), file))
{
/* Remove newline at end */
len = strlen (buffer);
if (len > 0 && buffer[len-1] == '\n')
buffer[len-1] = 0;
p = buffer;
while (*p == ' ' || *p == '\t')
p++;
if (strncmp (p, "XDG_", 4) != 0)
continue;
p += 4;
if (strncmp (p, type, strlen (type)) != 0)
continue;
p += strlen (type);
if (strncmp (p, "_DIR", 4) != 0)
continue;
p += 4;

while (*p == ' ' || *p == '\t')
p++;

if (*p != '=')
continue;
p++;
while (*p == ' ' || *p == '\t')
p++;

if (*p != '"')
continue;
p++;
relative = 0;
if (strncmp (p, "$HOME/", 6) == 0)
{
p += 6;
relative = 1;
}
else if (*p != '/')
continue;
if (relative)
{
user_dir = (char*) malloc (strlen (home_dir) + 1 + strlen (p) + 1);
if (user_dir == NULL)
goto error2;

strcpy (user_dir, home_dir);
strcat (user_dir, "/");
}
else
{
user_dir = (char*) malloc (strlen (p) + 1);
if (user_dir == NULL)
goto error2;

*user_dir = 0;
}
d = user_dir + strlen (user_dir);
while (*p && *p != '"')
{
if ((*p == '\\') && (*(p+1) != 0))
p++;
*d++ = *p++;
}
*d = 0;
}
error2:
fclose (file);

if (user_dir)
return user_dir;

error:
if (fallback)
return strdup (fallback);
return NULL;
}

/**
* xdg_user_dir_lookup:
* @type: a string specifying the type of directory
* @returns: a newly allocated absolute pathname
*
* Looks up a XDG user directory of the specified type.
* Example of types are "DESKTOP" and "DOWNLOAD".
*
* The return value is always != NULL (unless out of memory),
* and if a directory
* for the type is not specified by the user the default
* is the home directory. Except for DESKTOP which defaults
* to ~/Desktop.
*
* The return value is newly allocated and must be freed with
* free().
**/
char *
xdg_user_dir_lookup (const char *type)
{
char *dir, *home_dir, *user_dir;
dir = xdg_user_dir_lookup_with_fallback (type, NULL);
if (dir != NULL)
return dir;
home_dir = getenv ("HOME");
if (home_dir == NULL)
return strdup ("/tmp");
/* Special case desktop for historical compatibility */
if (strcmp (type, "DESKTOP") == 0)
{
user_dir = (char*) malloc (strlen (home_dir) + strlen ("/Desktop") + 1);
if (user_dir == NULL)
return NULL;

strcpy (user_dir, home_dir);
strcat (user_dir, "/Desktop");
return user_dir;
}
return strdup (home_dir);
}

/* vim: set ts=8 : */

Loading…
Cancel
Save