/* givepage.c - Read characters from standard input until eof. * Then read and write the index.html file */ #include #include #include #include #include #include char *buffer; /* buffer where the file is read into */ int bufsize; /* size of buffer = file size + 1 */ void filetobuffer(const char *filename) { int fid; /* descriptor for file to be sent to client */ struct stat filestat; /* stats for fid */ if ((fid = open(filename, O_RDONLY)) == -1) { fprintf(stderr, "Unable to open %s\n", filename); exit(1); } if (fstat(fid, &filestat) == -1) { fprintf(stderr, "Unable to stat %s\n", filename); exit(1); } bufsize = filestat.st_size; buffer = (char *)malloc((size_t)(1+bufsize)); if (buffer == 0) { perror("Out of memory"); exit(1); } if (read(fid, buffer, bufsize) != bufsize) { fprintf(stderr, "Cannot read all of %s\n", filename); exit(1); } buffer[bufsize] = 0; /* So we can use printf to write buffer out */ } int main(int argc, char *argv[]) { int c; /* read until EOF */ for ( ; ; ) if((c = getchar()) == EOF) break; /* Read file to buffer */ filetobuffer("index.html"); /* Write out */ printf("%s", buffer); return 0; }