home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / tools / ftran.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  4.4 KB  |  213 lines

  1. #ifdef linux
  2. #include <linux/types.h>
  3. #include <linux/fcntl.h>
  4. #include <linux/string.h>
  5. #include <linux/unistd.h>
  6. static inline _syscall3(int,write,int,fd,const void *,buf,off_t,count)
  7. static inline _syscall3(int,read,int,fd,void *,buf,off_t,count)
  8. static inline _syscall2(int,fchmod,int,fd,mode_t,mode)
  9. static inline _syscall1(int,close,int,fd)
  10. extern void _exit (int);
  11. #define exit _exit
  12. extern int open (const char *, int, ...);
  13. extern int printf (const char *, ...);
  14. #else
  15. #include <stdio.h>
  16. #include <fcntl.h>
  17. #endif
  18.  
  19. #include <sys/stat.h>
  20.  
  21. #ifdef linux
  22. inline _syscall2(int,fstat,int,fd,struct stat *,buf)
  23. #endif
  24.  
  25. #define MAX_NAMELEN  255
  26.  
  27. struct ftranbuf {
  28.     off_t  len;
  29.     size_t namelen;
  30.     char   name[MAX_NAMELEN+1];
  31.     mode_t mode;
  32. };
  33.  
  34. char *argv0;
  35.  
  36. static void Usage (void) {
  37.     printf ("Usage: %s -w file name outdev\n", argv0);
  38.     printf ("   or: %s -r dev\n", argv0);
  39.     printf ("\te.g. %s -w ls /bin/ls flat:df0\n", argv0);
  40.     printf ("\tor   %s -r /dev/fd0\n", argv0);
  41.     exit (1);
  42. }
  43.  
  44. void dowrite (char *infile, char *name, char *outdev);
  45. void doread (char *indev);
  46.  
  47. int main(int argc, char **argv)
  48. {
  49.     argv0 = argv[0];
  50.     if (argc != 3 && argc != 5)
  51.         Usage ();
  52.  
  53.     if (strcmp (argv[1], "-w") == 0 && argc == 5) {
  54.         dowrite (argv[2], argv[3], argv[4]);
  55.     } else if (strcmp (argv[1], "-r") == 0 && argc == 3) {
  56.         doread (argv[2]);
  57.     } else
  58.         Usage ();
  59.  
  60.     exit (0);
  61. }
  62.  
  63. void dowrite (char *infile, char *name, char *outdev)
  64. {
  65.     int infd, outfd;
  66.     struct ftranbuf fbuf;
  67.     static char buf[1024];
  68.     static struct stat sbuf;
  69.     off_t size;
  70.  
  71.     fbuf.namelen = strlen (name);
  72.     if (!fbuf.namelen || fbuf.namelen > MAX_NAMELEN) {
  73.         printf ("%s: name length must be > 0 and < %d\n", argv0,
  74.             MAX_NAMELEN);
  75.         exit (1);
  76.     }
  77.     strcpy (fbuf.name, name);
  78.  
  79.     if ((infd = open(infile, O_RDONLY)) == -1) {
  80.         printf ("%s: Unable to open input file '%s'\n", argv0,
  81.             infile);
  82.         exit (1);
  83.     }
  84.  
  85.     if (fstat(infd, &sbuf) == -1) {
  86.         printf ("%s: Unable to stat input file\n", argv0);
  87.         close (infd);
  88.     }
  89.     fbuf.len = size = sbuf.st_size;
  90.     fbuf.mode = sbuf.st_mode;
  91.  
  92.     if ((outfd = open(outdev, O_WRONLY)) == -1) {
  93.         printf ("%s: Unable to open output device '%s'\n", argv0,
  94.             outdev);
  95.         close (infd);
  96.         exit (1);
  97.     }
  98.  
  99.     if (write (outfd, &fbuf, sizeof (fbuf)) != sizeof(fbuf)) {
  100.         printf ("%s: Unable to write file information\n", argv0);
  101.         close (infd);
  102.         close (outfd);
  103.         exit (1);
  104.     }
  105.  
  106.     while (size > 0) {
  107.         int len = size > 1024 ? 1024 : size;
  108.  
  109.         if (read (infd, buf, len) != len) {
  110.             printf ("%s: Unable to read file\n", argv0);
  111.             close (infd);
  112.             close (outfd);
  113.             exit (1);
  114.         }
  115.         if (write (outfd, buf, len) != len) {
  116.             printf ("%s: Unable to write file\n", argv0);
  117.             close (infd);
  118.             close (outfd);
  119.             exit (1);
  120.         }
  121.         size -= len;
  122.     }
  123.  
  124.     close (infd);
  125.     close (outfd);
  126.  
  127.     printf ("%s: %d bytes of file '%s' written to device '%s' "
  128.         "as name '%s'\n", argv0, sbuf.st_size, infile, outdev,
  129.          name);
  130. }
  131.  
  132. void doread (char *indev)
  133. {
  134.     int infd, outfd;
  135.     struct ftranbuf fbuf;
  136.     static char buf[1024];
  137.     off_t size;
  138.  
  139.     if ((infd = open(indev, O_RDONLY)) == -1) {
  140.         printf ("%s: Unable to open input device '%s'\n", argv0,
  141.             indev);
  142.         exit (1);
  143.     }
  144.  
  145.     if (read (infd, &fbuf, sizeof (fbuf)) != sizeof (fbuf)) {
  146.         printf ("%s: Unable to read file information\n", argv0);
  147.         close (infd);
  148.         exit (1);
  149.     }
  150.  
  151.     /* sanity check */
  152.     if (!fbuf.namelen || fbuf.namelen > MAX_NAMELEN || !fbuf.len) {
  153.         printf ("%s: bad information in file header\n", argv0);
  154.         close (infd);
  155.         exit (1);
  156.     }
  157.  
  158.     if ((outfd = open (fbuf.name, O_CREAT | O_WRONLY, fbuf.mode)) == -1) {
  159.         printf ("%s: Unable to open output file '%s'\n", argv0,
  160.             fbuf.name);
  161.         close (infd);
  162.         exit (1);
  163.     }
  164.     fchmod (outfd, fbuf.mode);
  165.  
  166.     size = fbuf.len;
  167.     while (size > 0) {
  168.         int len = size > 1024 ? 1024 : size;
  169.  
  170.         if (read (infd, buf, len) != len) {
  171.             printf ("%s: Unable to read file\n", argv0);
  172.             close (infd);
  173.             close (outfd);
  174.             exit (1);
  175.         }
  176.         if (write (outfd, buf, len) != len) {
  177.             printf ("%s: Unable to write file\n", argv0);
  178.             close (infd);
  179.             close (outfd);
  180.             exit (1);
  181.         }
  182.         size -= len;
  183.     }
  184.  
  185.     close (infd);
  186.     close (outfd);
  187.  
  188.     printf ("%s: %ld bytes of file '%s' read from device '%s'\n",
  189.         argv0, fbuf.len, fbuf.name, indev);
  190. }
  191.  
  192. #ifdef linux
  193. void _main (void) {}
  194.  
  195. #include <stdarg.h>
  196.  
  197. int printf (const char *fmt, ...)
  198. {
  199.     extern int vsprintf(char * buf, const char * fmt, va_list args);
  200.     static char buf[256];
  201.     va_list args;
  202.     int i;
  203.  
  204.     va_start (args, fmt);
  205.     i = vsprintf (buf, fmt, args);
  206.     va_end (args);
  207.  
  208.     write (1, buf, i);
  209.  
  210.     return i;
  211. }
  212. #endif
  213.