PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/C/MISC.C

#
C | 196 lines | 107 code | 13 blank | 76 comment | 22 complexity | 6a7044de33633a9d8a231100b8c876a7 MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0
  1. /*
  2. * GNU vmslib - Helps people port GNU software to VMS.
  3. * Copyright (C) 1994-2008 the Free Software Foundation, Inc.
  4. *
  5. * Author: Roland B. Roberts <roland@astrofoto.org>
  6. * Maintainer: Chris Bryant <chrisbryant@ucla.edu>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*
  22. * Miscellaneous utilities used by hackargv().
  23. * Some of these are useful in their own right.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <lib$routines.h>
  30. /* Print message and force core dump (unix) or traceback (vms)
  31. * These are taken from other GNU software more-or-less as-is
  32. */
  33. void fatal (char *s)
  34. {
  35. int *x;
  36. printf ("%s\n", s);
  37. lib$signal(44);
  38. }
  39. void *xmalloc (int size)
  40. {
  41. register void *val;
  42. /* Avoid failure if malloc (0) returns 0. */
  43. if (size == 0)
  44. size = 1;
  45. val = (void *) malloc (size);
  46. if (!val) fatal ("xmalloc: can't satisfy request");
  47. return val;
  48. }
  49. void *xrealloc (void *block, int size)
  50. {
  51. register void *val;
  52. /* Avoid failure if malloc (0) returns 0. */
  53. if (size == 0)
  54. size = 1;
  55. val = (void *) realloc (block, size);
  56. if (!val) fatal ("xrealloc: can't satisfy request");
  57. return val;
  58. }
  59. /* See in misc.h why it is done like this. */
  60. void x_free (void *block)
  61. {
  62. free (block);
  63. }
  64. /*
  65. * Some string utilities.
  66. */
  67. char *downcase (char *s)
  68. {
  69. register char *t;
  70. for (t = s ; *t; t++)
  71. *t = tolower(*t);
  72. return (s);
  73. }
  74. char *strdup (char *src) {
  75. char *dst = (char*) xmalloc (strlen(src) + 1);
  76. strcpy (dst, src);
  77. return (dst);
  78. }
  79. char *strndup (char *src, int len) {
  80. char *dst = (char *) xmalloc (len + 1);
  81. strncpy (dst, src, len);
  82. dst[len] = 0;
  83. return (dst);
  84. }
  85. #include <string.h>
  86. /*
  87. * int fixpath (char *path)
  88. *
  89. * Synopsis:
  90. * `Fix' VMS pathnames, converting them to canonical form.
  91. *
  92. * Description:
  93. * The following conversions are performed
  94. * x:[y.][000000.z] --> x:[y.z]
  95. * x:[y.][z] --> x:[y.z]
  96. * x:[000000.y] --> x:[y]
  97. *
  98. * Author:
  99. * Roland B Roberts (roberts@nsrl.rochester.edu)
  100. * March 1994
  101. */
  102. int fixpath (char *path)
  103. {
  104. char *s, *d, *t;
  105. int skip = 0;
  106. d = s = path;
  107. if (t = strstr(path ,".][000000"))
  108. skip = 9;
  109. else if (t = strstr(path,"]["))
  110. skip = 2;
  111. else if (t = strstr(path,"[000000."))
  112. t++, skip = 7;
  113. if (t) {
  114. while (s < t)
  115. *d++ = *s++;
  116. s += skip;
  117. while (*d++ = *s++);
  118. }
  119. return 0;
  120. }
  121. #include <ctype.h>
  122. #include <string.h>
  123. #ifndef TRUE
  124. #define TRUE 1
  125. #define FALSE 0
  126. #endif
  127. /*
  128. * char *argvconcat (int argc, char **argv)
  129. *
  130. * Synopsis:
  131. * Concatenate all elements of argv into a single string suitable for
  132. * use as a command line.
  133. *
  134. * Description:
  135. * This is intended for use with hackargv() in order to build a command
  136. * line for background() or popen(). Each element of argv (except the
  137. * first) is surrounded by double quotes to insure the command line is
  138. * unaltered when DCL rereads it.
  139. *
  140. * Side Effect:
  141. * Space for the new string is allocated with xmalloc().
  142. *
  143. * Author:
  144. * Roland B Roberts (roberts@nsrl.rochester.edu)
  145. * March 1994
  146. */
  147. char *argvconcat (int argc, char **argv)
  148. {
  149. int i, j, n, addquotes, flags, pid, status;
  150. char *cmdline;
  151. /*
  152. * Allocate space
  153. */
  154. for (j = n = 0; j < argc; j++)
  155. n += 3 + strlen(argv[j]); /* Need 3 extra spaces, not 1; see below */
  156. cmdline = (char *) xmalloc ((n + 1) * sizeof (char));
  157. sprintf (cmdline, "%s ", argv[0]);
  158. for (j = 1, addquotes = FALSE; j < argc; j++) {
  159. /*
  160. * Add double quotes to arg if it contains uppercase of spaces.
  161. * Hence, the need to allocate three extra spaces for each argument.
  162. */
  163. for (i = 0; i < strlen(argv[j]); i++)
  164. if (isupper(argv[j][i]) || isspace(argv[j][i])) {
  165. addquotes = TRUE;
  166. break;
  167. }
  168. if (addquotes) {
  169. strcat (cmdline, argv[j]);
  170. strcat (cmdline, " ");
  171. }
  172. else {
  173. strcat (cmdline, "\""); /* use quotes to preserve case */
  174. strcat (cmdline, argv[j]);
  175. strcat (cmdline, "\" ");
  176. }
  177. }
  178. cmdline[strlen(cmdline)-1] = 0;
  179. return (cmdline);
  180. }