home *** CD-ROM | disk | FTP | other *** search
- /*
- * fixdepend - Fixup the dpendency list generated by makedepend to properly
- * reference library members.
- *
- * This program and source is copyright 1993 by Chris Olson, All Rights
- * Reserved. Permission is granted for personal use, and it may be
- * distributed by any means as long as no more than a fair duplication
- * and media costs are charged. If this program is going to be sold as
- * part of a larger product, please contact the author at the following
- * electronic address:
- *
- * chris@Sterling.COM
- *
- * fixdepend -l library [-f Makefile] [-s "marker-line"] object ...
- *
- * Chris Olson
- * v1.0
- * 06/10/93
- */
- #include <stdio.h>
- #include <string.h>
- #include <sys/param.h>
-
- static char *File = "Makefile";
- static char *Lib = "";
- static char *Start = "# DO NOT DELETE THIS LINE -- make depend depends on it.";
-
- /*
- * Tear apart the command line and fill in all of the variables. Used instead
- * of 'getarg' because I could not guarentee a 'getarg' on all systems this
- * program might be ported to.
- */
- static int parse_args (argc, argv)
- int argc; /* Number of arguments on the command line. */
- char **argv; /* The argument list */
- {
- int i; /* Simple loop counter. */
-
- for (i = 1; i < argc; i++)
- if (argv[i][0] != '-')
- return i;
- else
- switch (argv[i][1]) {
- case 'f':
- if (argv[i][2] == '\0')
- File = argv[++i];
- else
- File = &argv[i][2];
- break;
- case 'l':
- if (argv[i][2] == '\0')
- Lib = argv[++i];
- else
- Lib = &argv[i][2];
- break;
- case 's':
- if (argv[i][2] == '\0')
- Start = argv[++i];
- else
- Start = &argv[i][2];
- break;
- default:
- fprintf(stderr, "Bad option '%s'.\n", argv[i]);
- exit(-1);
- }
- return i;
- } /* parse_args */
-
- /*
- * Rename a file. Does NOT work across volumes, but does not need to for this
- * program.
- */
- static int mv(init, new)
- char *init; /* Initial name of the file. */
- char *new; /* New name for the file. */
- {
- unlink(new); /* Don't care if this fails... */
- if (link(init, new))
- return -1;
- return unlink(init);
- } /* mv */
-
- /*
- * Main part of the code.
- */
- int main (argc, argv)
- int argc; /* Number of command line arguments. */
- char **argv; /* Command line arguments. */
- {
- FILE *fp_in; /* Stream reading from (Makefile.bak). */
- FILE *fp_out; /* Stream writing too (new Makefile) */
- char *colon; /* Position of the colon in the string. */
- char line[BUFSIZ]; /* IO buffer. */
- char save_file[MAXPATHLEN]; /* Orginal filename. */
- int i; /* Loop counter. */
- int start; /* Where in the argument list the files are. */
-
- /*
- * Parse the command line.
- */
- start = parse_args(argc, argv);
- if (!strlen(Lib)) {
- fprintf(stderr, "You must specify a library name!\n");
- exit(-1);
- }
-
- /*
- * Make a backup copy of the makefile, and open up the files.
- */
- sprintf(save_file, "%s.bak", File);
- if (mv(File, save_file) == -1) {
- fprintf(stderr, "Unable to make backup file '%s'\n", save_file);
- exit(-1);
- }
- if ((fp_out = fopen(File, "w")) == NULL) {
- fprintf(stderr, "Unable to open new '%s'.\n", File);
- if (mv(save_file, File))
- fprintf(stderr, "Unable to recover '%s', origional in '%s'.\n",
- File, save_file);
- exit(-1);
- }
- if ((fp_in = fopen(save_file, "r")) == NULL) {
- fprintf(stderr, "Can't open '%s' for reading.\n", File);
- if (mv(save_file, File))
- fprintf(stderr, "Unable to recover '%s', origional in '%s'.\n",
- File, save_file);
- exit(-1);
- }
-
- /*
- * Copy the file unchanged until we find the line of code where
- * makedepend left the do-not-edit-beyond-this-point comment.
- */
- while (fgets(line, BUFSIZ, fp_in)) {
- fputs(line, fp_out);
- if (!strncmp(Start, line, strlen(Start)))
- break;
- }
-
- /*
- * Now adjust the lines for the objects requested.
- */
- while (fgets(line, BUFSIZ, fp_in)) {
- if ((colon = strchr(line, ':')) == NULL) {
- /*
- * Not a dependency intro, just copy it out.
- */
- fputs(line, fp_out);
- continue;
- }
-
- /*
- * Terminate the item, and mark the rest of line.
- */
- *(colon++) = '\0';
- for (i = start; i < argc; i++)
- if (!strcmp(line, argv[i])) {
- /*
- * Adjust the reference to refer to a library.
- */
- *strchr(line, '.') = '\0';
- fputs(Lib, fp_out);
- fprintf(fp_out, "(%s.o):", Lib, line);
- fputs(colon, fp_out);
- break;
- }
- if (i == argc)
- /*
- * Not one of the ones we where looking for, just output it.
- */
- fprintf(fp_out, "%s:%s", line, colon);
- }
-
- /*
- * Close all of the files and exit...
- */
- fclose(fp_in);
- fclose(fp_out);
- exit(0);
- } /* main */
-