remove OSF1 support
[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 #include        "builtins.h"
100 #include        "ulimit.h"
101
102 #ifdef _no_ulimit
103         int     b_ulimit __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
104                 NOT_USED(argc);
105                 NOT_USED(argv);
106                 NOT_USED(extra);
107                 error(ERROR_exit(2),e_nosupport);
108                 return(0);
109         }
110 #else
111
112 #define HARD    1
113 #define SOFT    2
114
115 int     b_ulimit __PARAM__((int argc,char *argv[],__V_ *extra), (argc, argv, extra)) __OTORP__(int argc;char *argv[];__V_ *extra;){
116         register char *limit;
117         register int flag = 0, mode=0, n;
118 #ifdef _lib_getrlimit
119         struct rlimit rlp;
120 #endif /* _lib_getrlimit */
121         const Shtable_t *tp;
122         int label, unit, noargs;
123         long i;
124         NOT_USED(extra);
125         while((n = optget(argv,sh_optulimit))) switch(n)
126         {
127                 case 'H':
128                         mode |= HARD;
129                         continue;
130                 case 'S':
131                         mode |= SOFT;
132                         continue;
133                 case 'f':
134                         flag |= (1<<1);
135                         break;
136                 case 'a':
137 #ifdef _lib_ulimit
138                         flag = (1<<1);
139                         break;
140 #else
141                         flag = (0x2f
142 #   ifdef RLIMIT_RSS
143                         |(1<<4)
144 #   endif /* RLIMIT_RSS */
145 #   ifdef RLIMIT_NOFILE
146                         |(1<<6)
147 #   endif /* RLIMIT_NOFILE */
148 #   ifdef RLIMIT_VMEM
149                         |(1<<7)
150 #   endif /* RLIMIT_VMEM */
151                                 );
152                         break;
153                 case 't':
154                         flag |= 1;
155                         break;
156 #   ifdef RLIMIT_RSS
157                 case 'm':
158                         flag |= (1<<4);
159                         break;
160 #   endif /* RLIMIT_RSS */
161                 case 'd':
162                         flag |= (1<<2);
163                         break;
164                 case 's':
165                         flag |= (1<<3);
166                         break;
167                 case 'c':
168                         flag |= (1<<5);
169                         break;
170 #   ifdef RLIMIT_NOFILE
171                 case 'n':
172                         flag |= (1<<6);
173                         break;
174 #   endif /* RLIMIT_NOFILE */
175 #   ifdef RLIMIT_VMEM
176                 case 'v':
177                         flag |= (1<<7);
178                         break;
179 #   endif /* RLIMIT_VMEM */
180 #endif /* _lib_ulimit */
181                 case ':':
182                         error(2, opt_arg);
183                         break;
184                 case '?':
185                         error(ERROR_usage(2), opt_arg);
186                         break;
187         }
188         limit = argv[opt_index];
189         /* default to -f */
190         if(noargs=(flag==0))
191                 flag |= (1<<1);
192         /* only one option at a time for setting */
193         label = (flag&(flag-1));
194         if(error_info.errors || (limit && label) || argc>opt_index+1)
195                 error(ERROR_usage(2),optusage((char*)0));
196         tp = shtab_limits;
197         if(mode==0)
198                 mode = (HARD|SOFT);
199         for(; flag; tp++,flag>>=1)
200         {
201                 if(!(flag&1))
202                         continue;
203                 n = tp->sh_number>>11;
204                 unit = tp->sh_number&0x7ff;
205                 if(limit)
206                 {
207                         if(sh.subshell)
208                                 sh_subfork();
209                         if(strcmp(limit,e_unlimited)==0)
210                                 i = INFINITY;
211                         else
212                         {
213                                 char *last;
214                                 if((i=sh_strnum(limit,&last)) < 0 || *last)
215                                         error(ERROR_system(1),e_number,limit);
216                                 i *= unit;
217                         }
218 #ifdef _lib_getrlimit
219                         if(getrlimit(n,&rlp) <0)
220                                 error(ERROR_system(1),e_number,limit);
221                         if(mode&HARD)
222                                 rlp.rlim_max = i;
223                         if(mode&SOFT)
224                                 rlp.rlim_cur = i;
225                         if(setrlimit(n,&rlp) <0)
226                                 error(ERROR_system(1),e_overlimit,limit);
227 #else
228                         if((i=vlimit(n,i)) < 0)
229                                 error(ERROR_system(1),e_number,limit);
230 #endif /* _lib_getrlimit */
231                 }
232                 else
233                 {
234 #ifdef  _lib_getrlimit
235                         if(getrlimit(n,&rlp) <0)
236                                 error(ERROR_system(1),e_number,limit);
237                         if(mode&HARD)
238                                 i = rlp.rlim_max;
239                         if(mode&SOFT)
240                                 i = rlp.rlim_cur;
241 #else
242 #   ifdef _lib_ulimit
243                         n--;
244 #   endif /* _lib_ulimit */
245                         i = -1;
246                         if((i=vlimit(n,i)) < 0)
247                                 error(ERROR_system(1),e_number,limit);
248 #endif /* _lib_getrlimit */
249                         if(label)
250                                 sfputr(sfstdout,tp->sh_name,' ');
251                         if(i!=INFINITY || noargs)
252                         {
253                                 if(!noargs)
254                                         i += (unit-1);
255                                 sfprintf(sfstdout,"%d\n",i/unit);
256                         }
257                         else
258                                 sfputr(sfstdout,e_unlimited,'\n');
259                 }
260         }
261         return(0);
262 }
263 #endif /* _no_ulimit */