libnjb: sendfile.c

libnjb 2.2.7

sendfile.c
#include "common.h"
#include <sys/stat.h>
#include <string.h>
static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
{
int percent = (sent*100)/total;
#ifdef __WIN32__
printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
#else
printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
#endif
fflush(stdout);
return 0;
}
static void usage (void)
{
fprintf(stderr, "usage: sendfile [ -D<debuglvl> ] [ -F<foldername> ] <path>\n");
exit(1);
}
int main(int argc, char **argv)
{
njb_t njbs[NJB_MAX_DEVICES], *njb;
int n, opt, debug;
extern int optind;
extern char *optarg;
char *path;
char *foldername = NULL;
u_int32_t fileid;
char *lang;
debug= 0;
while ( (opt = getopt(argc, argv, "D:F::")) != -1 ) {
switch (opt) {
case 'D':
debug = atoi(optarg);
break;
case 'F':
foldername = optarg;
break;
default:
usage();
break;
}
}
argc-= optind;
argv+= optind;
if ( argc != 1 ) usage();
if (foldername != NULL) {
if (foldername[0] != '\\' || foldername[strlen(foldername)-1] != '\\') {
fprintf(stderr, "Illegal folder parameter: \"%s\"\n", foldername);
fprintf(stderr, "Folder names must begin and end with a backslash, and be doubly escaped e.g.:\n");
fprintf(stderr, "sendfile -F\"\\\\foo\\\\bar\\\\fnord\\\\\" foo.txt\n");
exit(1);
}
}
if ( debug ) NJB_Set_Debug(debug);
/*
* Check environment variables $LANG and $LC_CTYPE
* to see if we want to support UTF-8 unicode
* $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
* trigger unicode support.
*/
lang = getenv("LANG");
if (lang != NULL) {
if (strlen(lang) > 5) {
if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
NJB_Set_Unicode(NJB_UC_UTF8);
}
}
}
path = argv[0];
printf("Sending file:\n");
printf("Filename: %s\n", path);
if (NJB_Discover(njbs, 0, &n) == -1) {
fprintf(stderr, "could not locate any jukeboxes\n");
return 1;
}
if ( n == 0 ) {
fprintf(stderr, "no NJB devices found\n");
return 0;
}
njb = njbs;
if ( NJB_Open(njb) == -1 ) {
NJB_Error_Dump(njb,stderr);
return 1;
}
NJB_Capture(njb);
if ( NJB_Send_File(njb, path, NULL, foldername, progress, NULL, &fileid) == -1 ) {
NJB_Error_Dump(njb,stderr);
} else {
printf("\nNJB ID: %u\n", fileid);
}
printf("\n");
NJB_Release(njb);
NJB_Close(njb);
return 0;
}
NJB_Discover
int NJB_Discover(njb_t *njbs, int limit, int *n)
Definition: procedure.c:108
NJB_UC_UTF8
#define NJB_UC_UTF8
Definition: libnjb.h:147
NJB_Open
int NJB_Open(njb_t *njb)
Definition: procedure.c:130
NJB_Close
void NJB_Close(njb_t *njb)
Definition: procedure.c:184
njb_struct
Definition: libnjb.h:182
NJB_Set_Unicode
void NJB_Set_Unicode(int unicode_flag)
Definition: procedure.c:2833
NJB_Capture
int NJB_Capture(njb_t *njb)
Definition: procedure.c:223
NJB_MAX_DEVICES
#define NJB_MAX_DEVICES
Definition: libnjb.h:60
NJB_Set_Debug
void NJB_Set_Debug(int debug_flags)
Definition: procedure.c:2819
NJB_Send_File
int NJB_Send_File(njb_t *njb, const char *path, const char *name, const char *folder, NJB_Xfer_Callback *callback, void *data, u_int32_t *fileid)
Definition: procedure.c:1723
NJB_Error_Dump
void NJB_Error_Dump(njb_t *njb, FILE *fp)
Definition: njb_error.c:418
NJB_Release
int NJB_Release(njb_t *njb)
Definition: procedure.c:266