xref: /freebsd/crypto/krb5/src/util/ss/invocation.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2007 Massachusetts Institute of Technology.
4  * All Rights Reserved.
5  *
6  * Export of this software from the United States of America may
7  *   require a specific license from the United States Government.
8  *   It is the responsibility of any person or organization contemplating
9  *   export to obtain such a license before exporting.
10  *
11  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
12  * distribute this software and its documentation for any purpose and
13  * without fee is hereby granted, provided that the above copyright
14  * notice appear in all copies and that both that copyright notice and
15  * this permission notice appear in supporting documentation, and that
16  * the name of M.I.T. not be used in advertising or publicity pertaining
17  * to distribution of the software without specific, written prior
18  * permission.  Furthermore if you modify this software you must label
19  * your software as modified software and not distribute it in such a
20  * fashion that it might be confused with the original M.I.T. software.
21  * M.I.T. makes no representations about the suitability of
22  * this software for any purpose.  It is provided "as is" without express
23  * or implied warranty.
24  */
25 /*
26  * Copyright 1987, 1988 by MIT Student Information Processing Board
27  *
28  * For copyright information, see copyright.h.
29  */
30 #include "ss_internal.h"
31 #include "copyright.h"
32 #define size    sizeof(ss_data *)
33 
34 /* XXX The memory in _ss_table never gets freed up until program exit!
35    If you change the code to free it and stick a null pointer into
36    _ss_table[sci_idx], make sure you change the allocation routine to
37    not assume there are no null pointers in the middle of the
38    array.  */
39 int
ss_create_invocation(char * subsystem_name,char * version_string,char * info_ptr,ss_request_table * request_table_ptr,int * code_ptr)40 ss_create_invocation(char *subsystem_name, char *version_string,
41                      char *info_ptr, ss_request_table *request_table_ptr,
42                      int *code_ptr)
43 {
44     int sci_idx;
45     ss_data *new_table;
46     ss_data **table, **tmp;
47 
48     *code_ptr = 0;
49     table = _ss_table;
50     new_table = (ss_data *) malloc(sizeof(ss_data));
51     if (new_table == NULL) {
52         *code_ptr = errno;
53         return -1;
54     }
55 
56     if (table == (ss_data **) NULL) {
57         table = (ss_data **) malloc(2 * size);
58         if (table == NULL) {
59             *code_ptr = errno;
60             return -1;
61         }
62         table[0] = table[1] = (ss_data *)NULL;
63         _ss_table = table;
64     }
65     initialize_ss_error_table ();
66 
67     for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
68         ;
69     tmp = (ss_data **) realloc((char *)table,
70                                ((unsigned)sci_idx+2)*size);
71     if (tmp == NULL) {
72         *code_ptr = errno;
73         return 0;
74     }
75     _ss_table = table = tmp;
76     table[sci_idx+1] = (ss_data *) NULL;
77     table[sci_idx] = NULL;
78 
79     new_table->subsystem_name = subsystem_name;
80     new_table->subsystem_version = version_string;
81     new_table->argv = (char **)NULL;
82     new_table->current_request = (char *)NULL;
83     new_table->info_dirs = (char **)malloc(sizeof(char *));
84     if (new_table->info_dirs == NULL) {
85         *code_ptr = errno;
86         free(new_table);
87         return 0;
88     }
89     *new_table->info_dirs = (char *)NULL;
90     new_table->info_ptr = info_ptr;
91     if (asprintf(&new_table->prompt, "%s:  ", subsystem_name) < 0) {
92         *code_ptr = errno;
93         free(new_table->info_dirs);
94         free(new_table);
95         return 0;
96     }
97     new_table->abbrev_info = NULL;
98     new_table->flags.escape_disabled = 0;
99     new_table->flags.abbrevs_disabled = 0;
100     new_table->rqt_tables =
101         (ss_request_table **) calloc(2, sizeof(ss_request_table *));
102     if (new_table->rqt_tables == NULL) {
103         *code_ptr = errno;
104         free(new_table->prompt);
105         free(new_table->info_dirs);
106         free(new_table);
107         return 0;
108     }
109     *(new_table->rqt_tables) = request_table_ptr;
110     *(new_table->rqt_tables+1) = (ss_request_table *) NULL;
111     table[sci_idx] = new_table;
112     return(sci_idx);
113 }
114 
115 void
ss_delete_invocation(int sci_idx)116 ss_delete_invocation(int sci_idx)
117 {
118     ss_data *t;
119     int ignored_code;
120 
121     t = ss_info(sci_idx);
122     free(t->prompt);
123     free(t->rqt_tables);
124     while(t->info_dirs[0] != (char *)NULL)
125         ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
126     free(t->info_dirs);
127     free(t);
128 }
129