The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

120 lines
3.8 KiB

2 months ago
  1. /**
  2. @file
  3. @ingroup util
  4. @brief Tilde expansion.
  5. @copyright@parblock
  6. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  7. All rights reserved.
  8. Permission is hereby granted, without written agreement and without license
  9. or royalty fees, to use, copy, modify, and distribute this software and its
  10. documentation for any purpose, provided that the above copyright notice and
  11. the following two paragraphs appear in all copies of this software.
  12. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  13. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  14. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  15. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  19. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  20. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  21. @endparblock
  22. @copyright@parblock
  23. Copyright (c) 1999-2015, Regents of the University of Colorado
  24. All rights reserved.
  25. Redistribution and use in source and binary forms, with or without
  26. modification, are permitted provided that the following conditions
  27. are met:
  28. Redistributions of source code must retain the above copyright
  29. notice, this list of conditions and the following disclaimer.
  30. Redistributions in binary form must reproduce the above copyright
  31. notice, this list of conditions and the following disclaimer in the
  32. documentation and/or other materials provided with the distribution.
  33. Neither the name of the University of Colorado nor the names of its
  34. contributors may be used to endorse or promote products derived from
  35. this software without specific prior written permission.
  36. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  37. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  38. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  39. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  40. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  41. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  42. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  44. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  46. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  47. POSSIBILITY OF SUCH DAMAGE.
  48. @endparblock
  49. */
  50. #include "util.h"
  51. #ifdef BSD
  52. #include <pwd.h>
  53. #endif
  54. /**
  55. * @brief Expands tilde in a file name
  56. */
  57. char *
  58. util_tilde_expand(char const *fname)
  59. {
  60. #ifdef BSD
  61. struct passwd *userRecord;
  62. char username[256], *filename;
  63. register int i, j;
  64. filename = ALLOC(char, strlen(fname) + 256);
  65. /* Clear the return string */
  66. i = 0;
  67. filename[0] = '\0';
  68. /* Tilde? */
  69. if (fname[0] == '~') {
  70. j = 0;
  71. i = 1;
  72. while ((fname[i] != '\0') && (fname[i] != '/')) {
  73. username[j++] = fname[i++];
  74. }
  75. username[j] = '\0';
  76. if (username[0] == '\0') {
  77. /* ~/ resolves to home directory of current user */
  78. if ((userRecord = getpwuid(getuid())) != 0) {
  79. (void) strcat(filename, userRecord->pw_dir);
  80. } else {
  81. i = 0;
  82. }
  83. } else {
  84. /* ~user/ resolves to home directory of 'user' */
  85. if ((userRecord = getpwnam(username)) != 0) {
  86. (void) strcat(filename, userRecord->pw_dir);
  87. } else {
  88. i = 0;
  89. }
  90. }
  91. }
  92. /* Concantenate remaining portion of file name */
  93. (void) strcat(filename, fname + i);
  94. return filename;
  95. #else
  96. return util_strsav(fname);
  97. #endif
  98. }