home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 19 / AACD19.BIN / AACD / Programming / cvs-1.11 / source / src / filesubr.c next >
Encoding:
C/C++ Source or Header  |  2001-02-03  |  22.6 KB  |  1,013 lines

  1. /* filesubr.c --- subroutines for dealing with files
  2.    Jim Blandy <jimb@cyclic.com>
  3.  
  4.    This file is part of GNU CVS.
  5.  
  6.    GNU CVS is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by the
  8.    Free Software Foundation; either version 2, or (at your option) any
  9.    later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.  */
  15.  
  16. /* These functions were moved out of subr.c because they need different
  17.    definitions under operating systems (like, say, Windows NT) with different
  18.    file system semantics.  */
  19.  
  20. #include "cvs.h"
  21.  
  22. static int deep_remove_dir PROTO((const char *path));
  23.  
  24. /*
  25.  * Copies "from" to "to".
  26.  */
  27. void
  28. copy_file (from, to)
  29.     const char *from;
  30.     const char *to;
  31. {
  32.     struct stat sb;
  33.     struct utimbuf t;
  34.     int fdin, fdout;
  35.  
  36.     if (trace)
  37.     (void) fprintf (stderr, "%s-> copy(%s,%s)\n",
  38.             CLIENT_SERVER_STR, from, to);
  39.     if (noexec)
  40.     return;
  41.  
  42.     /* If the file to be copied is a link or a device, then just create
  43.        the new link or device appropriately. */
  44.     if (islink (from))
  45.     {
  46.     char *source = xreadlink (from);
  47.     symlink (source, to);
  48.     free (source);
  49.     return;
  50.     }
  51.  
  52.     if (isdevice (from))
  53.     {
  54. #if defined(HAVE_MKNOD) && defined(HAVE_ST_RDEV)
  55.     if (stat (from, &sb) < 0)
  56.         error (1, errno, "cannot stat %s", from);
  57.     mknod (to, sb.st_mode, sb.st_rdev);
  58. #else
  59.     error (1, 0, "cannot copy device files on this system (%s)", from);
  60. #endif
  61.     }
  62.     else
  63.     {
  64.     /* Not a link or a device... probably a regular file. */
  65.     if ((fdin = open (from, O_RDONLY, 0)) < 0) /* olsen */
  66.         error (1, errno, "cannot open %s for copying", from);
  67.     if (fstat (fdin, &sb) < 0)
  68.         error (1, errno, "cannot fstat %s", from);
  69.     if ((fdout = creat (to, (int) sb.st_mode & 07777)) < 0)
  70.         error (1, errno, "cannot create %s for copying", to);
  71.     if (sb.st_size > 0)
  72.     {
  73.         char buf[BUFSIZ];
  74.         int n;
  75.         
  76.         for (;;) 
  77.         {
  78.         n = read (fdin, buf, sizeof(buf));
  79.         if (n == -1)
  80.         {
  81. #ifdef EINTR
  82.             if (errno == EINTR)
  83.             continue;
  84. #endif
  85.             error (1, errno, "cannot read file %s for copying", from);
  86.         }
  87.         else if (n == 0) 
  88.             break;
  89.         
  90.         if (write(fdout, buf, n) != n) {
  91.             error (1, errno, "cannot write file %s for copying", to);
  92.         }
  93.         }
  94.  
  95. #ifdef HAVE_FSYNC
  96.         if (fsync (fdout)) 
  97.         error (1, errno, "cannot fsync file %s after copying", to);
  98. #endif
  99.     }
  100.  
  101.     if (close (fdin) < 0) 
  102.         error (0, errno, "cannot close %s", from);
  103.     if (close (fdout) < 0)
  104.         error (1, errno, "cannot close %s", to);
  105.     }
  106.  
  107.     /* now, set the times for the copied file to match those of the original */
  108.     memset ((char *) &t, 0, sizeof (t));
  109.     t.actime = sb.st_atime;
  110.     t.modtime = sb.st_mtime;
  111.     (void) utime (to, &t);
  112.  
  113.     #ifdef AMIGA
  114.     (void) chmod (to, sb.st_mode | S_IREAD | S_IWRITE | S_IDELETE);
  115.     #endif /* AMIGA */
  116. }
  117.  
  118. /* FIXME-krp: these functions would benefit from caching the char * &
  119.    stat buf.  */
  120.  
  121. /*
  122.  * Returns non-zero if the argument file is a directory, or is a symbolic
  123.  * link which points to a directory.
  124.  */
  125. int
  126. isdir (file)
  127.     const char *file;
  128. {
  129.     struct stat sb;
  130.  
  131.     if (stat (file, &sb) < 0)
  132.     return (0);
  133.     return (S_ISDIR (sb.st_mode));
  134. }
  135.  
  136. /*
  137.  * Returns non-zero if the argument file is a symbolic link.
  138.  */
  139. int
  140. islink (file)
  141.     const char *file;
  142. {
  143. #ifdef S_ISLNK
  144.     struct stat sb;
  145.  
  146.     if (CVS_LSTAT (file, &sb) < 0)
  147.     return (0);
  148.     return (S_ISLNK (sb.st_mode));
  149. #else
  150.     return (0);
  151. #endif
  152. }
  153.  
  154. /*
  155.  * Returns non-zero if the argument file is a block or
  156.  * character special device.
  157.  */
  158. int
  159. isdevice (file)
  160.     const char *file;
  161. {
  162.     struct stat sb;
  163.  
  164.     if (CVS_LSTAT (file, &sb) < 0)
  165.     return (0);
  166. #ifdef S_ISBLK
  167.     if (S_ISBLK (sb.st_mode))
  168.     return 1;
  169. #endif
  170. #ifdef S_ISCHR
  171.     if (S_ISCHR (sb.st_mode))
  172.     return 1;
  173. #endif
  174.     return 0;
  175. }
  176.  
  177. /*
  178.  * Returns non-zero if the argument file exists.
  179.  */
  180. int
  181. isfile (file)
  182.     const char *file;
  183. {
  184.     return isaccessible(file, F_OK);
  185. }
  186.  
  187. /*
  188.  * Returns non-zero if the argument file is readable.
  189.  */
  190. int
  191. isreadable (file)
  192.     const char *file;
  193. {
  194.     return isaccessible(file, R_OK);
  195. }
  196.  
  197. /*
  198.  * Returns non-zero if the argument file is writable.
  199.  */
  200. int
  201. iswritable (file)
  202.     const char *file;
  203. {
  204.     return isaccessible(file, W_OK);
  205. }
  206.  
  207. /*
  208.  * Returns non-zero if the argument file is accessable according to
  209.  * mode.  If compiled with SETXID_SUPPORT also works if cvs has setxid
  210.  * bits set.
  211.  */
  212. int
  213. isaccessible (file, mode)
  214.     const char *file;
  215.     const int mode;
  216. {
  217. #ifdef SETXID_SUPPORT
  218.     struct stat sb;
  219.     int umask = 0;
  220.     int gmask = 0;
  221.     int omask = 0;
  222.     int uid;
  223.     
  224.     if (stat(file, &sb) == -1)
  225.     return 0;
  226.     if (mode == F_OK)
  227.     return 1;
  228.  
  229.     uid = geteuid();
  230.     if (uid == 0)        /* superuser */
  231.     {
  232.     if (mode & X_OK)
  233.         return sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH);
  234.     else
  235.         return 1;
  236.     }
  237.     
  238.     if (mode & R_OK)
  239.     {
  240.     umask |= S_IRUSR;
  241.     gmask |= S_IRGRP;
  242.     omask |= S_IROTH;
  243.     }
  244.     if (mode & W_OK)
  245.     {
  246.     umask |= S_IWUSR;
  247.     gmask |= S_IWGRP;
  248.     omask |= S_IWOTH;
  249.     }
  250.     if (mode & X_OK)
  251.     {
  252.     umask |= S_IXUSR;
  253.     gmask |= S_IXGRP;
  254.     omask |= S_IXOTH;
  255.     }
  256.  
  257.     if (sb.st_uid == uid)
  258.     return (sb.st_mode & umask) == umask;
  259.     else if (sb.st_gid == getegid())
  260.     return (sb.st_mode & gmask) == gmask;
  261.     else
  262.     return (sb.st_mode & omask) == omask;
  263. #else
  264.     return access(file, mode) == 0;
  265. #endif
  266. }
  267.  
  268. /*
  269.  * Open a file and die if it fails
  270.  */
  271. FILE *
  272. open_file (name, mode)
  273.     const char *name;
  274.     const char *mode;
  275. {
  276.     FILE *fp;
  277.  
  278.     if ((fp = fopen (name, mode)) == NULL)
  279.     error (1, errno, "cannot open %s", name);
  280.     return (fp);
  281. }
  282.  
  283. /*
  284.  * Make a directory and die if it fails
  285.  */
  286. void
  287. make_directory (name)
  288.     const char *name;
  289. {
  290.     struct stat sb;
  291.  
  292.     if (stat (name, &sb) == 0 && (!S_ISDIR (sb.st_mode)))
  293.         error (0, 0, "%s already exists but is not a directory", name);
  294.     if (!noexec && mkdir (name, 0777) < 0)
  295.     error (1, errno, "cannot make directory %s", name);
  296. }
  297.  
  298. /*
  299.  * Make a path to the argument directory, printing a message if something
  300.  * goes wrong.
  301.  */
  302. void
  303. make_directories (name)
  304.     const char *name;
  305. {
  306.     char *cp;
  307.  
  308.     if (noexec)
  309.     return;
  310.  
  311.     if (mkdir (name, 0777) == 0 || errno == EEXIST)
  312.     return;
  313.     if (! existence_error (errno))
  314.     {
  315.     error (0, errno, "cannot make path to %s", name);
  316.     return;
  317.     }
  318.     if ((cp = strrchr (name, '/')) == NULL)
  319.     return;
  320.     *cp = '\0';
  321.     make_directories (name);
  322.     *cp++ = '/';
  323.     if (*cp == '\0')
  324.     return;
  325.     (void) mkdir (name, 0777);
  326. }
  327.  
  328. /* Create directory NAME if it does not already exist; fatal error for
  329.    other errors.  Returns 0 if directory was created; 1 if it already
  330.    existed.  */
  331. int
  332. mkdir_if_needed (name)
  333.     char *name;
  334. {
  335.     if (mkdir (name, 0777) < 0)
  336.     {
  337.     if (!(errno == EEXIST
  338.           || (errno == EACCES && isdir (name))))
  339.         error (1, errno, "cannot make directory %s", name);
  340.     return 1;
  341.     }
  342.     return 0;
  343. }
  344.  
  345. /*
  346.  * Change the mode of a file, either adding write permissions, or removing
  347.  * all write permissions.  Either change honors the current umask setting.
  348.  *
  349.  * Don't do anything if PreservePermissions is set to `yes'.  This may
  350.  * have unexpected consequences for some uses of xchmod.
  351.  */
  352. void
  353. xchmod (fname, writable)
  354.     char *fname;
  355.     int writable;
  356. {
  357.     struct stat sb;
  358.     mode_t mode, oumask;
  359.  
  360.     if (preserve_perms)
  361.     return;
  362.  
  363.     if (stat (fname, &sb) < 0)
  364.     {
  365.     if (!noexec)
  366.         error (0, errno, "cannot stat %s", fname);
  367.     return;
  368.     }
  369.     oumask = umask (0);
  370.     (void) umask (oumask);
  371.     if (writable)
  372.     {
  373.     mode = sb.st_mode | (~oumask
  374.                  & (((sb.st_mode & S_IRUSR) ? S_IWUSR : 0)
  375.                 | ((sb.st_mode & S_IRGRP) ? S_IWGRP : 0)
  376.                 | ((sb.st_mode & S_IROTH) ? S_IWOTH : 0)));
  377.     }
  378.     else
  379.     {
  380.     mode = sb.st_mode & ~(S_IWRITE | S_IWGRP | S_IWOTH) & ~oumask;
  381.     }
  382.  
  383.     if (trace)
  384.     (void) fprintf (stderr, "%s-> chmod(%s,%o)\n",
  385.             CLIENT_SERVER_STR, fname,
  386.             (unsigned int) mode);
  387.     if (noexec)
  388.     return;
  389.  
  390.     if (chmod (fname, mode) < 0)
  391.     error (0, errno, "cannot change mode of file %s", fname);
  392. }
  393.  
  394. /*
  395.  * Rename a file and die if it fails
  396.  */
  397. void
  398. rename_file (from, to)
  399.     const char *from;
  400.     const char *to;
  401. {
  402.     if (trace)
  403.     (void) fprintf (stderr, "%s-> rename(%s,%s)\n",
  404.             CLIENT_SERVER_STR, from, to);
  405.     if (noexec)
  406.     return;
  407.  
  408.     if (rename (from, to) < 0)
  409.     error (1, errno, "cannot rename file %s to %s", from, to);
  410. }
  411.  
  412. /*
  413.  * unlink a file, if possible.
  414.  */
  415. int
  416. unlink_file (f)
  417.     const char *f;
  418. {
  419.     if (trace)
  420.     (void) fprintf (stderr, "%s-> unlink(%s)\n",
  421.             CLIENT_SERVER_STR, f);
  422.     if (noexec)
  423.     return (0);
  424.  
  425.     return (unlink (f));
  426. }
  427.  
  428. /*
  429.  * Unlink a file or dir, if possible.  If it is a directory do a deep
  430.  * removal of all of the files in the directory.  Return -1 on error
  431.  * (in which case errno is set).
  432.  */
  433. #ifndef AMIGA
  434. int
  435. unlink_file_dir (f)
  436.     const char *f;
  437. {
  438.     struct stat sb;
  439.  
  440.     if (trace
  441. #ifdef SERVER_SUPPORT
  442.     /* This is called by the server parent process in contexts where
  443.        it is not OK to send output (e.g. after we sent "ok" to the
  444.        client).  */
  445.     && !server_active
  446. #endif
  447.     )
  448.     (void) fprintf (stderr, "-> unlink_file_dir(%s)\n", f);
  449.  
  450.     if (noexec)
  451.     return (0);
  452.  
  453.     /* For at least some unices, if root tries to unlink() a directory,
  454.        instead of doing something rational like returning EISDIR,
  455.        the system will gleefully go ahead and corrupt the filesystem.
  456.        So we first call stat() to see if it is OK to call unlink().  This
  457.        doesn't quite work--if someone creates a directory between the
  458.        call to stat() and the call to unlink(), we'll still corrupt
  459.        the filesystem.  Where is the Unix Haters Handbook when you need
  460.        it?  */
  461.     if (stat (f, &sb) < 0)
  462.     {
  463.     if (existence_error (errno))
  464.     {
  465.         /* The file or directory doesn't exist anyhow.  */
  466.         return -1;
  467.     }
  468.     }
  469.     else if (S_ISDIR (sb.st_mode))
  470.     return deep_remove_dir (f);
  471.  
  472.     return unlink (f);
  473. }
  474. #endif /* AMIGA */
  475.  
  476. /* Remove a directory and everything it contains.  Returns 0 for
  477.  * success, -1 for failure (in which case errno is set).
  478.  */
  479.  
  480. static int
  481. deep_remove_dir (path)
  482.     const char *path;
  483. {
  484.     DIR          *dirp;
  485.     struct dirent *dp;
  486.  
  487.     if (rmdir (path) != 0)
  488.     {
  489.     if (errno == ENOTEMPTY
  490.         || errno == EEXIST
  491.         /* Ugly workaround for ugly AIX 4.1 (and 3.2) header bug
  492.            (it defines ENOTEMPTY and EEXIST to 17 but actually
  493.            returns 87).  */
  494.         || (ENOTEMPTY == 17 && EEXIST == 17 && errno == 87))
  495.     {
  496.         if ((dirp = opendir (path)) == NULL)
  497.         /* If unable to open the directory return
  498.          * an error
  499.          */
  500.         return -1;
  501.  
  502.         errno = 0;
  503.         while ((dp = readdir (dirp)) != NULL)
  504.         {
  505.         char *buf;
  506.  
  507.         if (strcmp (dp->d_name, ".") == 0 ||
  508.                 strcmp (dp->d_name, "..") == 0)
  509.             continue;
  510.  
  511.         buf = xmalloc (strlen (path) + strlen (dp->d_name) + 5);
  512.         sprintf (buf, "%s/%s", path, dp->d_name);
  513.  
  514.         /* See comment in unlink_file_dir explanation of why we use
  515.            isdir instead of just calling unlink and checking the
  516.            status.  */
  517.         if (isdir(buf)) 
  518.         {
  519.             if (deep_remove_dir(buf))
  520.             {
  521.             closedir(dirp);
  522.             free (buf);
  523.             return -1;
  524.             }
  525.         }
  526.         else
  527.         {
  528.             if (unlink (buf) != 0)
  529.             {
  530.             closedir(dirp);
  531.             free (buf);
  532.             return -1;
  533.             }
  534.         }
  535.         free (buf);
  536.  
  537.         errno = 0;
  538.         }
  539.         if (errno != 0)
  540.         {
  541.         int save_errno = errno;
  542.         closedir (dirp);
  543.         errno = save_errno;
  544.         return -1;
  545.         }
  546.         closedir (dirp);
  547.         return rmdir (path);
  548.     }
  549.     else
  550.         return -1;
  551.     }
  552.  
  553.     /* Was able to remove the directory return 0 */
  554.     return 0;
  555. }
  556.  
  557. /* Read NCHARS bytes from descriptor FD into BUF.
  558.    Return the number of characters successfully read.
  559.    The number returned is always NCHARS unless end-of-file or error.  */
  560. static size_t
  561. block_read (fd, buf, nchars)
  562.     int fd;
  563.     char *buf;
  564.     size_t nchars;
  565. {
  566.     char *bp = buf;
  567.     size_t nread;
  568.  
  569.     do 
  570.     {
  571.     nread = read (fd, bp, nchars);
  572.     if (nread == (size_t)-1)
  573.     {
  574. #ifdef EINTR
  575.         if (errno == EINTR)
  576.         continue;
  577. #endif
  578.         return (size_t)-1;
  579.     }
  580.  
  581.     if (nread == 0)
  582.         break; 
  583.  
  584.     bp += nread;
  585.     nchars -= nread;
  586.     } while (nchars != 0);
  587.  
  588.     return (size_t)(bp - buf); /* olsen */
  589.  
  590.     
  591. /*
  592.  * Compare "file1" to "file2". Return non-zero if they don't compare exactly.
  593.  * If FILE1 and FILE2 are special files, compare their salient characteristics
  594.  * (i.e. major/minor device numbers, links, etc.
  595.  */
  596. int
  597. xcmp (file1, file2)
  598.     const char *file1;
  599.     const char *file2;
  600. {
  601.     char *buf1, *buf2;
  602.     struct stat sb1, sb2;
  603.     int fd1, fd2;
  604.     int ret;
  605.  
  606.     if (CVS_LSTAT (file1, &sb1) < 0)
  607.     error (1, errno, "cannot lstat %s", file1);
  608.     if (CVS_LSTAT (file2, &sb2) < 0)
  609.     error (1, errno, "cannot lstat %s", file2);
  610.  
  611.     /* If FILE1 and FILE2 are not the same file type, they are unequal. */
  612.     if ((sb1.st_mode & S_IFMT) != (sb2.st_mode & S_IFMT))
  613.     return 1;
  614.  
  615.     /* If FILE1 and FILE2 are symlinks, they are equal if they point to
  616.        the same thing. */
  617.     if (S_ISLNK (sb1.st_mode) && S_ISLNK (sb2.st_mode))
  618.     {
  619.     int result;
  620.     buf1 = xreadlink (file1);
  621.     buf2 = xreadlink (file2);
  622.     result = (strcmp (buf1, buf2) == 0);
  623.     free (buf1);
  624.     free (buf2);
  625.     return result;
  626.     }
  627.  
  628. #ifndef AMIGA
  629.     /* If FILE1 and FILE2 are devices, they are equal if their device
  630.        numbers match. */
  631.     if (S_ISBLK (sb1.st_mode) || S_ISCHR (sb1.st_mode))
  632.     {
  633. #ifdef HAVE_ST_RDEV
  634.     if (sb1.st_rdev == sb2.st_rdev)
  635.         return 0;
  636.     else
  637.         return 1;
  638. #else
  639.     error (1, 0, "cannot compare device files on this system (%s and %s)",
  640.            file1, file2);
  641. #endif
  642.     }
  643. #endif /* AMIGA */
  644.  
  645.     if ((fd1 = open (file1, O_RDONLY, 0)) < 0) /* olsen */
  646.     error (1, errno, "cannot open file %s for comparing", file1);
  647.     if ((fd2 = open (file2, O_RDONLY, 0)) < 0) /* olsen */
  648.     error (1, errno, "cannot open file %s for comparing", file2);
  649.  
  650.     /* A generic file compare routine might compare st_dev & st_ino here 
  651.        to see if the two files being compared are actually the same file.
  652.        But that won't happen in CVS, so we won't bother. */
  653.  
  654.     if (sb1.st_size != sb2.st_size)
  655.     ret = 1;
  656.     else if (sb1.st_size == 0)
  657.     ret = 0;
  658.     else
  659.     {
  660.     /* FIXME: compute the optimal buffer size by computing the least
  661.        common multiple of the files st_blocks field */
  662.     size_t buf_size = 8 * 1024;
  663.     size_t read1;
  664.     size_t read2;
  665.  
  666.     buf1 = xmalloc (buf_size);
  667.     buf2 = xmalloc (buf_size);
  668.  
  669.     do 
  670.     {
  671.         read1 = block_read (fd1, buf1, buf_size);
  672.         if (read1 == (size_t)-1)
  673.         error (1, errno, "cannot read file %s for comparing", file1);
  674.  
  675.         read2 = block_read (fd2, buf2, buf_size);
  676.         if (read2 == (size_t)-1)
  677.         error (1, errno, "cannot read file %s for comparing", file2);
  678.  
  679.         /* assert (read1 == read2); */
  680.  
  681.         ret = memcmp(buf1, buf2, read1);
  682.     } while (ret == 0 && read1 == buf_size);
  683.  
  684.     free (buf1);
  685.     free (buf2);
  686.     }
  687.     
  688.     (void) close (fd1);
  689.     (void) close (fd2);
  690.     return (ret);
  691. }
  692.  
  693. #ifndef AMIGA
  694. /* Generate a unique temporary filename.  Returns a pointer to a newly
  695.    malloc'd string containing the name.  Returns successfully or not at
  696.    all.  */
  697. /* There are at least three functions for generating temporary
  698.    filenames.  We use tempnam (SVID 3) if possible, else mktemp (BSD
  699.    4.3), and as last resort tmpnam (POSIX). Reason is that tempnam and
  700.    mktemp both allow to specify the directory in which the temporary
  701.    file will be created.  */
  702. #ifdef HAVE_TEMPNAM
  703. char *
  704. cvs_temp_name ()
  705. {
  706.     char *retval;
  707.  
  708.     retval = tempnam (Tmpdir, "cvs");
  709.     if (retval == NULL)
  710.     error (1, errno, "cannot generate temporary filename");
  711.     /* tempnam returns a pointer to a newly malloc'd string, so there's
  712.        no need for a xstrdup  */
  713.     return retval;
  714. }
  715. #else
  716. char *
  717. cvs_temp_name ()
  718. {
  719. #  ifdef HAVE_MKTEMP
  720.     char *value;
  721.     char *retval;
  722.     char *fmt; /* olsen */
  723.  
  724.     value = xmalloc (strlen (Tmpdir) + 40);
  725.     fmt = "%s/%s"; /* olsen */
  726.  
  727.     #ifdef AMIGA
  728.     {
  729.         int len;
  730.  
  731.         len = strlen(Tmpdir);
  732.         if(len > 0 && (Tmpdir[len-1] != ':' && Tmpdir[len-1] != '/'))
  733.             fmt = "%s/%s";
  734.         else
  735.             fmt = "%s%s";
  736.     }
  737.     #endif /* AMIGA */
  738.  
  739.     sprintf (value, fmt, Tmpdir, "cvsXXXXXX" ); /* olsen */
  740.     retval = mktemp (value);
  741.  
  742.     if (retval == NULL)
  743.     error (1, errno, "cannot generate temporary filename");
  744.     return value;
  745. #  else
  746.     char value[L_tmpnam + 1];
  747.     char *retval;
  748.  
  749.     retval = tmpnam (value);
  750.     if (retval == NULL)
  751.     error (1, errno, "cannot generate temporary filename");
  752.     return xstrdup (value);
  753. #  endif
  754. }
  755. #endif
  756. #endif /* AMIGA */
  757.  
  758. #ifndef AMIGA
  759. /* Return non-zero iff FILENAME is absolute.
  760.    Trivial under Unix, but more complicated under other systems.  */
  761. int
  762. isabsolute (filename)
  763.     const char *filename;
  764. {
  765.     return filename[0] == '/';
  766. }
  767. #endif /* AMIGA */
  768.  
  769. /*
  770.  * Return a string (dynamically allocated) with the name of the file to which
  771.  * LINK is symlinked.
  772.  */
  773. char *
  774. xreadlink (link)
  775.     const char *link;
  776. {
  777.     char *file = NULL;
  778.     char *tfile;
  779.     int buflen = 128;
  780.     int link_name_len;
  781.  
  782.     if (!islink (link))
  783.     return NULL;
  784.  
  785.     /* Get the name of the file to which `from' is linked.
  786.        FIXME: what portability issues arise here?  Are readlink &
  787.        ENAMETOOLONG defined on all systems? -twp */
  788.     do
  789.     {
  790.     file = xrealloc (file, buflen);
  791.     link_name_len = readlink (link, file, buflen - 1);
  792.     buflen *= 2;
  793.     }
  794.     while (link_name_len < 0 && errno == ENAMETOOLONG);
  795.  
  796.     if (link_name_len < 0)
  797.     error (1, errno, "cannot readlink %s", link);
  798.  
  799.     file[link_name_len] = '\0';
  800.  
  801.     tfile = xstrdup (file);
  802.     free (file);
  803.  
  804.     return tfile;
  805. }
  806.  
  807. #ifndef AMIGA
  808. /* Return a pointer into PATH's last component.  */
  809. char *
  810. last_component (path)
  811.     char *path;
  812. {
  813.     char *last = strrchr (path, '/');
  814.     
  815.     if (last && (last != path))
  816.         return last + 1;
  817.     else
  818.         return path;
  819. }
  820. #endif /* AMIGA */
  821.  
  822. /* Return the home directory.  Returns a pointer to storage
  823.    managed by this function or its callees (currently getenv).
  824.    This function will return the same thing every time it is
  825.    called.  Returns NULL if there is no home directory.
  826.  
  827.    Note that for a pserver server, this may return root's home
  828.    directory.  What typically happens is that upon being started from
  829.    inetd, before switching users, the code in cvsrc.c calls
  830.    get_homedir which remembers root's home directory in the static
  831.    variable.  Then the switch happens and get_homedir might return a
  832.    directory that we don't even have read or execute permissions for
  833.    (which is bad, when various parts of CVS try to read there).  One
  834.    fix would be to make the value returned by get_homedir only good
  835.    until the next call (which would free the old value).  Another fix
  836.    would be to just always malloc our answer, and let the caller free
  837.    it (that is best, because some day we may need to be reentrant).
  838.  
  839.    The workaround is to put -f in inetd.conf which means that
  840.    get_homedir won't get called until after the switch in user ID.
  841.  
  842.    The whole concept of a "home directory" on the server is pretty
  843.    iffy, although I suppose some people probably are relying on it for
  844.    .cvsrc and such, in the cases where it works.  */
  845. char *
  846. get_homedir ()
  847. {
  848.     static char *home = NULL;
  849.     char *env = getenv ("HOME");
  850.     struct passwd *pw;
  851.  
  852.     if (home != NULL)
  853.     return home;
  854.  
  855.     if (env)
  856.     home = env;
  857.     else if ((pw = (struct passwd *) getpwuid (getuid ()))
  858.          && pw->pw_dir)
  859.     home = xstrdup (pw->pw_dir);
  860.     else
  861.     return 0;
  862.  
  863.     return home;
  864. }
  865.  
  866. #ifndef AMIGA
  867. /* See cvs.h for description.  On unix this does nothing, because the
  868.    shell expands the wildcards.  */
  869. void
  870. expand_wild (argc, argv, pargc, pargv)
  871.     int argc;
  872.     char **argv;
  873.     int *pargc;
  874.     char ***pargv;
  875. {
  876.     int i;
  877.     *pargc = argc;
  878.     *pargv = (char **) xmalloc (argc * sizeof (char *));
  879.     for (i = 0; i < argc; ++i)
  880.     (*pargv)[i] = xstrdup (argv[i]);
  881. }
  882. #endif /* AMIGA */
  883.  
  884. #ifdef SERVER_SUPPORT
  885. /* Case-insensitive string compare.  I know that some systems
  886.    have such a routine, but I'm not sure I see any reasons for
  887.    dealing with the hair of figuring out whether they do (I haven't
  888.    looked into whether this is a performance bottleneck; I would guess
  889.    not).  */
  890. int
  891. cvs_casecmp (str1, str2)
  892.     char *str1;
  893.     char *str2;
  894. {
  895.     char *p;
  896.     char *q;
  897.     int pqdiff;
  898.  
  899.     p = str1;
  900.     q = str2;
  901.     while ((pqdiff = tolower (*p) - tolower (*q)) == 0)
  902.     {
  903.     if (*p == '\0')
  904.         return 0;
  905.     ++p;
  906.     ++q;
  907.     }
  908.     return pqdiff;
  909. }
  910.  
  911. /* Case-insensitive file open.  As you can see, this is an expensive
  912.    call.  We don't regard it as our main strategy for dealing with
  913.    case-insensitivity.  Returns errno code or 0 for success.  Puts the
  914.    new file in *FP.  NAME and MODE are as for fopen.  If PATHP is not
  915.    NULL, then put a malloc'd string containing the pathname as found
  916.    into *PATHP.  *PATHP is only set if the return value is 0.
  917.  
  918.    Might be cleaner to separate the file finding (which just gives
  919.    *PATHP) from the file opening (which the caller can do).  For one
  920.    thing, might make it easier to know whether to put NAME or *PATHP
  921.    into error messages.  */
  922. int
  923. fopen_case (name, mode, fp, pathp)
  924.     char *name;
  925.     char *mode;
  926.     FILE **fp;
  927.     char **pathp;
  928. {
  929.     struct dirent *dp;
  930.     DIR *dirp;
  931.     char *dir;
  932.     char *fname;
  933.     char *found_name;
  934.     int retval;
  935.  
  936.     /* Separate NAME into directory DIR and filename within the directory
  937.        FNAME.  */
  938.     dir = xstrdup (name);
  939.     fname = strrchr (dir, '/');
  940.     if (fname == NULL)
  941.     error (1, 0, "internal error: relative pathname in fopen_case");
  942.     *fname++ = '\0';
  943.  
  944.     found_name = NULL;
  945.     dirp = CVS_OPENDIR (dir);
  946.     if (dirp == NULL)
  947.     {
  948.     if (existence_error (errno))
  949.     {
  950.         /* This can happen if we are looking in the Attic and the Attic
  951.            directory does not exist.  Return the error to the caller;
  952.            they know what to do with it.  */
  953.         retval = errno;
  954.         goto out;
  955.     }
  956.     else
  957.     {
  958.         /* Give a fatal error; that way the error message can be
  959.            more specific than if we returned the error to the caller.  */
  960.         error (1, errno, "cannot read directory %s", dir);
  961.     }
  962.     }
  963.     errno = 0;
  964.     while ((dp = readdir (dirp)) != NULL)
  965.     {
  966.     if (cvs_casecmp (dp->d_name, fname) == 0)
  967.     {
  968.         if (found_name != NULL)
  969.         error (1, 0, "%s is ambiguous; could mean %s or %s",
  970.                fname, dp->d_name, found_name);
  971.         found_name = xstrdup (dp->d_name);
  972.     }
  973.     }
  974.     if (errno != 0)
  975.     error (1, errno, "cannot read directory %s", dir);
  976.     closedir (dirp);
  977.  
  978.     if (found_name == NULL)
  979.     {
  980.     *fp = NULL;
  981.     retval = ENOENT;
  982.     }
  983.     else
  984.     {
  985.     char *p;
  986.  
  987.     /* Copy the found name back into DIR.  We are assuming that
  988.        found_name is the same length as fname, which is true as
  989.        long as the above code is just ignoring case and not other
  990.        aspects of filename syntax.  */
  991.     p = dir + strlen (dir);
  992.     *p++ = '/';
  993.     strcpy (p, found_name);
  994.     *fp = fopen (dir, mode);
  995.     if (*fp == NULL)
  996.         retval = errno;
  997.     else
  998.         retval = 0;
  999.     }
  1000.  
  1001.     if (pathp == NULL)
  1002.     free (dir);
  1003.     else if (retval != 0)
  1004.     free (dir);
  1005.     else
  1006.     *pathp = dir;
  1007.     free (found_name);
  1008.  out:
  1009.     return retval;
  1010. }
  1011. #endif /* SERVER_SUPPORT */
  1012.