Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "file_image.h"
21 :
22 : #include <unistd.h>
23 : #include <errno.h>
24 : #include <stdio.h>
25 : #include <string.h>
26 :
27 : /* do_pagein */
28 0 : static int do_pagein (const char * filename, size_t * size)
29 : {
30 : int result;
31 0 : file_image image = FILE_IMAGE_INITIALIZER;
32 :
33 0 : if ((result = file_image_open (&image, filename)) != 0)
34 0 : return (result);
35 :
36 0 : if ((result = file_image_pagein (&image)) != 0)
37 : {
38 0 : fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
39 0 : goto cleanup_and_leave;
40 : }
41 :
42 0 : if (size)
43 : {
44 0 : *size = image.m_size;
45 : }
46 :
47 : cleanup_and_leave:
48 0 : file_image_close (&image);
49 0 : return (result);
50 : }
51 :
52 : extern int pagein_execute (int argc, char **argv);
53 :
54 : /* main */
55 0 : int pagein_execute (int argc, char **argv)
56 : {
57 0 : int i, v = 0;
58 0 : size_t nfiles = 0, nbytes = 0;
59 :
60 0 : if (argc < 2)
61 : {
62 0 : fprintf (
63 : stderr,
64 : "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
65 : argv[0]);
66 0 : return (1);
67 : }
68 :
69 0 : for (i = 1; i < argc; i++)
70 : {
71 0 : FILE * fp = 0;
72 0 : size_t k = 0;
73 :
74 0 : if (argv[i][0] == '-')
75 : {
76 : /* option */
77 0 : int j = 1;
78 0 : switch (argv[i][j])
79 : {
80 : case 'v':
81 : /* verbosity level */
82 0 : for (v += 1, j += 1; argv[i][j]; j++)
83 0 : v += (argv[i][j] == 'v');
84 0 : break;
85 : case 'L':
86 : /* search path */
87 0 : if (chdir (&(argv[i][2])) == -1)
88 0 : fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
89 0 : break;
90 : default:
91 : /* ignored */
92 0 : break;
93 : }
94 :
95 : /* next argv */
96 0 : continue;
97 : }
98 :
99 0 : if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
100 0 : {
101 : char fullpath[4096];
102 0 : char *path = NULL;
103 0 : memset(fullpath, 0, sizeof(fullpath));
104 0 : strncpy (fullpath, argv[i] + 1, 3000);
105 0 : if (!(path = strrchr (fullpath, '/')))
106 0 : path = fullpath;
107 : else
108 0 : path++;
109 :
110 0 : if ((fp = fopen (&(argv[i][1]), "r")) == 0)
111 : {
112 0 : fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
113 0 : continue;
114 : }
115 0 : while (fgets (path, 1024, fp) != 0)
116 : {
117 0 : path[strlen(path) - 1] = '\0', k = 0;
118 :
119 : /* paths relative to the location of the pagein file */
120 0 : if (do_pagein (fullpath, &k) == 0)
121 : {
122 : /* accumulate total size */
123 0 : nbytes += k;
124 : }
125 :
126 0 : if (v >= 2)
127 0 : fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
128 0 : nfiles += 1;
129 : }
130 0 : fclose (fp);
131 : }
132 : else
133 : {
134 0 : if (fp != 0)
135 0 : fclose (fp);
136 :
137 0 : if (do_pagein (argv[i], &k) == 0)
138 : {
139 : /* accumulate total size */
140 0 : nbytes += k;
141 : }
142 :
143 0 : if (v >= 2)
144 0 : fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
145 0 : nfiles += 1;
146 : }
147 : }
148 :
149 0 : if (v >= 1)
150 0 : fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
151 0 : return (0);
152 : }
153 :
154 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|