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.orig < prev    next >
Encoding:
Text File  |  1997-05-23  |  2.9 KB  |  140 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.   ret = pi_listen(sd,1);
  50.   if(ret == -1) {
  51.     perror("pi_listen");
  52.     exit(1);
  53.   }
  54.  
  55.   sd = pi_accept(sd, 0, 0);
  56.   if(sd == -1) {
  57.     perror("pi_accept");
  58.     exit(1);
  59.   }
  60.  
  61.   /* Ask the pilot who it is. */
  62.   dlp_ReadUserInfo(sd,&U);
  63.   
  64.   /* Tell user (via Pilot) that we are starting things up */
  65.   dlp_OpenConduit(sd);
  66.   
  67.   /* Open the Memo Pad's database, store access handle in db */
  68.   if(dlp_OpenDB(sd, 0, 0x80|0x40, "MemoDB", &db) < 0) {
  69.     puts("Unable to open MemoDB");
  70.     dlp_AddSyncLogEntry(sd, "Unable to open MemoDB.\n");
  71.     exit(1);
  72.   }
  73.   
  74.   l = dlp_ReadAppBlock(sd, db, 0, (unsigned char *)buf, 0xffff);
  75.   unpack_MemoAppInfo(&mai, (unsigned char *)buf, l);
  76.  
  77.   category = 0;
  78.   
  79.   for (i=2; i<argc; i++) {
  80.   
  81.     if (strcmp(argv[i],"-c")==0) {
  82.       for (l=0; l<16; l++)
  83.         if (strcasecmp(mai.CategoryName[l], argv[i+1]) == 0) {
  84.           category = l;
  85.           break;
  86.         }
  87.       if (l==16)
  88.         category = atoi(argv[i+1]);
  89.       i++;
  90.       continue;
  91.     }
  92.  
  93.     f = fopen(argv[i], "r");
  94.     if (f == NULL) {
  95.       perror("fopen");
  96.       exit(1);
  97.     }
  98.  
  99.     fseek(f, 0, SEEK_END);
  100.     memo_size = ftell(f);
  101.     fseek(f, 0, SEEK_SET);
  102.  
  103.     l = strlen(argv[i]);
  104.  
  105.     memo_buf = (char*)malloc(memo_size + l + 2);
  106.     if (memo_buf == NULL) {
  107.       perror("malloc()");
  108.       exit(1);
  109.     }
  110.  
  111.     strcpy(memo_buf, argv[i]);
  112.     memo_buf[l] = '\n';
  113.  
  114.     fread(memo_buf + l + 1, memo_size, 1, f);
  115.     memo_buf[l + 1 + memo_size] = '\0';
  116.  
  117.     /* dlp_exec(sd, 0x26, 0x20, &db, 1, NULL, 0); */
  118.     dlp_WriteRecord(sd, (unsigned char)db, 0, 0, category,
  119.             (unsigned char *)memo_buf, -1, 0);
  120.     free(memo_buf);
  121.   }
  122.  
  123.   /* Close the database */
  124.   dlp_CloseDB(sd, db);
  125.  
  126.   /* Tell the user who it is, with a different PC id. */
  127.   U.lastSyncPC = 0x00010000;
  128.   U.succSyncDate = time(NULL);
  129.   U.lastSyncDate = U.succSyncDate;
  130.   dlp_WriteUserInfo(sd,&U);
  131.  
  132.   dlp_AddSyncLogEntry(sd, "Wrote memo to Pilot.\n");
  133.   
  134.   /* All of the following code is now unnecessary, but harmless */
  135.   
  136.   dlp_EndOfSync(sd,0);
  137.   pi_close(sd);
  138.   exit(0);
  139. }
  140.