Imported Upstream version 1.10.2
[platform/upstream/krb5.git] / src / util / ss / invocation.c
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(subsystem_name, version_string, info_ptr,
40                          request_table_ptr, code_ptr)
41     char *subsystem_name, *version_string;
42     char *info_ptr;
43     ss_request_table *request_table_ptr;
44     int *code_ptr;
45 {
46     register int sci_idx;
47     register ss_data *new_table;
48     register ss_data **table, **tmp;
49
50     *code_ptr = 0;
51     table = _ss_table;
52     new_table = (ss_data *) malloc(sizeof(ss_data));
53     if (new_table == NULL) {
54         *code_ptr = errno;
55         return -1;
56     }
57
58     if (table == (ss_data **) NULL) {
59         table = (ss_data **) malloc(2 * size);
60         if (table == NULL) {
61             *code_ptr = errno;
62             return -1;
63         }
64         table[0] = table[1] = (ss_data *)NULL;
65         _ss_table = table;
66     }
67     initialize_ss_error_table ();
68
69     for (sci_idx = 1; table[sci_idx] != (ss_data *)NULL; sci_idx++)
70         ;
71     tmp = (ss_data **) realloc((char *)table,
72                                ((unsigned)sci_idx+2)*size);
73     if (tmp == NULL) {
74         *code_ptr = errno;
75         return 0;
76     }
77     _ss_table = table = tmp;
78     table[sci_idx+1] = (ss_data *) NULL;
79     table[sci_idx] = NULL;
80
81     new_table->subsystem_name = subsystem_name;
82     new_table->subsystem_version = version_string;
83     new_table->argv = (char **)NULL;
84     new_table->current_request = (char *)NULL;
85     new_table->info_dirs = (char **)malloc(sizeof(char *));
86     if (new_table->info_dirs == NULL) {
87         *code_ptr = errno;
88         free(new_table);
89         return 0;
90     }
91     *new_table->info_dirs = (char *)NULL;
92     new_table->info_ptr = info_ptr;
93     if (asprintf(&new_table->prompt, "%s:  ", subsystem_name) < 0) {
94         *code_ptr = errno;
95         free(new_table->info_dirs);
96         free(new_table);
97         return 0;
98     }
99     new_table->abbrev_info = NULL;
100     new_table->flags.escape_disabled = 0;
101     new_table->flags.abbrevs_disabled = 0;
102     new_table->rqt_tables =
103         (ss_request_table **) calloc(2, sizeof(ss_request_table *));
104     if (new_table->rqt_tables == NULL) {
105         *code_ptr = errno;
106         free(new_table->prompt);
107         free(new_table->info_dirs);
108         free(new_table);
109         return 0;
110     }
111     *(new_table->rqt_tables) = request_table_ptr;
112     *(new_table->rqt_tables+1) = (ss_request_table *) NULL;
113     table[sci_idx] = new_table;
114     return(sci_idx);
115 }
116
117 void
118 ss_delete_invocation(sci_idx)
119     int sci_idx;
120 {
121     register ss_data *t;
122     int ignored_code;
123
124     t = ss_info(sci_idx);
125     free(t->prompt);
126     free(t->rqt_tables);
127     while(t->info_dirs[0] != (char *)NULL)
128         ss_delete_info_dir(sci_idx, t->info_dirs[0], &ignored_code);
129     free(t->info_dirs);
130     free(t);
131 }