summaryrefslogtreecommitdiff
path: root/elf/sln.c
blob: e13b6d97b1f59502118cce11f6c39e65130602a9 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* `sln' program to create symboblic links between files.
   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>

#if !defined(S_ISDIR) && defined(S_IFDIR)
#define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif

static int makesymlink (const char *src, const char *dest);
static int makesymlinks (const char *file);

int
main (int argc, char **argv)
{
  switch (argc)
    {
    case 2:
      return makesymlinks (argv [1]) == 0 ? 0 : 1;
      break;

    case 3:
      return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
      break;

    default:
      printf ("Usage: %s src dest|file\n", argv [0]);
      return 1;
      break;
    }
}

static int
makesymlinks (file)
     const char *file;
{
#ifndef PATH_MAX
#define PATH_MAX 4095
#endif
  char buffer [PATH_MAX * 4];
  int i, ret, len;
  int lineno;
  const char *src;
  const char *dest;
  const char *error;
  FILE *fp;

  fp = fopen (file, "r");
  if (fp == NULL)
    {
      error = strerror (errno);
      fprintf (stderr, "%s: file open error: %s\n", file, error);
      return -1;
    }

  ret = 0;
  lineno = 0;
  while (fgets (buffer, sizeof (buffer) - 1, fp))
    {
      lineno++;
      src = dest = NULL;
      buffer [sizeof (buffer) - 1] = '\0';
      len = strlen (buffer);
      for (i = 0; i < len && isspace (buffer [i]); i++);
      if (i >= len)
        {
	  fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
	  	   buffer);
	  ret--;
	  continue;
	}

      src = &buffer [i];
      for (; i < len && !isspace (buffer [i]); i++);
      if (i < len)
	{
	  buffer [i++] = '\0';
	  for (; i < len && isspace (buffer [i]); i++);
	  if (i >= len)
	    {
	      fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
	      	       buffer);
	      ret--;
	      continue;
	    }
	  dest = &buffer [i];
	  for (; i < len && !isspace (buffer [i]); i++);
	  buffer [i] = '\0';
	}
      else
        {
	  fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
	  	   buffer);
	  ret--;
	  continue;
	}

      if (makesymlink (src, dest))
        {
	  fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
	  	   src, dest, lineno);
	  ret--;
	}
    }
  fclose (fp);

  return ret;
}

static int
makesymlink (src, dest)
     const char *src;
     const char *dest;
{
  struct stat stats;
  const char *error;

  /* Destination must not be a directory. */
  if (lstat (dest, &stats) == 0)
    {
      if (S_ISDIR (stats.st_mode))
	{
	  fprintf (stderr, "%s: destination must not be a directory\n",
	  	   dest);
	  return -1;
	}
      else if (unlink (dest) && errno != ENOENT)
	{
	  fprintf (stderr, "%s: failed to remove the old destination\n",
	  	   dest);
	  return -1;
	}
    }
  else if (errno != ENOENT)
    {
      error = strerror (errno);
      fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
      return -1;
    }

#ifdef S_ISLNK
  if (symlink (src, dest) == 0)
#else
  if (link (src, dest) == 0)
#endif
    {
      /* Destination must exist by now. */
      if (access (dest, F_OK))
        {
	  error = strerror (errno);
	  unlink (dest);
	  fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
	  	   src, dest, error);
	  return -1;
	}
      return 0;
    }
  else
    {
      error = strerror (errno);
      fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
      	       src, dest, error);
      return -1;
    }
}