Sophie

Sophie

distrib > Mageia > 6 > armv7hl > media > core-updates-src > by-pkgid > a93d147f7c4ed0b9fc9239cc1f532e95 > files > 4

links-2.20-1.mga6.src.rpm

diff -u links-2.15/cookies.c links-2.15/cookies.c.new
--- links-2.15/cookies.c	2017-11-20 02:45:24.000000000 +0100
+++ links-2.15/cookies.c.new	2018-05-03 13:08:21.066005385 +0200
@@ -201,17 +201,101 @@
 
 void free_cookies(void)
 {
+	struct cookie *c;
+	struct list_head *lc;
+	unsigned char *cookfile;
+	FILE *fp;
+
 	free_list(struct c_domain, c_domains);
-	/* !!! FIXME: save cookies */
+
+	cookfile = stracpy(links_home);
+	if (! cookfile) return;
+	add_to_strn(&cookfile, "cookies");
+
+	fp = fopen(cookfile, "w");
+	mem_free(cookfile);
+	if (fp == NULL) return;
+	
+	foreach (struct cookie, c, lc, all_cookies) {
+		if (c->expires && ! cookie_expired(c))
+			fprintf(fp, "%s %s %s %s %s %d %d\n", c->name, c->value,
+			    c->server?c->server:(unsigned char *)"", c->path?c->path:(unsigned char *)"",
+			    c->domain?c->domain:(unsigned char *)"", c->expires, c->secure);
+	}
 	while (!list_empty(all_cookies)) {
-		struct cookie *c = list_struct(all_cookies.next, struct cookie);
-		del_from_list(c);
-		free_cookie(c);
+		struct cookie *c1 = list_struct(all_cookies.next, struct cookie);
+		del_from_list(c1);
+		free_cookie(c1);
 	}
+
+	fclose(fp);
+	
+	free_list(struct cookie, all_cookies);
 }
 
 void init_cookies(void)
 {
-	/* !!! FIXME: read cookies */
+	unsigned char in_buffer[MAX_STR_LEN];
+	unsigned char *cookfile, *p, *q;
+	FILE *fp;
+
+	/* must be called after init_home */
+	if (! links_home) return;
+	
+	cookfile = stracpy(links_home);
+	if (! cookfile) return;
+	add_to_strn(&cookfile, "cookies");
+
+	fp = fopen(cookfile, "r");
+	mem_free(cookfile);
+	if (fp == NULL) return;
+	
+	while (fgets(in_buffer, MAX_STR_LEN, fp)) {
+		struct cookie *cookie;
+		
+		if (!(cookie = mem_alloc(sizeof(struct cookie)))) return;
+		memset(cookie, 0, sizeof(struct cookie));
+		
+		q = in_buffer; p = strchr(in_buffer, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->name = stracpy(q);
+		
+		q = p; p = strchr(p, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->value = stracpy(q);
+		
+		q = p; p = strchr(p, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->server = stracpy(q);
+		
+		q = p; p = strchr(p, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->path = stracpy(q);
+		
+		q = p; p = strchr(p, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->domain = stracpy(q);
+		
+		q = p; p = strchr(p, ' ');
+		if (p == NULL) goto inv;
+		*p++ = '\0';
+		cookie->expires = atoi(q);
+		
+		cookie->secure = atoi(p);
+		
+		accept_cookie(cookie);
+
+		continue;
+
+inv:
+		free_cookie(cookie);
+		free(cookie);
+	}
+	fclose(fp);
 }