home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1992 / 14 / putclip.c < prev    next >
C/C++ Source or Header  |  1992-06-03  |  2KB  |  78 lines

  1. /* 
  2. PUTCLIP.C -- put text onto Windows clipboard
  3. requires WINCLIP.C
  4. for example (Borland C++): bcc putclip.c winclip.c
  5.  
  6. Copyright (c) 1992 Ziff Davis Communications
  7. PC Magazine * Andrew Schulman (June 1992)
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <io.h>
  15. #include <fcntl.h>
  16. #include "winclip.h"
  17.  
  18. void fail(char *s) { puts(s); exit(1); }
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     unsigned len;
  23.     char *p;
  24.     
  25.     fputs("PUTCLIP version 1.0\n", stderr);
  26.     fputs("Copyright (c) 1992 Ziff Davis Communications "
  27.           "* Andrew Schulman\n\n", stderr);
  28.       
  29.     if ((argc < 2) ||
  30.         ((argc > 1) && (argv[1][0]=='/') && (argv[1][1]=='?'))) // /?
  31.         fail(
  32.     "PUTCLIP puts text onto the Windows clipboard\n"
  33.     "usage: PUTCLIP text        puts text into clipboard\n"
  34.     "       PUTCLIP @filename   puts file contents into clipboard\n"
  35.     "       PUTCLIP -           copies standard input into clipboard\n");
  36.  
  37.     if (! WindowsClipboard())
  38.          fail("This program must run in a DOS box "
  39.               "under Enhanced mode Windows");
  40.  
  41.     if (! (argv[1][0] == '-' || argv[1][0] == '@'))
  42.     {
  43.          /* Put string from command line onto clipboard */
  44.          static char buf[128];
  45.          char far *cmdline = MK_FP(_psp, 0x82);
  46.          int len = *((unsigned char far *) MK_FP(_psp, 0x80)) - 1;
  47.          _fmemcpy(buf, cmdline, len);
  48.          p=buf;
  49.     }
  50.     else
  51.     {
  52.          int f;
  53.          unsigned rc;    /* count of bytes read */
  54.          if (argv[1][0] == '-' && argv[1][1] == '\0')  // - on cmdline
  55.               f = 0;      // STDIN
  56.          else if (argv[1][0] == '@')   // filename on cmdline
  57.               if ((f = open(&argv[1][1], O_RDONLY)) == -1)
  58.                   fail("can't open file");
  59.          if ((len = filelength(f)) > (32 * 1024))// 32k max
  60.               fail("file too big");
  61.          if ((p = malloc(len+1)) == 0)
  62.               fail("insufficient memory");
  63.          if ((rc = read(f, p, len)) < 1) // means 32k max!
  64.               fail("can't read file");
  65.          close(f);
  66.          p[rc] = '\0';   /* must be NULL terminated */
  67.          len = rc;
  68.     }
  69.  
  70.     if (PutClipStrLen(p, len+1))   // +1 for NULL
  71.         puts("putclip successful");
  72.     else
  73.         puts("putclip failed");
  74.     
  75.     return 0;
  76. }
  77.  
  78.