home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / ENTERPRS / CPM / UTILS / S / UZIUTILS.ARK / UCP.C < prev    next >
Text File  |  1988-12-24  |  9KB  |  547 lines

  1.  
  2. /**************************************************
  3. UZI (Unix Z80 Implementation) Utilities:  ucp.c
  4. ***************************************************/
  5.  
  6.  
  7. #include <stdio.h>
  8. #include "unix.h";
  9. #include "extern.h"
  10.  
  11. int16  *syserror = &udata.u_error;
  12. static char cwd[100];
  13. static char line[128];
  14.  
  15. #ifndef CPM
  16. extern char *sys_errlist[];
  17. #endif
  18.  
  19.  
  20. main(argc,argval)
  21. int argc;
  22. char *argval[];
  23. {
  24.     int rdev;
  25.     char cmd[30], arg1[30], arg2[30], arg3[30];
  26.     int count;
  27.     struct filesys fsys;
  28.     int j;
  29.     char *argv[5];
  30.  
  31.     if (argc < 2)
  32.     rdev = 0;
  33.     else
  34.     rdev = atoi(argval[1]);
  35.  
  36.     xfs_init(rdev);
  37.     strcpy(cwd,"/");
  38.  
  39.     for(;;)
  40.     {
  41.     printf("unix: ");
  42.     if (gets(line) == NULL)
  43.     {
  44.         xfs_end();
  45.         exit();
  46.     }
  47.     cmd[0] = '\0';
  48.     *arg1 = '\0';
  49.     arg2[0] = '\0';
  50.     arg3[0] = '\0';
  51.     count = sscanf(line,"%s %s %s %s",cmd, arg1, arg2, arg3);
  52.     if (count == 0 || cmd[0] == '\0')
  53.         continue;
  54.  
  55.     _sync();
  56.  
  57.     if (strcmp(cmd,"\n") == 0)
  58.         continue;
  59.     else if (strcmp(cmd,"exit") == 0)
  60.     {
  61.         xfs_end();
  62.         exit();
  63.     }
  64.     else if (strcmp(cmd,"ls") == 0)
  65.     {
  66.         if (*arg1 )
  67.         ls(arg1);
  68.         else
  69.         ls(".");
  70.     }
  71.     else if (strcmp(cmd,"cd") == 0)
  72.     {
  73.         if (*arg1 )
  74.         {
  75.         strcpy(cwd,arg1);
  76.         if (_chdir(arg1) != 0)
  77.         {
  78.             printf("cd: error number %d\n",*syserror);
  79.         }
  80.         }
  81.     }
  82.     else if (strcmp(cmd,"mkdir") == 0)
  83.     {
  84.         if (*arg1 )
  85.         mkdir(arg1);
  86.     }
  87.     else if (strcmp(cmd,"mknod") == 0)
  88.     {
  89.         if (*arg1  && *arg2 && *arg3 )
  90.         mknod(arg1,arg2, arg3);
  91.     }
  92.     else if (strcmp(cmd,"chmod") == 0)
  93.     {
  94.         if (*arg1 && *arg2 )
  95.         chmod(arg1,arg2);
  96.     }
  97.     else if (strcmp(cmd,"get") == 0)
  98.     {
  99.             if (*arg1 )
  100.         get(arg1,0);
  101.     }
  102.     else if (strcmp(cmd,"bget") == 0)
  103.     {
  104.             if (*arg1 )
  105.         get(arg1,1);
  106.     }
  107.     else if (strcmp(cmd,"put") == 0)
  108.     {
  109.             if (*arg1 )
  110.         put(arg1,0);
  111.     }
  112.     else if (strcmp(cmd,"bput") == 0)
  113.     {
  114.             if (*arg1 )
  115.         put(arg1,1);
  116.     }
  117.     else if (strcmp(cmd,"type") == 0)
  118.     {
  119.             if (*arg1 )
  120.         type(arg1);
  121.     }
  122.     else if (strcmp(cmd,"dump") == 0)
  123.     {
  124.             if (*arg1 )
  125.         fdump(arg1);
  126.     }
  127.     else if (strcmp(cmd,"rm") == 0)
  128.     {
  129.             if (*arg1 )
  130.         unlink(arg1);
  131.     }
  132.     else if (strcmp(cmd,"df") == 0)
  133.     {
  134.         for (j=0; j < 4; ++j)
  135.         {
  136.         _getfsys(j,&fsys);
  137.         if (fsys.s_mounted)
  138.         {
  139.             printf("%d:   %u blks used %u blks free %u inodes used %u inodes free\n",
  140.             j,(fsys.s_fsize - fsys.s_isize)-fsys.s_tfree, fsys.s_tfree,
  141.             (8*(fsys.s_isize-2)-fsys.s_tinode), fsys.s_tinode);
  142.         }
  143.         }
  144.     }
  145.     else if (strcmp(cmd,"rmdir") == 0)
  146.     {
  147.             if (*arg1 )
  148.         rmdir(arg1);
  149.     }
  150.     else if (strcmp(cmd,"mount") == 0)
  151.     {
  152.         if (*arg1 && *arg2)
  153.         if (_mount(arg1,arg2,0) != 0)
  154.         {
  155.             printf("Mount error.%d\n", *syserror);
  156.         }
  157.     }
  158.     else if (strcmp(cmd,"umount") == 0)
  159.     {
  160.         if (*arg1)
  161.         if (_umount(arg1) != 0)
  162.         {
  163.             printf("Umount error.%d\n", *syserror);
  164.         }
  165.     }
  166.     else
  167.     {
  168.         printf("Unknown command\n");
  169.     }
  170.     }
  171. }
  172.  
  173.  
  174.  
  175. ls(path)
  176. char *path;
  177. {
  178.     struct direct buf;
  179.     struct stat statbuf;
  180.     char dname[128];
  181.     int d;
  182.     
  183.     d = _open(path,0);
  184.     if (d < 0)
  185.     {
  186.     printf("ls: can't open %s\n",path);
  187.     return;
  188.     }
  189.  
  190.     while (_read(d,(char *)&buf,16) == 16)
  191.     {
  192.     if (buf.d_name[0] == '\0')
  193.         continue;
  194.  
  195.     if (path[0] != '.' || path[1] )
  196.     {
  197.         strcpy(dname,path);
  198.         strcat(dname,"/");
  199.     }
  200.     else
  201.         dname[0] = '\0';
  202.     strcat(dname,buf.d_name);
  203.  
  204.     if (_stat(dname,&statbuf) != 0)
  205.     {
  206.         printf("ls: can't stat %s\n",dname);
  207.         continue;
  208.     }
  209.  
  210.     if ((statbuf.st_mode & F_MASK) == F_DIR)
  211.         strcat(dname,"/");
  212.  
  213.     printf("%-6d %-15s",
  214.         (statbuf.st_mode & F_CDEV) ?
  215.             statbuf.st_rdev :
  216.             512*statbuf.st_size.o_blkno + statbuf.st_size.o_offset,
  217.         dname);
  218.  
  219.     printf("  0%-6o %-2d %-5d\n", statbuf.st_mode, statbuf.st_nlink,
  220.         statbuf.st_ino);
  221.     }
  222.     _close(d);
  223. }
  224.  
  225.  
  226. chmod(path,modes)
  227. char *path;
  228. char *modes;
  229. {
  230.     int mode;
  231.     int dev;
  232.  
  233.     mode = -1;
  234.     sscanf(modes,"%o",&mode);
  235.     if (mode == -1 )
  236.     {
  237.     printf("chmod: bad mode\n");
  238.     return(-1);
  239.     }
  240.  
  241.     if (_chmod(path,mode))
  242.     {
  243.     printf("_chmod: error %d\n",*syserror);
  244.     return(-1);
  245.     }
  246.  
  247. }
  248.  
  249.  
  250. mknod(path,modes, devs)
  251. char *path;
  252. char *modes;
  253. char *devs;
  254. {
  255.     int mode;
  256.     int dev;
  257.  
  258.     mode = -1;
  259.     sscanf(modes,"%o",&mode);
  260.     if (mode == -1 )
  261.     {
  262.     printf("mknod: bad mode\n");
  263.     return(-1);
  264.     }
  265.     
  266.     if ((mode & F_MASK) != F_BDEV && (mode & F_MASK) != F_CDEV)
  267.     {
  268.     printf("mknod: mode is not device\n");
  269.     return(-1);
  270.     }
  271.  
  272.     dev = -1;
  273.     sscanf(devs,"%d",&dev);
  274.     if (dev == -1 )
  275.     {
  276.     printf("mknod: bad device\n");
  277.     return(-1);
  278.     }
  279.  
  280.     if (_mknod(path,mode,dev) != 0)
  281.     {
  282.     printf("_mknod: error %d\n",*syserror);
  283.     return(-1);
  284.     }
  285.  
  286.     return(0);
  287. }
  288.  
  289.  
  290.  
  291. mkdir(path)
  292. char *path;
  293. {
  294.  
  295.     char dot[100];
  296.  
  297.     if ( _mknod(path, 040000 | 0777, 0) != 0)
  298.     {
  299.     printf("mkdir: mknod error %d\n",*syserror);
  300.     return(-1);
  301.     }
  302.  
  303.     strcpy(dot,path);
  304.     strcat(dot,"/.");
  305.     if ( _link(path,dot) != 0)
  306.     {
  307.     printf("mkdir: link dot error %d\n",*syserror);
  308.     return(-1);
  309.     }
  310.  
  311.     strcpy(dot,path);
  312.     strcat(dot,"/..");
  313.     if ( _link(".",dot) != 0)
  314.     {
  315.     printf("mkdir: link dotdot error %d\n",*syserror);
  316.     return(-1);
  317.     }
  318.  
  319.     return(0);
  320. }
  321.  
  322.  
  323.  
  324.  
  325. get(arg,binflag)
  326. char *arg;
  327. int binflag;
  328. {
  329.     FILE *fp;
  330.     int d;
  331.     char cbuf[512];
  332.     int nread;
  333.  
  334.     fp = fopen(arg,binflag ? "rb" : "r");
  335.     if (fp == NULL)
  336.     {
  337.     printf("Source file not found\n");
  338.     return(-1);
  339.     }
  340.  
  341.     d = _creat(arg,0666);
  342.     if (d < 0)
  343.     {
  344.     printf("Cant open unix file error %d\n",*syserror);
  345.        return(-1);
  346.     }
  347.     
  348.     for (;;)
  349.     {
  350.         nread = fread(cbuf,1,512,fp);
  351.         if (nread == 0)
  352.         break;
  353.         if (_write(d,cbuf,nread) != nread)
  354.         {
  355.         printf("_write error %d\n",*syserror);
  356.         fclose(fp);
  357.         _close(d);
  358.         return(-1);
  359.         }
  360.     }
  361.     fclose(fp);
  362.     _close(d);
  363.     return(0);
  364. }
  365.  
  366.  
  367. put(arg,binflag)
  368. char *arg;
  369. int binflag;
  370. {
  371.     FILE *fp;
  372.     int d;
  373.     char cbuf[512];
  374.     int nread;
  375.  
  376.     fp = fopen(arg,binflag ? "wb" : "w");
  377.     if (fp == NULL)
  378.     {
  379.     printf("Cant open destination file.\n");
  380.     return(-1);
  381.     }
  382.  
  383.     d = _open(arg,0);
  384.     if (d < 0)
  385.     {
  386.     printf("Cant open unix file error %d\n",*syserror);
  387.        return(-1);
  388.     }
  389.     
  390.     for (;;)
  391.     {
  392.         if ((nread =_read(d,cbuf,512)) == 0)
  393.         break;
  394.         if (fwrite(cbuf,1,nread,fp) != nread)
  395.         {
  396.         printf("fwrite error");
  397.         fclose(fp);
  398.         _close(d);
  399.         return(-1);
  400.         }
  401.     }
  402.     fclose(fp);
  403.     _close(d);
  404.     return(0);
  405. }
  406.  
  407.  
  408. type(arg)
  409. char *arg;
  410. {
  411.     int d;
  412.     char cbuf[512];
  413.     int nread;
  414.  
  415.     d = _open(arg,0);
  416.     if (d < 0)
  417.     {
  418.     printf("Cant open unix file error %d\n",*syserror);
  419.        return(-1);
  420.     }
  421.  
  422.     for (;;)
  423.     {
  424.     if( (nread = _read(d,cbuf,512)) == 0)
  425.         break;
  426.  
  427.     fwrite(cbuf, 1, nread, stdout);
  428.     }
  429.     _close(d);
  430.     return(0);
  431. }
  432.  
  433.  
  434. fdump(arg)
  435. char *arg;
  436. {
  437.     int d;
  438.     char cbuf[512];
  439.     int nread;
  440.  
  441.     printf("Dump starting.\n");
  442.     d = _open(arg,0);
  443.     if (d < 0)
  444.     {
  445.     printf("Cant open unix file error %d\n",*syserror);
  446.        return(-1);
  447.     }
  448.  
  449.     for (;;)
  450.     {
  451.     if( (nread = _read(d,cbuf,512)) == 0)
  452.         break;
  453.     }
  454.     _close(d);
  455.     printf("Dump done.\n");
  456.     return(0);
  457. }
  458.  
  459.  
  460.  
  461.  
  462. unlink(path)
  463. char *path;
  464. {
  465.     struct stat statbuf;
  466.  
  467.     if (_stat(path,&statbuf) != 0)
  468.     {
  469.     printf("unlink: can't stat %s\n",path);
  470.     return(-1);
  471.     }
  472.  
  473.     if ((statbuf.st_mode & F_MASK) == F_DIR)
  474.     {
  475.     printf("unlink: %s directory\n",path);
  476.     return(-1);
  477.     }
  478.  
  479.     if (_unlink(path) != 0)
  480.     {
  481.     printf("unlink: _unlink errn=or %d\n",*syserror);
  482.     return(-1);
  483.     }
  484.  
  485.     return(0);
  486. }
  487.  
  488.  
  489. rmdir(path)
  490. char *path;
  491. {
  492.     struct stat statbuf;
  493.     char newpath[100];
  494.     struct direct dir;
  495.     int fd;
  496.  
  497.     if (_stat(path,&statbuf) != 0)
  498.     {
  499.     printf("rmdir: can't stat %s\n",path);
  500.     return(-1);
  501.     }
  502.  
  503.     if (statbuf.st_mode & F_DIR == 0)
  504.     {
  505.     printf("rmdir: %s not directory\n",path);
  506.     return(-1);
  507.     }
  508.  
  509.     if((fd = _open(path,0)) < 0) {
  510.         printf("rmdir: %s unreadable\n", path);
  511.         return(-1);
  512.     }
  513.     while(_read(fd, (char *)&dir, sizeof (dir)) == sizeof (dir)) {
  514.         if(dir.d_ino == 0) continue;
  515.         if(!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))
  516.             continue;
  517.         printf("rmdir: %s not empty\n", path);
  518.         _close(fd);
  519.         return(-1);
  520.     }
  521.     _close(fd);
  522.  
  523.     strcpy(newpath,path);
  524.     strcat(newpath,"/.");
  525.     if (_unlink(newpath) != 0)
  526.     {
  527.     printf("rmdir: can't unlink \".\"  error %d\n",*syserror);
  528.     return(-1);
  529.     }
  530.  
  531.     strcat(newpath,".");
  532.     if (_unlink(newpath) != 0)
  533.     {
  534.     printf("rmdir: can't unlink \"..\"  error %d\n",*syserror);
  535.     return(-1);
  536.     }
  537.  
  538.     if (_unlink(path) != 0)
  539.     {
  540.     printf("rmdir: _unlink error %d\n",*syserror);
  541.     return(-1);
  542.     }
  543.  
  544.     return(0);
  545. }
  546.  
  547.