misc.c

Go to the documentation of this file.
00001 /*
00002  * Miscellaneous routines.
00003  */
00004 
00005 #include "sysincludes.h"
00006 #include "msdos.h"
00007 #include "stream.h"
00008 #include "vfat.h"
00009 #include "mtools.h"
00010 
00011 
00012 void printOom(void)
00013 {
00014         fprintf(stderr, "Out of memory error");
00015 }
00016 
00017 char *get_homedir(void)
00018 {
00019         struct passwd *pw;
00020         uid_t uid;
00021         char *homedir;
00022         char *username;
00023         
00024         homedir = getenv ("HOME");    
00025         /* 
00026          * first we call getlogin. 
00027          * There might be several accounts sharing one uid 
00028          */
00029         if ( homedir )
00030                 return homedir;
00031         
00032         pw = 0;
00033         
00034         username = getenv("LOGNAME");
00035         if ( !username )
00036                 username = getlogin();
00037         if ( username )
00038                 pw = getpwnam( username);
00039   
00040         if ( pw == 0 ){
00041                 /* if we can't getlogin, look up the pwent by uid */
00042                 uid = geteuid();
00043                 pw = getpwuid(uid);
00044         }
00045         
00046         /* we might still get no entry */
00047         if ( pw )
00048                 return pw->pw_dir;
00049         return 0;
00050 }
00051 
00052 
00053 static void get_mcwd_file_name(char *file)
00054 {
00055         char *mcwd_path;
00056         char *homedir;
00057 
00058         mcwd_path = getenv("MCWD");
00059         if (mcwd_path == NULL || *mcwd_path == '\0'){
00060                 homedir= get_homedir();
00061                 if(!homedir)
00062                         homedir="/tmp";
00063                 strncpy(file, homedir, MAXPATHLEN-6);
00064                 file[MAXPATHLEN-6]='\0';
00065                 strcat( file, "/.mcwd");
00066         } else {
00067                 strncpy(file, mcwd_path, MAXPATHLEN);
00068                 file[MAXPATHLEN]='\0';
00069         }
00070 }
00071 
00072 void unlink_mcwd()
00073 {
00074         char file[MAXPATHLEN+1];
00075         get_mcwd_file_name(file);
00076         unlink(file);
00077 }
00078 
00079 FILE *open_mcwd(const char *mode)
00080 {
00081         struct stat sbuf;
00082         char file[MAXPATHLEN+1];
00083         time_t now;
00084         
00085         get_mcwd_file_name(file);
00086         if (*mode == 'r'){
00087                 if (stat(file, &sbuf) < 0)
00088                         return NULL;
00089                 /*
00090                  * Ignore the info, if the file is more than 6 hours old
00091                  */
00092                 getTimeNow(&now);
00093                 if (now - sbuf.st_mtime > 6 * 60 * 60) {
00094                         fprintf(stderr,
00095                                 "Warning: \"%s\" is out of date, removing it\n",
00096                                 file);
00097                         unlink(file);
00098                         return NULL;
00099                 }
00100         }
00101         
00102         return  fopen(file, mode);
00103 }
00104         
00105 
00106 /* Fix the info in the MCWD file to be a proper directory name.
00107  * Always has a leading separator.  Never has a trailing separator
00108  * (unless it is the path itself).  */
00109 
00110 const char *fix_mcwd(char *ans)
00111 {
00112         FILE *fp;
00113         char *s;
00114         char buf[MAX_PATH];
00115 
00116         fp = open_mcwd("r");
00117         if(!fp){
00118                 strcpy(ans, "A:/");
00119                 return ans;
00120         }
00121 
00122         if (!fgets(buf, MAX_PATH, fp))
00123                 return("A:/");
00124 
00125         buf[strlen(buf) -1] = '\0';
00126         fclose(fp);
00127                                         /* drive letter present? */
00128         s = skip_drive(buf);
00129         if (s > buf) {
00130                 strncpy(ans, buf, s - buf);
00131                 ans[s - buf] = '\0';
00132         } else 
00133                 strcpy(ans, "A:");
00134                                         /* add a leading separator */
00135         if (*s != '/' && *s != '\\') {
00136                 strcat(ans, "/");
00137                 strcat(ans, s);
00138         } else
00139                 strcat(ans, s);
00140 
00141 #if 0
00142                                         /* translate to upper case */
00143         for (s = ans; *s; ++s) {
00144                 *s = toupper(*s);
00145                 if (*s == '\\')
00146                         *s = '/';
00147         }
00148 #endif
00149                                         /* if only drive, colon, & separator */
00150         if (strlen(ans) == 3)
00151                 return(ans);
00152                                         /* zap the trailing separator */
00153         if (*--s == '/')
00154                 *s = '\0';
00155         return ans;
00156 }
00157 
00158 void *safe_malloc(size_t size)
00159 {
00160         void *p;
00161 
00162         p = malloc(size);
00163         if(!p){
00164                 printOom();
00165                 exit(1);
00166         }
00167         return p;
00168 }
00169 
00170 void print_sector(char *message, unsigned char *data, int size)
00171 {
00172         int col;
00173         int row;
00174 
00175         printf("%s:\n", message);
00176         
00177         for(row = 0; row * 16 < size; row++){
00178                 printf("%03x  ", row * 16);
00179                 for(col = 0; col < 16; col++)                   
00180                         printf("%02x ", data [row*16+col]);
00181                 for(col = 0; col < 16; col++) {
00182                         if(isprint(data [row*16+col]))
00183                                 printf("%c", data [row*16+col]);
00184                         else
00185                                 printf(".");
00186                 }
00187                 printf("\n");
00188         }
00189 }
00190 
00191 
00192 time_t getTimeNow(time_t *now)
00193 {
00194         static int haveTime = 0;
00195         static time_t sharedNow;
00196 
00197         if(!haveTime) {
00198                 time(&sharedNow);
00199                 haveTime = 1;
00200         }
00201         if(now)
00202                 *now = sharedNow;
00203         return sharedNow;
00204 }
00205 
00206 char *skip_drive(const char *filename)
00207 {
00208         char *p;
00209 
00210         /* Skip drive name.  Return pointer just after the `:', or a pointer
00211          * to the start of the file name if there is is no drive name.
00212          */
00213         p = strchr(filename, ':');
00214         return (p == NULL || p == filename) ? (char *) filename : p + 1;
00215 }
00216 
00217 char *get_drive(const char *filename, const char *def)
00218 {
00219         const char *path;
00220         char *drive;
00221         const char *rest;
00222         size_t len;
00223 
00224         /* Return the drive name part of a full filename. */
00225 
00226         path = filename;
00227         rest = skip_drive(path);
00228         if (rest == path) {
00229                 if (def == NULL) def = "A:";
00230                 path = def;
00231                 rest = skip_drive(path);
00232                 if (rest == path) {
00233                         path = "A:";
00234                         rest = path+2;
00235                 }
00236         }
00237         len = rest - path;
00238         drive = safe_malloc(len * sizeof(drive[0]));
00239         len--;
00240         memcpy(drive, path, len);
00241         drive[len] = 0;
00242         if (len == 1) drive[0] = toupper(drive[0]);
00243         return drive;
00244 }
00245 
00246 #if 0
00247 
00248 #undef free
00249 #undef malloc
00250 
00251 static int total=0;
00252 
00253 void myfree(void *ptr)
00254 {
00255         int *size = ((int *) ptr)-1;
00256         total -= *size;
00257         fprintf(stderr, "freeing %d bytes at %p total alloced=%d\n",
00258                 *size, ptr, total);
00259         free(size);
00260 }
00261 
00262 void *mymalloc(size_t size)
00263 {
00264         int *ptr;
00265         ptr = (int *)malloc(size+sizeof(int));
00266         if(!ptr)
00267                 return 0;
00268         *ptr = size;
00269         ptr++;
00270         total += size;
00271         fprintf(stderr, "allocating %d bytes at %p total allocated=%d\n",
00272                 size, ptr, total);
00273         return (void *) ptr;
00274 }
00275 
00276 void *mycalloc(size_t nmemb, size_t size)
00277 {
00278         void *ptr = mymalloc(nmemb * size);
00279         if(!ptr)
00280                 return 0;
00281         memset(ptr, 0, size);
00282         return ptr;
00283 }
00284 
00285 void *myrealloc(void *ptr, size_t size)
00286 {
00287         int oldsize = ((int *)ptr) [-1];
00288         void *new = mymalloc(size);
00289         if(!new)
00290                 return 0;
00291         memcpy(new, ptr, oldsize);
00292         myfree(ptr);
00293         return new;
00294 }
00295 
00296 char *mystrdup(char *src)
00297 {
00298         char *dest;
00299         dest = mymalloc(strlen(src)+1);
00300         if(!dest)
00301                 return 0;
00302         strcpy(dest, src);
00303         return dest;
00304 }
00305 
00306 
00307 #endif

Generated on Fri Apr 14 22:56:47 2006 for minix by  doxygen 1.4.6