Where can I find the program that runs the -wc command?

Hey guys, I was wondering. When I enter a command in the terminal -wcl for a word count, where is that program located in the kernel?

wc is not located in the kernel.

wc and similar shell builtins and other similar programs run in the shell, not the kernel.

Ah I see so where might I go to look at the code itself, any idea? Would it even be possible?

Hi Neo...

More to the point why is the OP interested in entering kernel space, (ring 0), at all?

@Circuits:
If you are running Linux you have two methods /dev/<device> for _direct_, (term used loosely), HW access and 'int 80' for assembly code...
Linux System Call Table

You mean the code for wc ?

The start to the answer for that question depends on the operation system you are using / interested in.

Yes wc, I am using Ubuntu 18.04.

For Ubuntu, you can read the source code directly yourself.

For example, you can pull it down and search it using, like so:

apt-get source linux-source-3.2.0

or, you can go to the Ubuntu archive here:

http://archive.ubuntu.com/ubuntu/

Enjoy!

1 Like

Also, FYI, you can easily find various versions of the free and open source code for wc on the net.

Here is an example:

Appendix A Source of the wc command

The source file wc.c ... (one of many examples on the net)

/* Sample implementation of wc utility. */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

typedef unsigned long count_t;  /* Counter type */

/* Current file counters: chars, words, lines */
count_t ccount;
count_t wcount;
count_t lcount;

/* Totals counters: chars, words, lines */
count_t total_ccount = 0;
count_t total_wcount = 0;
count_t total_lcount = 0;

/* Print error message and exit with error status. If PERR is not 0,
   display current errno status. */
static void
error_print (int perr, char *fmt, va_list ap)
{
  vfprintf (stderr, fmt, ap);
  if (perr)
    perror (" ");
  else
    fprintf (stderr, "\n");
  exit (1);  
}

/* Print error message and exit with error status. */
static void
errf (char *fmt, ...)
{
  va_list ap;
  
  va_start (ap, fmt);
  error_print (0, fmt, ap);
  va_end (ap);
}

/* Print error message followed by errno status and exit
   with error code. */
static void
perrf (char *fmt, ...)
{
  va_list ap;
  
  va_start (ap, fmt);
  error_print (1, fmt, ap);
  va_end (ap);
}

/* Output counters for given file */
void
report (char *file, count_t ccount, count_t wcount, count_t lcount)
{
  printf ("%6lu %6lu %6lu %s\n", lcount, wcount, ccount, file);
}

/* Return true if C is a valid word constituent */
static int
isword (unsigned char c)
{
  return isalpha (c);
}

/* Increase character and, if necessary, line counters */
#define COUNT(c)       \
      ccount++;        \
      if ((c) == '\n') \
        lcount++;

/* Get next word from the input stream. Return 0 on end
   of file or error condition. Return 1 otherwise. */
int
getword (FILE *fp)
{
  int c;
  int word = 0;
  
  if (feof (fp))
    return 0;
      
  while ((c = getc (fp)) != EOF)
    {
      if (isword (c))
        {
          wcount++;
          break;
        }
      COUNT (c);
    }

  for (; c != EOF; c = getc (fp))
    {
      COUNT (c);
      if (!isword (c))
        break;
    }

  return c != EOF;
}
      
/* Process file FILE. */
void
counter (char *file)
{
  FILE *fp = fopen (file, "r");
  
  if (!fp)
    perrf ("cannot open file `%s'", file);

  ccount = wcount = lcount = 0;
  while (getword (fp))
    ;
  fclose (fp);

  report (file, ccount, wcount, lcount);
  total_ccount += ccount;
  total_wcount += wcount;
  total_lcount += lcount;
}
  
int
main (int argc, char **argv)
{
  int i;
  
  if (argc < 2)
    errf ("usage: wc FILE [FILE...]");
  
  for (i = 1; i < argc; i++)
    counter (argv);
  
  if (argc > 2)
    report ("total", total_ccount, total_wcount, total_lcount);
  return 0;
}