/***********************************************************************
* Code listing from "Advanced Linux Programming," by CodeSourcery LLC  *
* Copyright (C) 2001 by New Riders Publishing                          *
* See COPYRIGHT for license information.                               *
***********************************************************************/

#include 
#include 
#include 
#include 
#include 
#include 

#include "server.h"

/* HTML source for the start of the page we generate.  */

static char* page_start =
  "\n"
  " \n"
  "  
\n";

/* HTML source for the end of the page we generate.  */

static char* page_end =
  "  
\n" " \n" "\n"; /* HTML source for the page indicating there was a problem opening /proc/issue. */ static char* error_page = "\n" " \n" "

Error: Could not open /proc/issue.

\n" " \n" "\n"; /* HTML source indicating an error. */ static char* error_message = "Error reading /proc/issue."; void module_generate (int fd) { int input_fd; struct stat file_info; int rval; /* Open /etc/issue. */ input_fd = open ("/etc/issue", O_RDONLY); if (input_fd == -1) system_error ("open"); /* Obtain file information about it. */ rval = fstat (input_fd, &file_info); if (rval == -1) /* Either we couldn't open the file or we couldn't read from it. */ write (fd, error_page, strlen (error_page)); else { int rval; off_t offset = 0; /* Write the start of the page. */ write (fd, page_start, strlen (page_start)); /* Copy from /proc/issue to the client socket. */ rval = sendfile (fd, input_fd, &offset, file_info.st_size); if (rval == -1) /* Something went wrong sending the contents of /proc/issue. Write an error message. */ write (fd, error_message, strlen (error_message)); /* End the page. */ write (fd, page_end, strlen (page_end)); } close (input_fd); }