/* html-ls-server.c
*
* Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in
* whole or in part in accordance to the General Public License (GPL).
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*****************************************************************************/
/*** html-ls-server.c (multitasking) ***/
/*** ***/
/*** Multitask directory listing server (HTTP). ***/
/*****************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MY_PORT 9999
void PERROR(char* msg);
#define PERROR(msg) { perror(msg); abort(); }
#define MAXBUF 1024
char buffer[MAXBUF];
char *Host="127.0.0.1:9999";
#define MAXPATH 150
/*---------------------------------------------------------------------*/
/*--- strtrim - trim the spaces off the front and back of a string. ---*/
/*---------------------------------------------------------------------*/
char* strtrim(char *str)
{ int tail=strlen(str);
while ( str[tail] <= ' ' && tail > 0 )
tail--;
str[tail+1] = 0;
while ( *str <= ' ' && *str != 0 )
str++;
return str;
}
/*---------------------------------------------------------------------*/
/*--- panic - report error and die. ---*/
/*---------------------------------------------------------------------*/
void panic(char* msg, ...)
{ va_list ap;
va_start(ap, msg);
vprintf(msg, ap);
va_end(ap);
exit(0);
}
/*---------------------------------------------------------------------*/
/*--- dir_up - find the parent directory in a path. ---*/
/*---------------------------------------------------------------------*/
char* dir_up(char* dirpath)
{ static char Path[MAXPATH];
int len;
strcpy(Path, dirpath);
len = strlen(Path);
if ( len > 1 && Path[len-1] == '/' )
len--;
while ( Path[len-1] != '/' && len > 1 )
len--;
Path[len] = 0;
return Path;
}
#define MAXNAME 25
/*---------------------------------------------------------------------*/
/*--- DirListing - read the directory and output an HTML table ---*/
/*---------------------------------------------------------------------*/
void DirListing(FILE* FP, char* Path)
{ struct dirent *dirent;
struct stat info;
char Filename[MAXPATH];
DIR* dir = opendir(Path);
fprintf(FP, "Directory Lister"
"Linux Directory Server
"
"");
fprintf(FP, "Directory of %s\n", Path);
if ( dir == 0 )
{
fprintf(FP, "
%s", strerror(errno));
return;
}
while ( (dirent = readdir(dir)) != 0 )
{
if ( strcmp(Path, "/") == 0 )
sprintf(Filename, "/%s", dirent->d_name);
else
sprintf(Filename, "%s/%s", Path, dirent->d_name);
fprintf(FP, "");
if ( stat(Filename, &info) == 0 )
{
fprintf(FP, "| %s | ", dirent->d_name);
if ( S_ISDIR(info.st_mode) )
{
if ( strcmp(dirent->d_name, "..") == 0 )
fprintf(FP, "(parent) | ", Host, dir_up(Path));
else
fprintf(FP, "%s | ", Host, Filename, dirent->d_name);
fprintf(FP, "Directory | ");
}
else if ( S_ISREG(info.st_mode) )
{
fprintf(FP, "%s | ", Host, Filename, dirent->d_name);
fprintf(FP, "%d | ", info.st_size);
}
else if ( S_ISLNK(info.st_mode) )
fprintf(FP, "Link | ");
else if ( S_ISCHR(info.st_mode) )
fprintf(FP, "Character Device | ");
else if ( S_ISBLK(info.st_mode) )
fprintf(FP, "Block Device | ");
else if ( S_ISFIFO(info.st_mode) )
fprintf(FP, "FIFO | ");
else if ( S_ISSOCK(info.st_mode) )
fprintf(FP, "Socket | ");
else
fprintf(FP, "(unknown) | ");
fprintf(FP, "%s | ", strtrim(ctime(&info.st_ctime)));
}
else
perror(Path);
fprintf(FP, "
\n");
}
fprintf(FP, "