CmdUtilityP.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: CmdUtilityP.c /main/4 1995/10/26 15:02:50 rswiston $ */
  24. /***************************************************************************
  25. *
  26. * File: CmdUtilityP.c
  27. * Description: Private Utility routines for the command invocation system.
  28. * Language: C
  29. *
  30. ** (c) Copyright 1993, 1994 Hewlett-Packard Company
  31. ** (c) Copyright 1993, 1994 International Business Machines Corp.
  32. ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  33. ** (c) Copyright 1993, 1994 Novell, Inc.
  34. ***************************************************************************/
  35. #include "CmdInvP.h"
  36. #include <stdio.h>
  37. #include <sys/param.h>
  38. #include <unistd.h>
  39. #include <limits.h>
  40. #include <sys/stat.h>
  41. #include <X11/Xlib.h>
  42. #include <X11/Intrinsic.h>
  43. #include <Dt/CommandM.h>
  44. /*****************************************************************************
  45. *
  46. * _DtCmdBuildPathList - this functions builds a list of the path names
  47. * that are part of the $PATH environment variable. This is done once
  48. * during initialization and the list is then searched whenever
  49. * a command is going to be executed.
  50. *
  51. * MODIFIED:
  52. *
  53. * char **cmd_Globals.path_list; - Initialized or set to NULL if
  54. * $PATH is undefined
  55. *
  56. *****************************************************************************/
  57. void
  58. _DtCmdBuildPathList( void )
  59. {
  60. int i=0;
  61. char *path;
  62. int indx;
  63. int n=0, pathLen;
  64. char *pend, /* Points to the beginning of a directory */
  65. *pbeg; /* Points to the end of a directory */
  66. /* Get the PATH */
  67. if ((path = (char *) (getenv ("PATH"))) == NULL) {
  68. cmd_Globals.path_list = (char **) NULL;
  69. return;
  70. }
  71. pathLen = strlen(path);
  72. pbeg = path;
  73. while (i < pathLen) {
  74. n++;
  75. cmd_Globals.path_list = (char **) XtRealloc ((char *)cmd_Globals.path_list, n * sizeof (char *));
  76. if ((indx = DtStrcspn (pbeg, ":")) >= pathLen) {
  77. /* At the end of the path */
  78. i = pathLen;
  79. pend = (char *) pbeg + indx;
  80. }
  81. else {
  82. /* Found a ":" */
  83. pend = (char *) pbeg + indx;
  84. i += (pend - pbeg) +1;
  85. }
  86. cmd_Globals.path_list[n-1] = (char *) XtMalloc (((pend - pbeg) + 1) *
  87. sizeof(char));
  88. (void) strncpy (cmd_Globals.path_list[n-1], pbeg, (pend - pbeg));
  89. /* Strncpy does not put a '\0' at the EOS if s2 >= s1 */
  90. cmd_Globals.path_list[n-1][pend-pbeg] = '\0';
  91. /* Move past the ":" */
  92. if (i < pathLen)
  93. pbeg = pend +1;
  94. }
  95. /* May need to NULL terminate cmd_Globals.path_list */
  96. if (n > 0) {
  97. n++;
  98. cmd_Globals.path_list = (char **) XtRealloc ((char *) cmd_Globals.path_list, n * sizeof (char *));
  99. cmd_Globals.path_list [n-1] = (char *) NULL;
  100. }
  101. }