home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / nsfast / root.9 / usr / ns-home / nsapi / include / base / file.h / file
Text File  |  1998-08-19  |  8KB  |  348 lines

  1. /*
  2.  * Copyright (c) 1994, 1995.  Netscape Communications Corporation.  All
  3.  * rights reserved.
  4.  * 
  5.  * Use of this software is governed by the terms of the license agreement for
  6.  * the Netscape FastTrack or Netscape Enterprise Server between the
  7.  * parties.
  8.  */
  9.  
  10.  
  11. /* ------------------------------------------------------------------------ */
  12.  
  13.  
  14. /*
  15.  * file.h: system specific functions for reading/writing files
  16.  * 
  17.  * Rob McCool
  18.  */
  19.  
  20.  
  21. #ifndef FILE_H
  22. #define FILE_H
  23.  
  24. #include "netsite.h"
  25. #include "systems.h"
  26.  
  27.  
  28. /*
  29.  * I cheat: These are set up such that system_read can be a macro for read
  30.  * under UNIX. IO_OKAY is anything positive.
  31.  */
  32.  
  33. #define IO_OKAY 1
  34. #define IO_ERROR -1
  35. #define IO_EOF 0
  36.  
  37.  
  38. #if defined(FILE_UNIX)
  39. #include <sys/types.h>
  40. #include <sys/file.h>
  41. #include <fcntl.h>
  42. #include <unistd.h>
  43. #endif
  44.  
  45.  
  46. /* -------------------------- File related defs --------------------------- */
  47.  
  48.  
  49. /* The disk page size on this machine. */
  50. #define FILE_BUFFERSIZE 4096
  51.  
  52.  
  53. /*
  54.  * The fd data type for this system.
  55.  */
  56.  
  57. #if defined(FILE_UNIX)
  58. typedef int SYS_FILE;
  59. #define SYS_ERROR_FD -1
  60. #define SYS_STDERR STDERR_FILENO
  61.  
  62. #elif defined(FILE_WIN32)
  63. #include "sem.h"
  64.  
  65. typedef struct {
  66.     HANDLE fh;
  67.     char *fname;
  68.     SEMAPHORE flsem;
  69. } file_s;
  70. typedef file_s* SYS_FILE;
  71.  
  72. #define SYS_ERROR_FD NULL
  73. #define SYS_STDERR NULL
  74. typedef HANDLE pid_t;
  75. #define ERROR_PIPE \
  76.     (ERROR_BROKEN_PIPE | ERROR_BAD_PIPE |\
  77.      ERROR_PIPE_BUSY | ERROR_PIPE_LISTENING | ERROR_PIPE_NOT_CONNECTED)
  78.  
  79. #define CONVERT_TO_PRINTABLE_FORMAT(Filename)     \
  80. {                                               \
  81.     register char *s;                           \
  82.     if (Filename)                               \
  83.     for (s = Filename; *s; s++)                \
  84.         if ( *s    == '\\')                       \
  85.             *s = '/';                           \
  86. }
  87.                                      
  88. #define CONVERT_TO_NATIVE_FS(Filename)        \
  89. {                                              \
  90.     register char *s;                          \
  91.     if (Filename)                           \
  92.         for (s = Filename; *s; s++)        \
  93.             if ( *s    == '/')                   \
  94.                 *s = '\\';                   \
  95. }                                     
  96.  
  97. #define rtfile_notfound() ((errno == ENOENT) || (errno == ESRCH))
  98.  
  99. #else
  100. #ifndef __EDG__
  101. #error "undefined file typing for current system"
  102. #endif
  103. #endif
  104.  
  105. #ifdef XP_UNIX
  106. #define FILE_PATHSEP '/'
  107. #define FILE_PARENT "../"
  108.  
  109. #define system_chdir chdir
  110. #elif defined XP_WIN32
  111. #define FILE_PATHSEP '/'
  112. #define system_chdir SetCurrentDirectory
  113. #define FILE_PARENT "..\\"
  114. #endif
  115.  
  116.  
  117. /*
  118.  * Wrapper for stat system call
  119.  */
  120. #include <sys/stat.h>
  121. NSAPI_PUBLIC int system_stat(char *name, struct stat *finfo);
  122.  
  123. /*
  124.  * Wrapper for rename system call
  125.  */
  126. #define system_rename(old, new) rename(old, new)
  127.  
  128. /*
  129.  * Wrapper to remove file
  130.  */
  131. #ifdef FILE_UNIX
  132. #define system_unlink(path) unlink(path)
  133. #else
  134. #define system_unlink(path) (DeleteFile(path) ? 0 : -1)
  135. #endif
  136.  
  137. /*
  138.  * system_fread reads sz bytes from fd into to buf, return number of bytes
  139.  * read, or IO_EOF if EOF, or IO_ERROR if error. 
  140.  */
  141.  
  142. #if defined(FILE_UNIX)
  143. #define system_fread(fd,buf,sz) read(fd,buf,sz)
  144.  
  145. #elif defined (FILE_WIN32)
  146. NSAPI_PUBLIC int system_fread(SYS_FILE fd, char *buf, int sz);
  147. NSAPI_PUBLIC int system_pread(SYS_FILE fd, char *buf, int sz);
  148. #endif
  149.  
  150. /*
  151.  * system_fopenRO opens a given file for reading only
  152.  * system_fopenWA opens a given file for writing, appending new output
  153.  */
  154.  
  155. #if defined(FILE_UNIX)
  156. #define system_fopenRO(path) open(path, O_RDONLY)
  157. #define system_fopenWA(path) \
  158.                open(path, O_RDWR | O_CREAT | O_APPEND, 0644)
  159. #define system_fopenRW(path) \
  160.                open(path, O_RDWR | O_CREAT, 0644)
  161. #define system_fopenWT(path) \
  162.                open(path, O_RDWR | O_CREAT | O_TRUNC, 0644)
  163. #elif defined(FILE_WIN32)
  164. NSAPI_PUBLIC SYS_FILE system_fopenRO(char *path);
  165. NSAPI_PUBLIC SYS_FILE system_fopenWA(char *path);
  166. NSAPI_PUBLIC SYS_FILE system_fopenRW(char *path);
  167. NSAPI_PUBLIC SYS_FILE system_fopenWT(char *path);
  168. #endif
  169.  
  170.  
  171. /*
  172.  * system_fclose closes the file fd
  173.  */
  174.  
  175. #if defined(FILE_UNIX)
  176. #define system_fclose(fd) close(fd)
  177.  
  178. #elif defined(FILE_WIN32)
  179. NSAPI_PUBLIC void system_fclose(SYS_FILE fd);
  180. #endif
  181.  
  182.  
  183. /*
  184.  * This call stops core dumps in a portable way. Returns -1 on error.
  185.  */
  186.  
  187. NSAPI_PUBLIC int system_nocoredumps(void);
  188.  
  189. #ifdef XP_WIN32
  190. NSAPI_PUBLIC char *system_winsockerr(void);
  191. NSAPI_PUBLIC char *system_winerr(void);
  192. #endif
  193.  
  194. #if defined(FILE_UNIX)
  195. #define system_lseek lseek
  196.  
  197. #elif defined(FILE_WIN32)
  198. #define system_lseek(fd, off, wh) \
  199.   SetFilePointer(fd->fh, (long) off, NULL, wh)
  200. #endif
  201.  
  202. /*
  203.  * system_write writes sz bytes from buf to fd. The handler function should
  204.  * handle partial writes and anything else like that. Returns IO_*
  205.  */
  206.  
  207. NSAPI_PUBLIC int system_fwrite(SYS_FILE fd,char *buf,int sz);
  208.  
  209. /*
  210.  * system_fwrite_atomic locks the given fd before writing to it. This avoids
  211.  * interference between simultaneous writes. Returns IO_*
  212.  */
  213.  
  214. NSAPI_PUBLIC int system_fwrite_atomic(SYS_FILE fd, char *buf, int sz);
  215.  
  216. /*
  217.  * system_errmsg returns the last error that occured while processing file
  218.  * descriptor fd. fd does not have to be specified (if the error is a global
  219.  * such as in UNIX systems). PPS: Rob is a halfwit. This parameter is useless.
  220.  */
  221.  
  222. #ifdef FILE_WIN32
  223. NSAPI_PUBLIC int file_notfound(void);
  224. #else
  225. #include <errno.h>
  226. #define file_notfound() (errno == ENOENT)
  227. #define rtfile_notfound() (errno == ENOENT)
  228. #endif
  229.  
  230. #define system_errmsg(unused) system_errmsg_fn()
  231. NSAPI_PUBLIC char *system_errmsg_fn(void);
  232.  
  233.  
  234. /*
  235.  * flock locks a file against interference from other processes
  236.  * ulock unlocks it.
  237.  */
  238. #ifdef BSD_FLOCK
  239. #include <sys/file.h>
  240. #define system_initlock(fd) (0)
  241. #define system_flock(fd) flock(fd, LOCK_EX)
  242. #define system_ulock(fd) flock(fd, LOCK_UN)
  243. #define system_tlock(fd) flock(fd, LOCK_EX | LOCK_NB)
  244.  
  245. #elif defined(XP_UNIX)  /* !BSD_FLOCK */
  246. #include <unistd.h>
  247. #define system_initlock(fd) (0)
  248. #define system_flock(fd) lockf(fd, F_LOCK, 0)
  249. #define system_ulock(fd) lockf(fd, F_ULOCK, 0)
  250. #define system_tlock(fd) lockf(fd, F_TLOCK, 0)
  251.  
  252. #elif defined(FILE_WIN32)
  253. NSAPI_PUBLIC int system_initlock(SYS_FILE fd);
  254. #define system_flock(fd) sem_grab(fd->flsem)
  255. #define system_ulock(fd) sem_release(fd->flsem)
  256. #endif
  257.  
  258. /*
  259.  * setinherit sets whether the file descriptor fd can be inherited by child 
  260.  * processes. A non-zero value means they can. Return of -1 means error.
  261.  */
  262. NSAPI_PUBLIC int file_setinherit(SYS_FILE fd, int value);
  263.  
  264.  
  265. /*
  266.  * unix2local converts a unix-style pathname to a local one
  267.  */
  268.  
  269. #ifdef XP_UNIX
  270. #define file_unix2local(path,p2) strcpy(p2,path)
  271. #elif defined XP_WIN32
  272. NSAPI_PUBLIC void file_unix2local(char *path, char *p2);
  273. #endif /* XP_WIN32 */
  274.  
  275. /* -------------------------- Dir related defs ---------------------------- */
  276.  
  277.  
  278. #ifdef XP_UNIX
  279. #include <dirent.h>
  280. typedef DIR* SYS_DIR;
  281. typedef struct dirent SYS_DIRENT;
  282. #define dir_open opendir
  283. #define dir_read readdir
  284. #define dir_close closedir
  285. #define dir_create(path) mkdir(path, 0755)
  286. #define dir_remove rmdir
  287.  
  288. #elif defined XP_WIN32
  289.  
  290. typedef struct {
  291.     char *d_name;
  292. } dirent_s;
  293.  
  294. typedef struct {
  295.     HANDLE dp;
  296.     WIN32_FIND_DATA fdata;
  297.     dirent_s de;
  298. } dir_s;
  299.  
  300. typedef dir_s* SYS_DIR;
  301. typedef dirent_s SYS_DIRENT;
  302.  
  303. NSAPI_PUBLIC SYS_DIR dir_open(char *path);
  304. NSAPI_PUBLIC SYS_DIRENT *dir_read(SYS_DIR ds);
  305. NSAPI_PUBLIC void dir_close(SYS_DIR ds);
  306. #define dir_create _mkdir
  307. #define dir_remove _rmdir
  308.  
  309. #endif /* XP_WIN32 */
  310.  
  311. /*
  312.  * create a directory and any of its parents
  313.  */
  314. NSAPI_PUBLIC int dir_create_all(char *dir);
  315.  
  316.  
  317. /*
  318.  * Thread-safe variants of localtime and gmtime
  319.  */
  320. #ifdef HAVE_TIME_R
  321. #ifdef OSF1 /* grumble */
  322. #define system_localtime(curtime, ret) (localtime_r(curtime, ret), ret)
  323. #define system_gmtime(curtime, ret) (gmtime_r(curtime, ret), ret)
  324. #else
  325. #define system_localtime(curtime, ret) localtime_r(curtime, ret)
  326. #define system_gmtime(curtime, ret) gmtime_r(curtime, ret)
  327. #endif
  328.  
  329. #else
  330.  
  331. #ifdef THREAD_ANY
  332.  
  333. NSAPI_PUBLIC struct tm *
  334. system_localtime_r(const time_t *timer, struct tm *result);
  335. NSAPI_PUBLIC struct tm *
  336. system_gmtime_r(const time_t *timer, struct tm *result);
  337.  
  338. #define system_localtime(curtime, ret) system_localtime_r(curtime, ret)
  339. #define system_gmtime(curtime, ret) system_gmtime_r(curtime, ret)
  340. #else
  341. #define system_localtime(curtime, ret) localtime(curtime)
  342. #define system_gmtime(curtime, ret) gmtime(curtime)
  343. #endif
  344.  
  345. #endif
  346.  
  347. #endif
  348.