PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cvs-1.11.23/vms/misc.c

#
C | 155 lines | 78 code | 8 blank | 69 comment | 16 complexity | d6d43f47fa18a2063715fd42f95017e9 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0
  1. /*
  2. * Copyright Š 1994 the Free Software Foundation, Inc.
  3. *
  4. * Author: Roland B. Roberts (roberts@nsrl.rochester.edu)
  5. *
  6. * This file is a part of GNU VMSLIB, the GNU library for porting GNU
  7. * software to VMS.
  8. *
  9. * GNU VMSLIB is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * GNU VMSLIB is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. /*
  20. * Miscellaneous utilities used by hackargv().
  21. * Some of these are useful in their own right.
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <lib$routines.h>
  28. /* See in misc.h why it is done like this. */
  29. void x_free (void *block)
  30. {
  31. free (block);
  32. }
  33. /*
  34. * Some string utilities.
  35. */
  36. char *downcase (char *s)
  37. {
  38. register char *t;
  39. for (t = s ; *t; t++)
  40. *t = tolower(*t);
  41. return (s);
  42. }
  43. char *strndup (char *src, int len) {
  44. char *dst = (char *) xmalloc (len + 1);
  45. strncpy (dst, src, len);
  46. dst[len] = 0;
  47. return (dst);
  48. }
  49. #include <string.h>
  50. /*
  51. * int fixpath (char *path)
  52. *
  53. * Synopsis:
  54. * `Fix' VMS pathnames, converting them to canonical form.
  55. *
  56. * Description:
  57. * The following conversions are performed
  58. * x:[y.][000000.z] --> x:[y.z]
  59. * x:[y.][z] --> x:[y.z]
  60. * x:[000000.y] --> x:[y]
  61. *
  62. * Author:
  63. * Roland B Roberts (roberts@nsrl.rochester.edu)
  64. * March 1994
  65. */
  66. int fixpath (char *path)
  67. {
  68. char *s, *d, *t;
  69. int skip = 0;
  70. d = s = path;
  71. if (t = strstr(path ,".][000000"))
  72. skip = 9;
  73. else if (t = strstr(path,"]["))
  74. skip = 2;
  75. else if (t = strstr(path,"[000000."))
  76. t++, skip = 7;
  77. if (t) {
  78. while (s < t)
  79. *d++ = *s++;
  80. s += skip;
  81. while (*d++ = *s++);
  82. }
  83. return 0;
  84. }
  85. #include <ctype.h>
  86. #include <string.h>
  87. #ifndef TRUE
  88. #define TRUE 1
  89. #define FALSE 0
  90. #endif
  91. /*
  92. * char *argvconcat (int argc, char **argv)
  93. *
  94. * Synopsis:
  95. * Concatenate all elements of argv into a single string suitable for
  96. * use as a command line.
  97. *
  98. * Description:
  99. * This is intended for use with hackargv() in order to build a command
  100. * line for background() or popen(). Each element of argv (except the
  101. * first) is surrounded by double quotes to insure the command line is
  102. * unaltered when DCL rereads it.
  103. *
  104. * Side Effect:
  105. * Space for the new string is allocated with xmalloc().
  106. *
  107. * Author:
  108. * Roland B Roberts (roberts@nsrl.rochester.edu)
  109. * March 1994
  110. */
  111. char *argvconcat (int argc, char **argv)
  112. {
  113. int i, j, n, addquotes, flags, pid, status;
  114. char *cmdline;
  115. /*
  116. * Allocate space
  117. */
  118. for (j = n = 0; j < argc; j++)
  119. n += 3 + strlen(argv[j]); /* Need 3 extra spaces, not 1; see below */
  120. cmdline = (char *) xmalloc ((n + 1) * sizeof (char));
  121. sprintf (cmdline, "%s ", argv[0]);
  122. for (j = 1, addquotes = FALSE; j < argc; j++) {
  123. /*
  124. * Add double quotes to arg if it contains uppercase of spaces.
  125. * Hence, the need to allocate three extra spaces for each argument.
  126. */
  127. for (i = 0; i < strlen(argv[j]); i++)
  128. if (isupper(argv[j][i]) || isspace(argv[j][i])) {
  129. addquotes = TRUE;
  130. break;
  131. }
  132. if (addquotes) {
  133. strcat (cmdline, argv[j]);
  134. strcat (cmdline, " ");
  135. }
  136. else {
  137. strcat (cmdline, "\""); /* use quotes to preserve case */
  138. strcat (cmdline, argv[j]);
  139. strcat (cmdline, "\" ");
  140. }
  141. }
  142. cmdline[strlen(cmdline)-1] = 0;
  143. return (cmdline);
  144. }