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 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <locale.h> 31 #include <sys/file.h> 32 #include <fcntl.h> 33 #include <errno.h> 34 35 extern char *optarg; 36 37 /* 38 * FUNCTION: 39 * static char *_file_getline(FILE *fp) 40 * INPUT: 41 * FILE *fp - file pointer to read from 42 * OUTPUT: 43 * char *(return) - an entry from the stream 44 * DESCRIPTION: 45 * This routine will read in a line at a time. If the line ends in a 46 * newline, it returns. If the line ends in a backslash newline, it 47 * continues reading more. It will ignore lines that start in # or 48 * blank lines. 49 */ 50 static char * 51 _file_getline(FILE *fp) 52 { 53 char entry[BUFSIZ], *tmp; 54 int size; 55 56 size = sizeof (entry); 57 tmp = entry; 58 59 /* find an entry */ 60 while (fgets(tmp, size, fp)) { 61 if ((tmp == entry) && ((*tmp == '#') || (*tmp == '\n'))) { 62 continue; 63 } else { 64 if ((*tmp == '#') || (*tmp == '\n')) { 65 *tmp = '\0'; 66 break; 67 } 68 69 size -= strlen(tmp); 70 tmp += strlen(tmp); 71 72 if (*(tmp-2) != '\\') 73 break; 74 75 size -= 2; 76 tmp -= 2; 77 } 78 } 79 80 if (tmp == entry) 81 return (NULL); 82 else 83 return (strdup(entry)); 84 } 85 86 int 87 main(int ac, char *av[]) 88 { 89 int c; 90 char file[80], ofile[80]; 91 char *cp; 92 FILE *fp, *fp2; 93 94 (void) setlocale(LC_ALL, ""); 95 96 #if !defined(TEXT_DOMAIN) 97 #define TEXT_DOMAIN "SYS_TEST" 98 #endif 99 (void) textdomain(TEXT_DOMAIN); 100 101 while ((c = getopt(ac, av, "f:o:")) != EOF) 102 103 switch (c) { 104 case 'f': 105 (void) strlcpy(file, optarg, sizeof (file)); 106 break; 107 case 'o': 108 (void) strlcpy(ofile, optarg, sizeof (ofile)); 109 break; 110 default: 111 (void) fprintf(stderr, gettext( 112 "Usage: %s [-f file] [-o output file]\n"), 113 av[0]); 114 return (1); 115 } 116 117 if ((fp = fopen(file, "r")) != NULL) { 118 int fd; 119 120 fd = open(ofile, O_RDWR|O_APPEND); 121 if ((fd < 0) && (errno == ENOENT)) 122 fd = open(ofile, O_RDWR|O_CREAT|O_EXCL, 0644); 123 124 if (fd < 0) { 125 (void) fprintf(stderr, 126 gettext("Error trying to open file.\n")); 127 return (1); 128 } 129 130 lseek(fd, 0, SEEK_END); 131 132 if ((fp2 = fdopen(fd, "a")) != NULL) { 133 while ((cp = _file_getline(fp)) != NULL) { 134 (void) fprintf(fp2, "%s", cp); 135 } 136 return (0); 137 } else { 138 (void) fprintf(stderr, 139 gettext("Error trying to open file.\n")); 140 return (1); 141 } 142 } else { 143 (void) fprintf(stderr, 144 gettext("Error trying to open file.\n")); 145 return (1); 146 } 147 } 148