(root)/
strace-6.5/
tests/
subdir.c
       1  /*
       2   * Copyright (c) 2021 The strace developers.
       3   * All rights reserved.
       4   *
       5   * SPDX-License-Identifier: GPL-2.0-or-later
       6   */
       7  
       8  #include "tests.h"
       9  
      10  #include <dirent.h>
      11  #include <unistd.h>
      12  #include <sys/stat.h>
      13  
      14  static const char *subdir;
      15  static DIR *dirp;
      16  
      17  void
      18  create_and_enter_subdir(const char *name)
      19  {
      20  	dirp = opendir(".");
      21  	if (!dirp)
      22  		perror_msg_and_fail("opendir: %s", ".");
      23  	(void) mkdir(name, 0700);
      24  	if (chdir(name))
      25  		perror_msg_and_fail("chdir: %s", name);
      26  	subdir = name;
      27  }
      28  
      29  void
      30  leave_and_remove_subdir(void)
      31  {
      32  	if (fchdir(dirfd(dirp)))
      33  		perror_msg_and_fail("fchdir: %d", dirfd(dirp));
      34  	if (closedir(dirp))
      35  		perror_msg_and_fail("closedir");
      36  	dirp = 0;
      37  	if (rmdir(subdir))
      38  		perror_msg_and_fail("rmdir: %s", subdir);
      39  	subdir = 0;
      40  }