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