home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol11n15.zip / PUTCLIP.C < prev    next >
C/C++ Source or Header  |  1992-05-25  |  2KB  |  64 lines

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