xref: /aoo41x/main/desktop/source/pagein/pagein.c (revision e24a5033)
1*e24a5033SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*e24a5033SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*e24a5033SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*e24a5033SAndrew Rist  * distributed with this work for additional information
6*e24a5033SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*e24a5033SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*e24a5033SAndrew Rist  * "License"); you may not use this file except in compliance
9*e24a5033SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*e24a5033SAndrew Rist  *
11*e24a5033SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*e24a5033SAndrew Rist  *
13*e24a5033SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*e24a5033SAndrew Rist  * software distributed under the License is distributed on an
15*e24a5033SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*e24a5033SAndrew Rist  * KIND, either express or implied.  See the License for the
17*e24a5033SAndrew Rist  * specific language governing permissions and limitations
18*e24a5033SAndrew Rist  * under the License.
19*e24a5033SAndrew Rist  *
20*e24a5033SAndrew Rist  *************************************************************/
21*e24a5033SAndrew Rist 
22*e24a5033SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "file_image.h"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <unistd.h>
27cdf0e10cSrcweir #include <errno.h>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <string.h>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /* do_pagein */
do_pagein(const char * filename,size_t * size)32cdf0e10cSrcweir static int do_pagein (const char * filename, size_t * size)
33cdf0e10cSrcweir {
34cdf0e10cSrcweir 	int result;
35cdf0e10cSrcweir 	file_image image = FILE_IMAGE_INITIALIZER;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	if ((result = file_image_open (&image, filename)) != 0)
38cdf0e10cSrcweir 		return (result);
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 	if ((result = file_image_pagein (&image)) != 0)
41cdf0e10cSrcweir 	{
42cdf0e10cSrcweir 		fprintf (stderr, "file_image_pagein: %s\n", strerror(result));
43cdf0e10cSrcweir 		goto cleanup_and_leave;
44cdf0e10cSrcweir 	}
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 	if (size)
47cdf0e10cSrcweir 	{
48cdf0e10cSrcweir 		*size = image.m_size;
49cdf0e10cSrcweir 	}
50cdf0e10cSrcweir 
51cdf0e10cSrcweir cleanup_and_leave:
52cdf0e10cSrcweir 	file_image_close (&image);
53cdf0e10cSrcweir 	return (result);
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir /* main */
main(int argc,char ** argv)57cdf0e10cSrcweir int  main (int argc, char **argv)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	int    i, v = 0;
60cdf0e10cSrcweir 	size_t nfiles = 0, nbytes = 0;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	if (argc < 2)
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		fprintf (
65cdf0e10cSrcweir 			stderr,
66cdf0e10cSrcweir 			"%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
67cdf0e10cSrcweir 			argv[0]);
68cdf0e10cSrcweir 		return (1);
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	for (i = 1; i < argc; i++)
72cdf0e10cSrcweir 	{
73cdf0e10cSrcweir 		FILE   * fp = 0;
74cdf0e10cSrcweir 		size_t   k  = 0;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 		if (argv[i][0] == '-')
77cdf0e10cSrcweir 		{
78cdf0e10cSrcweir 			/* option */
79cdf0e10cSrcweir 			int j = 1;
80cdf0e10cSrcweir 			switch (argv[i][j])
81cdf0e10cSrcweir 			{
82cdf0e10cSrcweir 				case 'v':
83cdf0e10cSrcweir 					/* verbosity level */
84cdf0e10cSrcweir 					for (v += 1, j += 1; argv[i][j]; j++)
85cdf0e10cSrcweir 						v += (argv[i][j] == 'v');
86cdf0e10cSrcweir 					break;
87cdf0e10cSrcweir 				case 'L':
88cdf0e10cSrcweir 					/* search path */
89cdf0e10cSrcweir 					if (chdir (&(argv[i][2])) == -1)
90cdf0e10cSrcweir 						fprintf (stderr, "chdir: %s\n", strerror(errno));
91cdf0e10cSrcweir 					break;
92cdf0e10cSrcweir 				default:
93cdf0e10cSrcweir 					/* ignored */
94cdf0e10cSrcweir 					break;
95cdf0e10cSrcweir 			}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 			/* next argv */
98cdf0e10cSrcweir 			continue;
99cdf0e10cSrcweir 		}
100cdf0e10cSrcweir 
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
103cdf0e10cSrcweir 		{
104cdf0e10cSrcweir 			char path[1024];
105cdf0e10cSrcweir 			if ((fp = fopen (&(argv[i][1]), "r")) == 0)
106cdf0e10cSrcweir 			{
107cdf0e10cSrcweir 				fprintf (stderr, "fopen: %s\n", strerror(errno));
108cdf0e10cSrcweir 				continue;
109cdf0e10cSrcweir 			}
110cdf0e10cSrcweir 			while (fgets (path, sizeof(path), fp) != 0)
111cdf0e10cSrcweir 			{
112cdf0e10cSrcweir 				path[strlen(path) - 1] = '\0', k = 0;
113cdf0e10cSrcweir 				if (do_pagein (path, &k) == 0)
114cdf0e10cSrcweir 				{
115cdf0e10cSrcweir 					/* accumulate total size */
116cdf0e10cSrcweir 					nbytes += k;
117cdf0e10cSrcweir 				}
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 				if (v >= 2)
120cdf0e10cSrcweir 					fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
121cdf0e10cSrcweir 				nfiles += 1;
122cdf0e10cSrcweir 			}
123cdf0e10cSrcweir 			fclose (fp);
124cdf0e10cSrcweir 		}
125cdf0e10cSrcweir 		else
126cdf0e10cSrcweir 		{
127cdf0e10cSrcweir 			if (fp != 0)
128cdf0e10cSrcweir 				fclose (fp);
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 			if (do_pagein (argv[i], &k) == 0)
131cdf0e10cSrcweir 			{
132cdf0e10cSrcweir 				/* accumulate total size */
133cdf0e10cSrcweir 				nbytes += k;
134cdf0e10cSrcweir 			}
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 			if (v >= 2)
137cdf0e10cSrcweir 				fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
138cdf0e10cSrcweir 			nfiles += 1;
139cdf0e10cSrcweir 		}
140cdf0e10cSrcweir 	}
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 	if (v >= 1)
143cdf0e10cSrcweir 		fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
144cdf0e10cSrcweir 	return (0);
145cdf0e10cSrcweir }
146