diff -r 3e2d7841482d doc/ChangeLog
--- a/doc/ChangeLog	Wed Sep 10 08:42:42 2008 -0700
+++ b/doc/ChangeLog	Thu Oct 02 10:14:29 2008 -0400
@@ -1,3 +1,7 @@ 2008-09-10  Michael Kessler  <kessler.mi
+2008-10-02  Debarshi Ray  <rishi@gnu.org>
+
+	* wget.texi <Download Options>: Document the --limit-file option.
+
 2008-09-10  Michael Kessler  <kessler.michael@aon.at>
 
 	* wget.texi (Robot Exclusion): Fixed typo "downloads" ->
diff -r 3e2d7841482d doc/wget.texi
--- a/doc/wget.texi	Wed Sep 10 08:42:42 2008 -0700
+++ b/doc/wget.texi	Thu Oct 02 10:14:29 2008 -0400
@@ -775,6 +775,19 @@ Of course, the remote server may choose 
 Of course, the remote server may choose to terminate the connection
 sooner than this option requires.  The default read timeout is 900
 seconds.
+
+@cindex file, limit
+@item --limit-file=@var{number}
+Limit the number of downloaded files to @var{number}.  For example,
+@samp{--limit-file=20} will only download 20 files.
+
+Note that this limit will never affect downloading a single file, or when
+when several @sc{url}s are specified on the command-line.  However, it is
+respected when retrieving either recursively, or from an input file.
+Thus you may type @samp{wget --limit-file=20 -i sites} -- download will
+be aborted when the limit is exceeded.
+
+Setting the limit to 0 or to @samp{inf} removes the upper bound.
 
 @cindex bandwidth, limit
 @cindex rate, limit
diff -r 3e2d7841482d src/ChangeLog
--- a/src/ChangeLog	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/ChangeLog	Thu Oct 02 10:14:29 2008 -0400
@@ -1,3 +1,11 @@ 2008-09-09  Gisle Vanem  <gvanem@broadpa
+2008-10-02  Debarshi Ray  <rishi@gnu.org>
+
+	* init.c, main.c, options.h: Added option --limit-file.
+	* ftp.c (ftp_retrieve_list, ftp_retrieve_dirs,
+	ftp_retrieve_glob): Abort download if limit_file is reached.
+	* recur.c (retrieve_tree): Likewise.
+	* retr.c (retrieve_from_file): Likewise.
+
 2008-09-09  Gisle Vanem  <gvanem@broadpark.no>
 
 	* url.c (url_error): Use aprintf, not asprintf.
diff -r 3e2d7841482d src/ftp.c
--- a/src/ftp.c	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/ftp.c	Thu Oct 02 10:14:29 2008 -0400
@@ -1435,6 +1435,11 @@ ftp_retrieve_list (struct url *u, struct
     {
       char *old_target, *ofile;
 
+      if (opt.limit_file && opt.numurls >= opt.limit_file)
+        {
+          --depth;
+          return QUOTEXC;
+        }
       if (opt.quota && total_downloaded_bytes > opt.quota)
         {
           --depth;
@@ -1643,6 +1648,8 @@ ftp_retrieve_dirs (struct url *u, struct
       int size;
       char *odir, *newdir;
 
+      if (opt.limit_file && opt.numurls >= opt.limit_file)
+        break;
       if (opt.quota && total_downloaded_bytes > opt.quota)
         break;
       if (f->type != FT_DIRECTORY)
@@ -1690,7 +1697,9 @@ Not descending to %s as it is excluded/n
       /* Set the time-stamp?  */
     }
 
-  if (opt.quota && total_downloaded_bytes > opt.quota)
+  if (opt.limit_file && opt.numurls >= opt.limit_file)
+    return QUOTEXC;
+  else if (opt.quota && total_downloaded_bytes > opt.quota)
     return QUOTEXC;
   else
     return RETROK;
@@ -1829,7 +1838,9 @@ ftp_retrieve_glob (struct url *u, ccon *
         }
     }
   freefileinfo (start);
-  if (opt.quota && total_downloaded_bytes > opt.quota)
+  if (opt.limit_file && opt.numurls >= opt.limit_file)
+    return QUOTEXC;
+  else if (opt.quota && total_downloaded_bytes > opt.quota)
     return QUOTEXC;
   else
     /* #### Should we return `res' here?  */
diff -r 3e2d7841482d src/init.c
--- a/src/init.c	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/init.c	Thu Oct 02 10:14:29 2008 -0400
@@ -183,6 +183,7 @@ static const struct {
 #endif
   { "input",            &opt.input_filename,    cmd_file },
   { "keepsessioncookies", &opt.keep_session_cookies, cmd_boolean },
+  { "limitfile",        &opt.limit_file,        cmd_number_inf },
   { "limitrate",        &opt.limit_rate,        cmd_bytes },
   { "loadcookies",      &opt.cookies_input,     cmd_file },
   { "logfile",          &opt.lfilename,         cmd_file },
diff -r 3e2d7841482d src/main.c
--- a/src/main.c	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/main.c	Thu Oct 02 10:14:29 2008 -0400
@@ -201,6 +201,7 @@ static struct cmdline_option option_data
     { "input-file", 'i', OPT_VALUE, "input", -1 },
     { "keep-session-cookies", 0, OPT_BOOLEAN, "keepsessioncookies", -1 },
     { "level", 'l', OPT_VALUE, "reclevel", -1 },
+    { "limit-file", 0, OPT_VALUE, "limitfile", -1 },
     { "limit-rate", 0, OPT_VALUE, "limitrate", -1 },
     { "load-cookies", 0, OPT_VALUE, "loadcookies", -1 },
     { "max-redirect", 0, OPT_VALUE, "maxredirect", -1 },
@@ -463,6 +464,8 @@ Download:\n"),
   -Q,  --quota=NUMBER            set retrieval quota to NUMBER.\n"),
     N_("\
        --bind-address=ADDRESS    bind to ADDRESS (hostname or IP) on local host.\n"),
+    N_("\
+       --limit-file=NUMBER       limit download files to NUMBER.\n"),
     N_("\
        --limit-rate=RATE         limit download rate to RATE.\n"),
     N_("\
diff -r 3e2d7841482d src/options.h
--- a/src/options.h	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/options.h	Thu Oct 02 10:14:29 2008 -0400
@@ -119,6 +119,8 @@ struct options
   double waitretry;		/* The wait period between retries. - HEH */
   bool use_robots;		/* Do we heed robots.txt? */
 
+  int limit_file;		/* Limit the number of files to be
+				   downloaded and stored. */
   wgint limit_rate;		/* Limit the download rate to this
 				   many bps. */
   SUM_SIZE_INT quota;		/* Maximum file size to download and
diff -r 3e2d7841482d src/recur.c
--- a/src/recur.c	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/recur.c	Thu Oct 02 10:14:29 2008 -0400
@@ -219,6 +219,8 @@ retrieve_tree (const char *start_url)
       bool is_css = false;
       bool dash_p_leaf_HTML = false;
 
+      if (opt.limit_file && opt.numurls >= opt.limit_file)
+        break;
       if (opt.quota && total_downloaded_bytes > opt.quota)
         break;
       if (status == FWRITEERR)
@@ -443,6 +445,8 @@ retrieve_tree (const char *start_url)
     url_free (start_url_parsed);
   string_set_free (blacklist);
 
+  if (opt.limit_file && opt.numurls >= opt.limit_file)
+    return QUOTEXC;
   if (opt.quota && total_downloaded_bytes > opt.quota)
     return QUOTEXC;
   else if (status == FWRITEERR)
diff -r 3e2d7841482d src/retr.c
--- a/src/retr.c	Wed Sep 10 08:42:42 2008 -0700
+++ b/src/retr.c	Thu Oct 02 10:14:29 2008 -0400
@@ -863,6 +863,11 @@ retrieve_from_file (const char *file, bo
       if (cur_url->ignore_when_downloading)
         continue;
 
+      if (opt.limit_file && opt.numurls >= opt.limit_file)
+        {
+          status = QUOTEXC;
+          break;
+        }
       if (opt.quota && total_downloaded_bytes > opt.quota)
         {
           status = QUOTEXC;
