]> andersk Git - moira.git/blob - lib/strs.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / strs.c
1 /* $Id$
2  *
3  * Miscellaneous string functions.
4  *
5  * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6  * For copying and distribution information, please see the file
7  * <mit-copyright.h>.
8  */
9
10 #include <mit-copyright.h>
11 #include <moira.h>
12
13 #include <ctype.h>
14
15 RCSID("$Header$");
16
17 /*
18  * Trim whitespace off both ends of a string.
19  */
20 char *strtrim(char *save)
21 {
22   char *t, *s;
23
24   s = save;
25   while (isspace(*s))
26     s++;
27   /* skip to end of string */
28   if (*s == '\0')
29     {
30       if (*save)
31         *save = '\0';
32       return save;
33     }
34
35   for (t = s; *t; t++)
36     continue;
37   while (t > s)
38     {
39       --t;
40       if (!isspace(*t))
41         {
42           t++;
43           break;
44         }
45     }
46   if (*t)
47     *t = '\0';
48   return s;
49 }
50
51
52 /* Modify a string for all of the letters to be uppercase. */
53
54 char *uppercase(char *s)
55 {
56   char *p;
57
58   for (p = s; *p; p++)
59     {
60       if (islower(*p))
61         *p = toupper(*p);
62     }
63   return s;
64 }
65
66
67 char *lowercase(char *s)
68 {
69   char *p;
70
71   for (p = s; *p; p++)
72     {
73       if (isupper(*p))
74         *p = tolower(*p);
75     }
76   return s;
77 }
78
79 #ifndef HAVE_STRLCPY
80 /* Copy as much of SRC will fit into a DST of size SIZE, always
81  * NUL-terminating. (Originally from OpenBSD.)
82  */
83 size_t strlcpy(char *dst, const char *src, size_t size)
84 {
85   size_t len = strlen(src);
86
87   if (len < size)
88     memcpy(dst, src, len + 1);
89   else
90     {
91       memcpy(dst, src, size - 1);
92       dst[size - 1] = '\0';
93     }
94   return len;
95 }
96 #endif
97
98 #ifndef HAVE_STRLCAT
99 /* Catenate as must of SRC will fit onto the end of DST, which is
100  * in a buffer of size SIZE, always NUL-terminating. (Originally
101  * from OpenBSD.)
102  */
103 size_t strlcat(char *dst, const char *src, size_t size)
104 {
105   size_t dlen = strlen(dst);
106   size_t slen = strlen(src);
107
108   if (dlen + slen < size)
109     memcpy(dst + dlen, src, slen + 1);
110   else
111     {
112       memcpy(dst + dlen, src, size - dlen - 1);
113       dst[size - 1] = '\0';
114     }
115   return dlen + slen;
116 }
117 #endif
This page took 0.053875 seconds and 5 git commands to generate.