Good afternoon.
Code:
/*
* **************************** how to run *******************************
* in the command line:
*
* 1) [Executable] -x [pattern]
* - Only words which don't match the pattern are going to be printed
* -x is the "except" parameter
*
* 2) [Executable] -x -n [pattern]
* Words that don't match the pattern are going to be printed numerically
*
* 3) [Executable] -n [pattern]
* The words that match the pattern are going to be printed numerically
* ***********************************************************************
*/
/* I didn't understand how those two while work
* argv[0] is the name of the executable, right?
* is (*++argv)[0] incrementing the pointer to argv[1] (-n or -x)
* and accessing the first value pointed ? (-)
*
* Due to that, I thought *++argv[0] was doing the same of (*++argv)[0] because that
* increments the pointer to point to the first value of the parameter argv[1] which
* is '-'. How does it point to 'n' or 'x' ?
*/
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
int getLine(char s[], int lim)
{
int i,c;
for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
{
s[i] = c;
}
if(c == '\n')
{
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int main(int argc, char* argv[])
{
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0;
while(--argc > 0 && (*++argv)[0] == '-')
while( (c = *++argv[0]) )
switch (c)
{
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
printf("find: illegal option %c\n", c);
argc = 0;
found = -1;
break;
}
if(argc != 1)
printf("Usage: find -x -n pattern\n");
else
while(getLine(line, MAXLINE) > 0)
{
lineno++;
if((strstr(line, *argv) != NULL) != except)
{
if(number)
printf("%ld:", lineno);
printf("%s", line);
found++;
}
}
return found;
}