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:

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.

This article has been dead for over six months. Start a new discussion instead.