libnjb: sendfile.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# sendfile.c

    00001 #include "common.h"
    00002 #include <sys/stat.h>
    00003 #include <string.h>
    00004
    00005 static int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
    00006 {
    00007   int percent = (sent*100)/total;
    00008 #ifdef __WIN32__
    00009   printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
    00010 #else
    00011   printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
    00012 #endif
    00013   fflush(stdout);
    00014   return 0;
    00015 }
    00016
    00017 static void usage (void)
    00018 {
    00019   fprintf(stderr, "usage: sendfile [ -D<debuglvl> ] [ -F<foldername> ] <path>\n");
    00020   exit(1);
    00021 }
    00022
    00023
    00024 int main(int argc, char **argv)
    00025 {
    00026   njb_t njbs[NJB_MAX_DEVICES], *njb;
    00027   int n, opt, debug;
    00028   extern int optind;
    00029   extern char *optarg;
    00030   char *path;
    00031   char *foldername = NULL;
    00032   u_int32_t fileid;
    00033   char *lang;
    00034
    00035   debug= 0;
    00036   while ( (opt = getopt(argc, argv, "D:F::")) != -1 ) {
    00037     switch (opt) {
    00038     case 'D':
    00039       debug = atoi(optarg);
    00040       break;
    00041     case 'F':
    00042       foldername = optarg;
    00043       break;
    00044     default:
    00045       usage();
    00046       break;
    00047     }
    00048   }
    00049   argc-= optind;
    00050   argv+= optind;
    00051
    00052   if ( argc != 1 ) usage();
    00053
    00054   if (foldername != NULL) {
    00055     if (foldername[0] != '\\' || foldername[strlen(foldername)-1] != '\\') {
    00056       fprintf(stderr, "Illegal folder parameter: \"%s\"\n", foldername);
    00057       fprintf(stderr, "Folder names must begin and end with a backslash, and be doubly escaped e.g.:\n");
    00058       fprintf(stderr, "sendfile -F\"\\\\foo\\\\bar\\\\fnord\\\\\" foo.txt\n");
    00059       exit(1);
    00060     }
    00061   }
    00062
    00063   if ( debug ) NJB_Set_Debug(debug);
    00064
    00065   /*
    00066    * Check environment variables $LANG and $LC_CTYPE
    00067    * to see if we want to support UTF-8 unicode
    00068    * $LANG = "xx_XX.UTF-8" or $LC_CTYPE = "?"
    00069    * trigger unicode support.
    00070    */
    00071   lang = getenv("LANG");
    00072   if (lang != NULL) {
    00073     if (strlen(lang) > 5) {
    00074       if (!strcmp(&lang[strlen(lang)-5], "UTF-8")) {
    00075         NJB_Set_Unicode(NJB_UC_UTF8);
    00076       }
    00077     }
    00078   }
    00079
    00080   path = argv[0];
    00081
    00082   printf("Sending file:\n");
    00083   printf("Filename:     %s\n", path);
    00084
    00085   if (NJB_Discover(njbs, 0, &n) == -1) {
    00086     fprintf(stderr, "could not locate any jukeboxes\n");
    00087     return 1;
    00088   }
    00089
    00090   if ( n == 0 ) {
    00091     fprintf(stderr, "no NJB devices found\n");
    00092     return 0;
    00093   }
    00094
    00095   njb = njbs;
    00096
    00097   if ( NJB_Open(njb) == -1 ) {
    00098     NJB_Error_Dump(njb,stderr);
    00099     return 1;
    00100   }
    00101
    00102   NJB_Capture(njb);
    00103
    00104   if ( NJB_Send_File(njb, path, NULL, foldername, progress, NULL, &fileid) == -1 ) {
    00105     NJB_Error_Dump(njb,stderr);
    00106   } else {
    00107     printf("\nNJB ID:    %u\n", fileid);
    00108   }
    00109   printf("\n");
    00110
    00111   NJB_Release(njb);
    00112
    00113   NJB_Close(njb);
    00114
    00115   return 0;
    00116 }