home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / misc.c < prev    next >
Text File  |  1997-11-13  |  3KB  |  200 lines

  1. /*
  2.  * Miscellaneous routines.
  3.  */
  4.  
  5. #include "sysincludes.h"
  6. #include "msdos.h"
  7. #include "stream.h"
  8. #include "vfat.h"
  9. #include "mtools.h"
  10.  
  11. char *get_homedir(void)
  12. {
  13.     struct passwd *pw;
  14. #ifdef _OSK
  15. unsigned
  16. #endif
  17.     int uid;
  18.     char *homedir;
  19.     char *username;
  20.     
  21.     homedir = getenv ("HOME");    
  22.     /* 
  23.      * first we call getlogin. 
  24.      * There might be several accounts sharing one uid 
  25.      */
  26.     if ( homedir )
  27.         return homedir;
  28.     
  29. #ifndef _OSK
  30.     pw = 0;
  31.     
  32.     username = getenv("LOGNAME");
  33.     if ( !username )
  34.         username = getlogin();
  35.     if ( username )
  36.         pw = getpwnam( username);
  37.   
  38.     if ( pw == 0 ){
  39.         /* if we can't getlogin, look up the pwent by uid */
  40.         uid = geteuid();
  41.         pw = getpwuid(uid);
  42.     }
  43.     
  44.     /* we might still get no entry */
  45.     if ( pw )
  46.         return pw->pw_dir;
  47. #endif
  48.     return 0;
  49. }
  50.  
  51. FILE *open_mcwd(const char *mode)
  52. {
  53.     struct stat sbuf;
  54.     char file[MAXPATHLEN+EXPAND_BUF];
  55.     time_t now;
  56.     char *mcwd_path;
  57.     char *homedir;
  58.  
  59.     mcwd_path = getenv("MCWD");
  60.     if (mcwd_path == NULL || *mcwd_path == '\0'){
  61.         homedir= get_homedir();
  62.         if ( homedir ){
  63.             strncpy(file, homedir, MAXPATHLEN);
  64.             file[MAXPATHLEN]='\0';
  65.             strcat( file, "/.mcwd");
  66.         } else
  67.             strcpy(file,"/tmp/.mcwd");
  68.     } else
  69.         expand(mcwd_path,file);
  70.  
  71.     if (*mode == 'r'){
  72.         if (stat(file, &sbuf) < 0)
  73.             return NULL;
  74.         /*
  75.          * Ignore the info, if the file is more than 6 hours old
  76.          */
  77.         time(&now);
  78.         if (now - sbuf.st_mtime > 6 * 60 * 60) {
  79.             fprintf(stderr,
  80.                 "Warning: \"%s\" is out of date, removing it\n",
  81.                 file);
  82.             unlink(file);
  83.             return NULL;
  84.         }
  85.     }
  86.     
  87.     return  fopen(file, mode);
  88. }
  89.     
  90.  
  91. /* Fix the info in the MCWD file to be a proper directory name.
  92.  * Always has a leading separator.  Never has a trailing separator
  93.  * (unless it is the path itself).  */
  94.  
  95. const char *fix_mcwd(char *ans)
  96. {
  97.     FILE *fp;
  98.     char *s;
  99.     char buf[BUFSIZ];
  100.  
  101.     fp = open_mcwd("r");
  102.     if(!fp){
  103.         strcpy(ans, "A:/");
  104.         return ans;
  105.     }
  106.  
  107.     if (!fgets(buf, BUFSIZ, fp))
  108.         return("A:/");
  109.  
  110.     buf[strlen(buf) -1] = '\0';
  111.     fclose(fp);
  112.                     /* drive letter present? */
  113.     s = buf;
  114.     if (buf[0] && buf[1] == ':') {
  115.         strncpy(ans, buf, 2);
  116.         ans[2] = '\0';
  117.         s = &buf[2];
  118.     } else 
  119.         strcpy(ans, "A:");
  120.                     /* add a leading separator */
  121.     if (*s != '/' && *s != '\\') {
  122.         strcat(ans, "/");
  123.         strcat(ans, s);
  124.     } else
  125.         strcat(ans, s);
  126.  
  127. #if 0
  128.                     /* translate to upper case */
  129.     for (s = ans; *s; ++s) {
  130.         *s = toupper(*s);
  131.         if (*s == '\\')
  132.             *s = '/';
  133.     }
  134. #endif
  135.                     /* if only drive, colon, & separator */
  136.     if (strlen(ans) == 3)
  137.         return(ans);
  138.                     /* zap the trailing separator */
  139.     if (*--s == '/')
  140.         *s = '\0';
  141.     return ans;
  142. }
  143.  
  144. size_t maximize(size_t *target, long max)
  145. {
  146.     if(max < 0)
  147.         max = 0;
  148.     if (*target > max)
  149.         *target = max;
  150.     return *target;
  151. }
  152.  
  153. size_t minimize(size_t *target, long min)
  154. {
  155.     if (*target < min)
  156.         *target = min;
  157.     return *target;
  158. }
  159.  
  160.  
  161. void *safe_malloc(size_t size)
  162. {
  163.     void *p;
  164.  
  165.     p = malloc(size);
  166.     if(!p){
  167.         fprintf(stderr,"Out of memory error\n");
  168.         exit(1);
  169.     }
  170.     return p;
  171. }
  172.  
  173. void print_sector(char *message, unsigned char *data)
  174. {
  175.     int col;
  176.     int row;
  177.  
  178.     printf("%s:\n", message);
  179.     
  180.     for(row = 0; row < 32; row++){
  181.         printf("%03x  ", row * 16);
  182.         for(col = 0; col < 16; col++)
  183.             printf("%02x ", data [row*16+col]);
  184.         for(col = 0; col < 16; col++) {
  185.             if(isprint(data [row*16+col]))
  186.                 printf("%c", data [row*16+col]);
  187.             else
  188.                 printf(".");
  189.         }
  190.         printf("\n");
  191.     }
  192. }
  193.  
  194. #ifdef _OSK
  195. void cleanup_and_exit(int code)
  196.    exit(code);
  197. }
  198. #endif
  199.