home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / make / ststuff.c < prev    next >
C/C++ Source or Header  |  1988-10-13  |  8KB  |  396 lines

  1. /***************************************************************\
  2. *                                *
  3. *  PDMAKE, Atari ST version                    *
  4. *                                *
  5. *  Adapted from mod.sources Vol 7 Issue 71, 1986-12-03.        *
  6. *                                *
  7. *  This port makes extensive use of the original net.sources    *
  8. *  port by Jwahar Bammi.                    *
  9. *                                *
  10. *      Ton van Overbeek                        *
  11. *      Email: TPC862@ESTEC.BITNET                *
  12. *             TPC862%ESTEC.BITNET@WISCVM.WISC.EDU    (ARPA)    *
  13. *             ...!mcvax!tpc862%estec.bitnet   (UUCP Europe)    *
  14. *             ...!ucbvax!tpc862%estec.bitnet  (UUCP U.S.A.)    *
  15. *             71450,3537  (CompuServe)                *
  16. *                                *
  17. \***************************************************************/
  18.  
  19. /*
  20.  *
  21.  * ststuff.c - Retrofit routines
  22.  *             for the Atari St's
  23.  *
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include "h.h"
  29. #include "astat.h"
  30.  
  31. /*
  32.  * Get file statistics - sort of like stat(2)
  33.  */
  34.  
  35. int    getstat(fname, buf)
  36. register char    *fname;
  37. register struct stat *buf;
  38. {
  39.     register struct stat *save_dta;
  40.     register int    status;
  41.  
  42.     /* Save old DTA */
  43.     save_dta = (struct stat *)Fgetdta();
  44.  
  45.     /* set the new DTA */
  46.     Fsetdta(buf);
  47.  
  48.     /* Find file stat */
  49.     status = Fsfirst(fname, 0);
  50.  
  51.     /* reset dta */
  52.     Fsetdta(save_dta);
  53.  
  54.     /* return status */
  55.     return (status == 0) ? 0 : -1;
  56.  
  57. }
  58.  
  59.  
  60. /*
  61.  * system - execute a command and return status
  62.  */
  63. int    system(cmd)
  64. register char    *cmd;
  65. {
  66.     char    command[128], tail[130];
  67.     register char    *p, *save, *findcmd();
  68.     register int    n;
  69.  
  70.     if (*cmd == '%')
  71.         /* Atari special internal command */
  72.         return st_special(cmd);
  73.  
  74.     /* Break up command into command and command tail */
  75.     for (p = save = command; !isspace(*cmd); *p++ = *cmd++)    /* copy */
  76.         ;
  77.     *p = '\0';
  78.  
  79.     while (isspace(*cmd)) 
  80.         cmd++;                /* skip blanks */
  81.     if ((n = strlen(cmd)) > 128) {
  82.         fprintf(stderr, "Command '%s' too long\n", save);
  83.         return - 1;
  84.     }
  85.  
  86.     tail[0] = (char) n;
  87.     strcpy(&tail[1], cmd);
  88.  
  89.     return (int)Pexec(0, findcmd(command), tail, (char *)NULL);
  90. }
  91.  
  92.  
  93. /*
  94.  * Find a command given a partially qualified command name
  95.  */
  96. static char    *suf[] = {
  97.     "prg",
  98.     "ttp",
  99.     "tos",
  100.     (char *) 0
  101. };
  102.  
  103.  
  104. char    *
  105. findcmd(cmd)
  106. char    *cmd;
  107. {
  108.     static char    file[128];
  109.     char    pathbuf[512];
  110.     char    *path, *p;
  111.     char    *baseptr, *strrchr(), *strchr(), *getenv(), *strtok();
  112.     int    hassuf, i;
  113.     struct stat sbuf;
  114.  
  115.     if ((baseptr = strrchr(cmd, '\\')) == NULL)
  116.         baseptr = cmd;
  117.  
  118.     hassuf = (strchr(baseptr, '.') != NULL);
  119.  
  120.     if (baseptr != cmd && hassuf)        /* absolute path with suffix */
  121.         return cmd;
  122.  
  123.     path = getenv("PATH");
  124.  
  125.     if (baseptr != cmd || path == NULL) {    /* absolute, or no path */
  126.         for (i = 0; suf[i] != NULL ; i++) {    /* abs path, no suf */
  127.             sprintf(file, "%s.%s", cmd, suf[i]);
  128.             if (getstat(file, &sbuf) == 0)
  129.                 return file;
  130.         }
  131.         return cmd;            /* will have to do */
  132.     }
  133.     strcpy(pathbuf, path);
  134.  
  135.     for (p = strtok(pathbuf, ";,"); p != NULL ; p = strtok(NULL, ";,")) {
  136.         if (hassuf) {
  137.             sprintf(file, "%s\\%s", p, cmd);
  138.             if (getstat(file, &sbuf) == 0)
  139.                 return file;
  140.         } else {
  141.             for (i = 0; suf[i] != NULL ; i++) {
  142.                 sprintf(file, "%s\\%s.%s", p, cmd, suf[i]);
  143.                 if (getstat(file, &sbuf) == 0)
  144.                     return file;
  145.             }
  146.         }
  147.     }
  148.     return cmd;                /* will have to do */
  149. }
  150.  
  151.  
  152. /*
  153.  * Atari St special commands
  154.  *
  155.  */
  156. int    st_special(cmd)
  157. register char    *cmd;
  158. {
  159.     extern int    rm(), cp();
  160.  
  161.     switch (cmd[1]) {
  162.     case 'r':
  163.     case 'R':    /* remove */
  164.         return rm(&cmd[2]);
  165.  
  166.     case 'c':
  167.     case 'C':    /* copy */
  168.         return cp(&cmd[2]);
  169.  
  170.     case 'e':
  171.     case 'E':
  172.         return echo(&cmd[2]);
  173.  
  174.     default:
  175.         fprintf(stderr, "Warning: '%s' - Unknown Atari Special Command\n",
  176.             cmd);
  177.     }
  178.     return 0;
  179. }
  180.  
  181.  
  182. /*
  183.  * remove file(s)
  184.  *
  185.  */
  186. int    rm(list)
  187. register char    *list;
  188. {
  189.     char    name[128];
  190.     register char    *p;
  191.  
  192.     while ((!isspace(*list)) && (*list != '\0')) 
  193.         list++;
  194.     while (*list != '\0') {
  195.         while (isspace(*list)) 
  196.             list++;
  197.  
  198.         for (p = name; !isspace(*list) && (*list != '\0'); 
  199.             *p++ = *list++) /* copy */
  200.             ;
  201.         *p = '\0';
  202.  
  203.         if (p != name)
  204.             wremove(name);    /* never mind the return value
  205.                                    * we are doing 'rm -f'
  206.                      */
  207.     }
  208.     return 0;
  209. }
  210.  
  211.  
  212. #define iswild(F) ((strchr(F,'*')!=(char *)NULL)||(strchr(F,'?')!=(char *)NULL))
  213.  
  214. /*
  215.  * this routine actually removes the files, dealing with wildcards
  216.  *
  217.  */
  218. wremove(filename)
  219. register char    *filename;
  220. {
  221.     register struct stat *save_dta;
  222.     struct stat buf;
  223.     char    fbuf[128];
  224.     char    pbuf[128];
  225.     register char    *path, *p;
  226.     extern char    *strcpy(), *strcat(), *strchr(), *strrchr();
  227.  
  228.     if (!iswild(filename)) {
  229.         /* not a wild card */
  230.         unlink(filename);
  231.         return;
  232.     }
  233.  
  234.     /* Wild Card */
  235.     if ((p = strrchr(filename, '\\')) != (char *)NULL) {
  236.         register char    *q;
  237.  
  238.         /* Pick up path */
  239.         p++;
  240.         for (path = pbuf, q = filename; q != p; *path++ = *q++) /* Copy */
  241.             ;
  242.         *path = '\0';
  243.         path = pbuf;
  244.     } else
  245.         /* No path */
  246.         path = (char *)NULL;
  247.  
  248.     /* Save old DTA */
  249.     save_dta = (struct stat *)Fgetdta();
  250.  
  251.     /* set the new DTA */
  252.     Fsetdta(&buf);
  253.  
  254.     /* Unlink the first match for wild card */
  255.     if (Fsfirst(filename, (0x01 | 0x010 | 0x020)) != 0)
  256.         /* No such file(s), simply return */
  257.         return;
  258.  
  259.     unlink ( (path == (char *)NULL) ? buf.st_sp2
  260.          : strcat(strcpy(fbuf, path), buf.st_sp2) );
  261.  
  262.     /* Unlink any other match(s) for wild card */
  263.     while (Fsnext() == 0)
  264.         /* rest of them */
  265.         unlink ( (path == (char *)NULL) ? buf.st_sp2
  266.              : strcat(strcpy(fbuf, path), buf.st_sp2) );
  267.  
  268.     /* reset dta */
  269.     Fsetdta(save_dta);
  270. }
  271.  
  272.  
  273. /*
  274.  * copy files
  275.  *
  276.  */
  277. int    cp(list)
  278. register char    *list;
  279. {
  280.     char    source[128], dest[128];
  281.     char    buf[512];
  282.     register char    *p;
  283.     register int    fsource, fdest;
  284.     register long    count;
  285.  
  286.     while ((!isspace(*list)) && (*list != '\0')) 
  287.         list++;
  288.     while (isspace(*list)) 
  289.         list++;
  290.     if (*list == '\0') {
  291.         /* no source specified */
  292.         fprintf(stderr, "Usage: ${CP} <source file> <destination file>\n");
  293.         return 1;
  294.     }
  295.  
  296.     for (p = source; !isspace(*list) && (*list != '\0'); *p++ = *list++) /* copy */
  297.         ;
  298.     *p = '\0';
  299.  
  300.     while (isspace(*list)) 
  301.         list++;
  302.     if (*list == '\0') {
  303.         /* no destination specified */
  304.         fprintf(stderr, "Usage: ${CP} <source file> <destination file>\n");
  305.         return 2;
  306.     }
  307.  
  308.     for (p = dest; !isspace(*list) && (*list != '\0'); *p++ = *list++) /* copy */
  309.         ;
  310.     *p = '\0';
  311.  
  312.     if (*list != 0) {
  313.         fprintf(stderr, "Only 2 parameters allowed\n");
  314.         fprintf(stderr, "Usage: $(CP) <source file> <destination file>\n");
  315.         return 6;
  316.     }
  317.  
  318.  
  319.     if ((fsource = Fopen(source, 0)) < 0) {
  320.         fprintf(stderr, "%s: no such file\n", source);
  321.         return 3;
  322.     }
  323.  
  324.     if ((fdest = Fcreate(dest, 0)) < 0) {
  325.         /* May already exist */
  326.         if ((fdest = Fopen(dest, 1)) < 0) {
  327.             fprintf(stderr, "%s: cannot open for write\n", dest);
  328.             Fclose(fsource);
  329.             return 4;
  330.         }
  331.     }
  332.  
  333.     while ((count = Fread(fsource, 512L, buf)) > 0) {
  334.         if (Fwrite(fdest, count, buf) != count) {
  335.             fprintf(stderr, "Error writing %s\n", dest);
  336.             Fclose(fsource);
  337.             Fclose(fdest);
  338.             return 5;
  339.         }
  340.     }
  341.  
  342.     Fclose(fsource);
  343.     Fclose(fdest);
  344.  
  345.     return 0;
  346. }
  347.  
  348.  
  349. /*
  350.  * Echo arguments
  351.  *
  352.  */
  353. int    echo(list)
  354. register char    *list;
  355. {
  356.     while ((!isspace(*list)) && (*list != '\0')) 
  357.         list++;
  358.  
  359.     if (*list != '\0') {
  360.         while (isspace(*list)) 
  361.             list++;
  362.         printf("%s\n", list);
  363.     }
  364.  
  365.     return 0;
  366. }
  367.  
  368.  
  369. /*
  370.  * rtime - stuff current time & date into long (ptr passed)
  371.  *
  372.  */
  373. rtime(t)
  374. long    *t;
  375. {
  376.     *t = Gettime();    /* Ikbd's time */
  377. }
  378.  
  379.  
  380. /*
  381.  * Flips Word of a long, used to get the date into the Higher order bits,
  382.  * and time into the lower order bits of a long, so that comparisons can
  383.  * later be done using a simple C relational operator.
  384.  *
  385.  */
  386. void
  387. FlipWords(i)
  388. unsigned int    i[];
  389. {
  390.     register unsigned int    temp;
  391.  
  392.     temp = i[0];
  393.     i[0] = i[1];
  394.     i[1] = temp;
  395. }
  396.