home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs57pc3.zip / diff / pc / pc.c < prev    next >
C/C++ Source or Header  |  1994-10-31  |  2KB  |  89 lines

  1. /* Functions specific to PC operating systems
  2.    Copyright (C) 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF 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 GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <string.h>
  21.  
  22. char *
  23. filename_lastdirchar (char const *filename)
  24. {
  25.   char const *last = 0;
  26.  
  27.   for (;  *filename;  filename++)
  28.     if (*filename == '/' || *filename == '\\' || (*filename == ':' && !last))
  29.       last = filename;
  30.  
  31.   return (char *) last;
  32. }
  33.  
  34. /* Place into QUOTED a quoted version of ARG suitable for `system'.
  35.    Return the length of the resulting string (which is not null-terminated).
  36.    If QUOTED is null, return the length without any side effects.  */
  37.  
  38. size_t
  39. system_quote_arg (char *quoted, char const *arg)
  40. {
  41.   int needs_quoting = !*arg || strchr (arg, ' ') || strchr (arg, '\t');
  42.   size_t backslashes = 0;
  43.   size_t len = 0;
  44.  
  45.   if (needs_quoting)
  46.     {
  47.       if (quoted)
  48.     quoted[len] = '"';
  49.       len++;
  50.     }
  51.  
  52.   for (;;)
  53.     {
  54.       char c = *arg++;
  55.       switch (c)
  56.     {
  57.     case 0:
  58.       if (needs_quoting)
  59.         {
  60.           if (quoted)
  61.         {
  62.           memset (quoted + len, '\\', backslashes);
  63.           quoted[len + backslashes] = '"';
  64.         }
  65.           len += backslashes + 1;
  66.         }
  67.       return len;
  68.  
  69.     case '"':
  70.       backslashes++;
  71.       if (quoted)
  72.         memset (quoted + len, '\\', backslashes);
  73.       len += backslashes;
  74.       /* fall through */
  75.     default:
  76.       backslashes = 0;
  77.       break;
  78.  
  79.     case '\\':
  80.       backslashes++;
  81.       break;
  82.     }
  83.  
  84.       if (quoted)
  85.     quoted[len] = c;
  86.       len++;
  87.     }
  88. }
  89.