misc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* $Xorg: misc.c,v 1.4 2001/02/09 02:06:01 xorgcvs Exp $ */
  2. /******************************************************************************
  3. Copyright 1993, 1998 The Open Group
  4. Permission to use, copy, modify, distribute, and sell this software and its
  5. documentation for any purpose is hereby granted without fee, provided that
  6. the above copyright notice appear in all copies and that both that
  7. copyright notice and this permission notice appear in supporting
  8. documentation.
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of The Open Group shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from The Open Group.
  20. ******************************************************************************/
  21. /* $XFree86: xc/programs/xsm/misc.c,v 1.5 2001/07/25 15:05:29 dawes Exp $ */
  22. #include "xsm.h"
  23. #ifndef HAVE_PUTENV
  24. /*
  25. * define our own putenv() if the system doesn't have one.
  26. * putenv(s): place s (a string of the form "NAME=value") in
  27. * the environment; replacing any existing NAME. s is placed in
  28. * environment, so if you change s, the environment changes (like
  29. * putenv on a sun). Binding removed if you putenv something else
  30. * called NAME.
  31. */
  32. int
  33. putenv(char *s)
  34. {
  35. char *v;
  36. int varlen, idx;
  37. char **newenv;
  38. static int virgin = 1; /* true while "environ" is a virgin */
  39. v = strchr(s, '=');
  40. if(v == 0)
  41. return 0; /* punt if it's not of the right form */
  42. varlen = (v + 1) - s;
  43. for (idx = 0; environ[idx] != 0; idx++) {
  44. if (strncmp(environ[idx], s, varlen) == 0) {
  45. if(v[1] != 0) { /* true if there's a value */
  46. environ[idx] = s;
  47. return 0;
  48. } else {
  49. do {
  50. environ[idx] = environ[idx+1];
  51. } while(environ[++idx] != 0);
  52. return 0;
  53. }
  54. }
  55. }
  56. /* add to environment (unless no value; then just return) */
  57. if(v[1] == 0)
  58. return 0;
  59. if(virgin) {
  60. register i;
  61. newenv = (char **) XtMalloc((unsigned) ((idx + 2) * sizeof(char*)));
  62. if(newenv == 0)
  63. return -1;
  64. for(i = idx-1; i >= 0; --i)
  65. newenv[i] = environ[i];
  66. virgin = 0; /* you're not a virgin anymore, sweety */
  67. } else {
  68. newenv = (char **) realloc((char *) environ,
  69. (unsigned) ((idx + 2) * sizeof(char*)));
  70. if (newenv == 0)
  71. return -1;
  72. }
  73. environ = newenv;
  74. environ[idx] = s;
  75. environ[idx+1] = 0;
  76. return 0;
  77. }
  78. #endif /* NOPUTENV */
  79. int
  80. strbw(const char *a, const char *b)
  81. {
  82. return !strncmp (a, b, strlen (b));
  83. }
  84. #if defined(sun) && defined(SVR4)
  85. #include <sys/wait.h>
  86. int
  87. System(char *s)
  88. {
  89. int pid, status;
  90. if ((pid = fork ()) == 0) {
  91. (void) setpgrp();
  92. execl ("/bin/sh", "sh", "-c", s, NULL);
  93. } else
  94. waitpid (pid, &status, 0);
  95. return status;
  96. }
  97. #endif
  98. void
  99. nomem(void)
  100. {
  101. fprintf (stderr, "Insufficient memory.\n");
  102. exit (255);
  103. }