gpmisc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (C) 2000-2003 artofcode LLC. All rights reserved.
  2. This software is provided AS-IS with no warranty, either express or
  3. implied.
  4. This software is distributed under license and may not be copied,
  5. modified or distributed except as expressly authorized under the terms
  6. of the license contained in the file LICENSE in this distribution.
  7. For more information about licensing, please refer to
  8. http://www.ghostscript.com/licensing/. For information on
  9. commercial licensing, go to http://www.artifex.com/licensing/ or
  10. contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  12. */
  13. /* $Id: gpmisc.c,v 1.22 2003/12/04 12:35:35 igor Exp $ */
  14. /* Miscellaneous support for platform facilities */
  15. #include "unistd_.h"
  16. #include "fcntl_.h"
  17. #include "stdio_.h"
  18. #include "stat_.h"
  19. #include "memory_.h"
  20. #include "string_.h"
  21. #include "gp.h"
  22. #include "gpgetenv.h"
  23. #include "gpmisc.h"
  24. /*
  25. * Get the name of the directory for temporary files, if any. Currently
  26. * this checks the TMPDIR and TEMP environment variables, in that order.
  27. * The return value and the setting of *ptr and *plen are as for gp_getenv.
  28. */
  29. int
  30. gp_gettmpdir(char *ptr, int *plen)
  31. {
  32. int max_len = *plen;
  33. int code = gp_getenv("TMPDIR", ptr, plen);
  34. if (code != 1)
  35. return code;
  36. *plen = max_len;
  37. return gp_getenv("TEMP", ptr, plen);
  38. }
  39. /*
  40. * Open a temporary file, using O_EXCL and S_I*USR to prevent race
  41. * conditions and symlink attacks.
  42. */
  43. FILE *
  44. gp_fopentemp(const char *fname, const char *mode)
  45. {
  46. int flags = O_EXCL;
  47. /* Scan the mode to construct the flags. */
  48. const char *p = mode;
  49. int fildes;
  50. FILE *file;
  51. while (*p)
  52. switch (*p++) {
  53. case 'a':
  54. flags |= O_CREAT | O_APPEND;
  55. break;
  56. case 'r':
  57. flags |= O_RDONLY;
  58. break;
  59. case 'w':
  60. flags |= O_CREAT | O_WRONLY | O_TRUNC;
  61. break;
  62. #ifdef O_BINARY
  63. /* Watcom C insists on this non-ANSI flag being set. */
  64. case 'b':
  65. flags |= O_BINARY;
  66. break;
  67. #endif
  68. case '+':
  69. flags = (flags & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
  70. break;
  71. default: /* e.g., 'b' */
  72. break;
  73. }
  74. fildes = open(fname, flags, S_IRUSR | S_IWUSR);
  75. if (fildes < 0)
  76. return 0;
  77. /*
  78. * The DEC VMS C compiler incorrectly defines the second argument of
  79. * fdopen as (char *), rather than following the POSIX.1 standard,
  80. * which defines it as (const char *). Patch this here.
  81. */
  82. file = fdopen(fildes, (char *)mode); /* still really const */
  83. if (file == 0)
  84. close(fildes);
  85. return file;
  86. }
  87. /* Append a string to buffer. */
  88. private inline bool
  89. append(char **bp, const char *bpe, const char **ip, uint len)
  90. {
  91. if (bpe - *bp < len)
  92. return false;
  93. memcpy(*bp, *ip, len);
  94. *bp += len;
  95. *ip += len;
  96. return true;
  97. }
  98. /* Search a separator forward. */
  99. private inline uint
  100. search_separator(const char **ip, const char *ipe, const char *item, int direction)
  101. { uint slen = 0;
  102. for (slen = 0; (*ip - ipe) * direction < 0; (*ip) += direction)
  103. if((slen = gs_file_name_check_separator(*ip, ipe - *ip, item)) != 0)
  104. break;
  105. return slen;
  106. }
  107. /*
  108. * Combine a file name with a prefix.
  109. * Concatenates two paths and reduce parent references and current
  110. * directory references from the concatenation when possible.
  111. * The trailing zero byte is being added.
  112. *
  113. * Examples :
  114. * "/gs/lib" + "../Resource/CMap/H" --> "/gs/Resource/CMap/H"
  115. * "C:/gs/lib" + "../Resource/CMap/H" --> "C:/gs/Resource/CMap/H"
  116. * "hard disk:gs:lib" + "::Resource:CMap:H" -->
  117. * "hard disk:gs:Resource:CMap:H"
  118. * "DUA1:[GHOSTSCRIPT.LIB" + "-.RESOURCE.CMAP]H" -->
  119. * "DUA1:[GHOSTSCRIPT.RESOURCE.CMAP]H"
  120. * "\\server\share/a/b///c/../d.e/./" + "../x.e/././/../../y.z/v.v" -->
  121. * "\\server\share/a/y.z/v.v"
  122. */
  123. gp_file_name_combine_result
  124. gp_file_name_combine_generic(const char *prefix, uint plen, const char *fname, uint flen,
  125. bool no_sibling, char *buffer, uint *blen)
  126. {
  127. /*
  128. * THIS CODE IS SHARED FOR MULTIPLE PLATFORMS.
  129. * PLEASE DON'T CHANGE IT FOR A SPECIFIC PLATFORM.
  130. * Change gp_file_name_combine instead.
  131. */
  132. char *bp = buffer, *bpe = buffer + *blen;
  133. const char *ip, *ipe;
  134. uint slen;
  135. uint infix_type = 0; /* 0=none, 1=current, 2=parent. */
  136. uint infix_len = 0;
  137. uint rlen = gp_file_name_root(fname, flen);
  138. /* We need a special handling of infixes only immediately after a drive. */
  139. if (rlen != 0) {
  140. /* 'fname' is absolute, ignore the prefix. */
  141. ip = fname;
  142. ipe = fname + flen;
  143. } else {
  144. /* Concatenate with prefix. */
  145. ip = prefix;
  146. ipe = prefix + plen;
  147. rlen = gp_file_name_root(prefix, plen);
  148. }
  149. if (!append(&bp, bpe, &ip, rlen))
  150. return gp_combine_small_buffer;
  151. slen = gs_file_name_check_separator(bp, buffer - bp, bp); /* Backward search. */
  152. if (rlen != 0 && slen == 0) {
  153. /* Patch it against names like "c:dir" on Windows. */
  154. const char *sep = gp_file_name_directory_separator();
  155. slen = strlen(sep);
  156. if (!append(&bp, bpe, &sep, slen))
  157. return gp_combine_small_buffer;
  158. rlen += slen;
  159. }
  160. for (;;) {
  161. const char *item = ip;
  162. uint ilen;
  163. slen = search_separator(&ip, ipe, item, 1);
  164. ilen = ip - item;
  165. if (ilen == 0 && !gp_file_name_is_empty_item_meanful()) {
  166. ip += slen;
  167. slen = 0;
  168. } else if (gp_file_name_is_current(item, ilen)) {
  169. /* Skip the reference to 'current', except the starting one.
  170. * We keep the starting 'current' for platforms, which
  171. * require relative paths to start with it.
  172. */
  173. if (bp == buffer) {
  174. if (!append(&bp, bpe, &item, ilen))
  175. return gp_combine_small_buffer;
  176. infix_type = 1;
  177. infix_len = ilen;
  178. } else {
  179. ip += slen;
  180. slen = 0;
  181. }
  182. } else if (!gp_file_name_is_parent(item, ilen)) {
  183. if (!append(&bp, bpe, &item, ilen))
  184. return gp_combine_small_buffer;
  185. /* The 'item' pointer is now broken; it may be restored using 'ilen'. */
  186. } else if (bp == buffer + rlen + infix_len) {
  187. /* Input is a parent and the output only contains a root and an infix. */
  188. if (rlen != 0)
  189. return gp_combine_cant_handle;
  190. switch (infix_type) {
  191. case 1:
  192. /* Replace the infix with parent. */
  193. bp = buffer + rlen; /* Drop the old infix, if any. */
  194. infix_len = 0;
  195. /* Falls through. */
  196. case 0:
  197. /* We have no infix, start with parent. */
  198. if ((no_sibling && ipe == fname + flen && flen != 0) ||
  199. !gp_file_name_is_partent_allowed())
  200. return gp_combine_cant_handle;
  201. /* Falls through. */
  202. case 2:
  203. /* Append one more parent - falls through. */
  204. DO_NOTHING;
  205. }
  206. if (!append(&bp, bpe, &item, ilen))
  207. return gp_combine_small_buffer;
  208. infix_type = 2;
  209. infix_len += ilen;
  210. /* Recompute the separator length. We cannot use the old slen on Mac OS. */
  211. slen = gs_file_name_check_separator(ip, ipe - ip, ip);
  212. } else {
  213. /* Input is a parent and the output continues after infix. */
  214. /* Unappend the last separator and the last item. */
  215. uint slen1 = gs_file_name_check_separator(bp, buffer + rlen - bp, bp); /* Backward search. */
  216. char *bie = bp - slen1;
  217. bp = bie;
  218. DISCARD(search_separator((const char **)&bp, buffer + rlen, bp, -1));
  219. /* The cast above quiets a gcc warning. We believe it's a bug in the compiler. */
  220. /* Skip the input with separator. We cannot use slen on Mac OS. */
  221. ip += gs_file_name_check_separator(ip, ipe - ip, ip);
  222. if (no_sibling) {
  223. const char *p = ip;
  224. DISCARD(search_separator(&p, ipe, ip, 1));
  225. if (p - ip != bie - bp || memcmp(ip, bp, p - ip))
  226. return gp_combine_cant_handle;
  227. }
  228. slen = 0;
  229. }
  230. if (slen) {
  231. if (bp == buffer + rlen + infix_len)
  232. infix_len += slen;
  233. if (!append(&bp, bpe, &ip, slen))
  234. return gp_combine_small_buffer;
  235. }
  236. if (ip == ipe) {
  237. if (ipe == fname + flen) {
  238. /* All done.
  239. * Note that the case (prefix + plen == fname && flen == 0)
  240. * falls here without appending a separator.
  241. */
  242. const char *zero="";
  243. if (bp == buffer) {
  244. /* Must not return empty path. */
  245. const char *current = gp_file_name_current();
  246. int clen = strlen(current);
  247. if (!append(&bp, bpe, &current, clen))
  248. return gp_combine_small_buffer;
  249. }
  250. *blen = bp - buffer;
  251. if (!append(&bp, bpe, &zero, 1))
  252. return gp_combine_small_buffer;
  253. return gp_combine_success;
  254. } else {
  255. /* ipe == prefix + plen */
  256. /* Switch to fname. */
  257. ip = fname;
  258. ipe = fname + flen;
  259. if (slen == 0) {
  260. /* Insert a separator. */
  261. const char *sep;
  262. slen = search_separator(&ip, ipe, fname, 1);
  263. sep = (slen != 0 ? gp_file_name_directory_separator()
  264. : gp_file_name_separator());
  265. slen = strlen(sep);
  266. if (bp == buffer + rlen + infix_len)
  267. infix_len += slen;
  268. if (!append(&bp, bpe, &sep, slen))
  269. return gp_combine_small_buffer;
  270. ip = fname; /* Switch to fname. */
  271. }
  272. }
  273. }
  274. }
  275. }
  276. /*
  277. * Reduces parent references and current directory references when possible.
  278. * The trailing zero byte is being added.
  279. *
  280. * Examples :
  281. * "/gs/lib/../Resource/CMap/H" --> "/gs/Resource/CMap/H"
  282. * "C:/gs/lib/../Resource/CMap/H" --> "C:/gs/Resource/CMap/H"
  283. * "hard disk:gs:lib::Resource:CMap:H" -->
  284. * "hard disk:gs:Resource:CMap:H"
  285. * "DUA1:[GHOSTSCRIPT.LIB.-.RESOURCE.CMAP]H" -->
  286. * "DUA1:[GHOSTSCRIPT.RESOURCE.CMAP]H"
  287. * "\\server\share/a/b///c/../d.e/./../x.e/././/../../y.z/v.v" -->
  288. * "\\server\share/a/y.z/v.v"
  289. *
  290. */
  291. gp_file_name_combine_result
  292. gp_file_name_reduce(const char *fname, uint flen, char *buffer, uint *blen)
  293. {
  294. return gp_file_name_combine(fname, flen, fname + flen, 0, false, buffer, blen);
  295. }
  296. /*
  297. * Answers whether a file name is absolute (starts from a root).
  298. */
  299. bool
  300. gp_file_name_is_absolute(const char *fname, uint flen)
  301. {
  302. return (gp_file_name_root(fname, flen) > 0);
  303. }
  304. /*
  305. * Returns length of all starting parent references.
  306. */
  307. private uint
  308. gp_file_name_prefix(const char *fname, uint flen,
  309. bool (*test)(const char *fname, uint flen))
  310. {
  311. uint plen = gp_file_name_root(fname, flen), slen;
  312. const char *ip, *ipe;
  313. const char *item = fname; /* plen == flen could cause an indeterminizm. */
  314. if (plen > 0)
  315. return 0;
  316. ip = fname + plen;
  317. ipe = fname + flen;
  318. for (; ip < ipe; ) {
  319. item = ip;
  320. slen = search_separator(&ip, ipe, item, 1);
  321. if (!(*test)(item, ip - item))
  322. break;
  323. ip += slen;
  324. }
  325. return item - fname;
  326. }
  327. /*
  328. * Returns length of all starting parent references.
  329. */
  330. uint
  331. gp_file_name_parents(const char *fname, uint flen)
  332. {
  333. return gp_file_name_prefix(fname, flen, gp_file_name_is_parent);
  334. }
  335. /*
  336. * Returns length of all starting cwd references.
  337. */
  338. uint
  339. gp_file_name_cwds(const char *fname, uint flen)
  340. {
  341. return gp_file_name_prefix(fname, flen, gp_file_name_is_current);
  342. }