input to a function

Nesha Nicole Jones nnj20229 at uxa.cso.uiuc.edu
Fri May 31 05:11:16 AEST 1991


I wrote a program that acts like a grep only it greps the line you are
looking for and the line above it.  I would like to be able to do 
something like:
	rest "restart" /usr/bin/adm/sa/sar24 
                           or
	rest "restart" < /usr/bin/adm/sa/sar24


currently my program does the second of the two listed above.  If I use
file pointers and fopen will the program still work if I try to pipe input
into it?  below are the two functions I currently use to get the line
and check to see if the string is in the line.

int getline(char s[], int lim) /* reads in the line  from the file/stdin */
{
        int c,i;

        i = 0;
        while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
                s[i++] = c;
        if (c == '\n')
                s[i++] = c;
        s[i] = '\0';
        return(i);
}
/* if the following function returns a value greater than 0 the it writes the 
line  that was rad in getline to  to the  screen */

int index(char s[], char t[])
{
        int i,j,k;

        for (i = 0; s[i] != '\0'; i++) {
                for (j = i, k = 0; t[k] !='\0' && s[j] == t[k]; j++,k++)
                        ;
                if (t[k] == '\0')
                        return(i);
        }
        return(-1);
}



More information about the Comp.lang.c mailing list