fork, pipe exec and dub2

This code is supposed to print "Output from 'ls -l':" and append the result of 'ls -l', but it doesn't... Does anyone has a clue whats wrong with this?

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

void readStringFromFile (int file, char * readbuffer) {
    int nbytes = read(file, readbuffer, sizeof(readbuffer));
    readbuffer[nbytes] = 0;
}

int main(int argc, char const *argv[])
{
    int fd[2];
    pipe(fd);

    if (fork()==0)//child process
    {   
        close(fd[0]);
        dup2(fd[1],1);
        int retValue = execl("/bin/ls","ls","-l", NULL);
        printf("Exec failed: retValue = %d\n",retValue);
    } else
    {
        int status;
        close(fd[1]);
        wait(&status);
        char readbuffer[1024];
        readStringFromFile(fd[0],readbuffer);
        printf("Output from 'ls -l':\n %s", readbuffer);
    }
}

Answers


In your code the sizeof(readbuffer) is equal to 4 in the following snippet., so it reads 4 bytes max.

void readStringFromFile (int file, char * readbuffer) {
   int nbytes = read(file, readbuffer, sizeof(readbuffer));
   readbuffer[nbytes] = 0;
}

You can send the size of the buffer as another parameter, giving:

void readStringFromFile (int file, char * readbuffer, int maxsize) {
   int nbytes = read(file, readbuffer, maxsize);
   readbuffer[nbytes] = 0;
}

and invoke it with:

readStringFromFile(fd[0], readbuffer, sizeof(readbuffer));

Need Your Help

Excel 2007 VBA - Using Sheets.Add to create a chart at the end of a workbook

excel vba excel-vba ms-office office-2007

I'm trying to implement a private subroutine to add a Sheet to the active workbook (Sheet being used so that one can choose to add xlForms, xlWorksheet, etc).

Creating new workbook from the template that exist in the current workbook

vba excel-vba combobox userform excel

Here is what I have, need to be able to save any template called in ComboBox as separate workbook without any macro in the new one, but with preserving formulas with internal references, rest shoul...