cleanup-licensing-and-transarc-references-20030309
[openafs.git] / src / package / globfuncs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*------------------------------------------------------------------------
11  * globfuncs.c
12  *
13  * Description:
14  *      Generically useful functions for package, the AFS workstation
15  *      configuration tool.
16  *
17  *------------------------------------------------------------------------*/
18
19 #include <stdio.h>
20 #include "globals.h"
21
22 /*------------------------------------------------------------------------
23  * emalloc
24  *
25  * Description:
26  *      Malloc with error checking.
27  *
28  * Arguments:
29  *      unsigned size : Number of bytes to allocate.
30  *
31  * Returns:
32  *      Ptr to new space if successful, 
33  *      Exit from package on failure.
34  *
35  * Environment:
36  *      Nothing interesting.
37  *
38  * Side Effects:
39  *      As described; may exit from package.
40  *------------------------------------------------------------------------*/
41
42 char *emalloc(size)
43     unsigned size;
44
45 { /*emalloc*/
46
47     char *malloc();
48     char *ptr;
49
50     if ((ptr = malloc(size)) == NULL) {
51       fprintf(stderr,
52               "Error: Out of memory; malloc() failed allocating %d bytes\n",
53               size);
54       exit(ERR_OUTOFMEMORY);
55     }
56     else
57       return(ptr);
58
59 } /*emalloc*/
60
61 /*------------------------------------------------------------------------
62  * ecalloc
63  *
64  * Description:
65  *      Calloc() with error checking.
66  *
67  * Arguments:
68  *      unsigned nelem : Number of elements to allocate.
69  *      unsigned size  : Number of bytes for each.
70  *
71  * Returns:
72  *      Ptr to new space on success,
73  *      Exit from package on failure.
74  *
75  * Environment:
76  *      Nothing interesting.
77  *
78  * Side Effects:
79  *      As described; may exit from package.
80  *------------------------------------------------------------------------*/
81
82 char *ecalloc(nelem, size)
83     unsigned nelem;
84     unsigned size;
85
86 { /*ecalloc*/
87
88     char *calloc();
89     char *ptr;
90
91     if ((ptr = calloc(nelem, size)) == NULL) {
92       fprintf(stderr, "Error: Out of memory; calloc(%d, %d) failed\n",
93               nelem, size);
94       exit(ERR_OUTOFMEMORY);
95     }
96     else
97       return(ptr);
98
99 } /*ecalloc*/
100
101 /*------------------------------------------------------------------------
102  * efopen
103  *
104  * Description:
105  *      Fopen with error checking.
106  *
107  * Arguments:
108  *      char *filename  : Name of file to open.
109  *      char *type      : Open mode.
110  *
111  * Returns:
112  *      Ptr to file descriptor on success,
113  *      Exit from package on failure.
114  *
115  * Environment:
116  *      Nothign interesting.
117  *
118  * Side Effects:
119  *      As described; may exit from package.
120  *------------------------------------------------------------------------*/
121
122 FILE *efopen(filename, type)
123     char *filename;
124     char *type;
125
126 { /*efopen*/
127
128     FILE *f;
129
130     if ((f = fopen(filename, type)) == NULL) {
131       fprintf(stderr, "Error: Couldn't open file; fopen(%s, %s) failed\n",
132               filename, type);
133       exit(ERR_FOPENFAILED);
134     }
135     else
136       return(f);
137
138 } /*efopen*/