Back to index

glibc  2.9
sln.c
Go to the documentation of this file.
00001 /* `sln' program to create symbolic links between files.
00002    Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <error.h>
00021 #include <errno.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 #include <unistd.h>
00025 #include <errno.h>
00026 #include <ctype.h>
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <limits.h>
00030 
00031 #if !defined S_ISDIR && defined S_IFDIR
00032 #define       S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
00033 #endif
00034 
00035 static int makesymlink (const char *src, const char *dest);
00036 static int makesymlinks (const char *file);
00037 
00038 int
00039 main (int argc, char **argv)
00040 {
00041   switch (argc)
00042     {
00043     case 2:
00044       return makesymlinks (argv [1]);
00045       break;
00046 
00047     case 3:
00048       return makesymlink (argv [1], argv [2]);
00049       break;
00050 
00051     default:
00052       printf ("Usage: %s src dest|file\n", argv [0]);
00053       return 1;
00054       break;
00055     }
00056 }
00057 
00058 static int
00059 makesymlinks (file)
00060      const char *file;
00061 {
00062 #ifndef PATH_MAX
00063 #define PATH_MAX 4095
00064 #endif
00065   char *buffer = NULL;
00066   size_t bufferlen = 0;
00067   int ret;
00068   int lineno;
00069   FILE *fp;
00070 
00071   if (strcmp (file, "-") == 0)
00072     fp = stdin;
00073   else
00074     {
00075       fp = fopen (file, "r");
00076       if (fp == NULL)
00077        {
00078          fprintf (stderr, "%s: file open error: %m\n", file);
00079          return 1;
00080        }
00081     }
00082 
00083   ret = 0;
00084   lineno = 0;
00085   while (!feof_unlocked (fp))
00086     {
00087       ssize_t n = getline (&buffer, &bufferlen, fp);
00088       char *src;
00089       char *dest;
00090       char *cp = buffer;
00091 
00092       if (n < 0)
00093        break;
00094       if (buffer[n - 1] == '\n')
00095        buffer[n - 1] = '\0';
00096 
00097       ++lineno;
00098       while (isspace (*cp))
00099        ++cp;
00100       if (*cp == '\0')
00101        /* Ignore empty lines.  */
00102        continue;
00103       src = cp;
00104 
00105       do
00106        ++cp;
00107       while (*cp != '\0' && ! isspace (*cp));
00108       if (*cp != '\0')
00109        *cp++ = '\0';
00110 
00111       while (isspace (*cp))
00112        ++cp;
00113       if (*cp == '\0')
00114        {
00115          fprintf (stderr, "No target in line %d\n", lineno);
00116          ret = 1;
00117          continue;
00118        }
00119       dest = cp;
00120 
00121       do
00122        ++cp;
00123       while (*cp != '\0' && ! isspace (*cp));
00124       if (*cp != '\0')
00125        *cp++ = '\0';
00126 
00127       ret |= makesymlink (src, dest);
00128     }
00129   fclose (fp);
00130 
00131   return ret;
00132 }
00133 
00134 static int
00135 makesymlink (src, dest)
00136      const char *src;
00137      const char *dest;
00138 {
00139   struct stat stats;
00140   const char *error;
00141 
00142   /* Destination must not be a directory. */
00143   if (lstat (dest, &stats) == 0)
00144     {
00145       if (S_ISDIR (stats.st_mode))
00146        {
00147          fprintf (stderr, "%s: destination must not be a directory\n",
00148                  dest);
00149          return 1;
00150        }
00151       else if (unlink (dest) && errno != ENOENT)
00152        {
00153          fprintf (stderr, "%s: failed to remove the old destination\n",
00154                  dest);
00155          return 1;
00156        }
00157     }
00158   else if (errno != ENOENT)
00159     {
00160       error = strerror (errno);
00161       fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
00162       return -1;
00163     }
00164 
00165 #ifdef S_ISLNK
00166   if (symlink (src, dest) == 0)
00167 #else
00168   if (link (src, dest) == 0)
00169 #endif
00170     {
00171       /* Destination must exist by now. */
00172       if (access (dest, F_OK))
00173         {
00174          error = strerror (errno);
00175          unlink (dest);
00176          fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
00177                  src, dest, error);
00178          return 1;
00179        }
00180       return 0;
00181     }
00182   else
00183     {
00184       error = strerror (errno);
00185       fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
00186               src, dest, error);
00187       return 1;
00188     }
00189 }