ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

util.c
Go to the documentation of this file.
00001 /*
00002  * ReactOS log2lines
00003  * Written by Jan Roeloffzen
00004  *
00005  * - Misc utils
00006  */
00007 
00008 #include <errno.h>
00009 #include <stdio.h>
00010 #include <string.h>
00011 #include <stdlib.h>
00012 
00013 #include "config.h"
00014 #include "compat.h"
00015 #include "util.h"
00016 #include "options.h"
00017 
00018 int
00019 set_LogFile(FILE **plogFile)
00020 {
00021     if (*opt_logFile)
00022     {
00023         if (*plogFile)
00024             fclose(*plogFile);
00025         *plogFile = NULL;
00026 
00027         if (strcmp(opt_logFile,"none") == 0)
00028             return 0; //just close
00029 
00030         *plogFile = fopen(opt_logFile, opt_mod ? opt_mod : "a");
00031         if (*plogFile)
00032         {
00033             // disable buffering so fflush is not needed
00034             if (!opt_buffered)
00035             {
00036                 l2l_dbg(1, "Disabling log buffering on %s\n", opt_logFile);
00037                 setbuf(*plogFile, NULL);
00038             }
00039             else
00040                 l2l_dbg(1, "Enabling log buffering on %s\n", opt_logFile);
00041         }
00042         else
00043         {
00044             l2l_dbg(0, "Could not open logfile %s (%s)\n", opt_logFile, strerror(errno));
00045             return 2;
00046         }
00047     }
00048     return 0;
00049 }
00050 
00051 int
00052 file_exists(char *name)
00053 {
00054     FILE *f;
00055 
00056     f = fopen(name, "r");
00057     if (!f)
00058         return 0;
00059     fclose(f);
00060     return 1;
00061 }
00062 
00063 /* Do this in reverse (recursively)
00064    This saves many system calls if the path is likely
00065    to already exist (creating large trees).
00066 */
00067 int
00068 mkPath(char *path, int isDir)
00069 {
00070     char *s;
00071     int res = 0;
00072 
00073     if (isDir)
00074     {
00075         res = MKDIR(path);
00076         if (!res || (res == -1 && errno == EEXIST))
00077             return 0;
00078     }
00079     // create parent dir
00080     if ((s = strrchr(path, PATH_CHAR)))
00081     {
00082         *s = '\0';
00083         res = mkPath(path, 1);
00084         *s = PATH_CHAR;
00085     }
00086 
00087     if (!res && isDir)
00088         res = MKDIR(path);
00089 
00090     return res;
00091 }
00092 
00093 #if 0
00094 static FILE *
00095 rfopen(char *path, char *mode)
00096 {
00097     FILE *f = NULL;
00098     char tmppath[MAX_PATH]; // Don't modify const strings
00099 
00100     strcpy(tmppath, path);
00101     f = fopen(tmppath, mode);
00102     if (!f && !mkPath(tmppath, 0))
00103         f = fopen(tmppath, mode);
00104     return f;
00105 }
00106 #endif
00107 
00108 
00109 char *
00110 basename(char *path)
00111 {
00112     char *base;
00113 
00114     base = strrchr(path, PATH_CHAR);
00115     if (base)
00116         return ++base;
00117     return path;
00118 }
00119 
00120 const char *
00121 getFmt(const char *a)
00122 {
00123     const char *fmt = "%x";
00124 
00125     if (*a == '0')
00126     {
00127         switch (*++a)
00128         {
00129         case 'x':
00130             fmt = "%x";
00131             ++a;
00132             break;
00133         case 'd':
00134             fmt = "%d";
00135             ++a;
00136             break;
00137         default:
00138             fmt = "%o";
00139             break;
00140         }
00141     }
00142     return fmt;
00143 }
00144 
00145 long
00146 my_atoi(const char *a)
00147 {
00148     int i = 0;
00149     sscanf(a, getFmt(a), &i);
00150     return i;
00151 }
00152 
00153 int
00154 isOffset(const char *a)
00155 {
00156     int i = 0;
00157     if (strchr(a, '.'))
00158         return 0;
00159     return sscanf(a, getFmt(a), &i);
00160 }
00161 
00162 int
00163 copy_file(char *src, char *dst)
00164 {
00165     char Line[LINESIZE];
00166 
00167     sprintf(Line, CP_FMT, src, dst);
00168     l2l_dbg(2, "Executing: %s\n", Line);
00169     remove(dst);
00170     if (file_exists(dst))
00171     {
00172         l2l_dbg(0, "Cannot remove dst %s before copy\n", dst);
00173         return 1;
00174     }
00175     if (system(Line) < 0)
00176     {
00177         l2l_dbg(0, "Cannot copy %s to %s\n", src, dst);
00178         l2l_dbg(1, "Failed to execute: '%s'\n", Line);
00179         return 2;
00180     }
00181 
00182     if (!file_exists(dst))
00183     {
00184         l2l_dbg(0, "Dst %s does not exist after copy\n", dst);
00185         return 2;
00186     }
00187     return 0;
00188 }
00189 
00190 /* EOF */

Generated on Sat May 26 2012 04:20:43 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.