unix-io.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  3. %
  4. % File: PXK:UNIX-IO.C
  5. % Description: Unix PSL FileDescriptors are implemented as stdio streams
  6. % ("FILE *".)
  7. % Author: Russell D. Fish
  8. % Created: Thu Feb 16 1984
  9. % Modified: 17-Jul-84 22:49:12 (RAM)
  10. % Mode: Text
  11. % Package:
  12. % Status: Open Source: BSD License
  13. %
  14. % (c) Copyright 1983, Hewlett-Packard Company, see the file
  15. % HP_disclaimer at the root of the PSL file tree
  16. %
  17. % (c) Copyright 1982, University of Utah
  18. %
  19. % Redistribution and use in source and binary forms, with or without
  20. % modification, are permitted provided that the following conditions are met:
  21. %
  22. % * Redistributions of source code must retain the relevant copyright
  23. % notice, this list of conditions and the following disclaimer.
  24. % * Redistributions in binary form must reproduce the above copyright
  25. % notice, this list of conditions and the following disclaimer in the
  26. % documentation and/or other materials provided with the distribution.
  27. %
  28. % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. % AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  30. % THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  31. % PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
  32. % CONTRIBUTORS
  33. % BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. % CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. % SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. % INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. % CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. % ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. % POSSIBILITY OF SUCH DAMAGE.
  40. %
  41. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  42. */
  43. /*
  44. * There used to be a collection of comments here describing revisions
  45. * made from about 1982 to 1989. I think those are by now of interest
  46. * to archaeologists! So anybody who wants to see them can consult older
  47. * copies of this file in the repositories. It is neverthless proper to
  48. * record the names of those who (in addition to the original author)
  49. * have contributed:
  50. * T. Yamamoto, C. Burdorf, Leigh Stoller, RAM, Vicki O'Day
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <inttypes.h>
  57. #include <errno.h>
  58. #include "psl.h"
  59. /* There is an assumption here that coercing addresses into ints is OK */
  60. /* Initialize some PSL external variables with FileDescriptors for SysClearIO.
  61. */
  62. extern FILE *_(unixstdin), *_(unixstdout), *_(unixstderr), *_(unixtty);
  63. /* Import NULL and EOF constants for error returns from stdio fns.
  64. */
  65. extern int _(unixnull)[2], _(unixeof)[2];
  66. /* Tag( unixinitio )
  67. */
  68. void _(unixinitio)()
  69. { TR1("unixinitio");
  70. _(unixstdin) = stdin;
  71. _(unixstdout) = stdout;
  72. _(unixstderr) = stderr;
  73. _(unixnull)[0] = (long) NULL; /* Dodgy??? */
  74. _(unixnull)[1] = (long) NULL;
  75. _(unixeof)[0] = EOF;
  76. _(unixeof)[1] = EOF;
  77. _(unixtty) = fopen("/dev/tty", "r");
  78. TR1("end of unixinitio");
  79. }
  80. /* Tag( unixputc )
  81. * Used by kernel routines that write to the console
  82. */
  83. void _(unixputc)(int c)
  84. { TR1("unixputc");
  85. fputc(c, stdout);
  86. }
  87. /* Tag( unixputs )
  88. */
  89. void _(unixputs)(const char *str)
  90. { TR1("unixputs");
  91. printf("unixputs (A) %p\n", str);
  92. printf("unixputs (B) %s\n", str);
  93. fputs(str, stdout);
  94. }
  95. /* Tag( unixputn )
  96. */
  97. void _(unixputn)(int64_t n)
  98. { TR1("unixputn");
  99. fprintf(stdout, "%" PRId64, n);
  100. }
  101. /* Tag( unixcleario )
  102. */
  103. void _(unixcleario)()
  104. { TR1("unixcleario");
  105. _(unixinitio)();
  106. #if 0
  107. /* This is what the code use to do in non-Linux cases. For now I will
  108. comment it out but eventually I will need Windows and Macintosh
  109. code to tidy up...
  110. */
  111. /* set the stdin, stdout and stderr buffers to be empty */
  112. stdin->_cnt = 0;
  113. stdin->_ptr = stdin->_base;
  114. stdout->_cnt = 0;
  115. stdout->_ptr = stdout->_base;
  116. stderr->_cnt = 0;
  117. stderr->_ptr = stderr->_base;
  118. #endif
  119. }
  120. /* The function expand_file_name is used to expand shell variable references
  121. and ~ notation for directories in file names before calling fopen.
  122. This eliminates the need for the HPUX-PATH and VAX-PATH kludges that were
  123. once required, however, such a mechanism may still be used to override the
  124. values supplied by the shell environment. The file name is first copied
  125. to a temporary copy because the parsing algorithm must write into the
  126. string (this may change). For now, the maximum string length supported
  127. is 255 characters, but no checking is done to see if this is exceeded.
  128. $ and ~ variables are expanded in one pass by calling the functions getenv,
  129. getuid, getpwuid, and getpwnam. If any expansion fails, the original
  130. string is returned.
  131. */
  132. #include <pwd.h>
  133. struct passwd *getpwuid();
  134. struct passwd *getpwnam();
  135. char *getenv();
  136. #define LONGEST_LEGAL_FILENAME 255
  137. char collect[LONGEST_LEGAL_FILENAME], copy[LONGEST_LEGAL_FILENAME];
  138. /* Made global so it won't be overwritten
  139. Used to be local to expand_file_name.
  140. Involves a buffer-overrun risk. */
  141. /* Tag( expand_file_name )
  142. */
  143. char *_(expand_file_name)(const char *fname)
  144. { TR1("expand_file_name");
  145. char *c, *t, *e, *s, save;
  146. struct passwd *p;
  147. register int tilde;
  148. strcpy(copy, fname);
  149. s = copy;
  150. c = collect;
  151. *c = '\0';
  152. while (*s)
  153. { if ((tilde = (*s == '~')) || (*s == '$'))
  154. { for (e = ++s; (*e != '/' && *e != '\0' && *e != '$'); e++)
  155. ;
  156. t = 0; /* default initialization */
  157. if (e == s)
  158. { if (tilde) t = ((getpwuid(getuid())) -> pw_dir);
  159. }
  160. else
  161. { save = *e;
  162. *e = '\0';
  163. if (tilde)
  164. { if ((p = getpwnam(s)) != NULL) t = (p -> pw_dir);
  165. }
  166. else
  167. t = getenv(s);
  168. *e = save;
  169. s = e;
  170. }
  171. if (t)
  172. while ((*c++ = *t++) != 0)
  173. ;
  174. else
  175. { strcpy(copy, fname);
  176. return(copy); /* name not found, just return original fname */
  177. }
  178. c--;
  179. }
  180. for (; (*s != '\0' && *s != '$'); *c++ = *s++)
  181. ;
  182. *c = '\0';
  183. }
  184. return (collect);
  185. }
  186. extern int errno;
  187. FILE *_(unixopen)(const char *filename, const char *type)
  188. { TR1("unixopen");
  189. printf("\nunixopen \"%s\" \"%s\"\n", filename, type);
  190. return fopen(_(expand_file_name)(filename), type);
  191. }
  192. int _(unixcd)(const char *filename)
  193. { TR1("unixcd");
  194. return chdir(_(expand_file_name)(filename));
  195. }
  196. int _(unixfclose)( FILE *ix)
  197. { TR1("unixfclose");
  198. return fclose(ix);
  199. }
  200. int _(external_system)(const char *command)
  201. { TR1("external_system");
  202. return system(command);
  203. }
  204. /* Tag( external_exit )
  205. */
  206. int _(external_exit)(int status)
  207. { TR1("external_exit");
  208. exit(status);
  209. return 0;
  210. }
  211. /* This allows for up to 20 entries and then overflows... */
  212. char *static_argv[20]; /* static place to hold argv so it doesn't get gc'd */
  213. char **_(copy_argv)(int argc, char *argv[]) /* copy argv into static space. */
  214. { TR1("copy_argv");
  215. int i;
  216. for (i=0; i < argc; i++)
  217. static_argv[i]=argv[i];
  218. return(static_argv);
  219. }
  220. /* convert a pathname to canonical form */
  221. char *_(external_fullpath)(const char *relpath)
  222. { TR1("external_fullpath");
  223. return realpath(relpath,NULL);
  224. }
  225. int64_t _(xgetw)(FILE *f)
  226. { TR1("xgetw");
  227. int64_t a1,a2;
  228. a1 = (int64_t) getw(f);
  229. a2 = (int64_t) getw(f);
  230. return ((a2 << 32) | a1);
  231. }
  232. /* end of unix-io.c */