I noticed the lack of an option in wc to print the number of lines in a
file. As you know, the -l option counts the number of LF characters, which
is not the same as the number of lines if the file ends in the middle of a
line (the last character is not an LF).

I wrote a patch which adds such an option. Use it if you like. I have
patched against wc in textutils-2.1.

// Ulf Harnhammar
   [EMAIL PROTECTED]
--- wc.c.old    Wed Jan 15 22:34:23 2003
+++ wc.c        Wed Jan 15 23:35:33 2003
@@ -86,13 +86,14 @@
 /* Cumulative number of lines, words, chars and bytes in all files so far.
    max_line_length is the maximum over all files processed so far.  */
 static uintmax_t total_lines;
+static uintmax_t total_reallines;
 static uintmax_t total_words;
 static uintmax_t total_chars;
 static uintmax_t total_bytes;
 static uintmax_t max_line_length;
 
 /* Which counts to print. */
-static int print_lines, print_words, print_chars, print_bytes;
+static int print_lines, print_reallines, print_words, print_chars, print_bytes;
 static int print_linelength;
 
 /* Nonzero if we have ever read the standard input. */
@@ -110,6 +111,7 @@
   {"bytes", no_argument, NULL, 'c'},
   {"chars", no_argument, NULL, 'm'},
   {"lines", no_argument, NULL, 'l'},
+  {"reallines", no_argument, NULL, 'i'},
   {"words", no_argument, NULL, 'w'},
   {"max-line-length", no_argument, NULL, 'L'},
   {GETOPT_HELP_OPTION_DECL},
@@ -138,6 +140,7 @@
   -l, --lines            print the newline counts\n\
 "), stdout);
       fputs (_("\
+  -i, --reallines        print the line counts\n\
   -L, --max-line-length  print the length of the longest line\n\
   -w, --words            print the word counts\n\
 "), stdout);
@@ -150,6 +153,7 @@
 
 static void
 write_counts (uintmax_t lines,
+             uintmax_t reallines,
              uintmax_t words,
              uintmax_t chars,
              uintmax_t bytes,
@@ -166,6 +170,11 @@
       printf (format_int, human_readable (lines, buf, 1, 1));
       space = " ";
     }
+  if (print_reallines)
+    {
+      printf (format_int, human_readable (reallines, buf, 1, 1));
+      space = " ";
+    }
   if (print_words)
     {
       printf (format_sp_int, space, human_readable (words, buf, 1, 1));
@@ -195,10 +204,10 @@
 {
   char buf[BUFFER_SIZE + 1];
   ssize_t bytes_read;
-  uintmax_t lines, words, chars, bytes, linelength;
+  uintmax_t lines, reallines, words, chars, bytes, linelength;
   int count_bytes, count_chars, count_complicated;
 
-  lines = words = chars = bytes = linelength = 0;
+  lines = reallines = words = chars = bytes = linelength = 0;
 
   /* If in the current locale, chars are equivalent to bytes, we prefer
      counting bytes, because that's easier.  */
@@ -229,7 +238,7 @@
      `(dd ibs=99k skip=1 count=0; ./wc -c) < /etc/group'
      should make wc report `0' bytes.  */
 
-  if (count_bytes && !count_chars && !print_lines && !count_complicated)
+  if (count_bytes && !count_chars && !print_lines && !print_reallines && 
+!count_complicated)
     {
       off_t current_pos, end_pos;
       struct stat stats;
@@ -260,6 +269,8 @@
     {
       /* Use a separate loop when counting only lines or lines and bytes --
         but not chars or words.  */
+      char end_of_line = 1;
+
       while ((bytes_read = safe_read (fd, buf, BUFFER_SIZE)) > 0)
        {
          register char *p = buf;
@@ -270,12 +281,16 @@
              ++lines;
            }
          bytes += bytes_read;
+         end_of_line = (buf[bytes_read - 1] == '\n');
        }
       if (bytes_read < 0)
        {
          error (0, errno, "%s", file);
          exit_status = 1;
        }
+      reallines = lines;
+      if (!end_of_line)
+        reallines++;
     }
 #if HAVE_MBRTOWC && (MB_LEN_MAX > 1)
 # define SUPPORT_OLD_MBRTOWC 1
@@ -415,6 +430,10 @@
        linelength = linepos;
       if (in_word)
        words++;
+
+      reallines = lines;
+      if (linepos != 0)
+        reallines++;
     }
 #endif
   else
@@ -476,13 +495,18 @@
        linelength = linepos;
       if (in_word)
        words++;
+
+      reallines = lines;
+      if (linepos != 0)
+        reallines++;
     }
 
   if (count_chars < print_chars)
     chars = bytes;
 
-  write_counts (lines, words, chars, bytes, linelength, file);
+  write_counts (lines, reallines, words, chars, bytes, linelength, file);
   total_lines += lines;
+  total_reallines += reallines;
   total_words += words;
   total_chars += chars;
   total_bytes += bytes;
@@ -531,10 +555,10 @@
 
   exit_status = 0;
   posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
-  print_lines = print_words = print_chars = print_bytes = print_linelength = 0;
-  total_lines = total_words = total_chars = total_bytes = max_line_length = 0;
+  print_lines = print_reallines = print_words = print_chars = print_bytes = 
+print_linelength = 0;
+  total_lines = total_reallines = total_words = total_chars = total_bytes = 
+max_line_length = 0;
 
-  while ((optc = getopt_long (argc, argv, "clLmw", longopts, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "cliLmw", longopts, NULL)) != -1)
     switch (optc)
       {
       case 0:
@@ -552,6 +576,10 @@
        print_lines = 1;
        break;
 
+      case 'i':
+       print_reallines = 1;
+       break;
+
       case 'w':
        print_words = 1;
        break;
@@ -568,7 +596,7 @@
        usage (1);
       }
 
-  if (print_lines + print_words + print_chars + print_bytes + print_linelength
+  if (print_lines + print_reallines + print_words + print_chars + print_bytes + 
+print_linelength
       == 0)
     print_lines = print_words = print_bytes = 1;
 
@@ -585,7 +613,7 @@
        wc_file (argv[optind]);
 
       if (nfiles > 1)
-       write_counts (total_lines, total_words, total_chars, total_bytes,
+       write_counts (total_lines, total_reallines, total_words, total_chars, 
+total_bytes,
                      max_line_length, _("total"));
     }
 
_______________________________________________
Bug-textutils mailing list
[EMAIL PROTECTED]
http://mail.gnu.org/mailman/listinfo/bug-textutils

Reply via email to