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