xref: /titanic_44/usr/src/cmd/print/conv_fix/conv_fix.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
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 2005 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 int
89 main(int ac, char *av[])
90 {
91 	int   c;
92 	char  file[80], ofile[80];
93 	char *cp;
94 	FILE *fp, *fp2;
95 
96 	(void) setlocale(LC_ALL, "");
97 
98 #if	!defined(TEXT_DOMAIN)
99 #define	TEXT_DOMAIN "SYS_TEST"
100 #endif
101 	(void) textdomain(TEXT_DOMAIN);
102 
103 	while ((c = getopt(ac, av, "f:o:")) != EOF)
104 
105 		switch (c) {
106 		case 'f':
107 			(void) strlcpy(file, optarg, sizeof (file));
108 			break;
109 		case 'o':
110 			(void) strlcpy(ofile, optarg, sizeof (ofile));
111 			break;
112 		default:
113 			(void) fprintf(stderr, gettext(
114 				"Usage: %s [-f file] [-o output file]\n"),
115 				av[0]);
116 			return (1);
117 		}
118 
119 	if ((fp = fopen(file, "r")) != NULL) {
120 		int fd;
121 
122 		fd = open(ofile, O_RDWR|O_APPEND);
123 		if ((fd < 0) && (errno == ENOENT))
124 			fd = open(ofile, O_RDWR|O_CREAT|O_EXCL, 0644);
125 
126 		if (fd < 0) {
127 			(void) fprintf(stderr,
128 			    gettext("Error trying to open file.\n"));
129 			return (1);
130 		}
131 
132 		lseek(fd, 0, SEEK_END);
133 
134 		if ((fp2 = fdopen(fd, "a")) != NULL) {
135 			while ((cp = _file_getline(fp)) != NULL) {
136 				(void) fprintf(fp2, "%s", cp);
137 			}
138 			return (0);
139 		} else {
140 			(void) fprintf(stderr,
141 			    gettext("Error trying to open file.\n"));
142 			return (1);
143 		}
144 	} else {
145 		(void) fprintf(stderr,
146 		    gettext("Error trying to open file.\n"));
147 		return (1);
148 	}
149 }
150