English 中文(简体)
为什么从主机工作中的“编目”功能中获取功能,总是能够适当恢复-1?
原标题:Why the getopt function from function "cat" inside main doesnt work properly always returning -1?

The function cat compiled on its own works just fine. I pass char *matrix[] instead of char *argv[] to it. I checked the matrix vector and arguments are stored corectly in it.
Where could the problem be? Thanks in advance! Here comes the code:

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

#define TRUE 0
#define FALSE 1

void yes(int argc, char *argv[]);
int cat(int argc, char *argv[]);

char buf[50],c[10], *p2,*p, *pch;
int count;
char *matrix[20];


int main(int argc, char *argv[])
{
    int t=0;
    do
    {
        fprintf (stderr, "$ ");
        fgets (buf,50,stdin);
        p=buf;
        fprintf (stderr, "Comanda primita de la tastatura: ");
        fputs (buf, stderr);
        int i=0,j=0;
        //strcpy(p,buf);
        strcpy(c,"");
        while (buf[i] ==    )
        {
            i++;
            p++;
        }
        if (buf[i] ==  # )
            fprintf (stderr, "Nici o comanda, ci e un comentariu!
");
        else
        {
            j=0;
            while (buf[i] !=     && buf[i] !=  
 )
            {
                i++;
                j++;
            }
            strncpy (c,p,j);
            fprintf (stderr, "%s
",c);
            if (strcmp (c,"yes") == 0)
            {
                p2 = p+j+1;
                pch = strtok (p2," ");
                count = 0;
                while (pch != NULL)
                {
                    //printf ("%s
",pch);
                    matrix[count] = strdup(pch);
                    pch = strtok (NULL, " ");
                    count++;
                }
                yes(count, matrix);
                fprintf (stderr, "Aici se va executa comanda yes
");
            }
            else if (strcmp (c,"cat") == 0)
            {
                p2 = p+j+1;
                pch = strtok (p2," ");
                count = 0;
                while (pch != NULL)
                {
                    //printf ("%s
",pch);
                    matrix[count] = strdup(pch);
                    pch = strtok (NULL, " ");
                    count++;
                }


                cat(count,matrix);
                for(t=0;t<count;t++) fprintf(stderr,"|%s|
",matrix[t]);
                printf("asdasd");
                fprintf (stderr, "Aici se va executa comanda cat 
");
            }
            else if (strcmp (c,"tee") == 0)
            {
                //tee();
                fprintf(stderr, "Aici se va executa comanda tee
");
            }


            fprintf (stderr, "Aici se va executa comanda basename
");

            strcpy(buf,"");
        }
    }
    while (strcmp(c, "exit") != 0);
    fprintf (stderr, "Terminat corect!
");
    return 0;
}
int cat(int argc, char *argv[])
{
    int c ;

    opterr = 0 ;
    optind = 0 ;

    char number = 0;
    char squeeze = 0;
    char marker = 0;


    while ((c = getopt (argc, argv, "bnsE:")) != -1)
        switch (c)
        {
        case  b  :
            number = 1;
            break;
        case  n  :
            number = 2;
            break;
        case  m  :
            marker = 1;
            break;
        case  s  :
            squeeze = 1;
            break;
        case  E  :
            marker = 1;
            break;
        }
    if (optind + 1 != argc)
    {
        fprintf (stderr, "	Wrong arguments!
") ;
        return -1 ;
    }
    char *filename = strtok(argv[optind], "
");
    FILE * fd = fopen (filename, "r");
    printf("am deschis fisierul %s ",argv[optind]);
    if (fd == NULL)
    {

        return 1;
    }

    char line[1025];
    int line_count = 1;

    while (!feof(fd))
    {
        fgets(line, 1025, fd);
        int len = strlen(line);
        if (line[len - 1] ==  
 )
        {
            if(len - 2 >= 0)
            {
                if(line[len - 2] ==  
 )
                {
                    line[len - 2] =   ;
                    len -= 2;
                }
                else
                {
                    line[len - 1] =   ;
                    len -= 1;
                }
            }
            else
            {
                line[len - 1] =   ;
                len -= 1;
            }
        }

        if (squeeze == 1 && len == 0)
            continue;
        if (number == 1)
        {
            fprintf (stdout, "%4d ", line_count);
            line_count++;
        }
        else if (number == 2)
        {
            if (len > 0)
            {
                fprintf (stdout, "%4d ", line_count);
                line_count++;
            }
            else
                fprintf (stdout, "     ");
        }
        fprintf(stdout, "%s", line);
        if (marker == 1)
            fprintf(stdout, "$");
        fprintf(stdout, "
");
    }

    fclose (fd);

    return 0 ;
}
void yes(int argc, char *argv[])
{
    int i;
    while (1)
        if (puts("y") == EOF)
        {
            perror("yes");
            exit(FALSE);
        }


    while (1)
        for (i = 1; i < argc; i++)
            if (fputs(argv[i], stdout) == EOF || putchar(i == argc - 1 ?  
  :    ) == EOF)
            {
                perror("yes");
                exit(FALSE);
            }
}
最佳回答

通常argv[0]载有方案名称。 Maybe getopt will skip items argv[0],并造成问题。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签