121e6423017eb694dc90ecbfdf97bdb8fff010bd
[dragonfly.git] / gnu / usr.bin / man / lib / util.c
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 <stdlib.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24
25 #include "gripes.h"
26 #include "util.h"
27
28 extern int debug;
29
30 /*
31  * Extract last element of a name like /foo/bar/baz.
32  */
33 char *
34 mkprogname (char *s)
35 {
36   char *t;
37
38   t = strrchr (s, '/');
39   if (t == (char *)NULL)
40     t = s;
41   else
42     t++;
43
44   return strdup (t);
45 }
46
47 void
48 downcase (unsigned char *s)
49 {
50   unsigned char c;
51   while ((c = *s) != '\0')
52     {
53       if (isalpha (c))
54         *s = tolower (c);
55       s++;
56     }
57 }
58
59 /*
60  * Is file a newer than file b?
61  *
62  * case:
63  *
64  *   a newer than b         returns    1
65  *   a older than b         returns    0
66  *   stat on a fails        returns   -1
67  *   stat on b fails        returns   -2
68  *   stat on a and b fails  returns   -3
69  */
70 int
71 is_newer (char *fa, char *fb)
72 {
73   struct stat fa_sb;
74   struct stat fb_sb;
75   int fa_stat;
76   int fb_stat;
77   int status = 0;
78
79   fa_stat = stat (fa, &fa_sb);
80   if (fa_stat != 0)
81     status = 1;
82
83   fb_stat = stat (fb, &fb_sb);
84   if (fb_stat != 0)
85     status |= 2;
86
87   if (status != 0)
88     return -status;
89
90   return (fa_sb.st_mtime > fb_sb.st_mtime);
91 }
92
93 /*
94  * Is path a directory?
95  */
96 int
97 is_directory (char *path)
98 {
99   struct stat sb;
100   int status;
101
102   status = stat (path, &sb);
103
104   if (status != 0)
105     return -1;
106
107   return ((sb.st_mode & S_IFDIR) == S_IFDIR);
108
109 }
110
111 /*
112  * Attempt a system () call.  Return 1 for success and 0 for failure
113  * (handy for counting successes :-).
114  */
115 int
116 do_system_command (char *command)
117 {
118   int status = 0;
119
120   /*
121    * If we're debugging, don't really execute the command -- you never
122    * know what might be in that mangled string :-O.
123    */
124   if (debug)
125     fprintf (stderr, "\ntrying command: %s\n", command);
126   else
127     status = system (command);
128
129   /* check return value from system() function first */
130   if (status == -1) {
131     fprintf(stderr, 
132             "wait() for exit status of shell failed in function system()\n");
133     return 0;
134   } else if (status == 127 || status == (127 << 8)) {
135     fprintf(stderr, "execution of the shell failed in function system()\n");
136     return 0;
137   }
138
139   if (WIFSIGNALED(status))
140     return -1;
141   else if (WEXITSTATUS(status)) {
142     gripe_system_command (status);
143     return 0;
144   }
145   else
146     return 1;
147 }