home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / cmdline / setlink / setlink.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-04  |  5.3 KB  |  116 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*                                setlink                                 */
  4. /*                                                                        */
  5. /*  Author:                                                               */
  6. /*   Ted H. Emigh     23 June 1989                                        */
  7. /*                                                                        */
  8. /*  Usage:                                                                */
  9. /*   setlink -n[filename]|-a[filename] [options and files for linker]     */
  10. /*                                                                        */
  11. /*  Description:                                                          */
  12. /*   setlink creates (with option -n) or appends to (with option -a) a    */
  13. /*   file with name filename.  If the filename is not given (blank after  */
  14. /*   -n or -a, then the name used is FILENAME.  The remainder of the      */
  15. /*   line is copied to this file.  The Turbo Linker can use this file:    */
  16. /*   tlink @FILENAME    (e.g., tlink @makefile.lnk).                      */
  17. /*                                                                        */
  18. /*  Return Value:                                                         */
  19. /*   exit = 0 if no errors                                                */
  20. /*          1 if the usage is incorrect.                                  */
  21. /*          2 if there is a disk file error.                              */
  22. /*                                                                        */
  23. /*  Bugs:                                                                 */
  24. /*   Due to a limitation of argv, program is unable to output adjacent    */
  25. /*   <SPACE> characters to the file.  Multiple spaces on the command      */
  26. /*   line are translated as a single space.                               */
  27. /*                                                                        */
  28. /*------------------------------------------------------------------------*/
  29.  
  30. #include <stdio.h>
  31.  
  32. /*
  33.     FILENAME is the default name of the file to be used.
  34.     This name must match with the corresponding name used by the linker.
  35. */
  36. #define FILENAME "makefile.lnk"
  37.  
  38. void error(int err_type);      /* display error message and exit     */
  39.  
  40. char *fn=FILENAME;
  41.  
  42. void main(int argc, char *argv[])
  43. {
  44.   register int i,c;
  45.   FILE *fp;
  46.   char *argp;
  47.  
  48.   /*  Get the file creation option  */
  49.   argp=argv[1];
  50.   if( *argp=='-' || *argp=='/' ) {
  51.     c=*++argp;
  52.     if(*++argp) fn=argp;     /*  file name is given  */
  53.     switch(c) {
  54.       case 'n':       /* create file or erase existing one */
  55.       case 'N':
  56.         if((fp=fopen(fn,"wt"))==NULL) error(2); /* error opening file */
  57.         break;
  58.       case 'a':       /* append to an existing file */
  59.       case 'A':
  60.         if((fp=fopen(fn,"at"))==NULL) error(2); /* error opening file */
  61.         break;
  62.       default:
  63.         error(1);     /* unknown option */
  64.     }  /* switch(*argp) */
  65.   } /* if( *argp==  ) */
  66.   else  error(1); /* no setlink file option */
  67.  
  68.   for(i=2;i<argc;++i) {
  69.     if(i!=2) if(fputc(' ',fp)==EOF) error(2);
  70.     if(fputs(argv[i],fp)==EOF) error(2);
  71.   }
  72.   if(fputc('\n',fp)==EOF) error(2);
  73.   fclose(fp);
  74. }
  75.  
  76.  
  77. /*------------------------------------------------------------------------*/
  78. /*                                                                        */
  79. /*                                 error                                  */
  80. /*                                                                        */
  81. /*  Usage:                                                                */
  82. /*   void error(int err_type);                                            */
  83. /*                                                                        */
  84. /*  Description:                                                          */
  85. /*   Display error message and exit.                                      */
  86. /*   err_type = 1  command syntax.  Display usage message.                */
  87. /*              2  file error.  Unable to create or write to file.        */
  88. /*                                                                        */
  89. /*  Return Value:                                                         */
  90. /*   Routine exits to DOS with value err_type.                            */
  91. /*                                                                        */
  92. /*------------------------------------------------------------------------*/
  93.  
  94. void error(int err_type)
  95. {
  96.   switch(err_type) {
  97.     case 1: /* misuse of command -- display correct usage */
  98.       /* display first line of error text */
  99.       puts("usage: setlink -n[filename]|-a[filename] [options for linker]");
  100.  
  101.       /* display second line of error text with file name */
  102.       /* fputs is used rather than puts, since puts appends a newline  */
  103.       /* fputs/puts used rather than printf to save space  */
  104.       fputs("-n to create a new file ",stdout);
  105.       puts(fn);
  106.  
  107.       /* display third line of error text */
  108.       puts("-a to append to existing file");
  109.       exit(1);
  110.     case 2:
  111.       puts("fatal file error");
  112.       exit(2);
  113.   } /* switch(err_type) */
  114.  
  115. } /* error */
  116.