home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 August / PCO0897.ISO / filesbbs / os2 / plnk065.arj / PLNK065.ZIP / pilot-link.0.6.5 / install-memo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  3.2 KB  |  142 lines

  1. /* install-memo.c:  Pilot memo pad installer
  2.  *
  3.  * This is free software, licensed under the GNU Public License V2.
  4.  * See the file COPYING for details.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "pi-source.h"
  10. #include "pi-socket.h"
  11. #include "pi-dlp.h"
  12. #include "pi-memo.h"
  13.  
  14. int main(int argc, char *argv[])
  15. {
  16.   struct pi_sockaddr addr;
  17.   int db;
  18.   int sd;
  19.   int i;
  20.   int l;
  21.   int memo_size;
  22.   char *memo_buf;
  23.   FILE *f;
  24.   struct PilotUser U;
  25.   int ret;
  26.   char buf[0xffff];
  27.   int category;
  28.   struct MemoAppInfo mai;
  29.  
  30.   if (argc < 3) {
  31.     fprintf(stderr,"usage:%s %s [-c category] file [file] ...\n",argv[0],TTYPrompt);
  32.     exit(2);
  33.   }
  34.   if (!(sd = pi_socket(PI_AF_SLP, PI_SOCK_STREAM, PI_PF_PADP))) {
  35.     perror("pi_socket");
  36.     exit(1);
  37.   }
  38.     
  39.   addr.pi_family = PI_AF_SLP;
  40.   addr.pi_port = 3;
  41.   strcpy(addr.pi_device,argv[1]);
  42.   
  43.   ret = pi_bind(sd, &addr, sizeof(addr));
  44.   if(ret == -1) {
  45.     perror("pi_bind");
  46.     exit(1);
  47.   }
  48.  
  49.   fprintf(stderr, "Waiting for connection (press the HotSync button now)...\n");
  50.  
  51.   ret = pi_listen(sd,1);
  52.   if(ret == -1) {
  53.     perror("pi_listen");
  54.     exit(1);
  55.   }
  56.  
  57.   sd = pi_accept(sd, 0, 0);
  58.   if(sd == -1) {
  59.     perror("pi_accept");
  60.     exit(1);
  61.   }
  62.  
  63.   /* Ask the pilot who it is. */
  64.   dlp_ReadUserInfo(sd,&U);
  65.   
  66.   /* Tell user (via Pilot) that we are starting things up */
  67.   dlp_OpenConduit(sd);
  68.   
  69.   /* Open the Memo Pad's database, store access handle in db */
  70.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "MemoDB", &db) < 0) {
  71.     puts("Unable to open MemoDB");
  72.     dlp_AddSyncLogEntry(sd, "Unable to open MemoDB.\n");
  73.     exit(1);
  74.   }
  75.   
  76.   l = dlp_ReadAppBlock(sd, db, 0, (unsigned char *)buf, 0xffff);
  77.   unpack_MemoAppInfo(&mai, (unsigned char *)buf, l);
  78.  
  79.   category = 0;
  80.   
  81.   for (i=2; i<argc; i++) {
  82.   
  83.     if (strcmp(argv[i],"-c")==0) {
  84.       for (l=0; l<16; l++)
  85.         if (strcasecmp(mai.CategoryName[l], argv[i+1]) == 0) {
  86.           category = l;
  87.           break;
  88.         }
  89.       if (l==16)
  90.         category = atoi(argv[i+1]);
  91.       i++;
  92.       continue;
  93.     }
  94.  
  95.     f = fopen(argv[i], "r");
  96.     if (f == NULL) {
  97.       perror("fopen");
  98.       exit(1);
  99.     }
  100.  
  101.     fseek(f, 0, SEEK_END);
  102.     memo_size = ftell(f);
  103.     fseek(f, 0, SEEK_SET);
  104.  
  105.     l = strlen(argv[i]);
  106.  
  107.     memo_buf = (char*)malloc(memo_size + l + 2);
  108.     if (memo_buf == NULL) {
  109.       perror("malloc()");
  110.       exit(1);
  111.     }
  112.  
  113.     strcpy(memo_buf, argv[i]);
  114.     memo_buf[l] = '\n';
  115.  
  116.     fread(memo_buf + l + 1, memo_size, 1, f);
  117.     memo_buf[l + 1 + memo_size] = '\0';
  118.  
  119.     /* dlp_exec(sd, 0x26, 0x20, &db, 1, NULL, 0); */
  120.     dlp_WriteRecord(sd, (unsigned char)db, 0, 0, category,
  121.                     (unsigned char *)memo_buf, -1, 0);
  122.     free(memo_buf);
  123.   }
  124.  
  125.   /* Close the database */
  126.   dlp_CloseDB(sd, db);
  127.  
  128.   /* Tell the user who it is, with a different PC id. */
  129.   U.lastSyncPC = 0x00010000;
  130.   U.succSyncDate = time(NULL);
  131.   U.lastSyncDate = U.succSyncDate;
  132.   dlp_WriteUserInfo(sd,&U);
  133.  
  134.   dlp_AddSyncLogEntry(sd, "Wrote memo to Pilot.\n");
  135.   
  136.   /* All of the following code is now unnecessary, but harmless */
  137.   
  138.   dlp_EndOfSync(sd,0);
  139.   pi_close(sd);
  140.   exit(0);
  141. }
  142.