File: token.c

package info (click to toggle)
libgadu 1:1.12.1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,820 kB
  • ctags: 1,649
  • sloc: ansic: 21,459; perl: 370; makefile: 209; sh: 124
file content (152 lines) | stat: -rw-r--r-- 3,040 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 *  (C) Copyright 2001-2006 Wojtek Kaniewski <wojtekka@irc.pl>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License Version
 *  2.1 as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
 *  USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "libgadu.h"

#ifdef ASYNC

#ifdef _WIN32
#  include <winsock2.h>
#else
#  include <sys/select.h>
#  include <sys/wait.h>
#endif
#include <signal.h>
#include <errno.h>

#ifndef _WIN32
static void sigchld(int sig)
{
	wait(NULL);
	signal(SIGCHLD, sigchld);
}
#endif

#endif

static inline int
gg_mkstemp(char *path)
{
#if defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
	return (mkstemp(path) != -1);
#else
	return (strcmp(mktemp(path), "") != 0);
#endif
}

int main(void)
{
	struct gg_http *h;
	struct gg_token *t;
	char path[] = "token.XXXXXX";
	FILE *f;

	gg_debug_level = 255;

#ifndef ASYNC

	if (!(h = gg_token(0))) {
		printf("Błąd pobierania tokenu.\n");
		return 1;
	}

#else

#ifndef _WIN32
	signal(SIGCHLD, sigchld);
#endif

	if (!(h = gg_token(1)))
		return 1;

	while (1) {
		fd_set rd, wr, ex;

		FD_ZERO(&rd);
		FD_ZERO(&wr);
		FD_ZERO(&ex);

		if ((h->check & GG_CHECK_READ))
			FD_SET(h->fd, &rd);
		if ((h->check & GG_CHECK_WRITE))
			FD_SET(h->fd, &wr);
		FD_SET(h->fd, &ex);

		if (select(h->fd + 1, &rd, &wr, &ex, NULL) == -1 || FD_ISSET(h->fd, &ex)) {
			if (errno == EINTR)
				continue;
			gg_token_free(h);
			perror("select");
			return 1;
		}

		if (FD_ISSET(h->fd, &rd) || FD_ISSET(h->fd, &wr)) {
			if (gg_token_watch_fd(h) == -1) {
				gg_token_free(h);
				fprintf(stderr, "Błąd połączenia.\n");
				return 1;
			}
			if (h->state == GG_STATE_ERROR) {
				gg_token_free(h);
				fprintf(stderr, "Błąd pobierania tokenu.\n");
				return 1;
			}
			if (h->state == GG_STATE_DONE)
				break;

		}
	}

#endif

	t = h->data;

	if (!gg_mkstemp(path)) {
		printf("Błąd tworzenia pliku tymczasowego.\n");
		gg_token_free(h);
		return 1;
	}

	f = fopen(path, "w");

	if (f == NULL) {
		printf("Błąd otwierania pliku tymczasowego %s.\n", path);
		gg_token_free(h);
		return 1;
	}

	if (fwrite(h->body, h->body_size, 1, f) != 1) {
		printf("Błąd zapisu do pliku tymczasowego %s.\n", path);
		gg_token_free(h);
		fclose(f);
		unlink(path);
		return 1;
	}

	fclose(f);

	printf("id=%s\nwidth=%d\nheight=%d\nlength=%d\npath=%s\n", t->tokenid, t->width, t->height, t->length, path);

	gg_token_free(h);

	return 0;
}