(root)/
strace-6.5/
tests-m32/
readlink.c
       1  /*
       2   * Copyright (c) 2015 Gleb Fotengauer-Malinovskiy <glebfm@altlinux.org>
       3   * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@strace.io>
       4   * Copyright (c) 2015-2021 The strace developers.
       5   * All rights reserved.
       6   *
       7   * SPDX-License-Identifier: GPL-2.0-or-later
       8   */
       9  
      10  #include "tests.h"
      11  #include "scno.h"
      12  
      13  #ifdef __NR_readlink
      14  
      15  # include <stdio.h>
      16  # include <unistd.h>
      17  
      18  # define PREFIX "test.readlink"
      19  # define TARGET (PREFIX ".target")
      20  # define LINKPATH (PREFIX ".link")
      21  
      22  int
      23  main(void)
      24  {
      25  	const char * const fname = tail_memdup(LINKPATH, sizeof(LINKPATH));
      26  	const char * const hex_fname =
      27  		hexquote_strndup(fname, sizeof(LINKPATH) - 1);
      28  
      29  	const unsigned int size = sizeof(TARGET) - 1;
      30  	char * const buf = tail_alloc(size);
      31  
      32  	(void) unlink(fname);
      33  
      34  	long rc = syscall(__NR_readlink, fname, buf, size);
      35  	printf("readlink(\"%s\", %p, %u) = -1 ENOENT (%m)\n",
      36  	       hex_fname, buf, size);
      37  
      38  	if (symlink(TARGET, fname))
      39  		perror_msg_and_fail("symlink");
      40  
      41  	rc = syscall(__NR_readlink, fname, buf, size);
      42  	if (rc < 0) {
      43  		perror("readlink");
      44  		(void) unlink(fname);
      45  		return 77;
      46  	}
      47  	const char * const hex_buf = hexquote_strndup(buf, size);
      48  	printf("readlink(\"%s\", \"%s\", %u) = %u\n",
      49  	       hex_fname, hex_buf, size, size);
      50  
      51  	if (unlink(fname))
      52  		perror_msg_and_fail("unlink");
      53  
      54  	puts("+++ exited with 0 +++");
      55  	return 0;
      56  }
      57  
      58  #else
      59  
      60  SKIP_MAIN_UNDEFINED("__NR_readlink")
      61  
      62  #endif