Find the answer to your Linux question:
Results 1 to 2 of 2
Hello everyone, I would like to ask you about program which should work as cat command in linux. I know that first of all i have to use Code: ececl("/bin/cat","cat",argv[1],(char*)NULL) ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2012
    Posts
    1

    Write a program that works like cat


    Hello everyone,
    I would like to ask you about program which should work as cat command in linux. I know that first of all i have to use
    Code:
    ececl("/bin/cat","cat",argv[1],(char*)NULL)
    and also for
    Code:
    argv[2]
    and put this to the table and after that do "compile" function with those arguemnts, am I right ? but i have a problem with that:

    Code:
    #include <stdio.h>
    #include <fcntl.h>
    
    int main( int argc, const char* argv[] ) 
    {
        const char* tab1[512]=execl("/bin/cat","cat",argv[1],(char*)NULL);
        const char* tab2[512]=execl("/bin/cat","cat",argv[2],(char*)NULL);
        copyfile(tab1,tab2);
        return 1;
    }
    
    #define BLOCK 512
    
    copyfile(fd1,fd2)
    char *fd1,*fd2;
    {
        char buffer[BLOCK];
        int i=0;while(i<511){buffer[i]='0';i++}
        int n;
            while ((n = read(fd1, buffer, BLOCK)) > 0)
                write(fd2, buffer, n);
        printf("%s",buffer);
    }
    Last edited by just_jones; 12-08-2012 at 03:17 PM. Reason: change mistake in my code (increment i)

  2. #2
    Linux User
    Join Date
    Dec 2011
    Location
    Turtle Island West
    Posts
    344
    I dunno?

    What is your problem with that? Does it not compile properly?

    More info please. What exactly are you trying to accomplish? That copyfile() looks broken to me, as do the calls to execl(). I've been programming for 30 years and never used execl() in my recollection.

    If you're just trying to dump fd1 and fd2 to stdout, try

    fprintf ( stdout, "%s%s", fd1, fd2 );

    but, unfortunately, I don't think execl() works the way you think it does. Try the manpages: "man execl".

    Check out memset(buffer, 0, BLOCK) also. It's way prettier than
    int i=0;while(i<511){buffer[i]='0';i++} . Ugh.

    It looks like you're trying to read 2 files in and then dump them to the screen. If so, you're way off base. Try "man 2 open", "man 2 read", and "man 2 close". And if you want to see how cat really works, download the GNU coreutils and have a close look.

    Good luck, n00b, and don't give up. Look at the source code of stuff that does similar things to what you're trying to accomplish. There's stuff that's mostly broken, stuff that works, and there's stuff that _REALLY_ works. There are some very clever people out there. Avail yourself of them, their poetry (AKA code) speaks for itself.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •