]>
Commit | Line | Data |
---|---|---|
341988b9 RC |
1 | /* |
2 | * Copyright (c) 2000, 2001, Red Hat, Inc. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by Robert Collins <rbtcollins@redhat.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to put all general purpose file manipulation | |
17 | code in one place. */ | |
18 | ||
19 | #if 0 | |
20 | static const char *cvsid = | |
21 | "\n%%% $Id$\n"; | |
22 | #endif | |
23 | ||
24 | #include "win32.h" | |
25 | ||
26 | #include <stdio.h> | |
27 | #include <unistd.h> | |
28 | #include <ctype.h> | |
29 | ||
30 | #include "filemanip.h" | |
31 | ||
32 | unsigned int | |
33 | get_file_size (const char *name) | |
34 | { | |
35 | HANDLE h; | |
36 | WIN32_FIND_DATA buf; | |
37 | DWORD ret = 0; | |
38 | ||
39 | h = FindFirstFileA (name, &buf); | |
40 | if (h != INVALID_HANDLE_VALUE) | |
41 | { | |
42 | if (buf.nFileSizeHigh == 0) | |
43 | ret = buf.nFileSizeLow; | |
44 | FindClose (h); | |
45 | } | |
46 | return ret; | |
47 | } | |
48 | ||
49 | char * | |
50 | base (const char *s) | |
51 | { | |
52 | if (!s) | |
53 | return 0; | |
54 | const char *rv = s; | |
55 | while (*s) | |
56 | { | |
57 | if ((*s == '/' || *s == ':' || *s == '\\') && s[1]) | |
58 | rv = s + 1; | |
59 | s++; | |
60 | } | |
61 | return (char *) rv; | |
62 | } | |
63 | ||
9835fb4a RC |
64 | /* returns the number of characters of path that |
65 | * precede the extension | |
66 | */ | |
341988b9 RC |
67 | int |
68 | find_tar_ext (const char *path) | |
69 | { | |
70 | char *end = strchr (path, '\0'); | |
71 | /* check in longest first order */ | |
9835fb4a RC |
72 | char *ext; |
73 | if ((ext = strstr (path, ".tar.bz2")) && (end - ext) == 8) | |
74 | return ext - path; | |
75 | if ((ext = strstr (path, ".tar.gz")) && (end - ext) == 7) | |
76 | return ext - path; | |
77 | if ((ext = strstr (path, ".tar")) && (end - ext) == 4) | |
78 | return ext - path; | |
79 | return 0; | |
341988b9 RC |
80 | } |
81 | ||
82 | /* Parse a filename into package, version, and extension components. */ | |
83 | int | |
84 | parse_filename (const char *in_fn, fileparse & f) | |
85 | { | |
86 | char *p, *ver; | |
87 | char fn[strlen (in_fn) + 1]; | |
88 | ||
89 | strcpy (fn, in_fn); | |
90 | int n = find_tar_ext (fn); | |
91 | ||
92 | if (!n) | |
93 | return 0; | |
94 | ||
95 | strcpy (f.tail, fn + n); | |
96 | fn[n] = '\0'; | |
97 | f.pkg[0] = f.what[0] = '\0'; | |
98 | p = base (fn); | |
99 | for (ver = p; *ver; ver++) | |
100 | if (*ver == '-' || *ver == '_') | |
101 | if (isdigit (ver[1])) | |
102 | { | |
103 | *ver++ = 0; | |
104 | strcpy (f.pkg, p); | |
105 | break; | |
106 | } | |
107 | else if (strcasecmp (ver, "-src") == 0 || | |
108 | strcasecmp (ver, "-patch") == 0) | |
109 | { | |
110 | *ver++ = 0; | |
111 | strcpy (f.pkg, p); | |
112 | strcpy (f.what, strlwr (ver)); | |
113 | strcpy (f.pkgtar, p); | |
114 | strcat (f.pkgtar, f.tail); | |
115 | ver = strchr (ver, '\0'); | |
116 | break; | |
117 | } | |
118 | ||
119 | if (!f.pkg[0]) | |
120 | strcpy (f.pkg, p); | |
121 | ||
122 | if (!f.what[0]) | |
123 | { | |
124 | int n; | |
125 | p = strchr (ver, '\0'); | |
126 | strcpy (f.pkgtar, in_fn); | |
127 | if ((p -= 4) >= ver && strcasecmp (p, "-src") == 0) | |
128 | n = 4; | |
129 | else if ((p -= 2) >= ver && strcasecmp (p, "-patch") == 0) | |
130 | n = 6; | |
131 | else | |
132 | n = 0; | |
133 | if (n) | |
134 | { | |
135 | strcpy (f.what, p + 1); | |
136 | *p = '\0'; | |
137 | p = f.pkgtar + (p - fn) + n; | |
138 | memmove (p - n, p, strlen (p)); | |
139 | } | |
140 | } | |
141 | ||
142 | strcpy (f.ver, *ver ? ver : "0.0"); | |
143 | return 1; | |
144 | } |