xref: /titanic_44/usr/src/lib/krb5/ss/utils.c (revision d2ec54f7875f7e05edd56195adbeb593c947763f)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * Copyright 1987, 1988 by MIT Student Information Processing Board
5  *
6  * For copyright information, see copyright.h.
7  */
8 
9 #include <string.h>
10 #include "copyright.h"
11 #include "ss_internal.h"	/* includes stdio and string */
12 
13 extern FILE *output_file;
14 
15 char *gensym(), *str_concat3(), *quote();
16 extern long gensym_n;
17 
18 void write_ct(hdr, rql)
19     char const *hdr, *rql;
20 {
21     char *sym;
22     sym = gensym("ssu");
23     fputs("static ss_request_entry ", output_file);
24     fputs(sym, output_file);
25     fputs("[] = {\n", output_file);
26     fputs(rql, output_file);
27     fputs("    { 0, 0, 0, 0 }\n", output_file);
28     fputs("};\n\nss_request_table ", output_file);
29     fputs(hdr, output_file);
30     fprintf(output_file, " = { %d, ", SS_RQT_TBL_V2);
31     fputs(sym, output_file);
32     fputs(" };\n", output_file);
33 }
34 
35 char * generate_cmds_string(cmds)
36     char const *cmds;
37 {
38     char * var_name = gensym("ssu");
39     fputs("static char const * const ", output_file);
40     fputs(var_name, output_file);
41     fputs("[] = {\n", output_file);
42     fputs(cmds, output_file);
43     fputs(",\n    (char const *)0\n};\n", output_file);
44     return(var_name);
45 }
46 
47 void generate_function_definition(func)
48     char const *func;
49 {
50     fputs("extern void ", output_file);
51     fputs(func, output_file);
52     fputs(" __SS_PROTO;\n", output_file);
53 }
54 
55 char * generate_rqte(func_name, info_string, cmds, options)
56     char const *func_name;
57     char const *info_string;
58     char const *cmds;
59     int options;
60 {
61     int size;
62     char *string, *var_name, numbuf[16];
63     var_name = generate_cmds_string(cmds);
64     generate_function_definition(func_name);
65     size = 6;		/* "    { " */
66     size += strlen(var_name)+8; /* "quux, " */
67     size += strlen(func_name)+8; /* "foo, " */
68     size += strlen(info_string)+8; /* "\"Info!\", " */
69     sprintf(numbuf, "%d", options);
70     size += strlen(numbuf)+5;		/* " }," + NL + NUL */
71     string = malloc(size);
72     strcpy(string, "    { ");
73     strcat(string, var_name);
74     strcat(string, ",\n      ");
75     strcat(string, func_name);
76     strcat(string, ",\n      ");
77     strcat(string, info_string);
78     strcat(string, ",\n      ");
79     strcat(string, numbuf);
80     strcat(string, " },\n");
81     return(string);
82 }
83 
84 char *
85 gensym(name)
86 	char *name;
87 {
88 	char *symbol;
89 
90 	symbol = malloc((strlen(name)+6) * sizeof(char));
91 	gensym_n++;
92 	sprintf(symbol, "%s%05ld", name, gensym_n);
93 	return(symbol);
94 }
95 
96 /* concatenate three strings and return the result */
97 char *str_concat3(a, b, c)
98 	register char *a, *b, *c;
99 {
100 	char *result;
101 	int size_a = strlen(a);
102 	int size_b = strlen(b);
103 	int size_c = strlen(c);
104 
105 	result = malloc((size_a + size_b + size_c + 2)*sizeof(char));
106 	strcpy(result, a);
107 	strcpy(&result[size_a], c);
108 	strcpy(&result[size_a+size_c], b);
109 	return(result);
110 }
111 
112 /* return copy of string enclosed in double-quotes */
113 char *quote(string)
114 	register char *string;
115 {
116 	register char *result;
117 	int len;
118 	len = strlen(string)+1;
119 	result = malloc(len+2);
120 	result[0] = '"';
121 	strncpy(&result[1], string, len-1);
122 	result[len] = '"';
123 	result[len+1] = '\0';
124 	return(result);
125 }
126 
127 #ifndef HAVE_STRDUP
128 /* make duplicate of string and return pointer */
129 char *strdup(s)
130 	register char *s;
131 {
132 	register int len = strlen(s) + 1;
133 	register char *new;
134 	new = malloc(len);
135 	strncpy(new, s, len);
136 	return(new);
137 }
138 #endif
139