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.
25 #include <X11/Xatom.h>
28 #define LINESIZE (4*1024)
29 #define MAXDATA (64*1024)
37 "usage: %s [options] [filename]\n"
39 " -cutbuffer number\n"
47 errorHandler(Display
*dpy
, XErrorEvent
*err
)
49 /* ignore all errors */
54 main(int argc
, char **argv
)
62 char *display_name
="";
66 int clear_selection
= 0;
68 for (i
=1; i
<argc
; i
++) {
69 if (argv
[i
][0]=='-') {
70 if (argv
[i
][1]=='h') {
73 } else if (strcmp(argv
[i
],"-cutbuffer")==0) {
76 if (sscanf(argv
[i
],"%i", &buffer
)!=1) {
77 fprintf(stderr
, "%s: could not convert \"%s\" to int\n",
81 if (buffer
<0 || buffer
> 7) {
82 fprintf(stderr
, "%s: invalid buffer number %i\n",
90 } else if (strcmp(argv
[i
], "-display")==0) {
92 display_name
= argv
[++i
];
97 } else if (strcmp(argv
[i
],"-clearselection")==0) {
99 } else if (strcmp(argv
[i
],"-nolimit")==0) {
110 file
= fopen(filename
, "r");
113 sprintf(line
, "%s: could not open \"%s\"", argv
[0], filename
);
119 dpy
= XOpenDisplay(display_name
);
120 XSetErrorHandler(errorHandler
);
122 fprintf(stderr
, "%s: could not open display \"%s\"\n", argv
[0],
123 XDisplayName(display_name
));
127 XRotateBuffers(dpy
, 1);
130 while (!feof(file
)) {
132 char tmp
[LINESIZE
+2];
136 * Use read() instead of fgets() to preserve NULs, since
137 * especially since there's no reason to read one line at a time.
139 if ((nl
= fread(tmp
, 1, LINESIZE
, file
)) <= 0) {
143 nbuf
= malloc(buf_len
= l
+nl
+1);
146 if (buf_len
< l
+nl
+1) {
148 * To avoid terrible performance on big input buffers,
149 * grow by doubling, not by the minimum needed for the
152 buf_len
= 2 * buf_len
+ nl
+ 1;
153 nbuf
= realloc(buf
, buf_len
);
160 fprintf(stderr
, "%s: out of memory\n", argv
[0]);
165 * Don't strcat, since it would make the algorithm n-squared.
166 * Don't use strcpy, since it stops on a NUL.
168 memcpy(buf
+l
, tmp
, nl
);
170 if (limit_check
&& l
>=MAXDATA
) {
174 "%s: too much data in input - more than %d bytes\n"
175 " use the -nolimit argument to remove the limit check.\n",
182 if (clear_selection
) {
183 XSetSelectionOwner(dpy
, XA_PRIMARY
, None
, CurrentTime
);
186 XStoreBuffer(dpy
, buf
, l
, buffer
);
190 exit(buf
== NULL
|| errno
!= 0);