home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / cvs-1.8.7-src.tgz / tar.out / fsf / cvs / windows-NT / filesubr.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  23KB  |  983 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.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* These functions were moved out of subr.c because they need different
  21.    definitions under operating systems (like, say, Windows NT) with different
  22.    file system semantics.  */
  23.  
  24. #include <io.h>
  25. #include <windows.h>
  26.  
  27. #include "cvs.h"
  28.  
  29. static int deep_remove_dir PROTO((const char *path));
  30.  
  31. /*
  32.  * Copies "from" to "to".
  33.  */
  34. void
  35. copy_file (from, to)
  36.     const char *from;
  37.     const char *to;
  38. {
  39.     struct stat sb;
  40.     struct utimbuf t;
  41.     int fdin, fdout;
  42.  
  43.     if (trace)
  44. #ifdef SERVER_SUPPORT
  45.     (void) fprintf (stderr, "%c-> copy(%s,%s)\n",
  46.             (server_active) ? 'S' : ' ', from, to);
  47. #else
  48.     (void) fprintf (stderr, "-> copy(%s,%s)\n", from, to);
  49. #endif
  50.     if (noexec)
  51.     return;
  52.  
  53.     if ((fdin = open (from, O_RDONLY | O_BINARY)) < 0)
  54.     error (1, errno, "cannot open %s for copying", from);
  55.     if (fstat (fdin, &sb) < 0)
  56.     error (1, errno, "cannot fstat %s", from);
  57.     if ((fdout = open (to, O_CREAT | O_TRUNC | O_RDWR | O_BINARY,
  58.                (int) sb.st_mode & 07777)) < 0)
  59.     error (1, errno, "cannot create %s for copying", to);
  60.     if (sb.st_size > 0)
  61.     {
  62.     char buf[BUFSIZ];
  63.     int n;
  64.  
  65.     for (;;) 
  66.     {
  67.         n = read (fdin, buf, sizeof(buf));
  68.         if (n == -1)
  69.         {
  70. #ifdef EINTR
  71.         if (errno == EINTR)
  72.             continue;
  73. #endif
  74.         error (1, errno, "cannot read file %s for copying", from);
  75.         }
  76.             else if (n == 0) 
  77.         break;
  78.   
  79.         if (write(fdout, buf, n) != n) {
  80.         error (1, errno, "cannot write file %s for copying", to);
  81.         }
  82.     }
  83.  
  84. #ifdef HAVE_FSYNC
  85.     if (fsync (fdout)) 
  86.         error (1, errno, "cannot fsync file %s after copying", to);
  87. #endif
  88.     }
  89.  
  90.     if (close (fdin) < 0) 
  91.     error (0, errno, "cannot close %s", from);
  92.     if (close (fdout) < 0)
  93.     error (1, errno, "cannot close %s", to);
  94.  
  95.     /* now, set the times for the copied file to match those of the original */
  96.     memset ((char *) &t, 0, sizeof (t));
  97.     t.actime = sb.st_atime;
  98.     t.modtime = sb.st_mtime;
  99.     (void) utime (to, &t);
  100. }
  101.  
  102. /*
  103.  * link a file, if possible.  Warning: the Windows NT version of this
  104.  * function just copies the file, so only use this function in ways
  105.  * that can deal with either a link or a copy.
  106.  */
  107. int
  108. link_file (from, to)
  109.     const char *from;
  110.     const char *to;
  111. {
  112.     copy_file (from, to);
  113.     return 0;
  114. }
  115.  
  116. /* FIXME-krp: these functions would benefit from caching the char * &
  117.    stat buf.  */
  118.  
  119. /*
  120.  * Returns non-zero if the argument file is a directory, or is a symbolic
  121.  * link which points to a directory.
  122.  */
  123. int
  124. isdir (file)
  125.     const char *file;
  126. {
  127.     struct stat sb;
  128.  
  129.     if (stat (file, &sb) < 0)
  130.     return (0);
  131.     return (S_ISDIR (sb.st_mode));
  132. }
  133.  
  134. /*
  135.  * Returns non-zero if the argument file is a symbolic link.
  136.  */
  137. int
  138. islink (file)
  139.     const char *file;
  140. {
  141. #ifdef S_ISLNK
  142.     struct stat sb;
  143.  
  144.     if (lstat (file, &sb) < 0)
  145.     return (0);
  146.     return (S_ISLNK (sb.st_mode));
  147. #else
  148.     return (0);
  149. #endif
  150. }
  151.  
  152. /*
  153.  * Returns non-zero if the argument file exists.
  154.  */
  155. int
  156. isfile (file)
  157.     const char *file;
  158. {
  159.     return isaccessible(file, F_OK);
  160. }
  161.  
  162. /*
  163.  * Returns non-zero if the argument file is readable.
  164.  */
  165. int
  166. isreadable (file)
  167.     const char *file;
  168. {
  169.     return isaccessible(file, R_OK);
  170. }
  171.  
  172. /*
  173.  * Returns non-zero if the argument file is writable.
  174.  */
  175. int
  176. iswritable (file)
  177.     const char *file;
  178. {
  179.     return isaccessible(file, W_OK);
  180. }
  181.  
  182. /*
  183.  * Returns non-zero if the argument file is accessable according to
  184.  * mode.  If compiled with SETXID_SUPPORT also works if cvs has setxid
  185.  * bits set.
  186.  */
  187. int
  188. isaccessible (file, mode)
  189.     const char *file;
  190.     const int mode;
  191. {
  192. #ifdef SETXID_SUPPORT
  193.     struct stat sb;
  194.     int umask = 0;
  195.     int gmask = 0;
  196.     int omask = 0;
  197.     int uid;
  198.     
  199.     if (stat(file, &sb) == -1)
  200.     return 0;
  201.     if (mode == F_OK)
  202.     return 1;
  203.  
  204.     uid = geteuid();
  205.     if (uid == 0)        /* superuser */
  206.     {
  207.     if (mode & X_OK)
  208.         return sb.st_mode & (S_IXUSR|S_IXGRP|S_IXOTH);
  209.     else
  210.         return 1;
  211.     }
  212.     
  213.     if (mode & R_OK)
  214.     {
  215.     umask |= S_IRUSR;
  216.     gmask |= S_IRGRP;
  217.     omask |= S_IROTH;
  218.     }
  219.     if (mode & W_OK)
  220.     {
  221.     umask |= S_IWUSR;
  222.     gmask |= S_IWGRP;
  223.     omask |= S_IWOTH;
  224.     }
  225.     if (mode & X_OK)
  226.     {
  227.     umask |= S_IXUSR;
  228.     gmask |= S_IXGRP;
  229.     omask |= S_IXOTH;
  230.     }
  231.  
  232.     if (sb.st_uid == uid)
  233.     return (sb.st_mode & umask) == umask;
  234.     else if (sb.st_gid == getegid())
  235.     return (sb.st_mode & gmask) == gmask;
  236.     else
  237.     return (sb.st_mode & omask) == omask;
  238. #else
  239.     return access(file, mode) == 0;
  240. #endif
  241. }
  242.  
  243. /*
  244.  * Open a file and die if it fails
  245.  */
  246. FILE *
  247. open_file (name, mode)
  248.     const char *name;
  249.     const char *mode;
  250. {
  251.     FILE *fp;
  252.  
  253.     if ((fp = fopen (name, mode)) == NULL)
  254.     error (1, errno, "cannot open %s", name);
  255.     return (fp);
  256. }
  257.  
  258. /*
  259.  * Make a directory and die if it fails
  260.  */
  261. void
  262. make_directory (name)
  263.     const char *name;
  264. {
  265.     struct stat sb;
  266.  
  267.     if (stat (name, &sb) == 0 && (!S_ISDIR (sb.st_mode)))
  268.         error (0, 0, "%s already exists but is not a directory", name);
  269.     if (!noexec && mkdir (name) < 0)
  270.     error (1, errno, "cannot make directory %s", name);
  271. }
  272.  
  273. /*
  274.  * Make a path to the argument directory, printing a message if something
  275.  * goes wrong.
  276.  */
  277. void
  278. make_directories (name)
  279.     const char *name;
  280. {
  281.     char *cp;
  282.  
  283.     if (noexec)
  284.     return;
  285.  
  286.     if (mkdir (name) == 0 || errno == EEXIST)
  287.     return;
  288.     if (errno != ENOENT)
  289.     {
  290.     error (0, errno, "cannot make path to %s", name);
  291.     return;
  292.     }
  293.     if ((cp = strrchr (name, '/')) == NULL)
  294.     return;
  295.     *cp = '\0';
  296.     make_directories (name);
  297.     *cp++ = '/';
  298.     if (*cp == '\0')
  299.     return;
  300.     (void) mkdir (name);
  301. }
  302.  
  303. /* Create directory NAME if it does not already exist; fatal error for
  304.    other errors.  Returns 0 if directory was created; 1 if it already
  305.    existed.  */
  306. int
  307. mkdir_if_needed (name)
  308.     char *name;
  309. {
  310.     if (mkdir (name) < 0)
  311.     {
  312.     if (errno != EEXIST
  313. #ifdef EACCESS
  314.         /* This was copied over from the OS/2 code; I would guess it
  315.            isn't needed here but that has not been verified.  */
  316.         && errno != EACCESS
  317. #endif
  318. #ifdef EACCES
  319.         /* This is said to be needed by NT on Alpha or PowerPC
  320.            (not sure what version) --August, 1996.  */
  321.         && errno != EACCES
  322. #endif
  323.         )
  324.         error (1, errno, "cannot make directory %s", name);
  325.     return 1;
  326.     }
  327.     return 0;
  328. }
  329.  
  330. /*
  331.  * Change the mode of a file, either adding write permissions, or removing
  332.  * all write permissions.  Adding write permissions honors the current umask
  333.  * setting.
  334.  */
  335. void
  336. xchmod (fname, writable)
  337.     char *fname;
  338.     int writable;
  339. {
  340.     struct stat sb;
  341.     mode_t mode, oumask;
  342.  
  343.     if (stat (fname, &sb) < 0)
  344.     {
  345.     if (!noexec)
  346.         error (0, errno, "cannot stat %s", fname);
  347.     return;
  348.     }
  349.     if (writable)
  350.     {
  351.     oumask = umask (0);
  352.     (void) umask (oumask);
  353.     mode = sb.st_mode | ~oumask & (((sb.st_mode & S_IRUSR) ? S_IWUSR : 0) |
  354.                        ((sb.st_mode & S_IRGRP) ? S_IWGRP : 0) |
  355.                        ((sb.st_mode & S_IROTH) ? S_IWOTH : 0));
  356.     }
  357.     else
  358.     {
  359.     mode = sb.st_mode & ~(S_IWRITE | S_IWGRP | S_IWOTH);
  360.     }
  361.  
  362.     if (trace)
  363. #ifdef SERVER_SUPPORT
  364.     (void) fprintf (stderr, "%c-> chmod(%s,%o)\n",
  365.             (server_active) ? 'S' : ' ', fname, mode);
  366. #else
  367.     (void) fprintf (stderr, "-> chmod(%s,%o)\n", fname, mode);
  368. #endif
  369.     if (noexec)
  370.     return;
  371.  
  372.     if (chmod (fname, mode) < 0)
  373.     error (0, errno, "cannot change mode of file %s", fname);
  374. }
  375.  
  376.  
  377. /* Read the value of a symbolic link.
  378.    Under Windows NT, this function always returns EINVAL.  */
  379. int
  380. readlink (char *path, char *buf, int buf_size)
  381. {
  382.     errno = EINVAL;
  383.     return -1;
  384. }
  385.  
  386. /*
  387.  * Rename a file and die if it fails
  388.  */
  389. void
  390. rename_file (from, to)
  391.     const char *from;
  392.     const char *to;
  393. {
  394.     if (trace)
  395. #ifdef SERVER_SUPPORT
  396.     (void) fprintf (stderr, "%c-> rename(%s,%s)\n",
  397.             (server_active) ? 'S' : ' ', from, to);
  398. #else
  399.     (void) fprintf (stderr, "-> rename(%s,%s)\n", from, to);
  400. #endif
  401.     if (noexec)
  402.     return;
  403.  
  404.     /* Win32 unlink is stupid --- it fails if the file is read-only  */
  405.     chmod(to, S_IWRITE);
  406.     unlink(to);
  407.     if (rename (from, to) < 0)
  408.     error (1, errno, "cannot rename file %s to %s", from, to);
  409. }
  410.  
  411. /* Windows NT doesn't have hard links or symbolic links.
  412.    There was only one place in CVS which used this function,
  413.    so I rewrote it to work another way, so this function isn't
  414.    used any more.  */
  415. #if 0
  416. /*
  417.  * link a file, if possible.
  418.  */
  419. int
  420. link_file (from, to)
  421.     const char *from;
  422.     const char *to;
  423. {
  424.     if (trace)
  425. #ifdef SERVER_SUPPORT
  426.     (void) fprintf (stderr, "%c-> link(%s,%s)\n",
  427.             (server_active) ? 'S' : ' ', from, to);
  428. #else
  429.     (void) fprintf (stderr, "-> link(%s,%s)\n", from, to);
  430. #endif
  431.     if (noexec)
  432.     return (0);
  433.  
  434.     return (link (from, to));
  435. }
  436. #endif
  437.  
  438. /*
  439.  * unlink a file, if possible.
  440.  */
  441. int
  442. unlink_file (f)
  443.     const char *f;
  444. {
  445.     if (trace)
  446. #ifdef SERVER_SUPPORT
  447.     (void) fprintf (stderr, "%c-> unlink(%s)\n",
  448.             (server_active) ? 'S' : ' ', f);
  449. #else
  450.     (void) fprintf (stderr, "-> unlink(%s)\n", f);
  451. #endif
  452.     if (noexec)
  453.     return (0);
  454.  
  455.     /* Win32 unlink is stupid - it fails if the file is read-only */
  456.     chmod (f, _S_IWRITE);
  457.     return (unlink (f));
  458. }
  459.  
  460. /*
  461.  * Unlink a file or dir, if possible.  If it is a directory do a deep
  462.  * removal of all of the files in the directory.  Return -1 on error
  463.  * (in which case errno is set).
  464.  */
  465. int
  466. unlink_file_dir (f)
  467.     const char *f;
  468. {
  469.     if (trace)
  470. #ifdef SERVER_SUPPORT
  471.     (void) fprintf (stderr, "%c-> unlink_file_dir(%s)\n",
  472.             (server_active) ? 'S' : ' ', f);
  473. #else
  474.     (void) fprintf (stderr, "-> unlink_file_dir(%s)\n", f);
  475. #endif
  476.     if (noexec)
  477.     return (0);
  478.  
  479.     /* Win32 unlink is stupid - it fails if the file is read-only */
  480.     chmod (f, _S_IWRITE);
  481.     if (unlink (f) != 0)
  482.     {
  483.     /* under Windows NT, unlink returns EACCES if the path
  484.        is a directory.  Under Windows 95, ENOENT.  */
  485.         if (errno == EISDIR || errno == EACCES || errno == ENOENT)
  486.                 return deep_remove_dir (f);
  487.         else
  488.         /* The file wasn't a directory and some other
  489.          * error occured
  490.          */
  491.                 return -1;
  492.     }
  493.     /* We were able to remove the file from the disk */
  494.     return 0;
  495. }
  496.  
  497. /* Remove a directory and everything it contains.  Returns 0 for
  498.  * success, -1 for failure (in which case errno is set).
  499.  */
  500.  
  501. static int
  502. deep_remove_dir (path)
  503.     const char *path;
  504. {
  505.     DIR          *dirp;
  506.     struct dirent *dp;
  507.     char       buf[PATH_MAX];
  508.  
  509.     /* ENOTEMPTY for NT (obvious) but EACCES for Win95 (not obvious) */
  510.     if (rmdir (path) != 0
  511.     && (errno == ENOTEMPTY || errno == EACCES))
  512.     {
  513.     if ((dirp = opendir (path)) == NULL)
  514.         /* If unable to open the directory return
  515.          * an error
  516.          */
  517.         return -1;
  518.  
  519.     while ((dp = readdir (dirp)) != NULL)
  520.     {
  521.         if (strcmp (dp->d_name, ".") == 0 ||
  522.             strcmp (dp->d_name, "..") == 0)
  523.         continue;
  524.  
  525.         sprintf (buf, "%s/%s", path, dp->d_name);
  526.  
  527.         /* Win32 unlink is stupid - it fails if the file is read-only */
  528.         chmod (buf, _S_IWRITE);
  529.         if (unlink (buf) != 0 )
  530.         {
  531.         /* Under Windows NT, unlink returns EACCES if the path
  532.            is a directory.  Under Windows 95, ENOENT.  It
  533.            isn't really clear to me whether checking errno is
  534.            better or worse than using _stat to check for a directory.
  535.            We aren't really trying to prevent race conditions here
  536.            (e.g. what if something changes between readdir and
  537.            unlink?)  */
  538.         if (errno == EISDIR || errno == EACCES || errno == ENOENT)
  539.         {
  540.             if (deep_remove_dir (buf))
  541.             {
  542.             closedir (dirp);
  543.             return -1;
  544.             }
  545.         }
  546.         else
  547.         {
  548.             /* buf isn't a directory, or there are
  549.              * some sort of permision problems
  550.              */
  551.             closedir (dirp);
  552.             return -1;
  553.         }
  554.         }
  555.     }
  556.     closedir (dirp);
  557.     return rmdir (path);
  558.     }
  559.     /* Was able to remove the directory return 0 */
  560.     return 0;
  561. }
  562.  
  563. /* Read NCHARS bytes from descriptor FD into BUF.
  564.    Return the number of characters successfully read.
  565.    The number returned is always NCHARS unless end-of-file or error.  */
  566. static size_t
  567. block_read (fd, buf, nchars)
  568.     int fd;
  569.     char *buf;
  570.     size_t nchars;
  571. {
  572.     char *bp = buf;
  573.     size_t nread;
  574.  
  575.     do 
  576.     {
  577.     nread = read (fd, bp, nchars);
  578.     if (nread == (size_t)-1)
  579.     {
  580. #ifdef EINTR
  581.         if (errno == EINTR)
  582.         continue;
  583. #endif
  584.         return (size_t)-1;
  585.     }
  586.  
  587.     if (nread == 0)
  588.         break; 
  589.  
  590.     bp += nread;
  591.     nchars -= nread;
  592.     } while (nchars != 0);
  593.  
  594.     return bp - buf;
  595.  
  596.     
  597. /*
  598.  * Compare "file1" to "file2". Return non-zero if they don't compare exactly.
  599.  */
  600. int
  601. xcmp (file1, file2)
  602.     const char *file1;
  603.     const char *file2;
  604. {
  605.     char *buf1, *buf2;
  606.     struct stat sb1, sb2;
  607.     int fd1, fd2;
  608.     int ret;
  609.  
  610.     if ((fd1 = open (file1, O_RDONLY | O_BINARY)) < 0)
  611.     error (1, errno, "cannot open file %s for comparing", file1);
  612.     if ((fd2 = open (file2, O_RDONLY | O_BINARY)) < 0)
  613.     error (1, errno, "cannot open file %s for comparing", file2);
  614.     if (fstat (fd1, &sb1) < 0)
  615.     error (1, errno, "cannot fstat %s", file1);
  616.     if (fstat (fd2, &sb2) < 0)
  617.     error (1, errno, "cannot fstat %s", file2);
  618.  
  619.     /* A generic file compare routine might compare st_dev & st_ino here 
  620.        to see if the two files being compared are actually the same file.
  621.        But that won't happen in CVS, so we won't bother. */
  622.  
  623.     if (sb1.st_size != sb2.st_size)
  624.     ret = 1;
  625.     else if (sb1.st_size == 0)
  626.     ret = 0;
  627.     else
  628.     {
  629.     /* FIXME: compute the optimal buffer size by computing the least
  630.        common multiple of the files st_blocks field */
  631.     size_t buf_size = 8 * 1024;
  632.     size_t read1;
  633.     size_t read2;
  634.  
  635.     buf1 = xmalloc (buf_size);
  636.     buf2 = xmalloc (buf_size);
  637.  
  638.     do 
  639.     {
  640.         read1 = block_read (fd1, buf1, buf_size);
  641.         if (read1 == (size_t)-1)
  642.         error (1, errno, "cannot read file %s for comparing", file1);
  643.  
  644.         read2 = block_read (fd2, buf2, buf_size);
  645.         if (read2 == (size_t)-1)
  646.         error (1, errno, "cannot read file %s for comparing", file2);
  647.  
  648.         /* assert (read1 == read2); */
  649.  
  650.         ret = memcmp(buf1, buf2, read1);
  651.     } while (ret == 0 && read1 == buf_size);
  652.  
  653.     free (buf1);
  654.     free (buf2);
  655.     }
  656.     
  657.     (void) close (fd1);
  658.     (void) close (fd2);
  659.     return (ret);
  660. }
  661.  
  662.  
  663. /* The equivalence class mapping for filenames.
  664.    Windows NT filenames are case-insensitive, but case-preserving.
  665.    Both / and \ are path element separators.
  666.    Thus, this table maps both upper and lower case to lower case, and
  667.    both / and \ to /.  */
  668.  
  669. #if 0
  670. main ()
  671. {
  672.   int c;
  673.  
  674.   for (c = 0; c < 256; c++)
  675.     {
  676.       int t;
  677.  
  678.       if (c == '\\')
  679.         t = '/';
  680.       else
  681.         t = tolower (c);
  682.       
  683.       if ((c & 0x7) == 0x0)
  684.          printf ("    ");
  685.       printf ("0x%02x,", t);
  686.       if ((c & 0x7) == 0x7)
  687.          putchar ('\n');
  688.       else if ((c & 0x7) == 0x3)
  689.          putchar (' ');
  690.     }
  691. }
  692. #endif
  693.  
  694.  
  695. unsigned char
  696. WNT_filename_classes[] =
  697. {
  698.     0x00,0x01,0x02,0x03, 0x04,0x05,0x06,0x07,
  699.     0x08,0x09,0x0a,0x0b, 0x0c,0x0d,0x0e,0x0f,
  700.     0x10,0x11,0x12,0x13, 0x14,0x15,0x16,0x17,
  701.     0x18,0x19,0x1a,0x1b, 0x1c,0x1d,0x1e,0x1f,
  702.     0x20,0x21,0x22,0x23, 0x24,0x25,0x26,0x27,
  703.     0x28,0x29,0x2a,0x2b, 0x2c,0x2d,0x2e,0x2f,
  704.     0x30,0x31,0x32,0x33, 0x34,0x35,0x36,0x37,
  705.     0x38,0x39,0x3a,0x3b, 0x3c,0x3d,0x3e,0x3f,
  706.     0x40,0x61,0x62,0x63, 0x64,0x65,0x66,0x67,
  707.     0x68,0x69,0x6a,0x6b, 0x6c,0x6d,0x6e,0x6f,
  708.     0x70,0x71,0x72,0x73, 0x74,0x75,0x76,0x77,
  709.     0x78,0x79,0x7a,0x5b, 0x2f,0x5d,0x5e,0x5f,
  710.     0x60,0x61,0x62,0x63, 0x64,0x65,0x66,0x67,
  711.     0x68,0x69,0x6a,0x6b, 0x6c,0x6d,0x6e,0x6f,
  712.     0x70,0x71,0x72,0x73, 0x74,0x75,0x76,0x77,
  713.     0x78,0x79,0x7a,0x7b, 0x7c,0x7d,0x7e,0x7f,
  714.     0x80,0x81,0x82,0x83, 0x84,0x85,0x86,0x87,
  715.     0x88,0x89,0x8a,0x8b, 0x8c,0x8d,0x8e,0x8f,
  716.     0x90,0x91,0x92,0x93, 0x94,0x95,0x96,0x97,
  717.     0x98,0x99,0x9a,0x9b, 0x9c,0x9d,0x9e,0x9f,
  718.     0xa0,0xa1,0xa2,0xa3, 0xa4,0xa5,0xa6,0xa7,
  719.     0xa8,0xa9,0xaa,0xab, 0xac,0xad,0xae,0xaf,
  720.     0xb0,0xb1,0xb2,0xb3, 0xb4,0xb5,0xb6,0xb7,
  721.     0xb8,0xb9,0xba,0xbb, 0xbc,0xbd,0xbe,0xbf,
  722.     0xc0,0xc1,0xc2,0xc3, 0xc4,0xc5,0xc6,0xc7,
  723.     0xc8,0xc9,0xca,0xcb, 0xcc,0xcd,0xce,0xcf,
  724.     0xd0,0xd1,0xd2,0xd3, 0xd4,0xd5,0xd6,0xd7,
  725.     0xd8,0xd9,0xda,0xdb, 0xdc,0xdd,0xde,0xdf,
  726.     0xe0,0xe1,0xe2,0xe3, 0xe4,0xe5,0xe6,0xe7,
  727.     0xe8,0xe9,0xea,0xeb, 0xec,0xed,0xee,0xef,
  728.     0xf0,0xf1,0xf2,0xf3, 0xf4,0xf5,0xf6,0xf7,
  729.     0xf8,0xf9,0xfa,0xfb, 0xfc,0xfd,0xfe,0xff,
  730. };
  731.  
  732. /* Like strcmp, but with the appropriate tweaks for file names.
  733.    Under Windows NT, filenames are case-insensitive but case-preserving,
  734.    and both \ and / are path element separators.  */
  735. int
  736. fncmp (const char *n1, const char *n2)
  737. {
  738.     while (*n1 && *n2
  739.            && (WNT_filename_classes[(unsigned char) *n1]
  740.            == WNT_filename_classes[(unsigned char) *n2]))
  741.         n1++, n2++;
  742.     return (WNT_filename_classes[(unsigned char) *n1]
  743.             - WNT_filename_classes[(unsigned char) *n2]);
  744. }
  745.  
  746. /* Fold characters in FILENAME to their canonical forms.  
  747.    If FOLD_FN_CHAR is not #defined, the system provides a default
  748.    definition for this.  */
  749. void
  750. fnfold (char *filename)
  751. {
  752.     while (*filename)
  753.     {
  754.         *filename = FOLD_FN_CHAR (*filename);
  755.     filename++;
  756.     }
  757. }
  758.  
  759.  
  760. /* Generate a unique temporary filename.  Returns a pointer to a newly
  761.    malloc'd string containing the name.  Returns successfully or not at
  762.    all.  */
  763. char *
  764. cvs_temp_name ()
  765. {
  766.     char *retval;
  767.  
  768.     retval = _tempnam (NULL, NULL);
  769.     if (retval == NULL)
  770.     error (1, errno, "cannot generate temporary filename");
  771.     return retval;
  772. }
  773.  
  774. /* Return non-zero iff FILENAME is absolute.
  775.    Trivial under Unix, but more complicated under other systems.  */
  776. int
  777. isabsolute (filename)
  778.     const char *filename;
  779. {
  780.     return (ISDIRSEP (filename[0])
  781.             || (filename[0] != '\0'
  782.                 && filename[1] == ':'
  783.                 && ISDIRSEP (filename[2])));
  784. }
  785.  
  786. /* Return a pointer into PATH's last component.  */
  787. char *
  788. last_component (char *path)
  789. {
  790.     char *scan;
  791.     char *last = 0;
  792.  
  793.     for (scan = path; *scan; scan++)
  794.         if (ISDIRSEP (*scan))
  795.         last = scan;
  796.  
  797.     if (last)
  798.         return last + 1;
  799.     else
  800.         return path;
  801. }
  802.  
  803.  
  804. /* Read data from INFILE, and copy it to OUTFILE. 
  805.    Open INFILE using INFLAGS, and OUTFILE using OUTFLAGS.
  806.    This is useful for converting between CRLF and LF line formats.  */
  807. void
  808. convert_file (char *infile,  int inflags,
  809.           char *outfile, int outflags)
  810. {
  811.     int infd, outfd;
  812.     char buf[8192];
  813.     int len;
  814.  
  815.     if ((infd = open (infile, inflags)) < 0)
  816.         error (1, errno, "couldn't read %s", infile);
  817.     if ((outfd = open (outfile, outflags, S_IWRITE)) < 0)
  818.         error (1, errno, "couldn't write %s", outfile);
  819.  
  820.     while ((len = read (infd, buf, sizeof (buf))) > 0)
  821.         if (write (outfd, buf, len) < 0)
  822.         error (1, errno, "error writing %s", outfile);
  823.     if (len < 0)
  824.         error (1, errno, "error reading %s", infile);
  825.  
  826.     if (close (outfd) < 0)
  827.         error (0, errno, "warning: couldn't close %s", outfile);
  828.     if (close (infd) < 0)
  829.         error (0, errno, "warning: couldn't close %s", infile);
  830. }
  831.  
  832.  
  833. /* NT has two evironment variables, HOMEPATH and HOMEDRIVE, which,
  834.    when combined as ${HOMEDRIVE}${HOMEPATH}, give the unix equivalent
  835.    of HOME.  Some NT users are just too unixy, though, and set the
  836.    HOME variable themselves.  Therefore, we check for HOME first, and
  837.    then try to combine the other two if that fails.  */
  838.  
  839. char *
  840. get_homedir ()
  841. {
  842.     static char pathbuf[PATH_MAX * 2];
  843.     char *hd, *hp;
  844.  
  845.     if ((hd = getenv ("HOME")))
  846.     return hd;
  847.     else if ((hd = getenv ("HOMEDRIVE")) && (hp = getenv ("HOMEPATH")))
  848.     {
  849.     /* Watch for buffer overruns. */
  850.  
  851. #define cvs_min(x,y) ((x <= y) ? (x) : (y))
  852.  
  853.     int ld = cvs_min (PATH_MAX, strlen (hd));
  854.     int lp = cvs_min (PATH_MAX, strlen (hp));
  855.  
  856.     strncpy (pathbuf, hd, ld);
  857.     strncpy (pathbuf + ld, hp, lp);
  858.  
  859.     return pathbuf;
  860.     }
  861.     else
  862.     return NULL;
  863. }
  864.  
  865. /* See cvs.h for description.  */
  866. void
  867. expand_wild (argc, argv, pargc, pargv)
  868.     int argc;
  869.     char **argv;
  870.     int *pargc;
  871.     char ***pargv;
  872. {
  873.     int i;
  874.     int new_argc;
  875.     char **new_argv;
  876.     /* Allocated size of new_argv.  We arrange it so there is always room for
  877.        one more element.  */
  878.     int max_new_argc;
  879.  
  880.     new_argc = 0;
  881.     /* Add one so this is never zero.  */
  882.     max_new_argc = argc + 1;
  883.     new_argv = (char **) xmalloc (max_new_argc * sizeof (char *));
  884.     for (i = 0; i < argc; ++i)
  885.     {
  886.     HANDLE h;
  887.     WIN32_FIND_DATA fdata;
  888.  
  889.     /* These variables help us extract the directory name from the
  890.            given pathname. */
  891.  
  892.     char *last_forw_slash, *last_back_slash, *end_of_dirname;
  893.     int dirname_length = 0;
  894.  
  895.     /* FindFirstFile doesn't return pathnames, so we have to do
  896.        this ourselves.  Luckily, it's no big deal, since globbing
  897.        characters under Win32s can only occur in the last segment
  898.        of the path.  For example,
  899.                 /a/path/q*.h                      valid
  900.             /w32/q*.dir/cant/do/this/q*.h     invalid */
  901.  
  902.     /* Win32 can handle both forward and backward slashes as
  903.            filenames -- check for both. */
  904.          
  905.     last_forw_slash = strrchr (argv[i], '/');
  906.     last_back_slash = strrchr (argv[i], '\\');
  907.  
  908. #define cvs_max(x,y) ((x >= y) ? (x) : (y))
  909.  
  910.     end_of_dirname = cvs_max (last_forw_slash, last_back_slash);
  911.  
  912.     if (end_of_dirname == NULL)
  913.       dirname_length = 0;    /* no directory name */
  914.     else
  915.       dirname_length = end_of_dirname - argv[i] + 1; /* include slash */
  916.  
  917.     h = FindFirstFile (argv[i], &fdata);
  918.     if (h == INVALID_HANDLE_VALUE)
  919.     {
  920.         if (GetLastError () == ENOENT)
  921.         {
  922.         /* No match.  The file specified didn't contain a wildcard (in which case
  923.            we clearly should return it unchanged), or it contained a wildcard which
  924.            didn't match (in which case it might be better for it to be an error,
  925.            but we don't try to do that).  */
  926.         new_argv [new_argc++] = xstrdup (argv[i]);
  927.         if (new_argc == max_new_argc)
  928.         {
  929.             max_new_argc *= 2;
  930.             new_argv = xrealloc (new_argv, max_new_argc * sizeof (char *));
  931.         }
  932.         }
  933.         else
  934.         {
  935.         error (1, errno, "cannot find %s", argv[i]);
  936.         }
  937.     }
  938.     else
  939.     {
  940.         while (1)
  941.         {
  942.         new_argv[new_argc] =
  943.             (char *) xmalloc (strlen (fdata.cFileName) + 1
  944.                       + dirname_length);
  945.  
  946.         /* Copy the directory name, if there is one. */
  947.  
  948.         if (dirname_length)
  949.         {
  950.             strncpy (new_argv[new_argc], argv[i], dirname_length);
  951.             new_argv[new_argc][dirname_length] = '\0';
  952.         }
  953.         else
  954.             new_argv[new_argc][0] = '\0';
  955.  
  956.         /* Copy the file name. */
  957.         
  958.         strcat (new_argv[new_argc], fdata.cFileName);
  959.  
  960.         new_argc++;
  961.  
  962.         if (new_argc == max_new_argc)
  963.         {
  964.             max_new_argc *= 2;
  965.             new_argv = xrealloc (new_argv, max_new_argc * sizeof (char *));
  966.         }
  967.         if (!FindNextFile (h, &fdata))
  968.         {
  969.             if (GetLastError () == ERROR_NO_MORE_FILES)
  970.             break;
  971.             else
  972.             error (1, errno, "cannot find %s", argv[i]);
  973.         }
  974.         }
  975.         if (!FindClose (h))
  976.         error (1, GetLastError (), "cannot close %s", argv[i]);
  977.     }
  978.     }
  979.     *pargc = new_argc;
  980.     *pargv = new_argv;
  981. }
  982.