home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / clib / progs / utilslib / h / Utils < prev   
Encoding:
Text File  |  1991-10-20  |  7.2 KB  |  246 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. /* Temporary #define - undone at the end */
  12. #define FILE struct __FILE_struct
  13.  
  14. /* Set up size_t (as defined in standard headers) */
  15. #ifndef __size_t
  16. #define __size_t 1
  17. typedef unsigned int size_t;   /* from <stddef.h> */
  18. #endif
  19.  
  20. #endif
  21.  
  22. /* ARM System time format */
  23.  
  24. #ifndef __TIME_h
  25. #define __TIME_h
  26. typedef struct
  27. {
  28.     unsigned char t[5];    /* Low byte first - ie. t[0] is low */
  29. }
  30. TIME;
  31. #endif
  32.  
  33. /* Standard external function definitions */
  34.  
  35. /* Error message handling */
  36. extern void panic (const char *format, ...);
  37. extern void message (const char *format, ...);
  38.  
  39. /* Extra string handling */
  40. extern char *strdup (const char *str);
  41. extern char *strndup (const char *str, int n);
  42. extern char *strupper (char *str);
  43. extern char *strlower (char *str);
  44. extern char *strpcpy (char *s, const char *t);
  45. extern char *strnpcpy (char *s, const char *t, int n);
  46. extern int strlcmp (const char *s, const char *t);
  47. extern int strnlcmp (const char *s, const char *t, int n);
  48. extern int strucmp (const char *s, const char *t);
  49. extern int strnucmp (const char *s, const char *t, int n);
  50. extern int strcchr (const char *s, char c);
  51.  
  52. /* Automatically reclaimed memory allocation */
  53. extern void *alloca (size_t size);
  54.  
  55. /* Safe memory allocation */
  56. extern void *emalloc (size_t size);
  57. extern void *erealloc (void *ptr, size_t size);
  58. extern void *ecalloc (size_t count, size_t size);
  59.  
  60. /* Safe file opening */
  61. extern FILE *efopen (const char *file, const char *mode);
  62. extern FILE *efreopen (const char *file, const char *mode, FILE *fp);
  63.  
  64. /* Robust file copy (even across file systems */
  65. extern int frename (const char *old, const char *new);
  66.  
  67. /* File pointer information */
  68. extern int isatty (FILE *fp);
  69.  
  70. /* File system manipulation */
  71. extern int chdir (const char *dir);
  72. extern int touch (const char *file);
  73. extern char *getcwd (int preserve_pwd, char **pwd_ptr);
  74. #define getwd() getcwd (1, 0)
  75.  
  76. /* Temporary file handling */
  77. extern char *temp_dir;
  78. extern char *mktemp (const char *file);
  79.  
  80. /* Simulation of pipes */
  81. extern FILE *popen (char *command, char *mode);
  82. extern int pclose (FILE *fd);
  83.  
  84. /* Random number generator */
  85. void srandom (unsigned seed);
  86. long random (void);
  87. char *initstate (unsigned seed, char *arg_state, int n);
  88. char *setstate (char *arg_state);
  89.  
  90. /* Directory scan for wildcards */
  91. extern char *dirscan (const char *file);
  92.  
  93. /* File information */
  94. extern int filetype (const char *file);
  95. extern TIME filetime (const char *file);
  96. extern unsigned filelen (const char *file);
  97.  
  98. /* Results from filetype() */
  99. #define F_NONE        (-1)
  100. #define F_DIR        (-2)
  101. #define F_UNSTAMPED    (-3)
  102. #define F_ERROR        (-4)
  103.  
  104. /* Debugging utilities */
  105. extern char *caller (int);
  106.  
  107. /* Linker areas (names translated for C use) */
  108. extern void *_RO_Base;
  109. extern void *_RO_Limit;
  110. extern void *_RW_Base;
  111. extern void *_RW_Limit;
  112. extern void *_ZI_Base;
  113. extern void *_ZI_Limit;
  114.  
  115. /* The following is basically a functional version of _RO_Limit. It is
  116.  * retained for compatibility with older versions of this library only.
  117.  */
  118. extern void *ProgLimit (void);
  119.  
  120. /* Unexec for the Archimedes.
  121.  *
  122.  * Used as unexec(filename), which dumps a full core image of the
  123.  * current program to file filename. This image is a proper unsqueezed
  124.  * Archimedes application. You can squeeze the resulting executable just
  125.  * like any other program.
  126.  */
  127.  
  128. extern void unexec (char *);
  129.  
  130. /* Coroutines
  131.  *
  132.  * ----------------------------------------------------------------------
  133.  *
  134.  * Based on an article by David McQuillan in Archive, Vol 4 No 6.
  135.  * The following notice is from the original article.
  136.  *
  137.  * No responsibility accepted if you come a cropper using the code.
  138.  * In particular, I have not tried stack extension and interrupts with it.
  139.  *
  140.  * (c) put in Public Domain by David McQuillan, Jan 1991.
  141.  *
  142.  * ----------------------------------------------------------------------
  143.  *
  144.  * The coroutine mechanism must be initialised by calling co_initialise()
  145.  * in the main program, passing it the address of a coroutine_t variable
  146.  * in which the root coroutine data is stored. Coroutines are then created
  147.  * by calling co_create, passing the address of the coroutine variable which
  148.  * is to hold the coroutine data, the initial stack size, and the procedure
  149.  * (a co_procedure_t) to be executed when the coroutine is started.
  150.  *
  151.  * Control is transferred to a coroutine via the co_resume() procedure.
  152.  * The first parameter is the address of the coroutine to resume, and the
  153.  * second parameter is a value to be passed to the coroutine. On the first
  154.  * entry to the coroutine, this parameter is passed as the parameter to the
  155.  * coroutine start procedure. On second and subsequent entries to the
  156.  * coroutine, the parameter is returned as the result of the co_resume()
  157.  * call which transferred control out of the coroutine.
  158.  *
  159.  * Note that a coroutine main procedure should never return. When it is
  160.  * finished, it should co_resume() the main procedure. If a coroutine does
  161.  * return, the coroutine package signals an error and terminates the program.
  162.  *
  163.  * When a coroutine is finished with, it can be deleted using co_delete().
  164.  * This frees up the memory allocated to the coroutine stack and register set.
  165.  */
  166.  
  167. /* Coroutine type. The entries are
  168.  *
  169.  *    env   - a pointer to a jmp_buf holding the coroutine's register set.
  170.  *        The extra indirection is used to avoid the need to know the
  171.  *        size of a jmp_buf (which would require us to include
  172.  *        <setjmp.h>).
  173.  *    stack - the coroutine stack. The stack_chunk structure is declared
  174.  *        in "kernel.h".
  175.  */
  176. typedef struct
  177. {
  178.     void *env;            /* Really (jmp_buf *) */
  179.     struct stack_chunk *stack;
  180. }
  181. coroutine_t;
  182.  
  183. /* Coroutine main procedure type */
  184. typedef void co_procedure_t (void *);
  185.  
  186. /* Function declarations */
  187. void co_initialise (coroutine_t *);
  188. void co_create (coroutine_t *, size_t, co_procedure_t *);
  189. void co_delete (coroutine_t *);
  190. void *co_resume (coroutine_t *, void *);
  191.  
  192. /* Copy data to the heap. This should only be used for l-values (including
  193.  * array names).
  194.  *
  195.  * For example, with the following declarations:
  196.  *
  197.  * void *heap_ptr;
  198.  * char a[100];
  199.  * time_t t;
  200.  *
  201.  * Use:
  202.  *
  203.  * heap_ptr = VarToHeap(a);         -- copies the whole array (100 bytes).
  204.  * heap_ptr = VarToHeap(t);         -- copies t, even if t is a structure type.
  205.  *
  206.  */
  207.  
  208. #define VarToHeap(var)  ( memcpy( emalloc(sizeof(var)), &(var), sizeof(var)) )
  209.  
  210. /* Machine dependent data types */
  211.  
  212. typedef unsigned char  byte;            /*  8-bit byte     */
  213. typedef unsigned short halfword;        /* 16-bit halfword */
  214. typedef unsigned int   word;            /* 32-bit word     */
  215.  
  216. /* Number of elements in array A */
  217.  
  218. #define DIM(a)  ( sizeof(a) / sizeof(*(a)) )
  219.  
  220. /* Maximum and minimum */
  221.  
  222. #define MAX(a,b)        ( (a) > (b) ? (a) : (b) )
  223. #define MIN(a,b)        ( (a) < (b) ? (a) : (b) )
  224.  
  225. /* Scope control pseudo-keywords */
  226.  
  227. #define public
  228. #define private static
  229.  
  230. /* Flag a given variable (function argument) as used.
  231.  *
  232.  * Notes: No code is compiled (isn't optimisation wonderful).
  233.  *        Side effects in "var" are not evaluated.
  234.  *        Var must be an l-value.
  235.  */
  236.  
  237. #define USE(var)        (void)( 0 ? (var)=(var) : 0 )
  238.  
  239. /* Now undo the temporary definition of FILE */
  240.  
  241. #ifndef __stdio_h
  242. #undef FILE
  243. #endif
  244.  
  245. #endif
  246.