lctypewriter.c 947B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. static void syntax(){
  4. fprintf(stderr, "Syntax: lctypewriter <device-name>\n");
  5. fprintf(stderr, "For exampe, if your printer is connected using USB/LPT adapter,"
  6. " use lctypewriter /dev/usb/lp0\n");
  7. exit(1);
  8. }
  9. int main(int argc, const char* argv[]){
  10. if(argc != 2) syntax();
  11. FILE* f = fopen(argv[1], "wb");
  12. if(f == NULL){
  13. perror("Can not open device");
  14. exit(1);
  15. }
  16. fprintf(f, "\033@");
  17. fprintf(f, "\033i1");
  18. fflush(f);
  19. int c;
  20. while( (c = getchar()) != EOF){
  21. if(c == '\n') c='\r';
  22. if(fputc(c, f) == EOF){
  23. perror("Can not write output");
  24. }else if(c=='\r'){
  25. if(fputc('\n', f) == EOF){
  26. perror("Can not write output");
  27. }
  28. }
  29. fflush(f);
  30. }
  31. fprintf(f, "\033i0");
  32. if(ferror(stdin)){
  33. perror("Can not read input");
  34. fclose(f);
  35. exit(1);
  36. }
  37. fclose(f);
  38. }