File: rename.c

package info (click to toggle)
bincompat 1.1.0-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 152 kB
  • ctags: 97
  • sloc: ansic: 1,291; makefile: 78; asm: 74; sh: 2
file content (85 lines) | stat: -rw-r--r-- 2,056 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define _GNU_SOURCE
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

/* This program has been hacked slightly to not call the usual libc
   initialisation code, because it doesn't need it and doing this saves
   a lot of space because it will be linked statically.  */

asm ("\
	.section \".text\"
	.align 2
	.globl _start
	.type _start,@function
_start:
 # save the stack pointer, in case we're statically linked under Linux
	mr 8,1
 # set up an initial stack frame, and clear the LR
	addi 1,1,-16
	clrrwi 1,1,4
	li 0,0
	stw 0,0(1)
	mtlr 0
 # set r13 to point at the 'small data area'
	lis 13,_SDA_BASE_@ha
	addi 13,13,_SDA_BASE_@l
 # and continue below.
	b __start1
0:
	.size	 _start,0b-_start
 # undo '.section text'.
	.previous
");

#define put_string(x) write(STDOUT_FILENO, x, strlen(x))
#define put_error(x) write(STDERR_FILENO, x, strlen(x))

int
main(int argc, char **argv)
{
  int c;
  size_t i, ct;

  if (argc != 3
      || strcmp(argv[1],"--help") == 0
      || strcmp(argv[2],"--help") == 0)
    {
      put_string ("Usage: ");
      put_string (argv[0]);
      put_string (" SOURCE DESTINATION\n"
	    "Rename SOURCE to DESTINATION, in one simple system call.\n");
      return 0;
    }
  if (rename(argv[1],argv[2]) != 0)
    {
      put_error(argv[0]);
      put_error(": ");
      put_error(sys_errlist[errno]);
      put_error(".\n");
      return 1;
    }
  return 0;
}

void
__start1(int argc, char **argv, char **envp,
	 void *auxvec, void (*exitfn) (void),
	 char **stack_on_entry)
{
  /* the PPC SVR4 ABI says that the top thing on the stack will
     be a NULL pointer, so if not we assume that we're being called
     as a statically-linked program by Linux...	 */
  if (*stack_on_entry != NULL)
    {
      /* ...in which case, we have argc as the top thing on the
	 stack, followed by argv (NULL-terminated), envp (likewise),
	 and the auxilary vector.  */
      argc = *(int *) stack_on_entry;
      argv = stack_on_entry + 1;
    }

  /* the rest of the program */
  _exit (main (argc, argv));
}