summaryrefslogtreecommitdiff
path: root/main.c
blob: 455633805965a6f7f41a15891c1fdc634bb801d2 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Author: Petter Reinholdtsen <pere@hungry.com>
 * date:   2001-01-20
 *
 * Alter ELF rpath information (insert, modify, remove).
 *
 * Based on source from Peeter Joot <peeterj@ca.ibm.com> and Geoffrey
 * Keating <geoffk@ozemail.com.au>.
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "protos.h"

#ifdef HAVE_GETOPT_LONG
#  define GETOPT_LONG getopt_long

static struct option long_options[] =
{
  {"convert",   0, 0, 'c'},
  {"delete",    0, 0, 'd'},
  {"help",      0, 0, 'h'},
  {"keepgoing", 0, 0, 'k'},
  {"list",      0, 0, 'l'},
  {"replace",   1, 0, 'r'},
  {"version",   0, 0, 'v'}
};

#else /* not HAVE_GETOPT_LONG */
#  define GETOPT_LONG(argc,argv,optstr,lopts,lidx) getopt(argc,argv,optstr)
#endif /* not HAVE_GETOPT_LONG */

static void
usage(char *progname)
{
  printf("Usage: %s [-v|-d|-c|-r <path>] <program> [<program> ...]\n\n",
         progname);
  printf("   -v|--version                Display program version number\n");
  printf("   -d|--delete                 Delete current rpath/runpath setting\n");
#if defined(DT_RUNPATH)
  printf("   -c|--convert                Convert rpath to runpath\n");
#endif /* DT_RUNPATH */
  printf("   -r <path>|--replace <path>  Replace current rpath/runpath setting\n");
  printf("                               with the path given\n");
  printf("   -l|--list                   List the current rpath/runpath (default)\n");
  printf("   -h|--help                   Show this usage information.\n");
#ifndef HAVE_GETOPT_LONG
  printf("\n *** The long options are not available on this platform");
#endif /* not HAVE_GETOPT_LONG */
#if !defined(DT_RUNPATH)
  printf("\n *** There is no support for runpath on this platform");
#endif /* DT_RUNPATH */
  printf("\n");
}

int
main(int argc, char * const argv[])
{
  int retval = 0;
  int convert = 0;      /* convert to given type */
  int remove = 0;       /* remove or not */
  int keep_going = 0;   /* Break on first error, or keep going? */
  char *newpath = NULL; /* insert this path */
  int opt;
#ifdef HAVE_GETOPT_LONG
  int option_index = 0;
#endif /* HAVE_GETOPT_LONG */

  if (argc < 2)
    {
      usage(argv[0]);
      return 1;
    }

  do {
    opt = GETOPT_LONG(argc, argv, "cdhklr:v", long_options, &option_index);
    switch (opt)
      {
#if defined(DT_RUNPATH)
      case 'c':
        convert = 1;
        break;
#endif /* DT_RUNPATH */
      case 'd':
        remove = 1;
        break;
      case 'k':
        keep_going = 1;
        break;
      case 'r':
        newpath = optarg;
        break;
      case 'v':
        printf("%s version %s\n", PACKAGE, VERSION);
        exit(0);
        break;
      case 'l': /* This is the default action */
        newpath = NULL;
        break;
      case -1:
        break;
      default:
        printf("Invalid argument '%c'\n", opt);
      case 'h':
        usage(argv[0]);
        exit(0);
        break;
      }
  } while (-1 != opt);

  while (optind < argc && (!retval || keep_going))
    {
      if (remove)
        retval |= killrpath(argv[optind++]);
      else
        /* list by default, replace if path is set */
        retval |= chrpath(argv[optind++], newpath, convert);
    }

  return retval;
}