xref: /onnv-gate/usr/src/cmd/print/conv_fix/conv_fix.c (revision 245:b644fbc06615)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*245Sjacobs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <locale.h>
330Sstevel@tonic-gate #include <sys/file.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate extern char *optarg;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * FUNCTION:
410Sstevel@tonic-gate  *	static char *_file_getline(FILE *fp)
420Sstevel@tonic-gate  * INPUT:
430Sstevel@tonic-gate  *	FILE *fp - file pointer to read from
440Sstevel@tonic-gate  * OUTPUT:
450Sstevel@tonic-gate  *	char *(return) - an entry from the stream
460Sstevel@tonic-gate  * DESCRIPTION:
470Sstevel@tonic-gate  *	This routine will read in a line at a time.  If the line ends in a
480Sstevel@tonic-gate  *	newline, it returns.  If the line ends in a backslash newline, it
490Sstevel@tonic-gate  *	continues reading more.  It will ignore lines that start in # or
500Sstevel@tonic-gate  *	blank lines.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate static char *
_file_getline(FILE * fp)530Sstevel@tonic-gate _file_getline(FILE *fp)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	char entry[BUFSIZ], *tmp;
560Sstevel@tonic-gate 	int size;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	size = sizeof (entry);
590Sstevel@tonic-gate 	tmp  = entry;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	/* find an entry */
620Sstevel@tonic-gate 	while (fgets(tmp, size, fp)) {
630Sstevel@tonic-gate 		if ((tmp == entry) && ((*tmp == '#') || (*tmp == '\n'))) {
640Sstevel@tonic-gate 			continue;
650Sstevel@tonic-gate 		} else {
660Sstevel@tonic-gate 			if ((*tmp == '#') || (*tmp == '\n')) {
670Sstevel@tonic-gate 				*tmp = NULL;
680Sstevel@tonic-gate 				break;
690Sstevel@tonic-gate 			}
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 			size -= strlen(tmp);
720Sstevel@tonic-gate 			tmp += strlen(tmp);
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 			if (*(tmp-2) != '\\')
750Sstevel@tonic-gate 				break;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 			size -= 2;
780Sstevel@tonic-gate 			tmp -= 2;
790Sstevel@tonic-gate 		}
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (tmp == entry)
830Sstevel@tonic-gate 		return (NULL);
840Sstevel@tonic-gate 	else
850Sstevel@tonic-gate 		return (strdup(entry));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
88*245Sjacobs int
main(int ac,char * av[])890Sstevel@tonic-gate main(int ac, char *av[])
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	int   c;
920Sstevel@tonic-gate 	char  file[80], ofile[80];
930Sstevel@tonic-gate 	char *cp;
940Sstevel@tonic-gate 	FILE *fp, *fp2;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #if	!defined(TEXT_DOMAIN)
990Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
1000Sstevel@tonic-gate #endif
1010Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	while ((c = getopt(ac, av, "f:o:")) != EOF)
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 		switch (c) {
1060Sstevel@tonic-gate 		case 'f':
1070Sstevel@tonic-gate 			(void) strlcpy(file, optarg, sizeof (file));
1080Sstevel@tonic-gate 			break;
1090Sstevel@tonic-gate 		case 'o':
1100Sstevel@tonic-gate 			(void) strlcpy(ofile, optarg, sizeof (ofile));
1110Sstevel@tonic-gate 			break;
1120Sstevel@tonic-gate 		default:
113*245Sjacobs 			(void) fprintf(stderr, gettext(
114*245Sjacobs 				"Usage: %s [-f file] [-o output file]\n"),
1150Sstevel@tonic-gate 				av[0]);
1160Sstevel@tonic-gate 			return (1);
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	if ((fp = fopen(file, "r")) != NULL) {
1200Sstevel@tonic-gate 		int fd;
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		fd = open(ofile, O_RDWR|O_APPEND);
1230Sstevel@tonic-gate 		if ((fd < 0) && (errno == ENOENT))
1240Sstevel@tonic-gate 			fd = open(ofile, O_RDWR|O_CREAT|O_EXCL, 0644);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		if (fd < 0) {
1270Sstevel@tonic-gate 			(void) fprintf(stderr,
1280Sstevel@tonic-gate 			    gettext("Error trying to open file.\n"));
1290Sstevel@tonic-gate 			return (1);
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		lseek(fd, 0, SEEK_END);
133*245Sjacobs 
1340Sstevel@tonic-gate 		if ((fp2 = fdopen(fd, "a")) != NULL) {
1350Sstevel@tonic-gate 			while ((cp = _file_getline(fp)) != NULL) {
1360Sstevel@tonic-gate 				(void) fprintf(fp2, "%s", cp);
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 			return (0);
1390Sstevel@tonic-gate 		} else {
1400Sstevel@tonic-gate 			(void) fprintf(stderr,
1410Sstevel@tonic-gate 			    gettext("Error trying to open file.\n"));
1420Sstevel@tonic-gate 			return (1);
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 	} else {
1450Sstevel@tonic-gate 		(void) fprintf(stderr,
1460Sstevel@tonic-gate 		    gettext("Error trying to open file.\n"));
1470Sstevel@tonic-gate 		return (1);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate }
150