qualify.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: qualify.c /main/3 1995/10/27 16:14:33 rswiston $ */
  24. /* *
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  26. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  27. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  28. * (c) Copyright 1993, 1994 Novell, Inc. *
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. /*********************************************************************
  34. * qualifyWithFirst
  35. *
  36. * takes: an unqualified filename like foo.txt, and
  37. * a colon-separated list of pathnames, such as
  38. * /etc/opt/dt:/opt/dt/config
  39. *
  40. * returns: a fully qualified filename. Space for the filename
  41. * has been allocated off the heap. It is the responsibility
  42. * of the calling function to dispose of the space.
  43. **********************************************************************/
  44. char * qualifyWithFirst
  45. (
  46. char * filename,
  47. char * searchPath
  48. )
  49. {
  50. char * paths = NULL;
  51. char * savepaths = NULL;
  52. char * path;
  53. char * chance;
  54. FILE * f;
  55. /* assert that the arguments cannot be NULL */
  56. if (filename == NULL || searchPath == NULL)
  57. return NULL;
  58. paths = strdup(searchPath);
  59. savepaths = paths;
  60. while (1) {
  61. /* if there is a :, zero it */
  62. if ((path = strchr(paths, ':')) != NULL)
  63. *path = 0;
  64. /* allocate space and create the qualified filename */
  65. chance = (char *)malloc(strlen(paths) + strlen(filename) + 2);
  66. sprintf(chance,"%s/%s",paths,filename);
  67. /* see if it is there by opening it for reading */
  68. if (f = fopen(chance,"r")) {
  69. /* it's there so close it, .... */
  70. fclose(f);
  71. /* ... restore the colon, .... */
  72. if (path)
  73. *path = ':';
  74. /* return the fully qualified filename */
  75. free(savepaths);
  76. return chance;
  77. }
  78. free(chance);
  79. /* reached the end of the list of paths */
  80. if (path == NULL)
  81. break;
  82. /* try the next path */
  83. paths = path + 1;
  84. }
  85. free(savepaths);
  86. return NULL;
  87. }