home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / forum4.lzh / SPRACHEN / C / LP / lp.c next >
C/C++ Source or Header  |  1988-02-14  |  3KB  |  130 lines

  1. /* lp fuer os-9/68k         Uwe Simon  4.9.1987 */
  2.  
  3. #include <stdio.h>
  4. #include <module.h>
  5. #include <pwd.h>
  6. #include "lp.h"
  7.  
  8. FILE *f;
  9. char s[300],titel[100],filename[100],dev[32],r_mesg[20];
  10. mod_exec *module;
  11. int ev;
  12.  
  13.  
  14. error()
  15.  
  16. {
  17.     fprintf(stderr,"Syntax: lp [<opts>] {<path>}\n");
  18.     fprintf(stderr,"Options:\n");    
  19.     fprintf(stderr,"       -z     get file names from standard input\n");
  20.     fprintf(stderr,"       -w     message on terminal when ready\n");
  21.     fprintf(stderr,"       -m     mail to user when ready\n");
  22.     fprintf(stderr,"       -n=xx  make xx copies (default 1)\n");
  23.     fprintf(stderr,"       -d=ptr output on device ptr\n");
  24.     fprintf(stderr,"       -s     silentmode\n");
  25.     _ev_unlink(ev);
  26.     munlink(module);
  27. }
  28.  
  29.  
  30. copy_file()
  31. {
  32.     while(fgets(s,256,stdin)) fputs(s,f);
  33. }
  34.  
  35.  
  36. main(argc,argv)
  37. int argc;
  38. char **argv;
  39.  
  40. {
  41.     int copies,mesg,h,files=0,idmesg=1;
  42.     struct passwd *pwd;
  43.     
  44.     if(strcmp(argv[1],"-?")==0) {
  45.         error();
  46.         exit(0);
  47.     }
  48.     module=(mod_exec *)modlink("spoolqueue",0x400);
  49.     if((int)module==-1) { exit(_errmsg(1,"Spooler not installed\n")); }
  50.     queue=(struct spoolqueue *)((long)module+module->_mexec);
  51.     if(!queue->active) fprintf(stderr,"WARNING: Spooler not active\n");
  52.     ev=_ev_link("spoolqueue");
  53.     filename[0]='\0'; titel[0]='\0'; dev[0]='\0';
  54.     copies=1; mesg=0; r_mesg[0]='\0';
  55.     
  56.     if((pwd=getpwuid(getuid()&0xffff))!=NULL) strcat(spooltmp,pwd->pw_name);
  57.     
  58.     strcat(spooltmp,".XXXXXX");
  59.     f=fopen(strcpy(filename,mktemp(spooltmp)),"w");
  60.     while (--argc) {
  61.         if(**++argv=='-') {
  62.             switch(toupper(*++*argv)) {
  63.                 case '?':
  64.                     error();
  65.                     exit(0);
  66.                     break;
  67.                 case 'Z':    
  68.                     break;
  69.                 case 'M':
  70.                     mesg=mesg | MAILMODE;
  71.                     break;
  72.                 case 'N':
  73.                     copies=atoi(*argv+(long)2);
  74.                     break;    
  75.                 case 'D':
  76.                     strcpy(dev,*argv+(long)2);
  77.                     break;
  78.                 case 'S':
  79.                     idmesg=0;
  80.                     break;
  81.                 case 'W':
  82.                     strcpy(r_mesg,getenv("PORT"));
  83.                     break;    
  84.                 default:
  85.                     error();
  86.                     exit(1);
  87.                     break;
  88.             }
  89.         }
  90.         else {
  91.             files++;
  92.             if(freopen(*argv,"r",stdin)) {
  93.                 if(titel[0]!='\0') { /* schon ein File */
  94.                     if(strlen(titel)+strlen(*argv)<90) {
  95.                         strcat(titel,", ");
  96.                         strcat(titel,*argv);
  97.                     }
  98.                     else strcat(titel,", ...");
  99.                     putc(0x0c,f);
  100.                 }
  101.                 else strcpy(titel,*argv);
  102.                 copy_file();
  103.             }
  104.             else fprintf(stderr,"can't open %s\n",*argv);
  105.         }
  106.     }
  107.     if((files==0) && (titel[0]=='\0')) {    /* kein File (also stdin !) */
  108.         strcpy(titel,"/stdin/");
  109.         copy_file();
  110.     }
  111.     fflush(f);
  112.     h=_gs_size(fileno(f));
  113.     fclose(f);
  114.     if(h>0) {
  115.         if((h=ins_job(ev,queue,dev,filename,titel,copies,r_mesg,mesg,h))== -1) {
  116.             fprintf(stderr,"Spoolqueue is full or unknown printer\n");
  117.             _ev_unlink(ev);
  118.             munlink(module);
  119.             exit(1);
  120.         }
  121.         if(idmesg) printf("Spoolid : %d\n",h);
  122.     }
  123.     else {
  124.         unlink(filename);
  125.         printf("empty input\n");
  126.     }
  127.     _ev_unlink(ev);
  128.     munlink(module);
  129. }
  130.