pslextras.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. % File: PXK:PSLEXTRAS.C
  5. % Description: Miscellaneous support routines.
  6. % Author: RAM, HP/FSD
  7. % Created: 9-Mar-84
  8. % Modified: 21-Mar-85 11:25:52
  9. % Mode: Text
  10. % Package:
  11. % Status: Open Source: BSD License
  12. %
  13. % (c) Copyright 1983, Hewlett-Packard Company, see the file
  14. % HP_disclaimer at the root of the PSL file tree
  15. %
  16. % Redistribution and use in source and binary forms, with or without
  17. % modification, are permitted provided that the following conditions are met:
  18. %
  19. % * Redistributions of source code must retain the relevant copyright
  20. % notice, this list of conditions and the following disclaimer.
  21. % * Redistributions in binary form must reproduce the above copyright
  22. % notice, this list of conditions and the following disclaimer in the
  23. % documentation and/or other materials provided with the distribution.
  24. %
  25. % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. % THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
  29. % CONTRIBUTORS
  30. % BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. % POSSIBILITY OF SUCH DAMAGE.
  37. %
  38. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  39. */
  40. /*
  41. * There used to be a collection of comments here describing revisions
  42. * made from about 1982 to 1989. I think those are by now of interest
  43. * to archaeologists! So anybody who wants to see them can consult older
  44. * copies of this file in the repositories. It is neverthless proper to
  45. * record the names of those who (in addition to the original author)
  46. * have contributed:
  47. * Julian Padget, Leigh Stoller, Harold Carr, Scott Marovich,
  48. * Vicki O'Day
  49. */
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <time.h>
  54. #include <unistd.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <sys/times.h>
  58. #include "psl.h"
  59. /*
  60. * "This interface is made obsolete by setitimer(2)"
  61. */
  62. unsigned int _(external_alarm)(unsigned long int sec)
  63. { return alarm(sec);
  64. }
  65. int _(external_ualarm)(unsigned long int usec, unsigned long int repeat)
  66. { return( ualarm(usec,repeat));
  67. }
  68. char *_(expand_file_name)(); /* from unix-io.c */
  69. /* Tag( external_time )
  70. */
  71. time_t _(external_time)(time_t *tloc)
  72. { return (time(tloc));
  73. }
  74. /* Tag( external_timc )
  75. */
  76. long _(external_timc)(struct tms *buffer)
  77. { return(times(buffer));
  78. }
  79. /* Tag( external_stat )
  80. */
  81. int _(external_stat)(const char *path, struct stat *buf)
  82. { return stat(_(expand_file_name)(path), buf);
  83. }
  84. int _(external_mkdir)(const char *name, int mode)
  85. { return mkdir(name, mode);
  86. }
  87. int _(external_rmdir)(const char *name)
  88. { return rmdir(name);
  89. }
  90. /* Tag( external_link )
  91. */
  92. int _(external_link)(const char *path1, const char *path2)
  93. { return link(_(expand_file_name)(path1), _(expand_file_name)(path2));
  94. }
  95. /* Tag( external_unlink )
  96. */
  97. int _(external_unlink)(const char *path)
  98. { return unlink(_(expand_file_name)(path));
  99. }
  100. /* Tag( external_strlen )
  101. */
  102. int _(external_strlen)(const char *s)
  103. { return strlen(s);
  104. }
  105. char *getenv(const char *name);
  106. /* Tag( external_getenv )
  107. */
  108. char *_(external_getenv)(const char *name)
  109. { return getenv(name);
  110. }
  111. int _(external_setenv)(const char *var, const char *val, int ov)
  112. { int i;
  113. extern char **environ;
  114. char **envnew;
  115. char var_plus_equal_sign[100];
  116. /* Look for first empty slot to find number of existing env variables. */
  117. for (i = 0 ; environ [i] != NULL ; i++) ;
  118. /* Make a new environment array with 2 new slots - 1 for var being set,
  119. and 1 extra empty slot. */
  120. envnew = (char **) calloc ((i + 2), sizeof(char *));
  121. bcopy((char *)environ, (char *)envnew, i * sizeof(char *));
  122. environ = envnew;
  123. strcpy(var_plus_equal_sign, var);
  124. strcat(var_plus_equal_sign, "=");
  125. return(setenv (var_plus_equal_sign, val,ov));
  126. }
  127. /*
  128. * sets the value of var to be arg in the Unix environment env.
  129. * Var should end with '=' (bindings are of the form "var=value").
  130. * This procedure assumes the memory for the first level of environ
  131. * was allocated using calloc, with enough extra room at the end so not
  132. * to have to do a realloc().
  133. */
  134. int setenv(const char *var, const char *value, int ov)
  135. { extern char **environ;
  136. int index = 0;
  137. int len = strlen(var);
  138. while (environ [index] != NULL)
  139. { if (strncmp (environ [index], var, len) == 0)
  140. { /* found it */
  141. environ[index] = (void *)malloc (len + strlen (value) + 1);
  142. strcpy (environ [index], var);
  143. strcat (environ [index], value);
  144. return (ov);
  145. }
  146. index ++;
  147. }
  148. environ [index] = (void *) malloc (len + strlen (value) + 1);
  149. strcpy (environ [index], var);
  150. strcat (environ [index], value);
  151. environ [++index] = NULL;
  152. return 0;
  153. }
  154. void _(block_copy)(const char *b1, char *b2, int length)
  155. { while (length-- > 0)
  156. *b2++ = *b1++;
  157. }
  158. #define LISPEOF 4 /* Lisp uses ctrl-D for end of file */
  159. /* Tag( unixreadrecord )
  160. */
  161. int _(unixreadrecord)(FILE *fp, char *buf)
  162. { int i;
  163. char c;
  164. for (i=0, c=' '; ((c != LISPEOF) && (c != '\n')); i++)
  165. { c = fgetc(fp);
  166. if (c == EOF )
  167. c = LISPEOF;
  168. *buf++ = c;
  169. }
  170. return i;
  171. }
  172. /* Tag( unixwriterecord )
  173. */
  174. int _(unixwriterecord)(FILE *fp, char *buf, int count)
  175. { int i;
  176. for (i=0; i<count; i++, buf++)
  177. fputc(*buf, fp);
  178. return 0;
  179. }
  180. /* end of pslextras.c */