home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / unix / unixtools / util / h / utils < prev   
Text File  |  1992-07-21  |  4KB  |  165 lines

  1. /* H.Utils: general utilities (header file) */
  2.  
  3. #ifndef __utils_h
  4. #define __utils_h
  5.  
  6. /* Type definitions required for function prototypes */
  7.  
  8. #ifndef __stdio_h
  9. /* If we've done <stdio.h> we've got everything we need already */
  10.  
  11. /* Set up size_t (as defined in standard headers) */
  12.  
  13. #ifndef __size_t
  14. #define __size_t 1
  15. typedef unsigned int size_t;   /* from <stddef.h> */
  16. #endif
  17.  
  18. /* Temporary #define - undone at the end */
  19.  
  20. #define FILE struct __FILE_struct
  21.  
  22. #endif
  23.  
  24. /* ARM System time format */
  25.  
  26. #ifndef __TIME_h
  27. #define __TIME_h
  28. typedef struct
  29. {
  30.     unsigned char t[5];    /* Low byte first - ie. t[0] is low */
  31. }
  32. TIME;
  33. #endif
  34.  
  35. /* File length/offset type */
  36.  
  37. #ifndef __off_t_h
  38. #define __off_t_h
  39. typedef unsigned int off_t;
  40. #endif
  41.  
  42. /* Standard external function definitions */
  43.  
  44. /* Error message handling */
  45. extern void fatal (int rc, const char *format, ...);
  46. extern void message (const char *format, ...);
  47.  
  48. /* Extra string handling */
  49. extern char *strdup (const char *str);
  50. extern char *strndup (const char *str, int n);
  51. extern char *strupper (char *str);
  52. extern char *strlower (char *str);
  53. extern char *strpcpy (char *s, const char *t);
  54. extern char *strnpcpy (char *s, const char *t, int n);
  55. extern int strlcmp (const char *s, const char *t);
  56. extern int strnlcmp (const char *s, const char *t, int n);
  57. extern int strcchr (const char *s, char c);
  58.  
  59. /* Automatically reclaimed memory allocation */
  60. extern void *alloca (size_t size);
  61.  
  62. /* Safe memory allocation */
  63. extern void *emalloc (size_t size);
  64. extern void *erealloc (void *ptr, size_t size);
  65. extern void *ecalloc (size_t count, size_t size);
  66.  
  67. /* Safe file opening */
  68. extern FILE *efopen (const char *file, const char *mode);
  69. extern FILE *efreopen (const char *file, const char *mode, FILE *fp);
  70.  
  71. /* File system manipulation */
  72. extern int chdir (const char *dir);
  73. extern int touch (const char *file);
  74. extern char *getcwd (int preserve_pwd, char **pwd_ptr);
  75. #define getwd() getcwd (1, 0)
  76.  
  77. /* Temporary file handling */
  78. extern char *temp_dir;
  79. extern char *mktemp (const char *file);
  80.  
  81. /* Simulation of pipes */
  82. extern FILE *popen (char *command, char *mode);
  83. extern int pclose (FILE *fd);
  84.  
  85. /* Random number generator */
  86. void srandom (unsigned seed);
  87. long random (void);
  88. char *initstate (unsigned seed, char *arg_state, int n);
  89. char *setstate (char *arg_state);
  90.  
  91. /* Program option processing */
  92. extern int  getopt (int argc, char *argv[], const char *optstring);
  93. extern char *optarg;
  94. extern int  optind;
  95. extern int  opterr;
  96.  
  97. /* Directory scan for wildcards */
  98. extern char *dirscan (const char *file);
  99.  
  100. /* File information */
  101. extern int filetype (const char *file);
  102. extern TIME filetime (const char *file);
  103. extern off_t filelen (const char *file);
  104.  
  105. /* Results from filetype() */
  106. #define F_NONE        (-1)
  107. #define F_DIR        (-2)
  108. #define F_UNSTAMPED    (-3)
  109. #define F_ERROR        (-4)
  110.  
  111. /* Copy data to the heap. This should only be used for l-values (including
  112.  * array names).
  113.  *
  114.  * For example, with the following declarations:
  115.  *
  116.  * void *heap_ptr;
  117.  * char a[100];
  118.  * time_t t;
  119.  *
  120.  * Use:
  121.  *
  122.  * heap_ptr = VarToHeap(a);         -- copies the whole array (100 bytes).
  123.  * heap_ptr = VarToHeap(t);         -- copies t, even if t is a structure type.
  124.  *
  125.  */
  126.  
  127. #define VarToHeap(var)  ( memcpy( emalloc(sizeof(var)), &(var), sizeof(var)) )
  128.  
  129. /* Machine dependent data types */
  130.  
  131. typedef unsigned char  byte;            /*  8-bit byte     */
  132. typedef unsigned short halfword;        /* 16-bit halfword */
  133. typedef unsigned int   word;            /* 32-bit word     */
  134.  
  135. /* Number of elements in array A */
  136.  
  137. #define DIM(a)  ( sizeof(a) / sizeof(*(a)) )
  138.  
  139. /* Maximum and minimum */
  140.  
  141. #define MAX(a,b)        ( (a) > (b) ? (a) : (b) )
  142. #define MIN(a,b)        ( (a) < (b) ? (a) : (b) )
  143.  
  144. /* Scope control pseudo-keywords */
  145.  
  146. #define public
  147. #define private static
  148.  
  149. /* Flag a given variable (function argument) as used.
  150.  *
  151.  * Notes: No code is compiled (isn't optimisation wonderful).
  152.  *        Side effects in "var" are not evaluated.
  153.  *        Var must be an l-value.
  154.  */
  155.  
  156. #define USE(var)        (void)( 0 ? (var)=(var) : 0 )
  157.  
  158. /* Now undo the temporary definition of FILE */
  159.  
  160. #ifndef __stdio_h
  161. #undef FILE
  162. #endif
  163.  
  164. #endif
  165.