home *** CD-ROM | disk | FTP | other *** search
/ Software Recommendations - 1998 Season 1 / DNBCD4.iso / share / DOS / ipxcopy / SRC.ZIP / SRC / MAIN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-19  |  7.9 KB  |  339 lines

  1. /*
  2.  
  3.    MAIN.C
  4.  
  5.    (c) 1996 Oliver Kraus
  6.  
  7.    Versions:
  8.       1.0        simple tranfer version
  9.       2.0        rewritten
  10.       2.1        keep date/time, crc-16
  11.       2.2        write error with zero-size files
  12.       2.3        rewitten file access
  13.  
  14. */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <conio.h>
  20. #include "tptx.h"
  21. #include "tprx.h"
  22. #include "crdir.h"
  23. #include "cmdline.h"
  24. #include "cbreak.h"
  25. /* #include "debugmem.h" */
  26.  
  27. char *init_error = "init error";
  28. /*
  29.       time    bytes/s size      pos/crc   name
  30.  xxx% xxxx.xs xxxxxxx xxxxxxxxx xxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  31. */
  32. char *head_str =
  33. "     time    bytes/s       pos/size crc  name\n"
  34. "------------------------------------------------------------------------------\n";
  35.  
  36.  
  37. long cl_socket = 2007;
  38. int is_subdir  = 0;
  39. int is_help    = 0;
  40. int is_test_mode = 0;
  41. int is_disable_crc = 0;
  42. int is_crc32 = 0;
  43. int is_verbose = 0;
  44.  
  45. cl_entry_struct cl_list[] =
  46. {
  47.    { CL_TYP_LONG, "sn",    &cl_socket,  0 },
  48.    { CL_TYP_ON,   "test",  &is_test_mode,  0 },
  49.    { CL_TYP_ON,   "nocrc", &is_disable_crc,  0 },
  50.    { CL_TYP_ON,   "crc32", &is_crc32,  0 },
  51.    { CL_TYP_ON,   "help",  &is_help,    0 },
  52.    { CL_TYP_ON,   "h",     &is_help,    0 },
  53.    { CL_TYP_ON,   "?",     &is_help,    0 },
  54.    { CL_TYP_ON,   "s",     &is_subdir,  0 },
  55.    { CL_TYP_ON,   "v",     &is_verbose,  0 },
  56.    CL_ENTRY_LAST
  57. };
  58.  
  59. void help(void)
  60. {
  61.    printf("ipxcopy [options] [filemask]          V2.4 " __DATE__ "\n");
  62.    printf("file transfer program, based on ipx\n");
  63.    printf("freeware by Oliver Kraus, kraus@lrs.e-technik.uni-erlangen.de\n");
  64.    printf("options:\n");
  65.    printf("   -h -?        show this text\n");
  66.    printf("   -sn num      socketnumber (current: %ld)\n", cl_socket );
  67.    printf("transmitter only:\n");
  68.    printf("   -v           verbose mode\n" );
  69.    printf("   -s           include subdirectories\n" );
  70.    printf("   -test        do not write files\n");
  71.    printf("   -nocrc       disable crc\n");
  72.    printf("   -crc32       use crc-32 (default: crc-16)\n");
  73.    printf("filemask:\n");
  74.    printf("   *   match zero, one or more characters\n");
  75.    printf("   ?   match exactly one character\n");
  76. }
  77.  
  78. void print_pdata(tp_pdata_struct *d, int is_end)
  79. {
  80.    long t;
  81.    size_t len, offset = 0;
  82.    t = clock()-d->file_start;
  83.    len = strlen(d->path);
  84.    if ( len > 37 )
  85.       offset = len-37;
  86.    if ( is_end == 0 )
  87.    {
  88.       printf("%3ld%% %4ld.%lds %7ld %9ld/%-9ld %-37s\r",
  89.          (long)(d->curr*100L)/d->total,
  90.          (long)t/CLOCKS_PER_SEC,
  91.          (long)(((t*10L)/CLOCKS_PER_SEC) % 10L),
  92.          (d->curr*CLOCKS_PER_SEC)/t,
  93.          (long)d->curr,
  94.          (long)d->total,
  95.          d->path+offset);
  96.    }
  97.    else
  98.    {
  99.       printf("%3ld%% %4ld.%lds %7ld %9ld  %08lx %-37s\r",
  100.          (long)(100L),
  101.          (long)t/CLOCKS_PER_SEC,
  102.          (long)(((t*10L)/CLOCKS_PER_SEC) % 10L),
  103.          (d->curr*CLOCKS_PER_SEC)/t,
  104.          (long)d->total,
  105.          (unsigned long)d->crc,
  106.          d->path+offset);
  107.    }
  108.    fflush(stdout);
  109. }
  110.  
  111. int rx_aux( int msg, void *data )
  112. {
  113.    tp_pdata_struct *d = (tp_pdata_struct *)data;
  114.    switch(msg)
  115.    {
  116.       case TP_MSG_PSTART:
  117.          break;
  118.       case TP_MSG_PDATA:
  119.          print_pdata(d, 0);
  120.          break;
  121.       case TP_MSG_PEND:
  122.          print_pdata(d, 1);
  123.          printf("\n");
  124.          break;
  125.    }
  126.    return 1;
  127. }
  128.  
  129.  
  130. int rx()
  131. {
  132.    tprx_type tprx;
  133.    tprx = tprx_Open((short)cl_socket);
  134.    if ( tprx == NULL )
  135.       return 1;
  136.    tprx_SetAux(tprx, rx_aux);
  137.    printf(head_str);
  138.    for(;;)
  139.    {
  140.       if ( tprx_Dispatch(tprx) != 0 )
  141.          break;
  142.    }
  143.    tprx_Close(tprx);
  144.    return 0;
  145. }
  146.  
  147. int tx_aux( int msg, void *data )
  148. {
  149.    tp_pdata_struct *d = (tp_pdata_struct *)data;
  150.    switch(msg)
  151.    {
  152.       case TP_MSG_PSTART:
  153.          break;
  154.       case TP_MSG_PDATA:
  155.          print_pdata(d, 0);
  156.          break;
  157.       case TP_MSG_PEND:
  158.          print_pdata(d, 1);
  159.          printf("\n");
  160.          break;
  161.    }
  162.    return 1;
  163. }
  164.  
  165. int tx(tptx_type tptx, char *phy_name, char *log_name)
  166. {
  167.    int ret;
  168.    tptx_Send(tptx, phy_name, log_name);
  169.  
  170.    for(;;)
  171.    {
  172.       ret = tptx_Dispatch(tptx);
  173.       if ( ret == 2 )
  174.          return 0;
  175.       if ( ret != 0 )
  176.          break;
  177.    }
  178.    return 1;
  179. }
  180.  
  181. int file_tx()
  182. {
  183.    static char drive[10];
  184.    static char dir[C_MAX_PATHNAME];
  185.    static char fname[C_MAX_FILE];
  186.    static char ext[C_MAX_FILE];
  187.    static char path[C_MAX_PATHNAME];
  188.    CRDIR *d;
  189.    struct c_dirent *e;
  190.    tptx_type tptx;
  191.    clock_t total_start = clock();
  192.    clock_t total_time;
  193.    long files, dirs;
  194.  
  195.    strupr(cl_file_list[0]);
  196.  
  197.    _splitpath( cl_file_list[0], drive, dir, fname, ext );
  198.    strcpy(path, drive);
  199.    strcat(path, dir);
  200.    strcat(fname, ext);
  201.    if ( strcmp(fname, "*.*") == 0 )
  202.       strcpy(fname, "*");
  203.  
  204.    if ( is_subdir )
  205.       d = c_openrdir(path, fname, CRDIR_GO_DIRS|CRDIR_RET_DIRS);
  206.    else
  207.       d = c_openrdir(path, fname, 0);
  208.  
  209.    if ( d == NULL )
  210.    {
  211.       printf("init error with path '%s', filemask '%s'\n", path, fname);
  212.       return 0;
  213.    }
  214.  
  215.    tptx = tptx_Open((short)cl_socket);
  216.    if ( tptx == NULL )
  217.    {
  218.       c_closerdir(d);
  219.       return 0;
  220.    }
  221.    tptx_SetAux(tptx, tx_aux);
  222.    if ( is_test_mode != 0 )
  223.       tptx_SetFlag(tptx, TP_FLAG_IS_TEST_MODE);
  224.    if ( is_disable_crc != 0 )
  225.       tptx_SetFlag(tptx, TP_FLAG_IS_DISABLE_CRC);
  226.    if ( is_crc32 != 0 )
  227.       tptx_SetFlag(tptx, TP_FLAG_IS_CRC32);
  228.  
  229.    files = 0;
  230.    dirs = 0;
  231.  
  232.    printf(head_str);
  233.    for(;;)
  234.    {
  235.       static char phy_path[_MAX_PATH];
  236.       static char log_path[_MAX_PATH];
  237.       e = c_readrdir(d);
  238.       if ( e == NULL )
  239.          break;
  240.       sprintf(phy_path, "%s%s","",c_get_name(e));
  241.       sprintf(log_path, "%s%s",c_getpath(d),c_get_name(e));
  242.       if ( is_verbose != 0 )
  243.       {
  244.          fprintf(stdout, "%s --> %s\n", phy_path, log_path);
  245.          fflush(stdout);
  246.       }
  247.       if ( c_is_dir(e) != 0 )
  248.       {
  249.          dirs++;
  250.          if ( tx(tptx, NULL, log_path) == 0 )
  251.             break;
  252.       }
  253.       else
  254.       {
  255.          files++;
  256.          if ( tx(tptx, phy_path, log_path) == 0 )
  257.             break;
  258.       }
  259.    }
  260.    c_closerdir(d);
  261.  
  262.    if ( e == NULL )
  263.    {
  264.       total_time = clock()-total_start;
  265.       printf("      ------ ------- ---------\n");
  266.       printf("     %4ld.%lds %7ld %9ld bytes in %ld files and %ld dirs\n",
  267.             (long)tptx->f_time_sum/CLOCKS_PER_SEC,
  268.             (long)(((tptx->f_time_sum*10L)/CLOCKS_PER_SEC) % 10L),
  269.             (tptx->f_len_sum*CLOCKS_PER_SEC)/tptx->f_time_sum,
  270.             (long)tptx->f_len_sum, files, dirs);
  271.       if ( is_verbose != 0 )
  272.       {
  273.          printf("total%4ld.%lds %7ld %9ld\n",
  274.                (long)total_time/CLOCKS_PER_SEC,
  275.                (long)(((total_time*10L)/CLOCKS_PER_SEC) % 10L),
  276.                (tptx->f_len_sum*CLOCKS_PER_SEC)/total_time,
  277.                (long)tptx->f_len_sum);
  278.       }
  279.    }
  280.  
  281.    tptx_Close(tptx);
  282.  
  283.    return 1;
  284. }
  285.  
  286.  
  287. int main(int argc, char *argv[])
  288. {
  289.    static char path[C_MAX_PATHNAME];
  290.    int ret;
  291.  
  292.    /*
  293.    FILE *fp = fopen("error.log", "w");
  294.    malloc_SetWarningLevel(8);
  295.    malloc_SetStream(fp);
  296.    */
  297.  
  298.    if ( cl_Do(cl_list, argc, argv) == 0 )
  299.    {
  300.       puts("cmdline error");
  301.       help();
  302.       return 1;
  303.    }
  304.  
  305.    if ( is_help != 0 )
  306.    {
  307.       help();
  308.       return 1;
  309.    }
  310.  
  311.    break_Install();
  312.    strcpy(path, c_getcwd());
  313.  
  314.    if ( cl_file_cnt < 1 )
  315.    {
  316.       puts("ipxcopy receiver (-h for help)");
  317.       ret = (rx() == 0 ? 1 : 0);
  318.    }
  319.    else
  320.    {
  321.       puts("ipxcopy transmitter");
  322.       if ( is_test_mode != 0 )
  323.          puts("test mode enabled");
  324.       if ( is_disable_crc != 0 )
  325.          puts("crc calculation disabled");
  326.       ret = (file_tx() == 0 ? 1 : 0);
  327.    }
  328.  
  329.    /*
  330.    malloc_PrintInfo();
  331.    fclose(fp);
  332.    */
  333.  
  334.    c_set_path(path);
  335.    break_Remove();
  336.  
  337.    return ret;
  338. }
  339.