8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sgs / libldmake / common / ld_file.c
blob3f8772049ed2b0ae0bdde511a772657d2be8193e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <libelf.h>
33 #include <sys/param.h>
34 #include <link.h>
36 #pragma init(ld_support_init)
38 #define SUNPRO_DEPENDENCIES "SUNPRO_DEPENDENCIES"
41 * Linked list of strings - used to keep lists of names
42 * of directories or files.
44 struct Stritem {
45 char *str;
46 void *next;
49 typedef struct Stritem Stritem;
51 static char *depend_file = NULL;
52 static Stritem *list = NULL;
54 void
55 ld_support_init()
57 depend_file = getenv(SUNPRO_DEPENDENCIES);
60 static void
61 prepend_str(Stritem **list, const char *str)
63 Stritem *new;
64 char *newstr;
65 const char *lib = "libldmake.so";
67 if (!(new = calloc(1, sizeof (Stritem)))) {
68 perror(lib);
69 return;
72 if (!(newstr = malloc(strlen(str) + 1))) {
73 perror(lib);
74 return;
77 new->str = strcpy(newstr, str);
78 new->next = *list;
79 *list = new;
83 /* ARGSUSED */
84 void
85 ld_file(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
88 * SUNPRO_DEPENDENCIES wasn't set, we don't collect .make.state
89 * information.
91 if (!depend_file)
92 return;
94 if ((flags & LD_SUP_DERIVED) && !(flags & LD_SUP_EXTRACTED))
95 prepend_str(&list, file);
98 void
99 ld_file64(const char *file, const Elf_Kind ekind, int flags, Elf *elf)
101 ld_file(file, ekind, flags, elf);
104 void
105 ld_atexit(int exit_code)
107 Stritem *cur;
108 char lockfile[MAXPATHLEN], *err, *space, *target;
109 FILE *ofp;
110 extern char *file_lock(char *, char *, int);
112 if (!depend_file || exit_code)
113 return;
115 if ((space = strchr(depend_file, ' ')) == NULL)
116 return;
117 *space = '\0';
118 target = &space[1];
120 (void) snprintf(lockfile, MAXPATHLEN, "%s.lock", depend_file);
121 if ((err = file_lock(depend_file, lockfile, 0))) {
122 (void) fprintf(stderr, "%s\n", err);
123 return;
126 if (!(ofp = fopen(depend_file, "a")))
127 return;
129 if (list)
130 (void) fprintf(ofp, "%s: ", target);
132 for (cur = list; cur; cur = cur->next)
133 (void) fprintf(ofp, " %s", cur->str);
135 (void) fputc('\n', ofp);
137 (void) fclose(ofp);
138 (void) unlink(lockfile);
139 *space = ' ';
142 void
143 ld_atexit64(int exit_code)
145 ld_atexit(exit_code);