chdir.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Implementation of the CHDIR intrinsic.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. Contributed by François-Xavier Coudert <coudert@clipper.ens.fr>
  4. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <errno.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #ifdef HAVE_UNISTD_H
  25. #include <unistd.h>
  26. #endif
  27. /* SUBROUTINE CHDIR(DIR, STATUS)
  28. CHARACTER(len=*), INTENT(IN) :: DIR
  29. INTEGER, INTENT(OUT), OPTIONAL :: STATUS */
  30. #ifdef HAVE_CHDIR
  31. extern void chdir_i4_sub (char *, GFC_INTEGER_4 *, gfc_charlen_type);
  32. iexport_proto(chdir_i4_sub);
  33. void
  34. chdir_i4_sub (char *dir, GFC_INTEGER_4 *status, gfc_charlen_type dir_len)
  35. {
  36. int val;
  37. char *str = fc_strdup (dir, dir_len);
  38. val = chdir (str);
  39. free (str);
  40. if (status != NULL)
  41. *status = (val == 0) ? 0 : errno;
  42. }
  43. iexport(chdir_i4_sub);
  44. extern void chdir_i8_sub (char *, GFC_INTEGER_8 *, gfc_charlen_type);
  45. iexport_proto(chdir_i8_sub);
  46. void
  47. chdir_i8_sub (char *dir, GFC_INTEGER_8 *status, gfc_charlen_type dir_len)
  48. {
  49. int val;
  50. char *str = fc_strdup (dir, dir_len);
  51. val = chdir (str);
  52. free (str);
  53. if (status != NULL)
  54. *status = (val == 0) ? 0 : errno;
  55. }
  56. iexport(chdir_i8_sub);
  57. extern GFC_INTEGER_4 chdir_i4 (char *, gfc_charlen_type);
  58. export_proto(chdir_i4);
  59. GFC_INTEGER_4
  60. chdir_i4 (char *dir, gfc_charlen_type dir_len)
  61. {
  62. GFC_INTEGER_4 val;
  63. chdir_i4_sub (dir, &val, dir_len);
  64. return val;
  65. }
  66. extern GFC_INTEGER_8 chdir_i8 (char *, gfc_charlen_type);
  67. export_proto(chdir_i8);
  68. GFC_INTEGER_8
  69. chdir_i8 (char *dir, gfc_charlen_type dir_len)
  70. {
  71. GFC_INTEGER_8 val;
  72. chdir_i8_sub (dir, &val, dir_len);
  73. return val;
  74. }
  75. #endif