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