1998-05-01 00:53:59 +04:00
|
|
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* routines to sort an array of file objects
|
|
|
|
* uses qsort alg
|
|
|
|
*
|
|
|
|
* Designed and implemented by Lou Montulli '94
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "mkutils.h"
|
|
|
|
#include "mkfsort.h"
|
|
|
|
#include "mkgeturl.h"
|
|
|
|
|
|
|
|
#ifdef PROFILE
|
|
|
|
#pragma profile on
|
|
|
|
#endif
|
|
|
|
|
|
|
|
PRIVATE int net_file_sort_method = SORT_BY_NAME;
|
|
|
|
|
|
|
|
/* print a text string in place of the NET_FileEntryInfo special_type int */
|
|
|
|
PUBLIC char *
|
|
|
|
NET_PrintFileType(int special_type)
|
|
|
|
{
|
|
|
|
switch(special_type)
|
|
|
|
{
|
|
|
|
case NET_FILE_TYPE:
|
|
|
|
return("FILE");
|
|
|
|
case NET_DIRECTORY:
|
|
|
|
return("DIRECTORY");
|
|
|
|
case NET_SYM_LINK:
|
|
|
|
return("SYMBOLIC-LINK");
|
|
|
|
case NET_SYM_LINK_TO_DIR:
|
|
|
|
return("SYM-DIRECTORY");
|
|
|
|
case NET_SYM_LINK_TO_FILE:
|
|
|
|
return("SYM-FILE");
|
|
|
|
default:
|
1998-05-19 04:54:09 +04:00
|
|
|
PR_ASSERT(0);
|
1998-05-01 00:53:59 +04:00
|
|
|
return("FILE");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE void
|
|
|
|
NET_SetFileSortMethod(int method)
|
|
|
|
{
|
|
|
|
net_file_sort_method = method;
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_PRIVATE void NET_FreeEntryInfoStruct(NET_FileEntryInfo *entry_info)
|
|
|
|
{
|
|
|
|
if(entry_info)
|
|
|
|
{
|
|
|
|
FREEIF(entry_info->filename);
|
|
|
|
/* free the struct */
|
1998-05-19 04:54:09 +04:00
|
|
|
PR_Free(entry_info);
|
1998-05-01 00:53:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_PRIVATE NET_FileEntryInfo * NET_CreateFileEntryInfoStruct (void)
|
|
|
|
{
|
1998-05-19 04:54:09 +04:00
|
|
|
NET_FileEntryInfo * new_entry = PR_NEW(NET_FileEntryInfo);
|
1998-05-01 00:53:59 +04:00
|
|
|
|
|
|
|
if(!new_entry)
|
|
|
|
return(NULL);
|
|
|
|
|
1998-05-19 04:54:09 +04:00
|
|
|
memset(new_entry, 0, sizeof(NET_FileEntryInfo));
|
1998-05-01 00:53:59 +04:00
|
|
|
|
|
|
|
new_entry->permissions = -1;
|
|
|
|
|
|
|
|
return(new_entry);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is used as a comparer function for the Qsort routine.
|
|
|
|
* It uses a function FE_FileSortMethod() to determine the
|
|
|
|
* field to sort on.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
PRIVATE int
|
1999-06-03 22:15:53 +04:00
|
|
|
NET_CompareFileEntryInfoStructs (const void *ent2, const void *ent1, void *unused)
|
1998-05-01 00:53:59 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
const NET_FileEntryInfo *entry1 = *(NET_FileEntryInfo **) ent1;
|
|
|
|
const NET_FileEntryInfo *entry2 = *(NET_FileEntryInfo **) ent2;
|
|
|
|
|
|
|
|
if(!entry1 || !entry2)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
switch(net_file_sort_method)
|
|
|
|
{
|
|
|
|
case SORT_BY_SIZE:
|
|
|
|
/* both equal or both 0 */
|
|
|
|
if(entry1->size == entry2->size)
|
1998-05-19 04:54:09 +04:00
|
|
|
return(PL_strcmp(entry2->filename, entry1->filename));
|
1998-05-01 00:53:59 +04:00
|
|
|
else
|
|
|
|
if(entry1->size > entry2->size)
|
|
|
|
return(-1);
|
|
|
|
else
|
|
|
|
return(1);
|
|
|
|
/* break; NOT NEEDED */
|
|
|
|
case SORT_BY_TYPE:
|
|
|
|
if(entry1->cinfo && entry1->cinfo->desc &&
|
|
|
|
entry2->cinfo && entry2->cinfo->desc)
|
|
|
|
{
|
1998-05-19 04:54:09 +04:00
|
|
|
status = PL_strcmp(entry1->cinfo->desc, entry2->cinfo->desc);
|
1998-05-01 00:53:59 +04:00
|
|
|
if(status)
|
|
|
|
return(status);
|
|
|
|
/* else fall to filename comparison */
|
|
|
|
}
|
1998-05-19 04:54:09 +04:00
|
|
|
return (PL_strcmp(entry2->filename, entry1->filename));
|
1998-05-01 00:53:59 +04:00
|
|
|
/* break; NOT NEEDED */
|
|
|
|
case SORT_BY_DATE:
|
|
|
|
if(entry1->date == entry2->date)
|
1998-05-19 04:54:09 +04:00
|
|
|
return(PL_strcmp(entry2->filename, entry1->filename));
|
1998-05-01 00:53:59 +04:00
|
|
|
else
|
|
|
|
if(entry1->size > entry2->size)
|
|
|
|
return(-1);
|
|
|
|
else
|
|
|
|
return(1);
|
|
|
|
/* break; NOT NEEDED */
|
|
|
|
case SORT_BY_NAME:
|
|
|
|
default:
|
1998-05-19 04:54:09 +04:00
|
|
|
return (PL_strcmp(entry2->filename, entry1->filename));
|
1998-05-01 00:53:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sort the files
|
|
|
|
*/
|
|
|
|
MODULE_PRIVATE void
|
|
|
|
NET_DoFileSort(SortStruct * sort_list)
|
|
|
|
{
|
|
|
|
NET_DoSort(sort_list, NET_CompareFileEntryInfoStructs);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PD_PUTS(s) \
|
|
|
|
do { \
|
|
|
|
if(status > -1) \
|
1998-05-19 04:54:09 +04:00
|
|
|
status = (*stream->put_block)(stream, s, PL_strlen(s)); \
|
1998-05-01 00:53:59 +04:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
PUBLIC int
|
|
|
|
NET_PrintDirectory(SortStruct **sort_base, NET_StreamClass * stream, char * path, URL_Struct *URL_s)
|
|
|
|
{
|
|
|
|
NET_FileEntryInfo * file_entry;
|
|
|
|
char out_buf[3096];
|
|
|
|
char *esc_path = NET_Escape(path, URL_PATH);
|
|
|
|
int i;
|
|
|
|
int status=0;
|
|
|
|
|
|
|
|
NET_DoFileSort(*sort_base);
|
|
|
|
|
|
|
|
/* emit 300: URL CRLF */
|
1998-05-19 04:54:09 +04:00
|
|
|
PL_strcpy(out_buf, "300: ");
|
1998-05-01 00:53:59 +04:00
|
|
|
PD_PUTS(out_buf);
|
|
|
|
PR_snprintf(out_buf, sizeof(out_buf), URL_s->address);
|
|
|
|
PD_PUTS(out_buf);
|
|
|
|
|
1998-05-19 04:54:09 +04:00
|
|
|
PL_strcpy(out_buf, CRLF);
|
1998-05-01 00:53:59 +04:00
|
|
|
PD_PUTS(out_buf);
|
|
|
|
|
|
|
|
/* emit 200: Filename Size Content-Type File-type Last-Modified */
|
1998-05-19 04:54:09 +04:00
|
|
|
PL_strcpy(out_buf, "200: Filename Content-Length Content-Type File-type Last-Modified"CRLF);
|
1998-05-01 00:53:59 +04:00
|
|
|
PD_PUTS(out_buf);
|
|
|
|
|
|
|
|
for(i=0; status > -1 && (file_entry = (NET_FileEntryInfo *)
|
|
|
|
NET_SortRetrieveNumber(*sort_base, i)) != 0;
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
char * esc_time = NET_Escape(ctime(&file_entry->date), URL_XALPHAS);
|
|
|
|
|
1998-05-19 04:54:09 +04:00
|
|
|
strtok(file_entry->filename, "/");
|
1998-05-01 00:53:59 +04:00
|
|
|
PR_snprintf(out_buf, sizeof(out_buf), "201: %s %ld %s %s %s"CRLF,
|
|
|
|
file_entry->filename,
|
|
|
|
file_entry->size,
|
|
|
|
file_entry->special_type == NET_DIRECTORY ? "application/http-index-format" :
|
|
|
|
file_entry->cinfo ? file_entry->cinfo->type : TEXT_PLAIN,
|
|
|
|
file_entry->special_type == NET_DIRECTORY ? "Directory" : "File",
|
|
|
|
esc_time);
|
|
|
|
|
1998-05-19 04:54:09 +04:00
|
|
|
PR_Free(esc_time);
|
1998-05-01 00:53:59 +04:00
|
|
|
|
|
|
|
PD_PUTS(out_buf);
|
|
|
|
|
|
|
|
NET_FreeEntryInfoStruct(file_entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
NET_SortFree(*sort_base);
|
|
|
|
*sort_base = 0;
|
|
|
|
|
1998-05-19 04:54:09 +04:00
|
|
|
PR_FREEIF(esc_path);
|
1998-05-01 00:53:59 +04:00
|
|
|
if(status < 0)
|
|
|
|
return(status);
|
|
|
|
return(MK_DATA_LOADED);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PROFILE
|
|
|
|
#pragma profile off
|
|
|
|
#endif
|
|
|
|
|