Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINECOUNT 15
static void manPrint ( char *buf );
static void addPage ( char *buf );
static void listPages ( void );
static void helpPage ( void );
static void collectGarbage ( FILE *file );
int main ( void )
{
char buffer[BUFSIZ];
printf ( "WindowsXP Man Pages\n\n$: " );
while ( fgets ( buffer, (int)sizeof buffer, stdin ) != NULL ) {
/* Clear the trailing newline and test the input */
buffer[strlen(buffer)-1] = '\0';
if ( buffer[0] == 'm' || buffer[0] == 'M' )
manPrint ( buffer );
else if ( buffer[0] == 'a' || buffer[0] == 'A' )
addPage ( buffer );
else if ( buffer[0] == 'l' || buffer[0] == 'L' )
listPages();
else if ( buffer[0] == 'h' || buffer[0] == 'H' )
helpPage();
else if ( buffer[0] == 'e' || buffer[0] == 'E' )
return EXIT_SUCCESS;
else
printf ( "Invalid input\n" );
printf ( "\n$: " );
}
return EXIT_SUCCESS;
}
void manPrint ( char *buf )
{
FILE *man,
*page;
int count = 0;
char lineBuf[BUFSIZ],
*temp;
if ( ( man = fopen ( "man.txt", "r" ) ) != NULL ) {
/* Eat the first user argument and get the next one */
temp = strtok ( buf, " " );
temp = strtok ( NULL, " " );
if ( temp != NULL ) {
/* Search for the item name in man. If found
** read the file path for that item and use it
** as the argument for opening page.
*/
while ( fscanf ( man, "%s", lineBuf ) == 1 ) {
if ( strcmp ( temp, lineBuf ) == 0 ) {
(void)fscanf ( man, "%s", lineBuf );
if ( ( page = fopen ( lineBuf, "r" ) ) != NULL ) {
while ( fgets ( lineBuf, (int)sizeof lineBuf, page ) != NULL ) {
printf ( "%s", lineBuf );
count++;
if ( count == LINECOUNT ) {
/* Wait for user input after
** a certain amount of lines.
** Added for smaller resolution screens
** that can't hold a full page.
*/
(void)getchar();
count = 0;
}
}
collectGarbage ( page );
}
else {
/* Handle open failure for page */
fprintf ( stderr, "ERROR %d: File open failure\n", errno );
return;
}
}
}
collectGarbage ( man );
}
else {
/* Handle NULL value temp */
fprintf ( stderr, "ERROR %d: Invalid data\n", errno );
return;
}
}
else {
/* Handle open failure for man */
fprintf ( stderr, "ERROR %d: File open failure\n", errno );
return;
}
}
void addPage ( char *buf )
{
FILE *man;
char *temp;
if ( ( man = fopen ( "man.txt", "a" ) ) != NULL ) {
fprintf ( man, "\n" );
/* Eat the first argument and get the next one */
temp = strtok ( buf, " " );
while ( ( temp = strtok ( NULL, " " ) ) != NULL )
/* Potential error, input is not checked for validity */
(void)fprintf ( man, "%s ", temp );
collectGarbage ( man );
}
else
fprintf ( stderr, "ERROR %d: File open failure\n", errno );
}
void listPages ( void )
{
FILE *man;
char tmpBuf[BUFSIZ];
if ( ( man = fopen ( "man.txt", "r" ) ) != NULL ) {
printf ( "Supported Page List\nType man <page> to view\n-----------------------\n" );
while ( fscanf ( man, "%s", tmpBuf ) == 1 ) {
printf ( "%s ", tmpBuf );
fscanf ( man, "%s", tmpBuf );
}
printf ( "\n" );
collectGarbage ( man );
}
else
fprintf ( stderr, "ERROR %d: File open failure\n", errno );
}
void helpPage ( void )
{
FILE *help;
char buf[BUFSIZ];
if ( ( help = fopen ( "help.txt", "r" ) ) != NULL ) {
while ( fgets ( buf, (int)sizeof buf, help ) != NULL )
printf ( "%s", buf );
printf ( "\n" );
collectGarbage ( help );
}
else
fprintf ( stderr, "ERROR %d: File open failure\n", errno );
}
void collectGarbage ( FILE *file )
{
if ( fclose ( file ) != 0 )
fprintf ( stderr, "ERROR %d: File close failure\n", errno );
}
-Prelude