home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / apps / text_ed / elv16b2 / st / alias.c next >
C/C++ Source or Header  |  1992-05-13  |  3KB  |  129 lines

  1. /* alias.c */
  2.  
  3. /* Author:
  4.  *        Peter Reinig
  5.  *        Universitaet Kaiserslautern
  6.  *        Postfach 3049
  7.  *        7650 Kaiserslautern
  8.  *        W. Germany
  9.  *        reinig@physik.uni-kl.de
  10.  */
  11.  
  12. /* This tiny program executes elvis with the flags that are appropriate
  13.  * for a given command name.  This program is used only on systems that
  14.  * don't allow UNIX-style file links.
  15.  *
  16.  * The benefit of this program is: instead of having 5 copies of elvis
  17.  * on your disk, you only need one copy of elvis and 4 copies of this
  18.  * little program.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include "config.h"
  23.  
  24. #if OSK
  25. #define ELVIS    "/dd/usr/cmds/elvis"
  26. #else
  27. #define ELVIS    "elvis"
  28. #endif
  29.  
  30. #if __STDC__
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #endif
  34.  
  35. #if MINT
  36. extern char *getenv();
  37. #endif
  38.  
  39. extern char **environ;
  40. extern int errno;
  41.  
  42. main(argc, argv)
  43.     int    argc;
  44.     char    *argv[];
  45. {
  46.     int    pid, i, j;
  47.     int    letter;
  48.     char    **argblk;
  49. #if OSK
  50.     extern int chainc();
  51. #endif
  52. #if TOS || MINT
  53.     extern char myname[];
  54. #endif
  55.  
  56.     /* allocate enough space for a copy of the argument list, plus a
  57.      * terminating NULL, plus maybe an added flag.
  58.      */
  59.     argblk = (char **) malloc((argc + 2) * sizeof(char *));
  60.     if (!argblk)
  61.     {
  62. #if OSK
  63.         _errmsg(errno, "Can't get enough memory\n");
  64. #else
  65.         perror(argv[0]);
  66. #endif
  67.         exit(2);
  68.     }
  69.  
  70. #if TOS || MINT
  71.     /* some stupid TOS shells haven't heared about argv[0]  -nox */
  72.     if (!**argv)
  73.         *argv = myname;    /* (see myname.c) */
  74. #endif
  75.  
  76.     /* find the last letter in the invocation name of this program */
  77.     i = strlen(argv[0]);
  78. #if MSDOS || TOS || MINT
  79.     /* we almost certainly must bypass ".EXE" or ".TTP" from argv[0] */
  80.     if (i > 4 && argv[0][i - 4] == '.')
  81.         i -= 4;
  82. #endif
  83.     letter = argv[0][i - 1];
  84.  
  85.     /* copy argv to argblk, possibly inserting a flag such as "-R" */
  86. #if MINT
  87.     /* see if elvis is the $EDITOR and has a path, so that execvp()
  88.        doesn't have to search $PATH (yes, GEMDOS is slow...)  -nox */
  89.     if (strpbrk(ELVIS, "/\\") || !(argblk[0]=getenv("EDITOR")) ||
  90.         !strpbrk(argblk[0], "/\\") ||
  91.         (strcmp(argblk[0]+strlen(argblk[0])-sizeof(ELVIS)+1, ELVIS) &&
  92.          strcmp(argblk[0]+strlen(argblk[0])-sizeof(ELVIS)-3, ELVIS ".ttp")))
  93. #endif
  94.     argblk[0] = ELVIS;
  95.     i = j = 1;
  96.     switch (letter)
  97.     {
  98.       case 'w':            /* "view" */
  99.       case 'W':
  100.         argblk[i++] = "-R";
  101.         break;
  102. #if !OSK
  103.       case 'x':            /* "ex" */
  104.       case 'X':
  105.         argblk[i++] = "-e";
  106.         break;
  107. #endif
  108.       case 't':            /* "input" */
  109.       case 'T':
  110.         argblk[i++] = "-i";
  111.         break;
  112.     }
  113.     while (j < argc)
  114.     {
  115.         argblk[i++] = argv[j++];
  116.     }
  117.     argblk[i] = (char *)0;
  118.  
  119.     /* execute the real ELVIS program */
  120. #if OSK
  121.     pid = os9exec(chainc, argblk[0], argblk, environ, 0, 0, 3);
  122.     fprintf(stderr, "%s: cannot execute\n", argblk[0]);
  123. #else
  124.     (void)execvp(argblk[0], argblk);
  125.     perror(ELVIS);
  126. #endif
  127.     exit(2);
  128. }
  129.