(pthread_cancel): Don't do anything if cancelation is disabled.
[platform/upstream/glibc.git] / elf / sln.c
1 /* `sln' program to create symboblic links between files.
2    Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19 \f
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <limits.h>
28
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
31 #endif
32
33 static int makesymlink (const char *src, const char *dest);
34 static int makesymlinks (const char *file);
35
36 int
37 main (int argc, char **argv)
38 {
39   switch (argc)
40     {
41     case 2:
42       return makesymlinks (argv [1]) == 0 ? 0 : 1;
43       break;
44
45     case 3:
46       return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
47       break;
48
49     default:
50       printf ("Usage: %s src dest|file\n", argv [0]);
51       return 1;
52       break;
53     }
54 }
55
56 static int
57 makesymlinks (file)
58      const char *file;
59 {
60 #ifndef PATH_MAX
61 #define PATH_MAX 4095
62 #endif
63   char buffer [PATH_MAX * 4];
64   int i, ret, len;
65   int lineno;
66   const char *src;
67   const char *dest;
68   const char *error;
69   FILE *fp;
70
71   fp = fopen (file, "r");
72   if (fp == NULL)
73     {
74       error = strerror (errno);
75       fprintf (stderr, "%s: file open error: %s\n", file, error);
76       return -1;
77     }
78
79   ret = 0;
80   lineno = 0;
81   while (fgets (buffer, sizeof (buffer) - 1, fp))
82     {
83       lineno++;
84       src = dest = NULL;
85       buffer [sizeof (buffer) - 1] = '\0';
86       len = strlen (buffer);
87       for (i = 0; i < len && isspace (buffer [i]); i++);
88       if (i >= len)
89         {
90           fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
91                    buffer);
92           ret--;
93           continue;
94         }
95
96       src = &buffer [i];
97       for (; i < len && !isspace (buffer [i]); i++);
98       if (i < len)
99         {
100           buffer [i++] = '\0';
101           for (; i < len && isspace (buffer [i]); i++);
102           if (i >= len)
103             {
104               fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
105                        buffer);
106               ret--;
107               continue;
108             }
109           dest = &buffer [i];
110           for (; i < len && !isspace (buffer [i]); i++);
111           buffer [i] = '\0';
112         }
113       else
114         {
115           fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
116                    buffer);
117           ret--;
118           continue;
119         }
120
121       if (makesymlink (src, dest))
122         {
123           fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
124                    src, dest, lineno);
125           ret--;
126         }
127     }
128   fclose (fp);
129
130   return ret;
131 }
132
133 static int
134 makesymlink (src, dest)
135      const char *src;
136      const char *dest;
137 {
138   struct stat stats;
139   const char *error;
140
141   /* Destination must not be a directory. */
142   if (lstat (dest, &stats) == 0)
143     {
144       if (S_ISDIR (stats.st_mode))
145         {
146           fprintf (stderr, "%s: destination must not be a directory\n",
147                    dest);
148           return -1;
149         }
150       else if (unlink (dest) && errno != ENOENT)
151         {
152           fprintf (stderr, "%s: failed to remove the old destination\n",
153                    dest);
154           return -1;
155         }
156     }
157   else if (errno != ENOENT)
158     {
159       error = strerror (errno);
160       fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
161       return -1;
162     }
163
164 #ifdef S_ISLNK
165   if (symlink (src, dest) == 0)
166 #else
167   if (link (src, dest) == 0)
168 #endif
169     {
170       /* Destination must exist by now. */
171       if (access (dest, F_OK))
172         {
173           error = strerror (errno);
174           unlink (dest);
175           fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
176                    src, dest, error);
177           return -1;
178         }
179       return 0;
180     }
181   else
182     {
183       error = strerror (errno);
184       fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
185                src, dest, error);
186       return -1;
187     }
188 }