trouble understanding file option and command line arguments

Hi,

I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves through program code. argc and argv look at one line at a time and they only look at what is on the command line. I am not sure how a program is broken up into files for WC to read.

this is the code I have so far:

/* wc simulate */

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

char *pgmname; /* name of this program */

int line_count = 0;
int word_count = 0;
int byte_count = 0;

FILE *fp;

void main(argc, argv)

int argc; char *argv[];
{


int i; 
char *cp;

pgmname = argv[0];
fp = stdin;

for(i = 1; i < argc; i++) {
        cp = argv;
        if(*cp == '-'){
                if(*++cp == '\n'){
                        line_count++;}
                
                
        }

        else {
                  if(fp != stdin) {
                   fprintf(stderr, "%s: too many arguments\n", pgmname);
                   exit(1);
                }

                fp = fopen(cp, "r")
                if(fp == NULL) {             
                   fprintf(stderr, "%s: unable to read %s\n", pgmname, cp);
                   exit(1);
                }

        }

                printf("%d\n", line_count);
}

                

}

Right now I have code to just count the lines. I am testing the program on a hello world program below:

#include <stdio.h>

main()
{
printf("Hello World!\n");
}

when I test the program, I get 0 for line count.

California State University, Northridge, USA, Prof Gabrovsky, Comp 322

I'm not entirely sure if i understand where your problem is. I suppose you don't understand (fully) the difference between "arguments" and "parameters" to a program. (If i am wrong in this assessment just point it out - sorry for the misunderstanding in this case.)

A commandline can consist of 2 fundamental parts: "arguments" and "parameters". Parameters change the way a program is working. Consider:

# ls
# ls -l

In the second line the same program is invoked (the "ls" command), but its output format has changed. Whereas before you saw only filenames you see a lot of information (size, modify date, owner, etc.) for each filename in the second case.

Opposed to parameters are arguments: they tell the program not how to work but what to work on - basically they are either data or locations where data can be found, like filenames, strings, etc..

Consider the lines:

# ls /some/file
# echo "Hello world"

The pathname "/some/file" does not change the way the "ls" command works, but what it is working on! Before it was working on the whole current working directory (this is the default for "ls"), then it was solely working on a file or directory named "/some/file" - but in the same default way it has worked on the currect working directory before, as the parameters have not changed.

It is possible to mix parameters and arguments, for instance:

# ls -l /some/path
# echo -n "Hello World."

But there arises a little problem: how should we distinguish the parameters from the arguments? Suppose there is a directory named "l", what should

# ls l

mean? "Parameter l" or "work on directory l"? This is why in Unix parameters (generally - there are some exceptions to this rule) are to be denominated by introducing them with a dash, just like the slash "/" does so in DOS/Windows.

Suppose now we would have multiple parameters to a command. We do not have to introduce every one of them with a dash (although this is allowed), we can combine them all together. The following two lines are absolutely identical:

# ls -lai
# ls -l -a -i

There is a system call which does the (standardized) parsing of the command line, btw.: getopts() In fact this is a command as well as a function and both work in the same way. You might want to read about getopts() in the man pages by entering

# man getopts

I hope this helps.

bakunin