Reactos
1/*
2 * ReactOS log2lines
3 * Written by Jan Roeloffzen
4 *
5 * - Misc utils
6 */
7
8#include <errno.h>
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12
13#include "config.h"
14#include "compat.h"
15#include "util.h"
16#include "options.h"
17
18int
19set_LogFile(FILE **plogFile)
20{
21 if (*opt_logFile)
22 {
23 if (*plogFile)
24 fclose(*plogFile);
25 *plogFile = NULL;
26
27 if (strcmp(opt_logFile,"none") == 0)
28 return 0; //just close
29
30 *plogFile = fopen(opt_logFile, opt_mod ? opt_mod : "a");
31 if (*plogFile)
32 {
33 // disable buffering so fflush is not needed
34 if (!opt_buffered)
35 {
36 l2l_dbg(1, "Disabling log buffering on %s\n", opt_logFile);
37 setbuf(*plogFile, NULL);
38 }
39 else
40 l2l_dbg(1, "Enabling log buffering on %s\n", opt_logFile);
41 }
42 else
43 {
44 l2l_dbg(0, "Could not open logfile %s (%s)\n", opt_logFile, strerror(errno));
45 return 2;
46 }
47 }
48 return 0;
49}
50
51int
52file_exists(char *name)
53{
54 FILE *f;
55
56 f = fopen(name, "r");
57 if (!f)
58 return 0;
59 fclose(f);
60 return 1;
61}
62
63/* Do this in reverse (recursively)
64 This saves many system calls if the path is likely
65 to already exist (creating large trees).
66*/
67int
68mkPath(char *path, int isDir)
69{
70 char *s;
71 int res = 0;
72
73 if (isDir)
74 {
75 res = MKDIR(path);
76 if (!res || (res == -1 && errno == EEXIST))
77 return 0;
78 }
79 // create parent dir
80 if ((s = strrchr(path, PATH_CHAR)))
81 {
82 *s = '\0';
83 res = mkPath(path, 1);
84 *s = PATH_CHAR;
85 }
86
87 if (!res && isDir)
88 res = MKDIR(path);
89
90 return res;
91}
92
93#if 0
94static FILE *
95rfopen(char *path, char *mode)
96{
97 FILE *f = NULL;
98 char tmppath[PATH_MAX]; // Don't modify const strings
99
100 strcpy(tmppath, path);
101 f = fopen(tmppath, mode);
102 if (!f && !mkPath(tmppath, 0))
103 f = fopen(tmppath, mode);
104 return f;
105}
106#endif
107
108
109char *
110basename(char *path)
111{
112 char *base;
113
114 base = strrchr(path, PATH_CHAR);
115 if (base)
116 return ++base;
117 return path;
118}
119
120const char *
121getFmt(const char *a)
122{
123 const char *fmt = "%x";
124
125 if (*a == '0')
126 {
127 switch (*++a)
128 {
129 case 'x':
130 fmt = "%x";
131 ++a;
132 break;
133 case 'd':
134 fmt = "%d";
135 ++a;
136 break;
137 default:
138 fmt = "%o";
139 break;
140 }
141 }
142 return fmt;
143}
144
145long
146my_atoi(const char *a)
147{
148 int i = 0;
149 sscanf(a, getFmt(a), &i);
150 return i;
151}
152
153int
154isOffset(const char *a)
155{
156 int i = 0;
157 if (strchr(a, '.'))
158 return 0;
159 return sscanf(a, getFmt(a), &i);
160}
161
162int
163copy_file(char *src, char *dst)
164{
165 char Line[LINESIZE];
166
167 sprintf(Line, CP_FMT, src, dst);
168 l2l_dbg(2, "Executing: %s\n", Line);
169 remove(dst);
170 if (file_exists(dst))
171 {
172 l2l_dbg(0, "Cannot remove dst %s before copy\n", dst);
173 return 1;
174 }
175 if (system(Line) < 0)
176 {
177 l2l_dbg(0, "Cannot copy %s to %s\n", src, dst);
178 l2l_dbg(1, "Failed to execute: '%s'\n", Line);
179 return 2;
180 }
181
182 if (!file_exists(dst))
183 {
184 l2l_dbg(0, "Dst %s does not exist after copy\n", dst);
185 return 2;
186 }
187 return 0;
188}
189
190/* EOF */