// pj1.h  - Princeton CS 318 2001 fall.
// Project 1 header file
//         By 
//    Brian J. Lai
// Starting: Sep 13, 2001

///////////////////////////////////////////////////////////////////////////
#ifndef INC_PJ1_H
#define INC_PJ1_H

#include <stdlib.h>
#include <unistd.h>

///////////////////////////////////////////////////////////////////////////

#define FILE_DIR    0
#define FILE_NORMAL 1
#define FILE_OTHER  2


#define FILE_INFO_ERR_NOT_EXIST     -1
#define FILE_INFO_ERR_ACCESS_DENIED -2
#define FILE_INFO_ERR_UNKNOWN       -3
    
#define os_assert(s) \
if(!(s)) {\
  fprintf(stderr, "%s->%s->%s\n", __FILE__, __FUNCTION__, __LINE__);\
  exit(1); \
}

///////////////////////////////////////////////////////////////////////////

typedef unsigned char  u8_t;
typedef unsigned short u16_t;
typedef unsigned long  u32_t;

typedef struct sDirEntry{
  char  fileName[1024];
  u8_t  fileType;
  u32_t lastModifyTime;
  u32_t fileSize;  
} SDirEntry; //struct sDirEntry

///////////////////////////////////////////////////////////////////////////

// set up my web server, portNum is the port that you want your server to 
// listen on. If you are not root, portNum should > 1024.
// 
// Return: file descriptor on success
//         -1 otherwise
int setup_server(int portNum);

///////////////////////////////////////////////////////////////////////////
// accept next incoming connection
//
// Return: file descriptor for the connection on success
//         -1 otherwise
int get_next_connection(int serverFD);

///////////////////////////////////////////////////////////////////////////
// get some basic information about a file
// is it a directory or a normal file? ( if not a file or a directory, omit it)
// 
// This function might be useful when the client asks for a specific file
//
// Return: 0 on success, with entry containing the inforamtion for that file
//         FILE_INFO_ERR_NOT_EXIST if the file doesn't exist
//         FILE_INFO_ERR_ACCESS_DENIED if you don't have access to it
//         FILE_INFO_ERR_UNKNOWN   if the error is unidentified

int get_file_info(const char *fileName, SDirEntry *entry);

///////////////////////////////////////////////////////////////////////////
// If you need to list the content of a directory, then
// Following three functions might be helpful;
///////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// open a directory for reading
//
// Return: 0  on success, 
//         -1 otherwise
int open_directory(const char *dirName);

///////////////////////////////////////////////////////////////////////////
// read an entry from a directory
//
// Return: entry name on success
//         NULL otherwise
// Warning!!!!!!!!!!!!!!!!!
//   copy the return value to an char array if you
//   need to manipulate it.
char* read_an_entry(void);

///////////////////////////////////////////////////////////////////////////
// close the directory
//
// call me when you have read through the whole directory
void close_directory(void);

///////////////////////////////////////////////////////////////////////////


#endif // pj1.h
