Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

  1. #include <unistd.h>
  2. #include "textcode.h"
  3. #include "str.h"
  4. #include "buffer.h"
  5. #include "open.h"
  6. int main(int argc,char* argv[]) {
  7. char buf[4096];
  8. char obuf[4096];
  9. buffer filein;
  10. buffer fileout;
  11. int fd=1;
  12. int ofd=-1;
  13. unsigned long mode=0,lineno=0;
  14. if (argc>1) {
  15. fd=open_read(argv[1]);
  16. if (fd<0) {
  17. buffer_puts(buffer_2,"error: could not open \"");
  18. buffer_puts(buffer_2,argv[1]);
  19. buffer_putsflush(buffer_2,"\"\n");
  20. return 1;
  21. }
  22. }
  23. buffer_init(&filein,read,fd,buf,sizeof buf);
  24. /* skip to "^begin " */
  25. for (;;) {
  26. char line[1000]; /* uuencoded lines can never be longer than 64 characters */
  27. int l;
  28. if ((l=buffer_getline(&filein,line,(sizeof line)-1))==0) {
  29. buffer_putsflush(buffer_2,"warning: hit end of file without finding any uuencoded data!\n");
  30. return 0;
  31. }
  32. ++lineno;
  33. if (l>0 && line[l-1]=='\r') --l; /* kill DOS line endings */
  34. line[l]=0;
  35. if (!str_diffn(line,"begin ",6)) {
  36. if (line[l=6+scan_8long(line+6,&mode)]==' ' && mode) {
  37. int i;
  38. ++l;
  39. if (line[l+(i=str_rchr(line+l,'/'))]) l+=i+1;
  40. while (line[l]=='.') ++l;
  41. if (line[l]) {
  42. ofd=open_excl(line+l);
  43. if (ofd<0) {
  44. buffer_puts(buffer_2,"error: could not create file \"");
  45. buffer_puts(buffer_2,line+l);
  46. buffer_putsflush(buffer_2,"\" (must not exist yet)\n");
  47. } else break;
  48. }
  49. }
  50. }
  51. }
  52. buffer_init(&fileout,write,ofd,buf,sizeof buf);
  53. /* read uuencoded lines */
  54. for (;;) {
  55. char line[1000]; /* uuencoded lines can never be longer than 64 characters */
  56. unsigned int scanned,x;
  57. char tmp[100];
  58. int l;
  59. if ((l=buffer_getline(&filein,line,(sizeof line)-1))==0) {
  60. buffer_putsflush(buffer_2,"warning: hit end of file without finding \"end\"!\n");
  61. return 0;
  62. }
  63. ++lineno;
  64. if (l>0 && line[l-1]=='\r') --l; /* kill DOS line endings */
  65. line[l]=0;
  66. x=scan_uuencoded(line,tmp,&scanned);
  67. if (!x) {
  68. if (str_equal(line,"end")) {
  69. buffer_flush(&fileout);
  70. fchmod(ofd,mode);
  71. close(ofd);
  72. return 0;
  73. } else {
  74. parseerror:
  75. buffer_puts(buffer_1,"parse error in line ");
  76. buffer_putulong(buffer_1,lineno);
  77. buffer_puts(buffer_1,": \"");
  78. buffer_puts(buffer_1,line);
  79. buffer_putsflush(buffer_1,"\"\n");
  80. exit(1);
  81. }
  82. }
  83. buffer_put(&fileout,tmp,scanned);
  84. }
  85. }