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 : #include "pagein.h"
22 :
23 : #include <unistd.h>
24 : #include <errno.h>
25 : #include <stdio.h>
26 : #include <string.h>
27 :
28 : /* do_pagein */
29 4992 : static int do_pagein (const char * filename, size_t * size)
30 : {
31 : int result;
32 4992 : file_image image = FILE_IMAGE_INITIALIZER;
33 :
34 4992 : if ((result = file_image_open (&image, filename)) != 0)
35 192 : return (result);
36 :
37 4800 : if ((result = file_image_pagein (&image)) != 0)
38 : {
39 0 : fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
40 0 : goto cleanup_and_leave;
41 : }
42 :
43 4800 : if (size)
44 : {
45 4800 : *size = image.m_size;
46 : }
47 :
48 : cleanup_and_leave:
49 4800 : file_image_close (&image);
50 4800 : return (result);
51 : }
52 :
53 : /* main */
54 96 : int pagein_execute (int argc, char **argv)
55 : {
56 96 : int i, v = 0;
57 96 : size_t nfiles = 0, nbytes = 0;
58 :
59 96 : if (argc < 2)
60 : {
61 0 : fprintf (
62 : stderr,
63 : "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
64 : argv[0]);
65 0 : return (1);
66 : }
67 :
68 192 : for (i = 1; i < argc; i++)
69 : {
70 96 : FILE * fp = 0;
71 96 : size_t k = 0;
72 :
73 96 : if (argv[i][0] == '-')
74 : {
75 : /* option */
76 0 : int j = 1;
77 0 : switch (argv[i][j])
78 : {
79 : case 'v':
80 : /* verbosity level */
81 0 : for (v += 1, j += 1; argv[i][j]; j++)
82 0 : v += (argv[i][j] == 'v');
83 0 : break;
84 : case 'L':
85 : /* search path */
86 0 : if (chdir (&(argv[i][2])) == -1)
87 0 : fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
88 0 : break;
89 : default:
90 : /* ignored */
91 0 : break;
92 : }
93 :
94 : /* next argv */
95 0 : continue;
96 : }
97 :
98 96 : if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
99 96 : {
100 : char fullpath[4096];
101 96 : char *path = NULL;
102 96 : memset(fullpath, 0, sizeof(fullpath));
103 96 : strncpy (fullpath, argv[i] + 1, 3000);
104 96 : if (!(path = strrchr (fullpath, '/')))
105 0 : path = fullpath;
106 : else
107 96 : path++;
108 :
109 96 : if ((fp = fopen (&(argv[i][1]), "r")) == 0)
110 : {
111 0 : fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
112 0 : continue;
113 : }
114 5184 : while (fgets (path, 1024, fp) != 0)
115 : {
116 4992 : path[strlen(path) - 1] = '\0', k = 0;
117 :
118 : /* paths relative to the location of the pagein file */
119 4992 : if (do_pagein (fullpath, &k) == 0)
120 : {
121 : /* accumulate total size */
122 4800 : nbytes += k;
123 : }
124 :
125 4992 : if (v >= 2)
126 0 : fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
127 4992 : nfiles += 1;
128 : }
129 96 : fclose (fp);
130 : }
131 : else
132 : {
133 0 : if (fp != 0)
134 0 : fclose (fp);
135 :
136 0 : if (do_pagein (argv[i], &k) == 0)
137 : {
138 : /* accumulate total size */
139 0 : nbytes += k;
140 : }
141 :
142 0 : if (v >= 2)
143 0 : fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
144 0 : nfiles += 1;
145 : }
146 : }
147 :
148 96 : if (v >= 1)
149 0 : fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
150 96 : return (0);
151 : }
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|