pwd-fn.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * pwd-fn.c - System-independent hack for returning current wrking directory.
  3. *
  4. * Author: Russell D. Fish
  5. * Computer Science Dept.
  6. * University of Utah
  7. * Date: Thu Jun 30 1983
  8. * Copyright (c) 1983 Russell D. Fish
  9. * Status: Open Source: BSD License
  10. *
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * * Redistributions of source code must retain the relevant copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  23. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNERS OR
  25. * CONTRIBUTORS
  26. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ******************************************************************************
  35. *
  36. * Wed Mar 13 15:17:12 1985, Robert Virding
  37. * Modified to use getwd(3). This might be more 4.2BSD specific.
  38. */
  39. /* getwd(3) should be defined on all 4.2bsd or compatible Unices. If
  40. * it is not present and you comment out USE_GETWD, do check that popen
  41. * uses vfork instead of fork! It is not going to work at all on small
  42. * machines, and will be very slow on big machines, to allocate a PSL
  43. * worth of swap space (potentially many megabytes) just to exec "pwd".
  44. */
  45. #include <stdlib.h>
  46. #include "psl.h"
  47. #ifdef USE_GETWD
  48. #define MAXPATHLEN 255
  49. #include "sys/param.h" /* For MAXPATHLEN, the longest path that can occur. */
  50. char Name[MAXPATHLEN + 2]; /* Need space for '/' and a NULL at the end. */
  51. char *_(external_pwd)()
  52. { TR;
  53. char *getwd();
  54. char *p = getwd(Name);
  55. if (p)
  56. /* PSL convention, terminate directory strings with a slash. */
  57. strcat(Name, "/");
  58. else
  59. /* An error occurred, just return NULL string. */
  60. Name[0] = 0;
  61. return Name;
  62. }
  63. #else /* Popen a pwd and read its output. */
  64. #include "stdio.h"
  65. char Name[BUFSIZ];
  66. char *_(external_pwd)()
  67. { TR;
  68. FILE *popen(); /* May not be in some stdio.h files. */
  69. FILE * PwdStream;
  70. char * Where, *index();
  71. /* Simpleminded- Popen a "pwd" cmd and read a line into Name buffer. */
  72. PwdStream = popen( "pwd", "r" );
  73. if ( PwdStream == NULL ) perror( "Pwd popen" ), abort(); /* Break loop. */
  74. if (fgets(Name, BUFSIZ, PwdStream) == NULL)
  75. printf("fgets failed %d %s\n", __LINE__, __FILE__);;
  76. pclose( PwdStream );
  77. /* Trash the newline at the end of the string and follow the PSL
  78. * convention that directory strings are terminated with a slash.
  79. */
  80. if ((Where = index( Name, '\n' )) != NULL)
  81. *Where = '/';
  82. return( Name ); /* To be imported from static area to a heap string. */
  83. }
  84. #endif
  85. /* end of pwd-fn.c */