1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
*/
/*
*
* gcat -- cat-like program using gio with byte-swapping support
*
*/
#include <gio.h>
#define BUFSIZE 4096
extern char *optarg;
extern int optind;
#ifdef ANSI_FUNC
static void swap2(char *to, char *from, size_t nbytes)
#else
static void swap2(to, from, nbytes)
char *to;
char *from;
size_t nbytes;
#endif
{
char c;
size_t i;
for(i=0; i < nbytes; i += 2, (to += 2), (from += 2)){
c = *from;
*(to) = *(from+1);
*(to+1) = c;
}
}
#ifdef ANSI_FUNC
static void swap4(char *to, char *from, size_t nbytes)
#else
static void swap4(to, from, nbytes)
char *to;
char *from;
size_t nbytes;
#endif
{
char c;
size_t i;
for(i=0; i < nbytes; i += 4, (to += 4), (from += 4)){
c = *from;
*to = *(from+3);
*(to+3) = c;
c = *(from+1);
*(to+1) = *(from+2);
*(to+2) = c;
}
}
#ifdef ANSI_FUNC
static void swap8(char *to, char *from, size_t nbytes)
#else
static void swap8(to, from, nbytes)
char *to;
char *from;
size_t nbytes;
#endif
{
char c;
size_t i;
for(i=0; i < nbytes; i += 8, (to += 8), (from += 8)){
c = *(from+0);
*(to+0) = *(from+7);
*(to+7) = c;
c = *(from+1);
*(to+1) = *(from+6);
*(to+6) = c;
c = *(from+2);
*(to+2) = *(from+5);
*(to+5) = c;
c = *(from+3);
*(to+3) = *(from+4);
*(to+4) = c;
}
}
#ifdef ANSI_FUNC
static void usage(char *s)
#else
static void usage(s)
char *s;
#endif
{
fprintf(stderr,
"usage: %s <switches> [iname [oname]]\n", s);
fprintf(stderr, "optional switches:\n");
fprintf(stderr, " -b bufsize # number of records to read at once (def: %d)\n", BUFSIZE);
fprintf(stderr, " -e elsize # size of an individual record (def: 1)\n");
fprintf(stderr, " -s elsize # swap bytes in a record of this size (def: no swapping)\n");
fprintf(stderr, " -v # verbose messages as data is transferred\n");
fprintf(stderr, "examples:\n");
fprintf(stderr, "# copy named files\n");
fprintf(stderr, "%s foo foo2\n", s);
fprintf(stderr, "# or copy file to socket 1234 on this host\n");
fprintf(stderr, "%s foo '$host:1234'\n", s);
fprintf(stderr, "# or copy from a socket on this host to another host\n");
fprintf(stderr, "%s '$host:1234' 'remotehost:2345'\n", s);
exit(0);
}
#ifdef ANSI_FUNC
int
main (int argc, char **argv)
#else
int main(argc, argv)
int argc;
char **argv;
#endif
{
int c;
int got;
int args;
int ntot=0;
int doswap=0;
int elsize=1;
int verbose=0;
int bufsize=BUFSIZE;
char *ibuf=NULL;
char *obuf=NULL;
char *iname=NULL;
char *oname=NULL;
GIO igio;
GIO ogio;
/* exit on gio errors */
setgerror(2);
/* we want the args in the same order in which they arrived, and
gnu getopt sometimes changes things without this */
putenv("POSIXLY_CORRECT=true");
/* process switch arguments */
while ((c = getopt(argc, argv, "b:e:hs:v")) != -1){
switch(c){
case 'h':
usage(argv[0]);
return 0;
case 'b':
bufsize = atoi(optarg);
break;
case 'e':
elsize = atoi(optarg);
break;
case 's':
elsize = atoi(optarg);
switch(elsize){
case 1:
doswap = 0;
break;
case 2:
case 4:
case 8:
doswap = 1;
break;
default:
gerror(stderr, "ERROR: invalid swap size argument: %d\n", elsize);
break;
}
break;
case 'v':
verbose++;
break;
}
}
/* process remaining command line arguments(i.e. file names) */
args = argc - optind;
switch(args){
case 0:
iname = "stdin";
oname = "stdout";
break;
case 1:
iname = argv[optind+0];
oname = "stdout";
break;
case 2:
iname = argv[optind+0];
oname = argv[optind+1];
break;
default:
usage(argv[0]);
exit(1);
}
/* sanity checks */
if( bufsize <= 0 ) gerror(stderr, "bufsize must be positive\n", bufsize);
if( elsize <= 0 ) gerror(stderr, "elsize must be positive\n", elsize);
/* allocate space */
ibuf = xcalloc(elsize, bufsize);
if( doswap )
obuf = xcalloc(elsize, bufsize);
else
obuf = ibuf;
/* open the input and output files */
if( !(igio = gopen(iname, "r")) )
gerror(stderr, "can't gopen input file: %s\n", iname);
if( !(ogio = gopen(oname, "w")) )
gerror(stderr, "can't gopen output file: %s\n", oname);
/* read/write all bytes */
while( 1 ){
/* read next buffer's worth */
got = gread(igio, ibuf, elsize, bufsize);
if( !got )
break;
else if( got < 0 ){
gerror(stderr, "error reading input file\n");
break;
}
/* swap bytes, if necessary */
if( doswap ){
switch(elsize){
case 2:
swap2(obuf, ibuf, elsize*got);
break;
case 4:
swap4(obuf, ibuf, elsize*got);
break;
case 8:
swap8(obuf, ibuf, elsize*got);
break;
default:
gerror(stderr, "invalid swap size: %d\n", elsize);
break;
}
}
/* verbose display */
if( verbose >= 2 ){
if( doswap ){
fprintf(stderr, "transferring %d swapped %d-byte records\n",
got, elsize);
}
else if( elsize > 1 ){
fprintf(stderr, "transferring %d %d-byte records\n",
got, elsize);
}
else{
fprintf(stderr, "transferring %d bytes\n", got);
}
}
/* write buffer's worth */
if( gwrite(ogio, obuf, elsize, got) != (unsigned int)got ){
gerror(stderr, "error writing output file\n");
}
ntot += got;
}
/* verbose display */
if( verbose ){
if( doswap ){
fprintf(stderr, "transfer from %s to %s: %d swapped %d-byte records\n",
iname, oname, ntot, elsize);
}
else if( elsize > 1 ){
fprintf(stderr, "transfer from %s to %s: %d %d-byte records\n",
iname, oname, ntot, elsize);
}
else{
fprintf(stderr, "transfer from %s to %s: %d bytes\n",
iname, oname, ntot);
}
}
/* close files */
gclose(igio);
gclose(ogio);
/* clean up */
if( ibuf ) xfree(ibuf );
if( doswap && obuf ) xfree(obuf);
return 0;
}
|