ReactOS 0.4.16-dev-338-g34e76ad
util.c
Go to the documentation of this file.
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
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 *
111{
112 char *base;
113
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
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 */
#define EEXIST
Definition: acclib.h:88
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define PATH_MAX
Definition: types.h:280
#define LINESIZE
Definition: chargen.c:18
#define NULL
Definition: types.h:112
GLdouble s
Definition: gl.h:2039
GLuint res
Definition: glext.h:9613
GLenum src
Definition: glext.h:6340
GLfloat f
Definition: glext.h:7540
GLenum mode
Definition: glext.h:6217
GLenum GLenum dst
Definition: glext.h:6340
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_CRTIMP void __cdecl setbuf(_Inout_ FILE *_File, _Inout_updates_opt_(BUFSIZ) _Post_readable_size_(0) char *_Buffer)
_Check_return_ _CRTIMP int __cdecl sscanf(_In_z_ const char *_Src, _In_z_ _Scanf_format_string_ const char *_Format,...)
#define f
Definition: ke_i.h:83
#define a
Definition: ke_i.h:78
#define sprintf(buf, format,...)
Definition: sprintf.c:55
const char * strerror(int err)
Definition: compat_str.c:23
int remove
Definition: msacm.c:1366
#define PATH_CHAR
Definition: rmkdir.c:17
#define errno
Definition: errno.h:18
int __cdecl system(_In_opt_z_ const char *_Command)
_CRT_RESTORE_GCC_WARNINGS _CRT_DISABLE_GCC_WARNINGS _Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
#define MKDIR(d)
Definition: compat.h:27
#define CP_FMT
Definition: config.h:16
char * opt_mod
Definition: options.c:42
int opt_buffered
Definition: options.c:21
char opt_logFile[PATH_MAX]
Definition: options.c:41
int mkPath(char *path, int isDir)
Definition: util.c:68
const char * getFmt(const char *a)
Definition: util.c:121
int set_LogFile(FILE **plogFile)
Definition: util.c:19
char * basename(char *path)
Definition: util.c:110
int isOffset(const char *a)
Definition: util.c:154
int file_exists(char *name)
Definition: util.c:52
int copy_file(char *src, char *dst)
Definition: util.c:163
long my_atoi(const char *a)
Definition: util.c:146
#define l2l_dbg(level,...)
Definition: util.h:35
Definition: ncftp.h:79
Definition: dsound.c:943
Definition: name.c:39