adji

Adji's a Decisive and Joyful Internet browser
git clone https://noxz.tech/git/adji.git
Log | Files | Tags | LICENSE

commit: ce37bd73868a86e943e196e4daffa248401417c0
parent: 762f699595ad4d340d89434d0e2260e7329b7a2b
author: Chris Noxz <chris@noxz.tech>
date:   Wed, 24 Jan 2024 12:59:21 +0100
change how state is loaded
Mbrowser.c56+++++++++++++-------
1 file changed, 38 insertions(+), 18 deletions(-)
diff --git a/browser.c b/browser.c
@@ -106,7 +106,6 @@ about_scheme_request(WebKitURISchemeRequest    *request,
 	g_string_free(m, TRUE);
 }
 
-
 struct Client*
 client_create(const gchar                      *uri,
               WebKitWebView                    *related_wv,
@@ -696,6 +695,7 @@ key_entry(struct Client                        *c,
 		g_free(u);
 		return TRUE;
 	}
+
 	return FALSE;
 }
 
@@ -843,18 +843,18 @@ load_configuration(void)
 void
 load_state(void)
 {
-	struct Client          *c;
-	char                   *u = NULL;           /* line/uri holder */
-	size_t                  l = URI_MAX;        /* length */
+	char                    u[URI_MAX];         /* line/uri holder */
 	FILE                   *fp = NULL;          /* file pointer */
-	int                     x,                  /* state indicators */
-	                        i = 0;              /* current page index */
+	int                     c,                  /* character holder */
+	                        x,                  /* state indicators */
+	                        idx = 0;            /* current page index */
+	unsigned                i = 0u;
 
 	/* only load states for ipc host */
 	if (CFG_S(StateFile) == NULL || gl.ipc != IPC_HOST)
 		return;
 
-	if ((fp = fopen(CFG_S(StateFile), "r")) == NULL) {
+	if (!(fp = fopen(CFG_S(StateFile), "r"))) {
 		/* fail if file exists as it cannot be accessed, else remove state lock
 		 * to see if saving the state will create it (ie. the parent exists) */
 		if (access(CFG_S(StateFile), F_OK) == 0)
@@ -864,27 +864,47 @@ load_state(void)
 		return;
 	}
 
-	if (!(u = (char *)malloc(l * sizeof(char))))
-		die(__NAME__ ": fatal: malloc failed\n");
+	do {
+		c = fgetc(fp);
+
+		if (i == URI_MAX && !(c == '\n' || c == EOF)) { /* uri is too long */
+			i = 0u;
+			while ((c = fgetc(fp)) != '\n' && c != EOF);
+			if (c == EOF)
+				break;
+		}
+
+		if (c != '\n' && c != EOF) {
+			u[i++] = (char)c;
+			continue;   /* character is added to string, not more to do */
+		}
+
+		if (i == 0)
+			continue;   /* skip empty lines */
+
+		u[i] = '\0';
+		i = 0u;
 
-	/* load uris into new clients */
-	while (getline(&u, &l, fp) >= 0) {
 		if (sscanf(u, "%d:%s", &x, u) != 2)
 			continue;   /* incorrect format, so skip it */
-		c = client_create(u, NULL, TRUE, TRUE);
-		set_javascript_policy(c, (x & STATE_JAVASCRIPT) != 0);
-		i = x & STATE_CURRENT ? gl.clients - 1 : i;
-	}
+
+		set_javascript_policy(
+		    client_create(u, NULL, TRUE, TRUE),
+		    (x & STATE_JAVASCRIPT) != 0
+		);
+		idx = x & STATE_CURRENT ? gl.clients - 1 : idx;
+
+	} while (c != EOF);
+
+	fclose(fp);
 
 	/* select current tab if any */
 	if (gl.clients > 0)
-		gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.nb), i);
+		gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.nb), idx);
 
 	/* enable state save */
 	gl.state_lock = FALSE;
 
-	fclose(fp);
-	free(u);
 }
 
 void