home *** CD-ROM | disk | FTP | other *** search
- /* (C) Tim Graves3rd Jan 95
- This code is supplied AS IS. no warrantee either expressed or implied
- is provided. This code may be freeley modified and modified as long as my
- origional authorship is acknowledged.
-
- Tim Graves
- Sun Microsystems
-
- */
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include "psion.h"
- #include "psxmodem.h"
-
-
- static char vsn [] = "@(#) psxmodem.c 3.2@(#)" ;
- /* this file contains the hooks for calling the xmodem routines on the psion
- the approach is that the psion routine has a builtin xmodem support, we
- cheat and call an external one to do the work for us (I can't be bothered to
- reinvent the wheel */
-
- /* protocol wise when sending a file we send the file down first (the other file
- information will already be there, and after the recieve has been
- completed on the psion we send the size so it can truncate the file
- using the pstf call . When recieving the file we use the psgetfsize
- command after the to help us and then truncate the file by hand
- (this is probabaly going to be nasty to do) after the recieve (but
- before any auto filtering happens) */
-
- /* psgfx, send the named file to the psion using xmodem */
- psgfx(fname)
- char * fname;
- {
- char * cmdline[4] ;
- char * tmp ;
- long fsize ;
- pid_t cpid ;
- int res ;
- if (debugcall >= 3)
- fprintf(stderr, "CALL: psgfx(fname = %s)\n", fname) ;
- /* build up the command to use */
- if ((tmp = findvar (VAR_XMDMCMD)) != NULL)
- cmdline[0] = tmp ;
- else
- cmdline [0] = DEF_XMDMCMD ;
- /* now install the arguments */
- if ((tmp = findvar(VAR_XMDMSENDARGS)) != NULL)
- cmdline [1] = tmp ;
- else
- cmdline[1] = DEF_XMDMSENDARGS ;
-
- /* install the file name in the command line */
- cmdline[2] = fname ;
-
- /* terminate the command line */
- cmdline[3] = NULL ;
-
- /* send the psion the command */
- pscommand("gf") ;
-
- /* doit, split the child from the main process */
- if ((cpid = fork()))
- {
- /* OK we are the parent, nothing to do but wait about
- untill the child has returned to us */
- if (wait(0) == -1)
- {
- /* PANIC, something nasty has happened, exit all */
- fprintf(stderr, "Danger Will Robinson, Danger\n") ;
- psshutdown(1) ;
- }
- /* we seem to have shutdown safely, set the files length */
- setremflen (fname) ;
- /* and return */
- return (OK) ;
- }
- else
- {
- /* alrightee, we are the child process */
- /* let's do stuff */
- /* dup the file descriptors so the devid is descriptor 0 and 1
- these being stdin and stdout, we'll leave stderr as it is */
- if (dup2(devid,0) < 0)
- {
- fprintf(stderr, "Error in dup2'ing stdin\n") ;
- return ;
- }
- if (dup2(devid, 1) < 0)
- {
- fprintf(stderr, "Error in dup2'ing stdout\n") ;
- return ;
- }
- /* OK, we're all setup let's do the pheonix bit */
- res = execl(cmdline[0], cmdline[0], cmdline[1], cmdline[2], NULL) ;
- /* problems if we got here */
- perror(NULL) ;
- fprintf(stderr, "Error exec failed, erro no %d\n", res) ;
- exit(0) ;
- }
- }
- /* set the remote files length */
- setremflen(fname)
- char * fname ;
- {
-
- struct stat fstr ;
- if (debugcall > 3)
- {
- fprintf(stderr, "setremflen (%s)\n", fname) ;
- }
-
- /* no real error checking needed, to get to here we must be able to read the file */
- stat (fname, &fstr) ;
- /* send the truncate info accross */
- pstf((int) fstr.st_size) ;
- }
-
- /* pspfx, recieve the named file to the psion using xmodem */
- pspfx(fname)
- char * fname;
- {
- char * cmdline[4] ;
- char * tmp ;
- long fsize ;
- pid_t cpid ;
- int res ;
- if (debugcall >= 3)
- fprintf(stderr, "CALL: pspfx(fname = %s)\n", fname) ;
- /* build up the command to use */
- if ((tmp = findvar (VAR_XMDMCMD)) != NULL)
- cmdline[0] = tmp ;
- else
- cmdline [0] = DEF_XMDMCMD ;
- /* now install the arguments */
- if ((tmp == findvar(VAR_XMDMRECARGS)) != NULL)
- cmdline [1] = tmp ;
- else
- cmdline[1] = DEF_XMDMSENDARGS ;
-
- /* install the file name in the command line */
- cmdline[2] = fname ;
-
- /* terminate the command line */
- cmdline[3] = NULL ;
-
- /* send the psion the command */
- pscommand("pf") ;
-
- /* doit, split the child from the main process */
- if ((cpid = fork()))
- {
- /* OK we are the parent, nothing to do but wait about
- untill the child has returned to us */
- if (wait(0) == -1)
- {
- /* PANIC, something nasty has happened, exit all */
- fprintf(stderr, "Danger Will Robinson, Danger\n") ;
- psshutdown(1) ;
- }
- /* we seem to have shutdown safely, set the files length */
- setlocflen (fname) ;
- /* and return */
- return (OK) ;
- }
- else
- {
- /* alrightee, we are the child process */
- /* let's do stuff */
- /* dup the file descriptors so the devid is descriptor 0 and 1
- these being stdin and stdout, we'll leave stderr as it is */
- if (dup2(devid,0) < 0)
- {
- fprintf(stderr, "Error in dup2'ing stdin\n") ;
- return ;
- }
- if (dup2(devid, 1) < 0)
- {
- fprintf(stderr, "Error in dup2'ing stdout\n") ;
- return ;
- }
- /* OK, we're all setup let's do the pheonix bit */
- res = execl(cmdline[0], cmdline[0], cmdline[1], cmdline[2], NULL) ;
- /* problems if we got here */
- perror(NULL) ;
- fprintf(stderr, "Error exec failed, erro no %d\n", res) ;
- exit(0) ;
- }
- }
-
- setlocflen (fname)
- char * fname ;
- {
- int psize ;
- if (debugcall >= 3)
- {
- fprintf(stderr, "setlocflen(%s)\n", fname) ;
- }
-
- /* first get the size of the origional file from the psion */
- psize = psgetfsize() ;
-
- /* truncate the file */
- truncate(fname, (off_t) psize) ;
- }
-