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