39 #include <sys/types.h>
47 {
const char *pgm =
"xrdcrc32c";
58 void Fatal(
const char *op,
const char *target)
63 std::cerr <<
"xrdcrc32c: Unable to "<<op<<
' '<<target<<
"; "<<
XrdSysE2T(errno)<<std::endl;
73 std::cerr <<
"\nUsage: xrdcrc32c [opts] [<path> | -]\n"
74 "\n<path> the path to the file whose checksum if to be computed."
75 "\n- compute checksum from data presented at standard in;"
76 "\n example: xrdcp <url> - | xrdcrc32c -\n"
77 "\nopts: -d -h -n -s -x\n"
78 "\n-d read data directly into the buffer, do not use the file cache."
79 "\n-h display usage information (arguments ignored)."
80 "\n-n do not end output with a newline character."
81 "\n-s do not include file path in output result."
82 "\n-x do not print leading zeroes in the checksum, if any."
91 int main(
int argc,
char *argv[])
95 static const int buffSZ = 1024*1024;
96 const char *fPath, *fmt =
"%08x";
97 int bytes, fd,
opts = O_RDONLY;
99 bool addPath =
true, addNL =
true;
105 if (argc > 1 &&
'-' == *argv[1])
106 while ((c = getopt(argc,argv,
"dhnsx")) && ((
unsigned char)c != 0xff))
113 case 'n': addNL =
false;
115 case 's': addPath =
false;
117 case 'x': fmt =
"%x";
119 default: std::cerr <<pgm <<
'-' <<char(
optopt) <<
" option is invalid" <<std::endl;
138 int rc = posix_memalign(&buffP, sysconf(_SC_PAGESIZE), buffSZ);
139 if (rc) {errno = rc;
Fatal(
"allocate buffer to read", fPath);}
143 while((bytes =
read(fd, buffP, buffSZ)) > 0)
148 if (bytes < 0)
Fatal(
"read", fPath);
152 sprintf(csBuff, fmt, csVal);
153 std::cout <<(
char *)csBuff;
154 if (addPath) std::cout <<
' ' <<fPath;
155 if (addNL) std::cout << std::endl;
int main(int argc, char *argv[])
void Fatal(const char *op, const char *target)
int open(const char *path, int oflag,...)
ssize_t read(int fildes, void *buf, size_t nbyte)
const char * XrdSysE2T(int errcode)
static uint32_t Calc32C(const void *data, size_t count, uint32_t prevcs=0)