home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / fido / pup_v2b.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1987-11-13  |  6KB  |  226 lines

  1. #include <puppy.h>
  2. #include <pupmem.h>
  3. #include <ascii.h>
  4.  
  5. /* 
  6.     Puppy's file system. 
  7. */
  8.  
  9.  
  10. /* Display downloadable files by reading lines from FILES.BBS. Pull off the 
  11. filename (supposed to be in col 0) and see if it matches. If so, display it, 
  12. then the rest of the line. Stop if we find a line beginning with @. */
  13.  
  14. listfile() {
  15.  
  16. struct _fileinfo fileinfo;
  17. char line[SS];            /* possibly long line */
  18. char name[SS];            /* current file name */
  19. char buff[SS];            /* assembled filename */
  20. char *cp;
  21. int file;
  22. int files;            /* how many we found */
  23.  
  24.     makefname(name,"files.pup");
  25.     file= open(name,0);
  26.     if (file == -1) {
  27.         mprintf("\r\nSORRY: No Files\r\n");
  28.         return(0);
  29.     }
  30.  
  31.     files= 0;                    /* none so far */
  32.     while (rline(file,line,sizeof(line)) && !abort) {
  33.         cp= line;                /* ptr to line text */
  34.         if (*cp == 26) break;            /* stop if ^Z */
  35.         if (*cp == '@') break;            /* logical end of file */
  36.  
  37.         if ((*cp == '-') || (*cp == ' ')) {    /* display comments */
  38.             mputs(cp);            /* as is */
  39.             mputs("\r\n");
  40.             continue;
  41.         }
  42.         cpyarg(name,cp);            /* get filename, */
  43.         cp= next_arg(cp);            /* cp is ptr to comments */
  44.         name[12]= NUL;                /* for idiot proofing */
  45.         mprintf("%-12s ",name);            /* display the name, */
  46.         makefname(buff,name);            /* make full pathname, */
  47.         if (getinfo(buff,0,&fileinfo))         /* get file information */
  48.             mprintf("%8lu ",fileinfo.size);
  49.         else mputs(" MISSING ");        /* cant find it */
  50.         mputs(cp);                /* display the comments, */
  51.         mputs("\r\n");
  52.         ++files;
  53.     }
  54.     close(file);
  55.     return(files);
  56. }
  57.  
  58. /* Download files. */
  59.  
  60. download() {
  61.  
  62. char name[SS];        /* FILENAME.EXE(nul) (13 bytes) name being processed */
  63. char fname[SS];        /* full filename with prefix */
  64. char c;
  65.  
  66.     if (ask("Want the list of files")) {
  67.         if (!listfile()) return;    /* possibly display files, */
  68.     }
  69.     cpyarg(name,getarg("File: "));        /* get the filename, */
  70.     if (! *name) return;            /* nothing entered */
  71.     name[13]= NUL;                /* max length */
  72.     stoupper(name);                /* upper case */
  73.  
  74.     if (!okname(name)) return;        /* illegal names, */
  75.     makefname(fname,name);            /* make full filename */
  76.     if (!dl_check(fname)) return;        /* check limits, etc */
  77.  
  78.     while (1) {                /* how to download, */
  79.         c= tolower(*getarg("How to download (T=Text, X=Xmodem, CR=Quit): "));
  80.         switch (c) {
  81.             case NUL: return;    /* CR only */
  82.  
  83.             case 'x':        /* XMODEM */
  84.                 mprintf("Ready to send \"%s\" using XMODEM\r\n",name);
  85.                 mprintf("Start now, Control-C to abort\r\n");
  86.                 filemode= XMODEM; 
  87.                 crcmode= 1;
  88.                 fileerr(transmit(fname,text,textsize));/* do it! */
  89.                 return;
  90.  
  91.             case 't':        /* Text */
  92.                 mputs("Hit a key to start, or Control-C to abort\r\n");
  93.                 if (mconin() == ETX) return;
  94.                 dspfile(fname);
  95.                 return;
  96.         }
  97.     }
  98. }
  99.  
  100. /* Upload a file, ask for a description, add it to the file list. */
  101.  
  102. upload() {
  103. char name[SS];
  104. char buff[SS];
  105. int f;
  106.  
  107.     cpyarg(name,getarg("File: "));        /* get the filename, */
  108.     if (! *name) return;
  109.     name[13]= NUL;
  110.     stoupper(name);                /* make upper case */
  111.  
  112.     if (!okname(name)) return;        /* device names, etc */
  113.     makefname(buff,name);            /* make full filename */
  114.     f= open(buff,0);            /* make sure it does NOT */
  115.     if (f != -1) {                /* exist yet */
  116.         close(f);
  117.         mprintf("SORRY: \"%s\" already exists!\r\n",name);
  118.         return;
  119.     }
  120.     mprintf("Ready to receive \"%s\" using XMODEM\r\n",name);
  121.     mprintf("Start now, Control-C to abort\r\n");
  122.  
  123.     filemode= XMODEM; 
  124.     crcmode= 1;
  125.     f= receive(buff,text,textsize);        /* do the upload */
  126.     if (fileerr(f)) return;            /* stop if error */
  127.  
  128.     mconflush();
  129.     cmdflush();                /* flush typeahead */
  130.     mputs("Wait ...\r\n");            /* may take a while */
  131.     makefname(buff,"FILES.PUP");        /* file list file name */
  132.     f= append(buff);            /* open/create, seek to end */
  133.     if (f == -1) {
  134.         mputs("OOPS: Cant open/create the file list\r\n");
  135.         return;
  136.     }
  137.     write(f,name,strlen(name));        /* write the file name, */
  138.     write(f," ",1);                /* a seperator */
  139.     input("Describe: ",buff,40);        /* get the description, */
  140.     write(f,buff,strlen(buff));        /* write that, */
  141.     write(f,"\r\n",2);            /* new line */
  142.     close(f);
  143. }
  144.  
  145. /* Perform checks on the passed filename for downloading. Check time limits,
  146. K limits, file existence, etc. Return 0 if this file cannot/should not
  147. be downloaded. */
  148.  
  149. dl_check(name)
  150. char *name;
  151. {
  152. struct _fileinfo fileinfo;
  153. int n;
  154. int bpm;                        /* blocks per minute */
  155.  
  156.     if (! getinfo(name,0,&fileinfo)) {        /* get info on it */
  157.         mputs("SORRY: There is no such file!\r\n");
  158.         return(0);
  159.     }
  160.  
  161. /* OK, I guess I ought to explain this. Download time is:
  162.  
  163.     bytes= file size, bytes
  164.     bps= data rate, bytes per second, ie baud / 10
  165.  
  166.     download time, seconds =  bytes / baud / 10
  167.  
  168. This is stupid, I want it in blocks not bytes, and minutes not seconds,
  169. so why do all this high school arithmetic?, we're programmers and we 
  170. don't know any better anyways:
  171.  
  172.     blocks= (bytes + 127) / 128
  173.     blocks per minute [bpm]= ((baud / 10 * 60) + 127) / 128
  174.     bpm= ((baud * 6) + 127) / 128
  175. so ...
  176.     minutes= ((bytes + 127) / 128) / (((baud / 10 * 60) + 127) / 128)
  177. or ...
  178.     minutes= blocks / bpm
  179.  
  180. lets replace the division and multiplication by constants to shifts
  181. and addition because it's so awful on Z80s or whatever without hardware
  182. mult/div. */
  183.  
  184.     n= fileinfo.size;            /* MSDOS, n= file size */
  185.     n += 127L;                /* round up, */
  186.     n >>= 7;                /* n= (bytes + 127) / 128 */
  187.     mprintf("%d blocks\r\n",n);        /* display number of blocks, */
  188.  
  189.     bpm= (datarate << 2) + (datarate << 1);    /* b= baud * 6 */
  190.  
  191.     n= n / bpm;                  /* time to download, */
  192.     ++n;                    /* toss in an extra */
  193.  
  194.     if (limit > n) return(1);        /* return OK if less than limit */
  195.     mprintf("Pup says: \"You don't have enough time left\"\r\n");
  196.     return(0);
  197. }
  198.  
  199. /* Return true if this filename is OK to use. */
  200.  
  201. okname(name)
  202. char *name;
  203. {
  204.     if (badname(name) || (strcmp(name,"FILES.PUP") == 0)) {
  205.         mputs("SORRY: That is a filename reserved for Pup\r\n");
  206.         return(0);
  207.     }
  208.     return(1);
  209. }
  210.  
  211. /* Check the download/upload error code and return true if there is an
  212. error. */
  213.  
  214. fileerr(f)
  215. int f;
  216. {
  217.     mputs("\r\n");
  218.     switch (f) {
  219.         case 0: mputs("File sent OK\r\n"); return(0);
  220.         case -2: mputs("You canceled the transfer\r\n"); break;
  221.         case -3: mputs("SHIT! The disk is full!\r\n"); break;
  222.         default: mputs("Something went wrong\r\n"); break;
  223.     }
  224.     return(-1);
  225. }
  226.