a5c7002730aa3d51ddc478893d260c6b9c8eadda
[oweals/cde.git] / cde / programs / dtksh / ksh93 / src / cmd / ksh93 / bltins / ulimit.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: ulimit.c /main/3 1995/11/01 16:30:47 rswiston $ */
24 /***************************************************************
25 *                                                              *
26 *                      AT&T - PROPRIETARY                      *
27 *                                                              *
28 *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF        *
29 *                    AT&T BELL LABORATORIES                    *
30 *         AND IS NOT TO BE DISCLOSED OR USED EXCEPT IN         *
31 *            ACCORDANCE WITH APPLICABLE AGREEMENTS             *
32 *                                                              *
33 *                Copyright (c) 1995 AT&T Corp.                 *
34 *              Unpublished & Not for Publication               *
35 *                     All Rights Reserved                      *
36 *                                                              *
37 *       The copyright notice above does not evidence any       *
38 *      actual or intended publication of such source code      *
39 *                                                              *
40 *               This software was created by the               *
41 *           Advanced Software Technology Department            *
42 *                    AT&T Bell Laboratories                    *
43 *                                                              *
44 *               For further information contact                *
45 *                    {research,attmail}!dgk                    *
46 *                                                              *
47 ***************************************************************/
48
49 /* : : generated by proto : : */
50
51 #if !defined(__PROTO__)
52 #if defined(__STDC__) || defined(__cplusplus) || defined(_proto) || defined(c_plusplus)
53 #if defined(__cplusplus)
54 #define __MANGLE__      "C"
55 #else
56 #define __MANGLE__
57 #endif
58 #define __STDARG__
59 #define __PROTO__(x)    x
60 #define __OTORP__(x)
61 #define __PARAM__(n,o)  n
62 #if !defined(__STDC__) && !defined(__cplusplus)
63 #if !defined(c_plusplus)
64 #define const
65 #endif
66 #define signed
67 #define void            int
68 #define volatile
69 #define __V_            char
70 #else
71 #define __V_            void
72 #endif
73 #else
74 #define __PROTO__(x)    ()
75 #define __OTORP__(x)    x
76 #define __PARAM__(n,o)  o
77 #define __MANGLE__
78 #define __V_            char
79 #define const
80 #define signed
81 #define void            int
82 #define volatile
83 #endif
84 #if defined(__cplusplus) || defined(c_plusplus)
85 #define __VARARG__      ...
86 #else
87 #define __VARARG__
88 #endif
89 #if defined(__STDARG__)
90 #define __VA_START__(p,a)       va_start(p,a)
91 #else
92 #define __VA_START__(p,a)       va_start(p)
93 #endif
94 #endif
95 #include        <ast.h>
96 #include        <sfio.h>
97 #include        <error.h>
98 #include        <shell.h>
99 #ifdef __osf__
100 #include        <sys/resource.h>
101 #endif
102 #include        "builtins.h"
103 #include        "ulimit.h"
104
105 #ifdef _no_ulimit
106         int     b_ulimit __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
107                 NOT_USED(argc);
108                 NOT_USED(argv);
109                 NOT_USED(extra);
110                 error(ERROR_exit(2),e_nosupport);
111                 return(0);
112         }
113 #else
114
115 #define HARD    1
116 #define SOFT    2
117
118 int     b_ulimit __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
119         register char *limit;
120         register int flag = 0, mode=0, n;
121 #ifdef _lib_getrlimit
122         struct rlimit rlp;
123 #endif /* _lib_getrlimit */
124         const Shtable_t *tp;
125         int label, unit, noargs;
126         long i;
127         NOT_USED(extra);
128         while((n = optget(argv,sh_optulimit))) switch(n)
129         {
130                 case 'H':
131                         mode |= HARD;
132                         continue;
133                 case 'S':
134                         mode |= SOFT;
135                         continue;
136                 case 'f':
137                         flag |= (1<<1);
138                         break;
139                 case 'a':
140 #ifdef _lib_ulimit
141                         flag = (1<<1);
142                         break;
143 #else
144                         flag = (0x2f
145 #   ifdef RLIMIT_RSS
146                         |(1<<4)
147 #   endif /* RLIMIT_RSS */
148 #   ifdef RLIMIT_NOFILE
149                         |(1<<6)
150 #   endif /* RLIMIT_NOFILE */
151 #   ifdef RLIMIT_VMEM
152                         |(1<<7)
153 #   endif /* RLIMIT_VMEM */
154                                 );
155                         break;
156                 case 't':
157                         flag |= 1;
158                         break;
159 #   ifdef RLIMIT_RSS
160                 case 'm':
161                         flag |= (1<<4);
162                         break;
163 #   endif /* RLIMIT_RSS */
164                 case 'd':
165                         flag |= (1<<2);
166                         break;
167                 case 's':
168                         flag |= (1<<3);
169                         break;
170                 case 'c':
171                         flag |= (1<<5);
172                         break;
173 #   ifdef RLIMIT_NOFILE
174                 case 'n':
175                         flag |= (1<<6);
176                         break;
177 #   endif /* RLIMIT_NOFILE */
178 #   ifdef RLIMIT_VMEM
179                 case 'v':
180                         flag |= (1<<7);
181                         break;
182 #   endif /* RLIMIT_VMEM */
183 #endif /* _lib_ulimit */
184                 case ':':
185                         error(2, opt_arg);
186                         break;
187                 case '?':
188                         error(ERROR_usage(2), opt_arg);
189                         break;
190         }
191         limit = argv[opt_index];
192         /* default to -f */
193         if(noargs=(flag==0))
194                 flag |= (1<<1);
195         /* only one option at a time for setting */
196         label = (flag&(flag-1));
197         if(error_info.errors || (limit && label) || argc>opt_index+1)
198                 error(ERROR_usage(2),optusage((char*)0));
199         tp = shtab_limits;
200         if(mode==0)
201                 mode = (HARD|SOFT);
202         for(; flag; tp++,flag>>=1)
203         {
204                 if(!(flag&1))
205                         continue;
206                 n = tp->sh_number>>11;
207                 unit = tp->sh_number&0x7ff;
208                 if(limit)
209                 {
210                         if(sh.subshell)
211                                 sh_subfork();
212                         if(strcmp(limit,e_unlimited)==0)
213                                 i = INFINITY;
214                         else
215                         {
216                                 char *last;
217                                 if((i=sh_strnum(limit,&last)) < 0 || *last)
218                                         error(ERROR_system(1),e_number,limit);
219                                 i *= unit;
220                         }
221 #ifdef _lib_getrlimit
222                         if(getrlimit(n,&rlp) <0)
223                                 error(ERROR_system(1),e_number,limit);
224                         if(mode&HARD)
225                                 rlp.rlim_max = i;
226                         if(mode&SOFT)
227                                 rlp.rlim_cur = i;
228                         if(setrlimit(n,&rlp) <0)
229                                 error(ERROR_system(1),e_overlimit,limit);
230 #else
231                         if((i=vlimit(n,i)) < 0)
232                                 error(ERROR_system(1),e_number,limit);
233 #endif /* _lib_getrlimit */
234                 }
235                 else
236                 {
237 #ifdef  _lib_getrlimit
238                         if(getrlimit(n,&rlp) <0)
239                                 error(ERROR_system(1),e_number,limit);
240                         if(mode&HARD)
241                                 i = rlp.rlim_max;
242                         if(mode&SOFT)
243                                 i = rlp.rlim_cur;
244 #else
245 #   ifdef _lib_ulimit
246                         n--;
247 #   endif /* _lib_ulimit */
248                         i = -1;
249                         if((i=vlimit(n,i)) < 0)
250                                 error(ERROR_system(1),e_number,limit);
251 #endif /* _lib_getrlimit */
252                         if(label)
253                                 sfputr(sfstdout,tp->sh_name,' ');
254                         if(i!=INFINITY || noargs)
255                         {
256                                 if(!noargs)
257                                         i += (unit-1);
258                                 sfprintf(sfstdout,"%d\n",i/unit);
259                         }
260                         else
261                                 sfputr(sfstdout,e_unlimited,'\n');
262                 }
263         }
264         return(0);
265 }
266 #endif /* _no_ulimit */