Qualify.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /************************************<+>*************************************
  24. ****************************************************************************
  25. **
  26. ** File: Qualify.c
  27. **
  28. ** RCS: $XConsortium: Qualify.c /main/3 1995/10/26 15:09:47 rswiston $
  29. **
  30. ** Project: DT
  31. **
  32. ** Description: Fully qualify a file with the first path found
  33. ** in a list of colon-separated paths
  34. **
  35. ** (c) Copyright 1993 by Hewlett-Packard Company
  36. **
  37. ****************************************************************************
  38. ************************************<+>*************************************/
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. /*********************************************************************
  43. * _DtQualifyWithFirst
  44. *
  45. * takes: an unqualified filename like foo.txt, and
  46. * a colon-separated list of pathnames, such as
  47. * /etc/dt:/usr/dt/config
  48. *
  49. * returns: a fully qualified filename. Space for the filename
  50. * has been allocated off the heap using malloc. It is
  51. * the responsibility of the calling function to dispose
  52. * of the space using free.
  53. *
  54. * example: ...
  55. * char * filename;
  56. * ...
  57. * filename = _DtQualifyWithFirst("configFile",
  58. * "/foo/first/location:/foo/second/choice");
  59. * < use filename >
  60. * free(filename);
  61. *
  62. **********************************************************************/
  63. char * _DtQualifyWithFirst
  64. (
  65. char * filename,
  66. char * searchPath
  67. )
  68. {
  69. char * paths = searchPath;
  70. char * path;
  71. char * chance;
  72. FILE * f;
  73. /* assert that the arguments cannot be NULL and cannot be empty */
  74. if (filename == NULL || searchPath == NULL ||
  75. filename[0] == 0 || searchPath[0] == 0)
  76. return NULL;
  77. while (1) {
  78. /* if there is a :, zero it */
  79. if ((path = strchr(paths, ':')) != NULL)
  80. *path = 0;
  81. /* allocate space and create the qualified filename */
  82. chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
  83. if (filename[0] == '/')
  84. sprintf(chance,"%s%s",paths,filename);
  85. else
  86. sprintf(chance,"%s/%s",paths,filename);
  87. /* see if it is there by opening it for reading */
  88. if (f = fopen(chance,"r")) {
  89. fclose(f); /* it's there so close it, .... */
  90. if (path) /* ... restore the colon, .... */
  91. *path = ':';
  92. return chance; /* return the fully qualified filename */
  93. }
  94. free(chance);
  95. if (path == NULL) /* reached the end of the list of paths */
  96. break;
  97. *path = ':'; /* restore the colon */
  98. paths = path + 1; /* try the next path */
  99. }
  100. return NULL;
  101. }