bump product version to 4.1.6.2
[LibreOffice.git] / desktop / unx / source / pagein.c
blobef1f932b32275ed259c8780e03123b6b83cdb2a5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "file_image.h"
22 #include <unistd.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
27 /* do_pagein */
28 static int do_pagein (const char * filename, size_t * size)
30 int result;
31 file_image image = FILE_IMAGE_INITIALIZER;
33 if ((result = file_image_open (&image, filename)) != 0)
34 return (result);
36 if ((result = file_image_pagein (&image)) != 0)
38 fprintf (stderr, "file_image_pagein %s: %s\n", filename, strerror(result));
39 goto cleanup_and_leave;
42 if (size)
44 *size = image.m_size;
47 cleanup_and_leave:
48 file_image_close (&image);
49 return (result);
52 extern int pagein_execute (int argc, char **argv);
54 /* main */
55 int pagein_execute (int argc, char **argv)
57 int i, v = 0;
58 size_t nfiles = 0, nbytes = 0;
60 if (argc < 2)
62 fprintf (
63 stderr,
64 "%s: Usage: pagein [-v[v]] [-L<path>] [@]<filename> ...\n",
65 argv[0]);
66 return (1);
69 for (i = 1; i < argc; i++)
71 FILE * fp = 0;
72 size_t k = 0;
74 if (argv[i][0] == '-')
76 /* option */
77 int j = 1;
78 switch (argv[i][j])
80 case 'v':
81 /* verbosity level */
82 for (v += 1, j += 1; argv[i][j]; j++)
83 v += (argv[i][j] == 'v');
84 break;
85 case 'L':
86 /* search path */
87 if (chdir (&(argv[i][2])) == -1)
88 fprintf (stderr, "chdir %s: %s\n", &(argv[i][2]), strerror(errno));
89 break;
90 default:
91 /* ignored */
92 break;
95 /* next argv */
96 continue;
99 if ((argv[i][0] == '@') && ((fp = fopen (argv[i], "r")) == 0))
101 char fullpath[4096];
102 char *path = NULL;
103 memset(fullpath, 0, sizeof(fullpath));
104 strncpy (fullpath, argv[i] + 1, 3000);
105 if (!(path = strrchr (fullpath, '/')))
106 path = fullpath;
107 else
108 path++;
110 if ((fp = fopen (&(argv[i][1]), "r")) == 0)
112 fprintf (stderr, "fopen %s: %s\n", &(argv[i][1]), strerror(errno));
113 continue;
115 while (fgets (path, 1024, fp) != 0)
117 path[strlen(path) - 1] = '\0', k = 0;
119 /* paths relative to the location of the pagein file */
120 if (do_pagein (fullpath, &k) == 0)
122 /* accumulate total size */
123 nbytes += k;
126 if (v >= 2)
127 fprintf (stderr, "pagein(\"%s\") = %d bytes\n", path, (int) k);
128 nfiles += 1;
130 fclose (fp);
132 else
134 if (fp != 0)
135 fclose (fp);
137 if (do_pagein (argv[i], &k) == 0)
139 /* accumulate total size */
140 nbytes += k;
143 if (v >= 2)
144 fprintf (stderr, "pagein(\"%s\") = %d bytes\n", argv[i], (int) k);
145 nfiles += 1;
149 if (v >= 1)
150 fprintf (stderr, "Total: %d files (%d bytes)\n", (int) nfiles, (int) nbytes);
151 return (0);
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */