xref: /trunk/main/desktop/source/pagein/pagein.c (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "file_image.h"
29 
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 
35 /* do_pagein */
36 static int do_pagein (const char * filename, size_t * size)
37 {
38     int result;
39     file_image image = FILE_IMAGE_INITIALIZER;
40 
41     if ((result = file_image_open (&image, filename)) != 0)
42         return (result);
43 
44     if ((result = file_image_pagein (&image)) != 0)
45     {
46         fprintf (stderr, "file_image_pagein: %s\n", strerror(result));
47         goto cleanup_and_leave;
48     }
49 
50     if (size)
51     {
52         *size = image.m_size;
53     }
54 
55 cleanup_and_leave:
56     file_image_close (&image);
57     return (result);
58 }
59 
60 /* main */
61 int  main (int argc, char **argv)
62 {
63     int    i, v = 0;
64     size_t nfiles = 0, nbytes = 0;
65 
66     if (argc < 2)
67     {
68         fprintf (
69             stderr,
70             "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
71             argv[0]);
72         return (1);
73     }
74 
75     for (i = 1; i < argc; i++)
76     {
77         FILE   * fp = 0;
78         size_t   k  = 0;
79 
80         if (argv[i][0] == '-')
81         {
82             /* option */
83             int j = 1;
84             switch (argv[i][j])
85             {
86                 case 'v':
87                     /* verbosity level */
88                     for (v += 1, j += 1; argv[i][j]; j++)
89                         v += (argv[i][j] == 'v');
90                     break;
91                 case 'L':
92                     /* search path */
93                     if (chdir (&(argv[i][2])) == -1)
94                         fprintf (stderr, "chdir: %s\n", strerror(errno));
95                     break;
96                 default:
97                     /* ignored */
98                     break;
99             }
100 
101             /* next argv */
102             continue;
103         }
104 
105 
106         if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
107         {
108             char path[1024];
109             if ((fp = fopen (&(argv[i][1]), "r")) == 0)
110             {
111                 fprintf (stderr, "fopen: %s\n", strerror(errno));
112                 continue;
113             }
114             while (fgets (path, sizeof(path), fp) != 0)
115             {
116                 path[strlen(path) - 1] = '\0', k = 0;
117                 if (do_pagein (path, &k) == 0)
118                 {
119                     /* accumulate total size */
120                     nbytes += k;
121                 }
122 
123                 if (v >= 2)
124                     fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
125                 nfiles += 1;
126             }
127             fclose (fp);
128         }
129         else
130         {
131             if (fp != 0)
132                 fclose (fp);
133 
134             if (do_pagein (argv[i], &k) == 0)
135             {
136                 /* accumulate total size */
137                 nbytes += k;
138             }
139 
140             if (v >= 2)
141                 fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
142             nfiles += 1;
143         }
144     }
145 
146     if (v >= 1)
147         fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
148     return (0);
149 }
150