This leads to a segfault when we try to read line back in main (eg. "in strlen...") but I cannot see anything wrong:
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
int fdreadin (char *source, char *buffer, char end, int max) {
char *errmsg;
int FDIN=open(source,O_RDONLY);
if (FDIN<3) {
errmsg=strerror(errno);
buffer=malloc(strlen(errmsg+1));
strcpy(buffer,errmsg);
puts(buffer); // check buffer
return 0;
}
close(FDIN);
}
int main (int argc, char *argv[]) {
char *line;
fdreadin("asdfff", line, 0, 100);
puts("fault here"); //comes out
printf("%s\n",line); // doesn't
}
edit: Okay, I understand now -- I thought I did this "all the time" but evidently not quite. Why oh why can't I and at least there should be someone I can sue...
Is there a justification for this madness?