Hello, i have done a script that is supposed to read urls from the SOURCE. When a user types in vg.no in stdin or read a file that contains vg.no it gets the last 5/6 topics from the newspaper. the problem here is that from pipe 0 to write pipe 1 somthing goes wrong and it doenst do the cut,grep or sed ? can any 1 help me out here ?

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>

#define SOURCE "http://startsiden.no/sport/nyheter/"
#define LESFRA 0 //read from
#define SKRIVTIL 1 //write to

#define STDIN 0
#define STDOUT 1
#define STDERR 2


int lesURL(char* URL); //url from stdinn

int main(int argc,char *argv[]){
    int antall;
       int omFil = open(argv[1], O_RDONLY);
    char buffer[256];

   
  if (omFil >= 0)
  { /* argument er fil */ //if file
     while ( (antall=read(omFil, buffer, 254)) ) {
         buffer[antall-1]=0;    
         lesURL(buffer);
     }
    
     return 0;
exit(0);

  }
  else
  { /* Les argumenter fra stdin if stdin */
      while ( (antall = read(0, buffer, 254)) )
      {
       buffer[antall-1]=0;
       lesURL(buffer);
      }
  }    

return 0;
exit(0);
}

int lesURL(char* URL) {
    pid_t nyPid[4];
    //int nyPid[4];
    int piper[4][3];
    
    /* lynx SOURCE*/
    pipe(piper[0]);
    nyPid[1] = fork();
    if (nyPid[1]==0) {
        dup2(piper[0][SKRIVTIL],STDOUT);
        execl( "/usr/bin/lynx", "lynx", "-dump", SOURCE, 0);

    }

    /* grep URL */
    pipe(piper[1]);
    nyPid[2]=fork();
    if(nyPid[2]==0) {
        dup2(piper[0][LESFRA], STDIN);
        dup2(piper[2][SKRIVTIL], STDOUT);
        execlp("grep", "grep", URL, 0);
    
    }

    /* cut -d '.' -f1 */
    pipe(piper[2]);
    nyPid[3] = fork();
    if (nyPid[3]==0) {
        dup2(piper[1][LESFRA],STDIN);
        dup2(piper[2][SKRIVTIL], STDOUT);
        execlp("cut", "cut","-d", ".", "-f1",0);

    }

/* sed -e s/\ //g */
/* pipe(piper[3]);*/
nyPid[4] = fork();
if (nyPid[4] == 0) {
dup2(piper[2][LESFRA], STDIN);
execlp("sed", "sed", "-e", "s/\\ //g", 0);
}

waitpid(nyPid[1], NULL, 0);
waitpid(nyPid[2], NULL, 0);
waitpid(nyPid[3], NULL, 0);
waitpid(nyPid[4], NULL, 0);

return (0);
}

:rolleyes:

Recommended Answers

All 7 Replies

lol i know i have a sh script on it allrady but i need it in c just so later i would use it for somthing else... but is it the cut command that is wrong or the pipes ? that is set wrong..

lol i know i have a sh script on it allrady but i need it in c just so later i would use it for somthing else... but is it the cut command that is wrong or the pipes ? that is set wrong..

the pipes are fine. it's the cut command.

suggestions`
?

execlp("cut", "cut","-d", "\"", "-f2",0);

still the same :( it doenst cut...

ok ok so no one really can tell me anything

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.