File: main.c

package info (click to toggle)
splitdigest 2.3-4
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 144 kB
  • ctags: 103
  • sloc: ansic: 609; makefile: 106
file content (249 lines) | stat: -rw-r--r-- 6,948 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * main.c
 * Copyright (c) 1994,1995 by Christopher Heng. All rights reserved.
 * You may not remove or alter the copyright notice and/or the conditions for
 * use and distribution. See COPYING for additional conditions for use and
 * distribution. (The file COPYING contains the GNU General Public License
 * version 2.)
 *
 * Program to undigest a file comprising one or more digest files into
 * separate digests.
 *
 * Usage: splitdigest [options] [file...]
 * Options include
 *	-C <config>	Use configuration file specified.
 *	-c		Compress output file. (Obsolete)
 *	-h		Display usage.
 *	-l		Suppress the removal of the Content-Length: header.
 *	-o <outdir>	Output directory to place files (will be created
 *			if it does not exist).
 *	-s		Separate the digests only; do not undigest. (Obsolete)
 *	-t		Leave output file as uncompressed text file (default).
 *			(Obsolete)
 *	-u		Undigest while separating digests (default). (Obsolete)
 *	-v		Display version number.
 *
 * $Id: main.c,v 2.6 1995/07/08 10:04:45 chris Released $
 */

#include <getopt.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "cftables.h"
#include "splitdigest.h"
#include "version.h"

/* the following are for easy identification for bug reports and */
/* debugging purposes */
static char rcsid[] = "$Id: main.c,v 2.6 1995/07/08 10:04:45 chris Released $";
static char verstr[] = VERSTR;
static char shusagestr[] = SHUSAGESTR ;

/* global variables */
int compression = DEFTOCOMPRESS ;	/* whether to compress or not */
					/* (obsolete) */
#if (OLDMETHOD == 1)
int undigest = 0;			/* (obsolete) */
#else
int undigest = 1;	/* whether to undigest or not (by default) */
			/* (obsolete) */
#endif
int kill_contentlen = 1 ;	/* kill Content-Length lines */
char * pgmname ;	/* program name */
char * configfile = DEFCONFIG;	/* configuration filename */
char * infilename = "stdin" ;
char * newdir = NULL ;	/* directory to extract the files */

/* local functions */
static void cleanuponsig ( int sig );
static void cleanuponexit( void );
static int changedir( char * dir );

int main ( int argc, char ** argv )
{

	static char optstring[] = "C:chlo:stuv" ; /* options for getopt() */
	char * currdir ;
	FILE * infile ;
	int i, c ;

	/* form pgmname without path prefix */
	if ((pgmname = strrchr( argv[0], '/' )) == NULL)
		pgmname = argv[0] ;
	else pgmname++;

	/* trap SIGINT so that we can clean up temporary files */
	if (signal( SIGINT, SIG_IGN ) != SIG_IGN) {
		signal( SIGINT, cleanuponsig );
	}
	/* register an atexit function */
	atexit( cleanuponexit );

	while ( (c = getopt(argc, argv, optstring)) != EOF ) {
		switch( c ) {
			case 'C':	/* specify another config file */
				configfile = optarg ;
				break;
			case 'c':	/* compress output file */
				compression = 1; /* obsolete */
				break ;
			case 'h':	/* usage help requested */
				fprintf( stdout, "%s\n%s", verstr, USAGESTR );
				return EXIT_SUCCESS;
					/* quit immediately */
				break ;
			case 'l':	/* preserve Content-Length header */
				kill_contentlen = 0;
				break ;
			case 'o':	/* output directory specified */
				newdir = optarg;
				break ;
			case 's':	/* separate digests only (obsolete) */
				undigest = 0;
				break;
			case 't':	/* switch off compression (obsolete) */
				compression = 0;
				break ;
			case 'u':	/* undigest (obsolete) */
				undigest = 1;
				break;
			case 'v':	/* version number request */
				fprintf( stdout, "%s\n", verstr );
				return EXIT_SUCCESS;
					/* quit immediately */
				break ;
			default:	/* bad option */
				fprintf( stderr, "\n%s\n", shusagestr);
				return EXIT_FAILURE;
				break ;
		}
	}
	/* get current directory and save it */
	for (currdir = NULL, i = 128; currdir == NULL ; i+=128) {
		if ((currdir = getcwd( NULL, i )) == NULL) {
			if (errno == ERANGE)
				continue ;
			else
				goto nomemleft ;
		}
	}

	/* make directory and change to it if applicable */
	if (newdir != NULL && changedir(newdir))
		return EXIT_FAILURE ;
	/* load the configuration tables */
	if (inittables())
		return EXIT_FAILURE ;
	/* if no args assume stdin is the input */
	if (optind >= argc) {
		if (isatty(fileno(stdin))) {
			fprintf( stderr, "%s\n", shusagestr );
			return EXIT_FAILURE ;
		}
		uncatfile( stdin );
	}
	else {	/* invoked uncatfile for each arg */
		for (i = optind; i < argc; i++) {
			if (argv[i][0] == '/') {
				if ((infilename = malloc(strlen(argv[i])+1))
					== NULL)
					goto nomemleft ;
				strcpy(infilename, argv[i]);
			}
			else {
			/* we need to make infilename a full path because */
			/* we may have changed directory if the user */
			/* has specified -o xxx */
			    if ((infilename = malloc(
				strlen(currdir)+strlen(argv[i])+2 ))==NULL) {
nomemleft:				
				fprintf( stderr, "%s: out of memory.\n",
					pgmname );
				return EXIT_FAILURE ;
			    }
			    strcpy(infilename, currdir);
			    strcat(infilename, "/");
			    strcat(infilename, argv[i]);
			}
			if ((infile = fopen ( infilename, READMODE ))==NULL) {
				fprintf( stderr, "%s: cannot open %s. "
					"Skipped.\n", pgmname, argv[i] );
				continue;
			}
			uncatfile( infile );
			fclose(infile);
			free(infilename);
		}
	}
	return EXIT_SUCCESS;

}

/* clean up on SIGINT */
static void cleanuponsig ( int sig )
{
	exit(EXIT_SIGINT) ;	/* the atexit() function will do the cleaning*/
}

/* atexit cleanup function */
static void cleanuponexit( void )
{
	realcleanup();
}

/* change directory to specified directory. Create if it does not exist */
static int changedir ( char * dir )
{
	struct stat buf ;
	int mask, mode ;

	/* we stat to see if the "dir" string is a directory. If */
	/* it doesn't exist we'll create it. If it is a file */
	/* we'll abort. We'll also abort if the directory cannot */
	/* be created. The directory is created with the default */
	/* umask */

	if (!access(dir, F_OK)) { /* directory/file exists */
		/* now make sure it is a directory */
		if (stat(dir, &buf)) {
			fprintf( stderr, "%s: Error - can't stat %d.\n",
					pgmname, dir );
			return -1 ;
		}
		if (! S_ISDIR(buf.st_mode)) {
			fprintf( stderr, "%s: Error - %s is not a directory.\n",
					pgmname, dir );
			return -1 ;
		}
	}
	else {
		/* get umask */
		mask = umask( 0 );
		umask ( mask ); /* restore to original */
		mode = S_IRUSR|S_IWUSR|S_IXUSR|
			((mask&S_IRGRP)?0:S_IRGRP)|
			((mask&S_IWGRP)?0:S_IWGRP)|
			((mask&S_IXGRP)?0:S_IXGRP)|
			((mask&S_IROTH)?0:S_IROTH)|
			((mask&S_IWOTH)?0:S_IWOTH)|
			((mask&S_IXOTH)?0:S_IXOTH);
		if (mkdir(dir, mode)) { /* make dir if it doesn't exist */
			/* couldn't make directory. exit with error message */
			fprintf ( stderr, "%s: Error - Unable to make %s.\n",
				pgmname, dir );
			return -1 ;
		}
	}
	/* now change to the directory */
	if (chdir(dir)) {
		fprintf ( stderr, "%s: Error - Unable to change to directory %s\n",
			pgmname, dir );
		return -1 ;
	}
	return 0;
}