home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / comms / x_sun_psio / code / psxmodem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-10  |  5.4 KB  |  208 lines

  1. /* (C) Tim Graves3rd Jan 95
  2.    This code is supplied AS IS. no warrantee either expressed or implied 
  3.    is provided. This code may be freeley modified and modified as long as my
  4.    origional authorship is acknowledged. 
  5.    
  6.    Tim Graves
  7.     Sun Microsystems
  8.      
  9.      */
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include "psion.h"
  16. #include "psxmodem.h"
  17.  
  18.  
  19. static char  vsn [] = "@(#) psxmodem.c 3.2@(#)" ;
  20. /* this file contains the hooks for calling the xmodem routines on the psion
  21.    the approach is that the psion routine has a builtin xmodem support, we 
  22.    cheat and call an external one to do the work for us (I can't be bothered to
  23.    reinvent the wheel */
  24.  
  25. /* protocol wise when sending a file we send the file down first (the other file
  26.    information will already be there, and after the recieve has been
  27.    completed on the psion we send the size so it can truncate the file
  28.    using the pstf call . When recieving the file we use the psgetfsize
  29.    command after the to help us and then truncate the file by hand
  30.    (this is probabaly going to be nasty to do) after the recieve (but
  31.    before any auto filtering happens) */
  32.  
  33. /* psgfx, send the named file to the psion using xmodem */
  34. psgfx(fname)
  35. char * fname;
  36. {
  37.        char * cmdline[4] ;
  38.        char * tmp ;
  39.        long fsize ;
  40.        pid_t cpid ;
  41.        int res ;
  42.     if (debugcall >= 3)
  43.         fprintf(stderr, "CALL: psgfx(fname = %s)\n", fname) ;
  44.     /* build up the command to use */
  45.     if ((tmp = findvar (VAR_XMDMCMD)) != NULL) 
  46.         cmdline[0] = tmp ;
  47.     else
  48.         cmdline [0] =  DEF_XMDMCMD ;
  49.     /* now install the arguments */
  50.     if ((tmp = findvar(VAR_XMDMSENDARGS)) != NULL)
  51.         cmdline [1] = tmp ;
  52.     else
  53.         cmdline[1] =  DEF_XMDMSENDARGS ;
  54.     
  55.     /* install the file name in the command line */
  56.     cmdline[2] = fname ;
  57.     
  58.     /* terminate the command line */
  59.     cmdline[3] = NULL ;
  60.     
  61.     /* send the psion the command */
  62.     pscommand("gf") ;
  63.     
  64.     /* doit, split the child from the main process */
  65.     if ((cpid = fork()))
  66.     {
  67.         /* OK we are the parent, nothing to do but wait about
  68.            untill the child has returned to us */
  69.         if (wait(0) == -1)
  70.         {
  71.             /* PANIC, something nasty has happened, exit all */
  72.             fprintf(stderr, "Danger Will Robinson, Danger\n") ;
  73.             psshutdown(1) ;
  74.         }
  75.         /* we seem to have shutdown safely, set the files length */
  76.         setremflen (fname) ;
  77.         /* and return */
  78.         return (OK) ;
  79.     }
  80.     else
  81.     {
  82.         /* alrightee, we are the child process */
  83.         /* let's do stuff */
  84.         /* dup the file descriptors so the devid is descriptor 0 and 1
  85.            these being stdin and stdout, we'll leave stderr as it is */
  86.         if (dup2(devid,0) < 0)
  87.         {
  88.             fprintf(stderr, "Error in dup2'ing stdin\n") ;
  89.             return ;
  90.         }
  91.         if (dup2(devid, 1) < 0)
  92.         {
  93.             fprintf(stderr, "Error in dup2'ing stdout\n") ;
  94.             return ;
  95.         }
  96.         /* OK, we're all setup let's do the pheonix bit */
  97.         res = execl(cmdline[0], cmdline[0], cmdline[1], cmdline[2], NULL) ;
  98.         /* problems if we got here */
  99.         perror(NULL) ;
  100.         fprintf(stderr, "Error exec failed, erro no %d\n", res) ;
  101.         exit(0) ;
  102.     }
  103. }
  104. /* set the remote files length */
  105. setremflen(fname)
  106. char * fname ;
  107. {
  108.     
  109.     struct stat fstr ;
  110.     if (debugcall > 3)
  111.     {
  112.         fprintf(stderr, "setremflen (%s)\n", fname) ;
  113.     }
  114.     
  115.     /* no real error checking needed, to get to here we must be able to read the file */
  116.     stat (fname, &fstr) ;
  117.     /* send the truncate info accross */
  118.     pstf((int) fstr.st_size) ;
  119. }
  120.  
  121. /* pspfx, recieve the named file to the psion using xmodem */
  122. pspfx(fname)
  123. char * fname;
  124. {
  125.        char * cmdline[4] ;
  126.        char * tmp ;
  127.        long fsize ;
  128.        pid_t cpid ;
  129.        int res ;
  130.     if (debugcall >= 3)
  131.         fprintf(stderr, "CALL: pspfx(fname = %s)\n", fname) ;
  132.     /* build up the command to use */
  133.     if ((tmp = findvar (VAR_XMDMCMD)) != NULL) 
  134.         cmdline[0] = tmp ;
  135.     else
  136.         cmdline [0] =  DEF_XMDMCMD ;
  137.     /* now install the arguments */
  138.     if ((tmp == findvar(VAR_XMDMRECARGS)) != NULL)
  139.         cmdline [1] = tmp ;
  140.     else
  141.         cmdline[1] =  DEF_XMDMSENDARGS ;
  142.     
  143.     /* install the file name in the command line */
  144.     cmdline[2] = fname ;
  145.     
  146.     /* terminate the command line */
  147.     cmdline[3] = NULL ;
  148.     
  149.     /* send the psion the command */
  150.     pscommand("pf") ;
  151.     
  152.     /* doit, split the child from the main process */
  153.     if ((cpid = fork()))
  154.     {
  155.         /* OK we are the parent, nothing to do but wait about
  156.            untill the child has returned to us */
  157.         if (wait(0) == -1)
  158.         {
  159.             /* PANIC, something nasty has happened, exit all */
  160.             fprintf(stderr, "Danger Will Robinson, Danger\n") ;
  161.             psshutdown(1) ;
  162.         }
  163.         /* we seem to have shutdown safely, set the files length */
  164.         setlocflen (fname) ;
  165.         /* and return */
  166.         return (OK) ;
  167.     }
  168.     else
  169.     {
  170.         /* alrightee, we are the child process */
  171.         /* let's do stuff */
  172.         /* dup the file descriptors so the devid is descriptor 0 and 1
  173.            these being stdin and stdout, we'll leave stderr as it is */
  174.         if (dup2(devid,0) < 0)
  175.         {
  176.             fprintf(stderr, "Error in dup2'ing stdin\n") ;
  177.             return ;
  178.         }
  179.         if (dup2(devid, 1) < 0)
  180.         {
  181.             fprintf(stderr, "Error in dup2'ing stdout\n") ;
  182.             return ;
  183.         }
  184.         /* OK, we're all setup let's do the pheonix bit */
  185.         res = execl(cmdline[0], cmdline[0], cmdline[1], cmdline[2], NULL) ;
  186.         /* problems if we got here */
  187.         perror(NULL) ;
  188.         fprintf(stderr, "Error exec failed, erro no %d\n", res) ;
  189.         exit(0) ;
  190.     }
  191. }
  192.  
  193. setlocflen (fname)
  194. char * fname ;
  195. {
  196.     int psize ;
  197.     if (debugcall >= 3)
  198.     {
  199.         fprintf(stderr, "setlocflen(%s)\n", fname) ;
  200.     }
  201.     
  202.           /* first get the size of the origional file from the psion  */
  203.          psize = psgetfsize() ;
  204.          
  205.          /* truncate the file */
  206.          truncate(fname, (off_t) psize) ;
  207. }
  208.