xref: /titanic_44/usr/src/lib/krb5/ss/utils.c (revision 3db86aab554edbb4244c8d1a1c90f152eee768af)
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)+7; /* "quux, " */
67     size += strlen(func_name)+7; /* "foo, " */
68     size += strlen(info_string)+9; /* "\"Info!\", " */
69     sprintf(numbuf, "%d", options);
70     size += strlen(numbuf);
71     size += 4;		/* " }," + NL */
72     string = malloc(size * sizeof(char *));
73     strcpy(string, "    { ");
74     strcat(string, var_name);
75     strcat(string, ",\n      ");
76     strcat(string, func_name);
77     strcat(string, ",\n      ");
78     strcat(string, info_string);
79     strcat(string, ",\n      ");
80     strcat(string, numbuf);
81     strcat(string, " },\n");
82     return(string);
83 }
84 
85 char *
86 gensym(name)
87 	char *name;
88 {
89 	char *symbol;
90 
91 	symbol = malloc((strlen(name)+6) * sizeof(char));
92 	gensym_n++;
93 	sprintf(symbol, "%s%05ld", name, gensym_n);
94 	return(symbol);
95 }
96 
97 /* concatenate three strings and return the result */
98 char *str_concat3(a, b, c)
99 	register char *a, *b, *c;
100 {
101 	char *result;
102 	int size_a = strlen(a);
103 	int size_b = strlen(b);
104 	int size_c = strlen(c);
105 
106 	result = malloc((size_a + size_b + size_c + 2)*sizeof(char));
107 	strcpy(result, a);
108 	strcpy(&result[size_a], c);
109 	strcpy(&result[size_a+size_c], b);
110 	return(result);
111 }
112 
113 /* return copy of string enclosed in double-quotes */
114 char *quote(string)
115 	register char *string;
116 {
117 	register char *result;
118 	int len;
119 	len = strlen(string)+1;
120 	result = malloc(len+2);
121 	result[0] = '"';
122 	strncpy(&result[1], string, len-1);
123 	result[len] = '"';
124 	result[len+1] = '\0';
125 	return(result);
126 }
127 
128 #ifndef HAS_STRDUP
129 /* make duplicate of string and return pointer */
130 char *strdup(s)
131 	register char *s;
132 {
133 	register int len = strlen(s) + 1;
134 	register char *new;
135 	new = malloc(len);
136 	strncpy(new, s, len);
137 	return(new);
138 }
139 #endif
140