New modes for cygpath that terminate path with null byte, nothing

Daniel Colascione dancol@dancol.org
Fri Jul 27 08:08:00 GMT 2012


I wrote this patch because I often write this:

$ cygpath -aw foo > /dev/clipboard

Today, cygpath always appends a newline to the information in the
clipboard, which is annoying when trying to paste into a program that
interprets newlines specially. This patch implements two new options:
-0/--null and -n/--no-newline. The former separates all paths output by
cygpath with a null byte; the latter separates them with nothing at all.
With -n, my example above works more smoothly and pastes don't include a
newline.

-------------- next part --------------
Index: cygpath.cc
===================================================================
RCS file: /cvs/src/src/winsup/utils/cygpath.cc,v
retrieving revision 1.69
diff -u -r1.69 cygpath.cc
--- cygpath.cc	6 Jul 2012 14:52:33 -0000	1.69
+++ cygpath.cc	27 Jul 2012 08:01:19 -0000
@@ -37,6 +37,7 @@
 static char *prog_name;
 static char *file_arg, *output_arg;
 static int path_flag, unix_flag, windows_flag, absolute_flag;
+static int separator_mode; // 0 = newline, 1 = null, 2 = nothing
 static int shortname_flag, longname_flag;
 static int ignore_flag, allusers_flag, output_flag;
 static int mixed_flag, options_from_file_flag, mode_flag;
@@ -47,6 +48,8 @@
 static struct option long_options[] = {
   {(char *) "absolute", no_argument, NULL, 'a'},
   {(char *) "close", required_argument, NULL, 'c'},
+  {(char *) "no-newline", no_argument, NULL, 'n'},
+  {(char *) "null", no_argument, NULL, '0'},
   {(char *) "dos", no_argument, NULL, 'd'},
   {(char *) "file", required_argument, NULL, 'f'},
   {(char *) "help", no_argument, NULL, 'h'},
@@ -73,14 +76,14 @@
   {0, no_argument, 0, 0}
 };
 
-static char options[] = "ac:df:hilmMopst:uVwAC:DHOPSWF:";
+static char options[] = "ac:df:hilmMopst:uVwAC:DHOPSWF:n0";
 
 static void
 usage (FILE * stream, int status)
 {
   if (!ignore_flag || !status)
     fprintf (stream, "\
-Usage: %1$s (-d|-m|-u|-w|-t TYPE) [-f FILE] [OPTION]... NAME...\n\
+Usage: %1$s (-d|-m|-u|-w|-t TYPE) [-n0] [-f FILE] [OPTION]... NAME...\n\
        %1$s [-c HANDLE] \n\
        %1$s [-ADHOPSW] \n\
        %1$s [-F ID] \n\
@@ -132,6 +135,8 @@
   -f, --file FILE       read FILE for input; use - to read from STDIN\n\
   -o, --option          read options from FILE as well (for use with --file)\n\
   -c, --close HANDLE    close HANDLE (for use in captured process)\n\
+  -n, --no-newline      do not print a newline after the path\n\
+  -0, --null            print a null byte instead of a newline after each path\n\
   -i, --ignore          ignore missing argument\n\
   -h, --help            output usage information and exit\n\
   -V, --version         output version information and exit\n\
@@ -303,6 +308,29 @@
   return ret;
 }
 
+static void
+output_with_separator (const char* buf)
+{
+  fputs (buf, stdout);
+
+  switch (separator_mode)
+    {
+    case 0:
+      fputc ('\n', stdout);
+      break;
+
+    case 1:
+      fputc ('\0', stdout);
+      break;
+
+    case 2:
+      break;
+
+    default:
+      abort ();
+    }
+}
+
 static char *
 get_device_paths (char *path)
 {
@@ -621,7 +649,8 @@
       if (mixed_flag)
 	convert_slashes (buf);
     }
-  printf ("%s\n", buf);
+
+  output_with_separator (buf);
 }
 
 static void
@@ -756,7 +785,8 @@
 	}
     }
 
-  puts (print_tmp ? tmp : buf);
+  output_with_separator (print_tmp ? tmp : buf);
+  
   if (buf2)
     free (buf2);
   if (buf)
@@ -810,6 +840,14 @@
 	  CloseHandle ((HANDLE) strtoul (optarg, NULL, 16));
 	  break;
 
+        case '0':
+          separator_mode = 1;
+          break;
+
+        case 'n':
+          separator_mode = 2;
+          break;
+
 	case 'd':
 	  windows_flag = 1;
 	  shortname_flag = 1;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
URL: <http://cygwin.com/pipermail/cygwin-patches/attachments/20120727/d368be8b/attachment.sig>


More information about the Cygwin-patches mailing list