home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / OS2 / MAN11A.ZIP / src / util.c < prev    next >
C/C++ Source or Header  |  1991-08-26  |  3KB  |  151 lines

  1. /*
  2.  * util.c
  3.  *
  4.  * Copyright (c) 1990, 1991, John W. Eaton.
  5.  *
  6.  * You may distribute under the terms of the GNU General Public
  7.  * License as specified in the file COPYING that comes with the man
  8.  * distribution.  
  9.  *
  10.  * John W. Eaton
  11.  * jwe@che.utexas.edu
  12.  * Department of Chemical Engineering
  13.  * The University of Texas at Austin
  14.  * Austin, Texas  78712
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22.  
  23. #ifdef STDC_HEADERS
  24. #include <stdlib.h>
  25. #else
  26. extern int fprintf ();
  27. extern int tolower ();
  28. #endif
  29.  
  30. extern char *strdup ();
  31. extern int system ();
  32.  
  33. #include "gripes.h"
  34.  
  35. /*
  36.  * Extract last element of a name like /foo/bar/baz.
  37.  */
  38. char *
  39. mkprogname (s)
  40.      register char *s;
  41. {
  42.   char *t;
  43.  
  44.   t = strrchr (s, '/');
  45.   if (t == (char *)NULL)
  46.     t = s;
  47.   else
  48.     t++;
  49.  
  50.   return strdup (t);
  51. }
  52.  
  53. void
  54. downcase (s)
  55.      char *s;
  56. {
  57.   register char c;
  58.   while ((c = *s) != '\0')
  59.     {
  60.       if (isalpha (c))
  61.     *s++ = tolower (c);
  62.     }
  63. }
  64.  
  65. /*
  66.  * Is file a newer than file b?
  67.  *
  68.  * case:
  69.  *
  70.  *   a newer than b         returns    1
  71.  *   a older than b         returns    0
  72.  *   stat on a fails        returns   -1
  73.  *   stat on b fails        returns   -2
  74.  *   stat on a and b fails  returns   -3
  75.  */
  76. int
  77. is_newer (fa, fb)
  78.   register char *fa;
  79.   register char *fb;
  80. {
  81.   struct stat fa_sb;
  82.   struct stat fb_sb;
  83.   register int fa_stat;
  84.   register int fb_stat;
  85.   register int status = 0;
  86.  
  87.   fa_stat = stat (fa, &fa_sb);
  88.   if (fa_stat != 0)
  89.     status = 1;
  90.  
  91.   fb_stat = stat (fb, &fb_sb);
  92.   if (fb_stat != 0)
  93.     status |= 2;
  94.  
  95.   if (status != 0)
  96.     return -status;
  97.  
  98.   return (fa_sb.st_mtime > fb_sb.st_mtime);
  99. }
  100.  
  101. /*
  102.  * Is path a directory?
  103.  */
  104. int
  105. is_directory (path)
  106.      char *path;
  107. {
  108.   struct stat sb;
  109.   register int status;
  110.  
  111.   status = stat (path, &sb);
  112.  
  113.   if (status != 0)
  114.     return -1;
  115.  
  116.   return ((sb.st_mode & S_IFDIR) == S_IFDIR);
  117.  
  118. }
  119.  
  120. /*
  121.  * Attempt a system () call.  Return 1 for success and 0 for failure
  122.  * (handy for counting successes :-).
  123.  */
  124. int
  125. do_system_command (command)
  126.      char *command;
  127. {
  128.   int status = 0;
  129.   extern int debug;
  130.  
  131.   /*
  132.    * If we're debugging, don't really execute the command -- you never
  133.    * know what might be in that mangled string :-O.
  134.    */
  135.   if (debug)
  136.     fprintf (stderr, "\ntrying command: %s\n", command);
  137.   else
  138.     status = system (command);
  139.  
  140.   /*
  141.    * Ultrix returns 127 for failure.  Is this normal?
  142.    */
  143.   if (status == 127)
  144.     {
  145.       gripe_system_command (status);
  146.       return 0;
  147.     }
  148.   else
  149.     return 1;
  150. }
  151.