Re: [Bug-wget] [PATCH] replaced read_whole_file() by getline()

2013-05-17 Thread Giuseppe Scrivano
Tim Rühsen tim.rueh...@gmx.de writes:

 My diff is not a format-patch one (got some git technical problems), but I 
 hope 
 you can apply it. Be careful with src/ChangeLog, I guess you have to delete a 
 few lines after patching.

 In addition to the first patch, I replaced some tab indents by spaces in 
 ftp.c 
 and ftp-ls.c.

I have done some small changes in the ChangeLog file and pushed this
version.  Thanks again for your contribution!

From 099d8ee3da3a6eea5635581ae517035165f400a5 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen tim.rueh...@gmx.de
Date: Thu, 9 May 2013 22:37:17 +0200
Subject: [PATCH] replaced read_whole_file() by getline()

---
 src/ChangeLog |  15 ++
 src/cookies.c |   9 +++-
 src/ftp-ls.c  | 164 --
 src/ftp.c | 142 +-
 src/init.c|   7 +--
 src/netrc.c   |  50 ++
 src/utils.c   |  50 --
 src/utils.h   |   1 -
 8 files changed, 170 insertions(+), 268 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 5ce9f25..fd037a1 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,18 @@
+2013-05-14  Tim Ruehsen  tim.rueh...@gmx.de
+
+   * cookies.c (cookie_jar_load): Replaced read_whole_file() by getline().
+   * init.c (run_wgetrc): Likewise.
+   * netrc.c (parse_netrc): Likewise.
+   * utils.c: Likewise.
+   * ftp.c (getftp): Likewise.
+   * ftp-ls.c (ftp_parse_unix_ls, ftp_parse_winnt_ls, ftp_parse_vms_ls): 
Likewise.
+   * ftp-ls.c (clean_line): Accept the string length as parameter.
+   * ftp-ls.c: Replaced indent tabs by spaces.
+   * ftp.c: Likewise.
+   * utils.c: Removed read_whole_file() definition.
+   * netrc.c: Removed read_whole_file() definition for STANDALONE.
+   * utils.h: Removed read_whole_file() declaration.
+
 2013-05-09  Tim Ruehsen  tim.rueh...@gmx.de
 
* utils.c (acceptable): use standard string functions instead of
diff --git a/src/cookies.c b/src/cookies.c
index 87cc554..4efda88 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -1129,7 +1129,9 @@ domain_port (const char *domain_b, const char *domain_e,
 void
 cookie_jar_load (struct cookie_jar *jar, const char *file)
 {
-  char *line;
+  char *line = NULL;
+  size_t bufsize = 0;
+
   FILE *fp = fopen (file, r);
   if (!fp)
 {
@@ -1137,9 +1139,10 @@ cookie_jar_load (struct cookie_jar *jar, const char 
*file)
  quote (file), strerror (errno));
   return;
 }
+
   cookies_now = time (NULL);
 
-  for (; ((line = read_whole_line (fp)) != NULL); xfree (line))
+  while (getline (line, bufsize, fp)  0)
 {
   struct cookie *cookie;
   char *p = line;
@@ -1233,6 +1236,8 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
 abort_cookie:
   delete_cookie (cookie);
 }
+
+  xfree(line);
   fclose (fp);
 }
 
diff --git a/src/ftp-ls.c b/src/ftp-ls.c
index 3056651..28e32c4 100644
--- a/src/ftp-ls.c
+++ b/src/ftp-ls.c
@@ -68,16 +68,17 @@ symperms (const char *s)
replaces all TAB character with SPACE. Returns the length of the
modified line. */
 static int
-clean_line(char *line)
+clean_line (char *line, int len)
 {
-  int len = strlen (line);
-  if (!len) return 0;
-  if (line[len - 1] == '\n')
+  if (len = 0) return 0;
+
+  while (len  0  (line[len - 1] == '\n' || line[len - 1] == '\r'))
 line[--len] = '\0';
+
   if (!len) return 0;
-  if (line[len - 1] == '\r')
-line[--len] = '\0';
+
   for ( ; *line ; line++ ) if (*line == '\t') *line = ' ';
+
   return len;
 }
 
@@ -102,8 +103,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   int hour, min, sec, ptype;
   struct tm timestruct, *tnow;
   time_t timenow;
+  size_t bufsize = 0;
 
-  char *line, *tok, *ptok;  /* tokenizer */
+  char *line = NULL, *tok, *ptok;  /* tokenizer */
   struct fileinfo *dir, *l, cur; /* list creation */
 
   fp = fopen (file, rb);
@@ -115,22 +117,16 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   dir = l = NULL;
 
   /* Line loop to end of file: */
-  while ((line = read_whole_line (fp)) != NULL)
+  while ((len = getline (line, bufsize, fp))  0)
 {
-  len = clean_line (line);
+  len = clean_line (line, len);
   /* Skip if total...  */
   if (!strncasecmp (line, total, 5))
-{
-  xfree (line);
-  continue;
-}
+continue;
   /* Get the first token (permissions).  */
   tok = strtok (line,  );
   if (!tok)
-{
-  xfree (line);
-  continue;
-}
+continue;
 
   cur.name = NULL;
   cur.linkto = NULL;
@@ -368,7 +364,6 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   DEBUGP ((Skipping.\n));
   xfree_null (cur.name);
   xfree_null (cur.linkto);
-  xfree (line);
   continue;
 }
 
@@ -416,10 +411,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   

Re: [Bug-wget] [PATCH] replaced read_whole_file() by getline()

2013-05-14 Thread Tim Rühsen
Sorry, forgot to switch my IDE to GNU style.

But now that I made all the requested changes to my working tree, how do I 
make a diff to some commit back in time or to upstream ? Especially with git 
format-patch ? Locally, I didn't create my own branch, so i am on master.
(I have to read a git book someday...)

Regards, Tim



Re: [Bug-wget] [PATCH] replaced read_whole_file() by getline()

2013-05-14 Thread Daniel Stenberg

On Tue, 14 May 2013, Tim Rühsen wrote:

But now that I made all the requested changes to my working tree, how do I 
make a diff to some commit back in time or to upstream ? Especially with git 
format-patch ? Locally, I didn't create my own branch, so i am on master. (I 
have to read a git book someday...)


'git commit --amend [files]'

... to update your (most recent) commit. Then you can git format-patch again 
and resend.


But yes, working on stuff like this in your own local branch is often a better 
idea if you ask me...


--

 / daniel.haxx.se

Re: [Bug-wget] [PATCH] replaced read_whole_file() by getline()

2013-05-14 Thread Tim Rühsen
Thank you and Angel for your answers.

Am Dienstag, 14. Mai 2013 schrieb Daniel Stenberg:
 On Tue, 14 May 2013, Tim Rühsen wrote:
 
  But now that I made all the requested changes to my working tree, how do I 
  make a diff to some commit back in time or to upstream ? Especially with 
git 
  format-patch ? Locally, I didn't create my own branch, so i am on master. 
(I 
  have to read a git book someday...)
 
 'git commit --amend [files]'
 
 ... to update your (most recent) commit. Then you can git format-patch again 
 and resend.
 
 But yes, working on stuff like this in your own local branch is often a 
better 
 idea if you ask me...

Maybe it is a good idea since (at least i hope so) with an own branch you can 
still 'git pull' without conflicts while having own commits in the other 
branch. Have to try that out.

I didn't know how to amend one of the earlier commits.

Than I encountered 'git branch -a':
* master
  remotes/origin/HEAD - origin/master
  remotes/origin/master
  remotes/origin/parallel-wget

This let me try out 'git diff remotes/origin/master master'.
I piped the output into a file and hand-removed the unrelevant changes.

Not very elegant but should work.

Regards, Tim



[Bug-wget] [PATCH] replaced read_whole_file() by getline()

2013-05-11 Thread Tim Rühsen
Replaced read_whole_file(), which needs one malloc/free per line, by getline() 
which reuses a growable buffer.

getline() is a GNU function (but Wget is a GNU tool, isn't it ? :-).
Since Wget compiles/links with gnulib, I don't see a problem here.

Regards, Tim

From eeffb62ba0a13c5c20ddd66492c7774e0f713237 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen tim.rueh...@gmx.de
Date: Thu, 9 May 2013 22:37:17 +0200
Subject: [PATCH 2/3] replaced read_whole_file() by getline()

---
 src/ChangeLog |5 ++
 src/cookies.c |9 +++-
 src/ftp-ls.c  |  148 +++--
 src/ftp.c |   16 ---
 src/init.c|7 +--
 src/netrc.c   |   50 ++-
 src/utils.c   |   50 ---
 src/utils.h   |1 -
 8 files changed, 89 insertions(+), 197 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 84a9645..3b6733e 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,10 @@
 2013-05-09  Tim Ruehsen  tim.rueh...@gmx.de
 
+	* cookies.c, ftp-ls.c, ftp.c, init.c, netrc.c, utils.c, utils.h:
+	replaced read_whole_file() by getline().
+
+2013-05-09  Tim Ruehsen  tim.rueh...@gmx.de
+
 	* utils.c: use standard string functions instead of self-written
 	code in acceptable(), match_tail(), suffix(), has_wildcards_p().
 	Avoid some warnings in test code.
diff --git a/src/cookies.c b/src/cookies.c
index 87cc554..4efda88 100644
--- a/src/cookies.c
+++ b/src/cookies.c
@@ -1129,7 +1129,9 @@ domain_port (const char *domain_b, const char *domain_e,
 void
 cookie_jar_load (struct cookie_jar *jar, const char *file)
 {
-  char *line;
+  char *line = NULL;
+  size_t bufsize = 0;
+
   FILE *fp = fopen (file, r);
   if (!fp)
 {
@@ -1137,9 +1139,10 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
  quote (file), strerror (errno));
   return;
 }
+
   cookies_now = time (NULL);
 
-  for (; ((line = read_whole_line (fp)) != NULL); xfree (line))
+  while (getline (line, bufsize, fp)  0)
 {
   struct cookie *cookie;
   char *p = line;
@@ -1233,6 +1236,8 @@ cookie_jar_load (struct cookie_jar *jar, const char *file)
 abort_cookie:
   delete_cookie (cookie);
 }
+
+  xfree(line);
   fclose (fp);
 }
 
diff --git a/src/ftp-ls.c b/src/ftp-ls.c
index 3056651..401ae77 100644
--- a/src/ftp-ls.c
+++ b/src/ftp-ls.c
@@ -68,16 +68,17 @@ symperms (const char *s)
replaces all TAB character with SPACE. Returns the length of the
modified line. */
 static int
-clean_line(char *line)
+clean_line(char *line, int len)
 {
-  int len = strlen (line);
-  if (!len) return 0;
-  if (line[len - 1] == '\n')
+  if (len = 0) return 0;
+
+  while (len  0  (line[len - 1] == '\n' || line[len - 1] == '\r'))
 line[--len] = '\0';
+
   if (!len) return 0;
-  if (line[len - 1] == '\r')
-line[--len] = '\0';
+
   for ( ; *line ; line++ ) if (*line == '\t') *line = ' ';
+
   return len;
 }
 
@@ -102,8 +103,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   int hour, min, sec, ptype;
   struct tm timestruct, *tnow;
   time_t timenow;
+  size_t bufsize = 0;
 
-  char *line, *tok, *ptok;  /* tokenizer */
+  char *line = NULL, *tok, *ptok;  /* tokenizer */
   struct fileinfo *dir, *l, cur; /* list creation */
 
   fp = fopen (file, rb);
@@ -115,22 +117,16 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   dir = l = NULL;
 
   /* Line loop to end of file: */
-  while ((line = read_whole_line (fp)) != NULL)
+  while ((len = getline (line, bufsize, fp))  0)
 {
-  len = clean_line (line);
+  len = clean_line (line, len);
   /* Skip if total...  */
   if (!strncasecmp (line, total, 5))
-{
-  xfree (line);
   continue;
-}
   /* Get the first token (permissions).  */
   tok = strtok (line,  );
   if (!tok)
-{
-  xfree (line);
   continue;
-}
 
   cur.name = NULL;
   cur.linkto = NULL;
@@ -368,7 +364,6 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   DEBUGP ((Skipping.\n));
   xfree_null (cur.name);
   xfree_null (cur.linkto);
-  xfree (line);
   continue;
 }
 
@@ -416,10 +411,9 @@ ftp_parse_unix_ls (const char *file, int ignore_perms)
   timestruct.tm_isdst = -1;
   l-tstamp = mktime (timestruct); /* store the time-stamp */
   l-ptype = ptype;
-
-  xfree (line);
 }
 
+  xfree (line);
   fclose (fp);
   return dir;
 }
@@ -431,9 +425,10 @@ ftp_parse_winnt_ls (const char *file)
   int len;
   int year, month, day; /* for time analysis */
   int hour, min;
+  size_t bufsize = 0;
   struct tm timestruct;
 
-  char *line, *tok; /* tokenizer */
+  char *line = NULL, *tok; /* tokenizer */
   char *filename;
   struct fileinfo *dir, *l, cur; /* list creation */
 
@@ -446,29 +441,29 @@ ftp_parse_winnt_ls (const char *file)
   dir = l = NULL;
 
   /* Line loop to end of file: */
-  while ((line =