File: cat_path.c

package info (click to toggle)
autofs 5.1.7-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,836 kB
  • sloc: ansic: 35,461; yacc: 1,811; lex: 1,067; sh: 627; makefile: 553
file content (98 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ----------------------------------------------------------------------- *
 *
 *  cat_path.c - boundary aware buffer management routines
 *
 *   Copyright 2002-2003 Ian Kent <raven@themaw.net> - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

#include <string.h>
#include <limits.h>
#include <ctype.h>
#include "automount.h"
/*
 * sum = "dir/base" with attention to buffer overflows, and multiple
 * slashes at the joint are avoided.
 */
int cat_path(char *buf, size_t len, const char *dir, const char *base)
{
	char *d = (char *) dir;
	char *b = (char *) base;
	char *s = buf;
	size_t left = len;

	if ((*s = *d))
		while ((*++s = *++d) && --left) ;
	
	if (!left) {
		*s = '\0';
		return 0;
	}

	/* Now we have at least 1 left in output buffer */

	while (*--s == '/' && (left++ < len))
		*s = '\0';

	*++s = '/';
	left--;

	if (*b == '/') 
		while (*++b == '/');

	while (--left && (*++s = *b++)) ;

	if (!left) {
		*s = '\0';
		return 0;
	}

	return 1;
}

size_t _strlen(const char *str, size_t max)
{
	const char *s = str;
	size_t len = 0;

	while (*s++ && len < max)
		len++;

	return len;
}

/* 
 * sum = "dir/base" with attention to buffer overflows, and multiple
 * slashes at the joint are avoided.  The length of base is specified
 * explicitly.
 */
int ncat_path(char *buf, size_t len,
	      const char *dir, const char *base, size_t blen)
{
	char name[PATH_MAX+1];
	size_t alen = _strlen(base, blen);

	if (blen > PATH_MAX || !alen)
		return 0;
	
	strncpy(name, base, alen);
	name[alen] = '\0';

	return cat_path(buf, len, dir, name);
}

/* Compare first n bytes of s1 and s2 and that n == strlen(s1) */
int _strncmp(const char *s1, const char *s2, size_t n)
{
	size_t len = strlen(s1);

	if (n && n != len)
		return n - len;
	return strncmp(s1, s2, n);
}