1 /* wxcopy.c- copy stdin or file into cutbuffer
3 * Copyright (c) 1997-1999 Alfredo K. Kojima
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #define PROG_VERSION "wxcopy 0.3"
29 #include <X11/Xatom.h>
32 #define LINESIZE (4*1024)
33 #define MAXDATA (64*1024)
38 printf("Usage: %s [OPTIONS] [FILE]\n", progn
);
39 puts("Copies data from FILE or stdin into X cut buffer.");
41 puts(" -display <display> display to use");
42 puts(" --cutbuffer <number> cutbuffer number to put data");
43 puts(" --no-limit do not limit size of input data");
44 puts(" --clear-selection clears the current PRIMARY selection");
45 puts(" --help display this help and exit");
46 puts(" --version output version information and exit");
50 errorHandler(Display
*dpy
, XErrorEvent
*err
)
52 /* ignore all errors */
58 main(int argc
, char **argv
)
66 char *display_name
="";
70 int clear_selection
= 0;
72 for (i
=1; i
<argc
; i
++) {
73 if (argv
[i
][0]=='-') {
74 if (strcmp(argv
[i
], "--help")==0) {
77 } else if (strcmp(argv
[i
], "--version")==0) {
80 } else if (strcmp(argv
[i
],"-cutbuffer")==0
81 || strcmp(argv
[i
],"--cutbuffer")==0) {
84 if (sscanf(argv
[i
],"%i", &buffer
)!=1) {
85 fprintf(stderr
, "%s: could not convert '%s' to int\n",
89 if (buffer
<0 || buffer
> 7) {
90 fprintf(stderr
, "%s: invalid buffer number %i\n",
95 printf("%s: missing argument for '%s'\n", argv
[0], argv
[i
]);
96 printf("Try '%s --help' for more information\n", argv
[0]);
99 } else if (strcmp(argv
[i
], "-display")==0) {
101 display_name
= argv
[++i
];
103 printf("%s: missing argument for '%s'\n", argv
[0], argv
[i
]);
104 printf("Try '%s --help' for more information\n", argv
[0]);
107 } else if (strcmp(argv
[i
],"-clearselection")==0
108 || strcmp(argv
[i
],"--clear-selection")==0) {
110 } else if (strcmp(argv
[i
],"-nolimit")==0
111 || strcmp(argv
[i
],"--no-limit")==0) {
114 printf("%s: invalid argument '%s'\n", argv
[0], argv
[i
]);
115 printf("Try '%s --help' for more information\n", argv
[0]);
123 file
= fopen(filename
, "r");
126 sprintf(line
, "%s: could not open \"%s\"", argv
[0], filename
);
132 dpy
= XOpenDisplay(display_name
);
133 XSetErrorHandler(errorHandler
);
135 fprintf(stderr
, "%s: could not open display \"%s\"\n", argv
[0],
136 XDisplayName(display_name
));
140 XRotateBuffers(dpy
, 1);
143 while (!feof(file
)) {
145 char tmp
[LINESIZE
+2];
149 * Use read() instead of fgets() to preserve NULs, since
150 * especially since there's no reason to read one line at a time.
152 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
156 nbuf
= malloc(buf_len
= l
+nl
+1);
159 if (buf_len
< l
+nl
+1) {
161 * To avoid terrible performance on big input buffers,
162 * grow by doubling, not by the minimum needed for the
165 buf_len
= 2 * buf_len
+ nl
+ 1;
166 nbuf
= realloc(buf
, buf_len
);
173 fprintf(stderr
, "%s: out of memory\n", argv
[0]);
178 * Don't strcat, since it would make the algorithm n-squared.
179 * Don't use strcpy, since it stops on a NUL.
181 memcpy(buf
+l
, tmp
, nl
);
183 if (limit_check
&& l
>=MAXDATA
) {
187 "%s: too much data in input - more than %d bytes\n"
188 " use the -nolimit argument to remove the limit check.\n",
195 if (clear_selection
) {
196 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
199 XStoreBuffer(dpy
, buf
, l
, buffer
);
203 exit(buf
== NULL
|| errno
!= 0);