source: trunk/essentials/app-shells/bash/examples/loadables/cut.c@ 3280

Last change on this file since 3280 was 3231, checked in by bird, 19 years ago

eol style.

  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Adam S. Moskowitz of Menlo Consulting and Marciano Pitargue.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static const char copyright[] =
39"@(#) Copyright (c) 1989, 1993\n\
40 The Regents of the University of California. All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44static const char sccsid[] = "@(#)cut.c 8.3 (Berkeley) 5/4/95";
45#endif /* not lint */
46
47#include <config.h>
48
49#include <ctype.h>
50#include <stdio.h>
51#include <errno.h>
52
53#include "bashansi.h"
54
55#ifdef HAVE_LIMITS_H
56# include <limits.h>
57#endif
58
59#ifdef HAVE_UNISTD_H
60# include <unistd.h>
61#endif
62
63#include "builtins.h"
64#include "shell.h"
65#include "bashgetopt.h"
66
67#if !defined (errno)
68extern int errno;
69#endif
70
71#if !defined (_POSIX2_LINE_MAX)
72# define _POSIX2_LINE_MAX 2048
73#endif
74
75static int cflag;
76static char dchar;
77static int dflag;
78static int fflag;
79static int sflag;
80
81static int autostart, autostop, maxval;
82static char positions[_POSIX2_LINE_MAX + 1];
83
84static int c_cut __P((FILE *, char *));
85static int f_cut __P((FILE *, char *));
86static int get_list __P((char *));
87static char *_cut_strsep __P((char **, const char *));
88
89int
90cut_builtin(list)
91 WORD_LIST *list;
92{
93 FILE *fp;
94 int (*fcn) __P((FILE *, char *)) = NULL;
95 int ch;
96
97 fcn = NULL;
98 dchar = '\t'; /* default delimiter is \t */
99
100 /* Since we don't support multi-byte characters, the -c and -b
101 options are equivalent, and the -n option is meaningless. */
102 reset_internal_getopt ();
103 while ((ch = internal_getopt (list, "b:c:d:f:sn")) != -1)
104 switch(ch) {
105 case 'b':
106 case 'c':
107 fcn = c_cut;
108 if (get_list(list_optarg) < 0)
109 return (EXECUTION_FAILURE);
110 cflag = 1;
111 break;
112 case 'd':
113 dchar = *list_optarg;
114 dflag = 1;
115 break;
116 case 'f':
117 fcn = f_cut;
118 if (get_list(list_optarg) < 0)
119 return (EXECUTION_FAILURE);
120 fflag = 1;
121 break;
122 case 's':
123 sflag = 1;
124 break;
125 case 'n':
126 break;
127 case '?':
128 default:
129 builtin_usage();
130 return (EX_USAGE);
131 }
132
133 list = loptend;
134
135 if (fflag) {
136 if (cflag) {
137 builtin_usage();