1 /* wxcopy.c- copy stdin or file into cutbuffer
3 * Copyright (c) 1997 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.
26 #include <X11/Xatom.h>
29 #define LINESIZE (4*1024)
30 #define MAXDATA (64*1024)
38 "usage: %s [options] [filename]\n"
40 " -cutbuffer number\n"
48 errorHandler(Display
*dpy
, XErrorEvent
*err
)
50 /* ignore all errors */
55 main(int argc
, char **argv
)
63 char *display_name
="";
67 int clear_selection
= 0;
69 for (i
=1; i
<argc
; i
++) {
70 if (argv
[i
][0]=='-') {
71 if (argv
[i
][1]=='h') {
74 } else if (strcmp(argv
[i
],"-cutbuffer")==0) {
77 if (sscanf(argv
[i
],"%i", &buffer
)!=1) {
78 fprintf(stderr
, "%s: could not convert \"%s\" to int\n",
82 if (buffer
<0 || buffer
> 7) {
83 fprintf(stderr
, "%s: invalid buffer number %i\n",
91 } else if (strcmp(argv
[i
], "-display")==0) {
93 display_name
= argv
[++i
];
98 } else if (strcmp(argv
[i
],"-clearselection")==0) {
100 } else if (strcmp(argv
[i
],"-nolimit")==0) {
111 file
= fopen(filename
, "r");
114 sprintf(line
, "%s: could not open \"%s\"", argv
[0], filename
);
120 dpy
= XOpenDisplay(display_name
);
121 XSetErrorHandler(errorHandler
);
123 fprintf(stderr
, "%s: could not open display \"%s\"\n", argv
[0],
124 XDisplayName(display_name
));
128 XRotateBuffers(dpy
, 1);
131 while (!feof(file
)) {
133 char tmp
[LINESIZE
+2];
137 * Use read() instead of fgets() to preserve NULs, since
138 * especially since there's no reason to read one line at a time.
140 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
144 nbuf
= malloc(buf_len
= l
+nl
+1);
147 if (buf_len
< l
+nl
+1) {
149 * To avoid terrible performance on big input buffers,
150 * grow by doubling, not by the minimum needed for the
153 buf_len
= 2 * buf_len
+ nl
+ 1;
154 nbuf
= realloc(buf
, buf_len
);
161 fprintf(stderr
, "%s: out of memory\n", argv
[0]);
166 * Don't strcat, since it would make the algorithm n-squared.
167 * Don't use strcpy, since it stops on a NUL.
169 memcpy(buf
+l
, tmp
, nl
);
171 if (limit_check
&& l
>=MAXDATA
) {
175 "%s: too much data in input - more than %d bytes\n"
176 " use the -nolimit argument to remove the limit check.\n",
183 if (clear_selection
) {
184 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
187 XStoreBuffer(dpy
, buf
, l
, buffer
);
191 exit(buf
== NULL
|| errno
!= 0);