xref: /titanic_41/usr/src/cmd/print/conv_fix/conv_fix.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <sys/file.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 
37 extern char *optarg;
38 
39 /*
40  * FUNCTION:
41  *	static char *_file_getline(FILE *fp)
42  * INPUT:
43  *	FILE *fp - file pointer to read from
44  * OUTPUT:
45  *	char *(return) - an entry from the stream
46  * DESCRIPTION:
47  *	This routine will read in a line at a time.  If the line ends in a
48  *	newline, it returns.  If the line ends in a backslash newline, it
49  *	continues reading more.  It will ignore lines that start in # or
50  *	blank lines.
51  */
52 static char *
53 _file_getline(FILE *fp)
54 {
55 	char entry[BUFSIZ], *tmp;
56 	int size;
57 
58 	size = sizeof (entry);
59 	tmp  = entry;
60 
61 	/* find an entry */
62 	while (fgets(tmp, size, fp)) {
63 		if ((tmp == entry) && ((*tmp == '#') || (*tmp == '\n'))) {
64 			continue;
65 		} else {
66 			if ((*tmp == '#') || (*tmp == '\n')) {
67 				*tmp = NULL;
68 				break;
69 			}
70 
71 			size -= strlen(tmp);
72 			tmp += strlen(tmp);
73 
74 			if (*(tmp-2) != '\\')
75 				break;
76 
77 			size -= 2;
78 			tmp -= 2;
79 		}
80 	}
81 
82 	if (tmp == entry)
83 		return (NULL);
84 	else
85 		return (strdup(entry));
86 }
87 
88 main(int ac, char *av[])
89 {
90 	int   c;
91 	char  file[80], ofile[80];
92 	char *cp;
93 	FILE *fp, *fp2;
94 
95 	(void) setlocale(LC_ALL, "");
96 
97 #if	!defined(TEXT_DOMAIN)
98 #define	TEXT_DOMAIN "SYS_TEST"
99 #endif
100 	(void) textdomain(TEXT_DOMAIN);
101 
102 	while ((c = getopt(ac, av, "f:o:")) != EOF)
103 
104 		switch (c) {
105 		case 'f':
106 			(void) strlcpy(file, optarg, sizeof (file));
107 			break;
108 		case 'o':
109 			(void) strlcpy(ofile, optarg, sizeof (ofile));
110 			break;
111 		default:
112 			(void) fprintf(stderr,
113 				gettext("Usage: %s [-f file] [-o output file]\n"),
114 				av[0]);
115 			return (1);
116 		}
117 
118 	if ((fp = fopen(file, "r")) != NULL) {
119 		int fd;
120 
121 		fd = open(ofile, O_RDWR|O_APPEND);
122 		if ((fd < 0) && (errno == ENOENT))
123 			fd = open(ofile, O_RDWR|O_CREAT|O_EXCL, 0644);
124 
125 		if (fd < 0) {
126 			(void) fprintf(stderr,
127 			    gettext("Error trying to open file.\n"));
128 			return (1);
129 		}
130 
131 		lseek(fd, 0, SEEK_END);
132 
133 		if ((fp2 = fdopen(fd, "a")) != NULL) {
134 			while ((cp = _file_getline(fp)) != NULL) {
135 				(void) fprintf(fp2, "%s", cp);
136 			}
137 			return (0);
138 		} else {
139 			(void) fprintf(stderr,
140 			    gettext("Error trying to open file.\n"));
141 			return (1);
142 		}
143 	} else {
144 		(void) fprintf(stderr,
145 		    gettext("Error trying to open file.\n"));
146 		return (1);
147 	}
148 }
149