]> andersk Git - moira.git/blame - lib/strs.c
Command line printer manipulation client, and build goo.
[moira.git] / lib / strs.c
CommitLineData
fa59b86f 1/* $Id$
68687902 2 *
7ac48069 3 * Miscellaneous string functions.
68687902 4 *
7ac48069 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>.
68687902 8 */
9
babbc197 10#include <mit-copyright.h>
7ac48069 11#include <moira.h>
0fed7e9b 12
7ac48069 13#include <ctype.h>
68687902 14
7ac48069 15RCSID("$Header$");
5eaef520 16
68687902 17/*
18 * Trim whitespace off both ends of a string.
19 */
44d12d58 20char *strtrim(char *save)
68687902 21{
44d12d58 22 char *t, *s;
5eaef520 23
24 s = save;
25 while (isspace(*s))
26 s++;
27 /* skip to end of string */
28 if (*s == '\0')
29 {
0c99b4ff 30 if (*save)
31 *save = '\0';
5eaef520 32 return save;
9ca8ff13 33 }
68687902 34
5eaef520 35 for (t = s; *t; t++)
36 continue;
37 while (t > s)
38 {
39 --t;
40 if (!isspace(*t))
41 {
42 t++;
43 break;
68687902 44 }
45 }
5eaef520 46 if (*t)
47 *t = '\0';
48 return s;
68687902 49}
77f073b6 50
51
52/* Modify a string for all of the letters to be uppercase. */
53
5eaef520 54char *uppercase(char *s)
77f073b6 55{
44d12d58 56 char *p;
77f073b6 57
5eaef520 58 for (p = s; *p; p++)
59 {
77f073b6 60 if (islower(*p))
61 *p = toupper(*p);
5eaef520 62 }
63 return s;
77f073b6 64}
bc5b5d78 65
66
5eaef520 67char *lowercase(char *s)
bc5b5d78 68{
44d12d58 69 char *p;
bc5b5d78 70
5eaef520 71 for (p = s; *p; p++)
72 {
bc5b5d78 73 if (isupper(*p))
74 *p = tolower(*p);
501a4b9d 75 }
5eaef520 76 return s;
501a4b9d 77}
4dacdfbc 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 */
83size_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 */
103size_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.607141 seconds and 5 git commands to generate.