In NSFAQ Q.17 there are sources to the xpaste program. There is also an xcut program, which is attached below. The manual page for both is:
NAME
xcut, xpaste - cut and paste text via X11's cut buffer
SYNOPSIS
xcut [ text ... ]
xpaste
DESCRIPTION
Xcut places into the X11 cut buffer either the text speci-
fied by the text arguments or data taken from the standard
input.
Xpaste prints to standard output the contents of the X11
cut buffer.
BUGS
The only way to specify the display is to use the DISPLAY
environment variable
AUTHOR
An after lunch hack by Paul Borman (prb@cray.com)
October 26, 1993 1
The xcut.c source is:
/*
* Copyright (c) 1993 Paul Borman
* All rights reserved.
*
* Permission to redistribute this source code is hereby granted as long as
* this notice is included in all copies and there is no charge for either
* the source code or binary.
*
* This code is provided AS IS.
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
main(int ac, char **av)
{
Display *dpy;
Atom primary;
Atom string;
Atom selection;
Window win;
int loop;
char *buf;
int len;
len = 0;
if (ac > 1) {
int x;
char *f, *t;
for (x = 1; x < ac; ++x)
len += strlen(av[x]) + 1;
if ((buf = malloc(len)) == NULL) {
perror("xcut");
exit(1);
}
t = buf;
for (x = 1; x < ac; ++x) {
if (x > 1)
*t++ = ' ';
f = av[x];
while (*t = *f++)
++t;
}
} else {
int mlen = 8192;
int r;
if ((buf = malloc(mlen)) == NULL) {
perror("xcut");
exit(1);
}
do {
r = read(0, buf + len, mlen - len);
if (r > 0) {
if ((len += r) >= mlen) {
mlen += 8192;
if ((buf = realloc(buf, mlen)) == NULL) {
perror("xcut");
exit(1);
}
}
}
} while (r > 0);
}
dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "Could not open display: %s\n", XDisplayName(NULL));
exit(1);
}
primary = XInternAtom(dpy, "PRIMARY", False);
string = XInternAtom(dpy, "STRING", False);
selection = XInternAtom(dpy, "CUT_BUFFER0", False);
if (!primary) {
fprintf(stderr, "Failed to intern PRIMARY\n");
exit(1);
}
if (!string) {
fprintf(stderr, "Failed to intern STRING\n");
exit(1);
}
if (!selection) {
fprintf(stderr, "Failed to intern CUT_BUFFER0\n");
exit(1);
}
win = DefaultRootWindow(dpy);
loop = 10;
do {
XSetSelectionOwner(dpy, primary, win, CurrentTime);
if (XGetSelectionOwner(dpy, primary) == win)
break;
} while (loop-- > 0);
XChangeProperty(dpy, win, selection, string, 8,
PropModeReplace, (unsigned char *)buf, len);
XFlush(dpy);
XSync(dpy, False);
XCloseDisplay(dpy);
}