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 / util.h / util
Text File  |  1998-08-19  |  7KB  |  248 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.  * util.h: A hodge podge of utility functions and standard functions which 
  16.  *         are unavailable on certain systems
  17.  * 
  18.  * Rob McCool
  19.  */
  20.  
  21.  
  22. #ifndef HTTPD_UTIL_H
  23. #define HTTPD_UTIL_H
  24.  
  25. #include "buffer.h"    /* filebuf for getline */
  26.  
  27. #include <time.h>      /* struct tm */
  28.  
  29.  
  30. /* ------------------------------ Prototypes ------------------------------ */
  31.  
  32.  
  33. /*
  34.  * getline scans in buf until it finds a LF or CRLF, storing the string in
  35.  * l. It will terminate the string and return:
  36.  * 
  37.  *  0 when done, with the scanned line (minus CR or LF) in l
  38.  *  1 upon EOF, with the scanned line (minus CR or LF) in l
  39.  * -1 on error with the error description in l (uses lineno for information)
  40.  */
  41.  
  42. NSAPI_PUBLIC
  43. int util_getline(filebuffer *buf, int lineno, int maxlen, char *l);
  44.  
  45.  
  46. /*
  47.  * can_exec returns 1 if you can execute the file described by finfo, and 
  48.  * 0 if you can't.
  49.  */
  50.  
  51. #ifdef XP_UNIX
  52. #include <sys/stat.h>
  53. #include <sys/types.h>
  54.  
  55. NSAPI_PUBLIC int util_can_exec(struct stat *finfo, uid_t uid, gid_t gid);
  56.  
  57. #endif /* XP_UNIX */
  58. /*
  59.  * env_create creates a new environment with the given env, with n new
  60.  * entries, and places the current position that you should add your
  61.  * entries with at pos.
  62.  * 
  63.  * If env is NULL, it will allocate a new one. If not, it will reallocate
  64.  * that one.
  65.  */
  66.  
  67. NSAPI_PUBLIC char **util_env_create(char **env, int n, int *pos);
  68.  
  69. /*
  70.  * util_env_str allocates a string from the given name and value and
  71.  * returns it. It does not check for things like = signs in name.
  72.  */
  73.  
  74. NSAPI_PUBLIC char *util_env_str(char *name, char *value);
  75.  
  76. /*
  77.  * env_replace replaces the occurrence of the given variable with the 
  78.  * value you give.
  79.  */
  80.  
  81. NSAPI_PUBLIC void util_env_replace(char **env, char *name, char *value);
  82.  
  83. /*
  84.  * util_env_free frees an environment.
  85.  */
  86.  
  87. NSAPI_PUBLIC void util_env_free(char **env);
  88.  
  89. /*
  90.  * util_env_copy copies an env
  91.  */
  92. NSAPI_PUBLIC char **util_env_copy(char **src, char **dst);
  93.  
  94.  
  95. /*
  96.  * util_env_find looks through env for the named string. Returns the
  97.  * corresponding value if the named string is found, or NULL if not.
  98.  */
  99. NSAPI_PUBLIC char *util_env_find(char **env, char *name);
  100.  
  101.  
  102. /*
  103.  * hostname gets the local hostname. Returns NULL if it can't find a FQDN.
  104.  * You are free to realloc or free this string.
  105.  */
  106.  
  107. NSAPI_PUBLIC char *util_hostname(void);
  108.  
  109.  
  110. /*
  111.  * chdir2path changes the current directory to the one that the file
  112.  * path is in. path should point to a file. Caveat: path must be a writable
  113.  * string. It won't get modified permanently.
  114.  */
  115.  
  116. NSAPI_PUBLIC int util_chdir2path(char *path);
  117.  
  118. /*
  119.  * is_mozilla checks if the given user-agent is mozilla, of at least
  120.  * the given major and minor revisions. These are strings to avoid 
  121.  * ambiguities like 1.56 > 1.5
  122.  */
  123.  
  124. NSAPI_PUBLIC int util_is_mozilla(char *ua, char *major, char *minor);
  125.  
  126. /*
  127.  * is_url will return 1 if the given string seems to be a URL, or will 
  128.  * return 0 otherwise. 
  129.  * 
  130.  * Because of stupid news URLs, this will return 1 if the string has 
  131.  * all alphabetic characters up to the first colon and will not check for 
  132.  * the double slash.
  133.  */
  134.  
  135. NSAPI_PUBLIC int util_is_url(char *url);
  136.  
  137. /*
  138.  * util_later_than checks the date in the string ims, and if that date is 
  139.  * later than or equal to the one in the tm struct lms, then it returns 1.
  140.  *
  141.  * util_time_equal is above, but checks for exact equality.
  142.  *
  143.  * Handles RFC 822, 850, and ctime formats.
  144.  */
  145.  
  146. NSAPI_PUBLIC int util_later_than(struct tm *lms, char *ims);
  147. NSAPI_PUBLIC int util_time_equal(struct tm *lms, char *ims);
  148.  
  149. /* 
  150.  * util_str_time_equal checks the character-string dates are equal.
  151.  * Supports rfc1123 and rfc850 formats.  t1 must be rfc1123
  152.  * Returns 0 if equal, -1 otherwise
  153.  */
  154. NSAPI_PUBLIC int util_str_time_equal(char *t1, char *t2);
  155.  
  156. /*
  157.  * util_uri_is_evil returns 1 if a URL has ../ or // in it.
  158.  */
  159. NSAPI_PUBLIC int util_uri_is_evil(char *t);
  160.  
  161. /*
  162.  * util_uri_parse gets rid of /../, /./, and //.
  163.  * 
  164.  * Assumes that either the string starts with a /, or the string will
  165.  * not .. right off of its beginning.  As such, ../foo.gif will
  166.  * not be changed, although /../foo.gif will become /foo.gif.
  167.  */
  168.  
  169. NSAPI_PUBLIC void util_uri_parse(char *uri);
  170.  
  171. /*
  172.  * util_uri_unescape unescapes the given URI in place (% conversions only).
  173.  */
  174.  
  175. NSAPI_PUBLIC void util_uri_unescape(char *s);
  176.  
  177. /*
  178.  * util_uri_escape escapes any nasty chars in s and copies the string into d.
  179.  * If d is NULL, it will allocate and return a properly sized string.
  180.  * Warning: does not check bounds on a given d.
  181.  *
  182.  * util_url_escape does the same thing but does it for a url, i.e. ?:+ is 
  183.  * not escaped.
  184.  */
  185.  
  186. NSAPI_PUBLIC char *util_uri_escape(char *d, char *s);
  187. NSAPI_PUBLIC char *util_url_escape(char *d, char *s);
  188.  
  189. /*
  190.  * util_sh_escape places a \ in front of any shell-special characters.
  191.  * Returns a newly-allocated copy of the string.
  192.  */
  193.  
  194. NSAPI_PUBLIC char *util_sh_escape(char *s);
  195.  
  196.  
  197. /*
  198.  * util_mime_separator generates a new MIME separator into the given buffer.
  199.  * The buffer should be more than 4 + 3*10 + 1 bytes long. A CRLF is prepended
  200.  * to the beginning of the string, along with two dashes. The string is null
  201.  * terminated, with no CRLF. The intent is that you create your content-type
  202.  * header by accessing &sep[4], and afterwards print sep followed by CRLF
  203.  * for message boundaries.
  204.  *
  205.  * Returns the length of the string.
  206.  */
  207. NSAPI_PUBLIC int util_mime_separator(char *sep);
  208.  
  209. /*
  210.  * util_itoa converts the given integer to a string into a.
  211.  */
  212.  
  213. NSAPI_PUBLIC int util_itoa(int i, char *a);
  214.  
  215. /*
  216.  * util_vsprintf and util_sprintf are simplified clones of the System V 
  217.  * vsprintf and sprintf routines.
  218.  * 
  219.  * Returns the number of characters printed. Only handles %d and %s,
  220.  * does not handle any width or precision.
  221.  */
  222.  
  223. #include <stdarg.h>
  224.  
  225. NSAPI_PUBLIC int util_vsprintf(char *s, register char *fmt, va_list args);
  226. NSAPI_PUBLIC int util_sprintf(char *s, char *fmt, ...);
  227.  
  228. /* These routines perform bounds checks. */
  229. NSAPI_PUBLIC int util_vsnprintf(char *s, int n, register char *fmt, 
  230.                                 va_list args);
  231. NSAPI_PUBLIC int util_snprintf(char *s, int n, char *fmt, ...);
  232.  
  233. #ifdef XP_WIN32
  234. /* util_delete_directory()
  235.  * This routine deletes all the files in a directory.  If delete_directory is
  236.  * TRUE it will also delete the directory itself.
  237.  */
  238. NSAPI_PUBLIC VOID util_delete_directory(char *FileName, BOOL delete_directory);
  239. #endif
  240.  
  241. /* util_strftime()
  242.  * Thread safe version of strftime.
  243.  * No bounds checking is done s.  t must be a valid tm structure.
  244.  */
  245. NSAPI_PUBLIC int util_strftime(char *s, const char *format, const struct tm *t);
  246.  
  247. #endif
  248.