Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/cvsfs/str.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include <fcall.h>
#include <thread.h>
#include <9p.h>

typedef struct Heap Heap;
struct Heap {
	Heap *l, *r;
	char *s;
};

static Heap *
heap(Heap *h, char **s)
{
	int d;

	if (! h){
		h = emalloc9p(sizeof(Heap)+strlen(*s)+1);
		h->l = h->r = nil;
		h->s = ((char *)h)+sizeof(Heap);
		strcpy(h->s, *s);
		*s = h->s;
	}
	else
	if ((d = strcmp(*s, h->s)) == 0)
		*s = h->s;
	else
	if (d < 0)
		h->l = heap(h->l, s);
	else
		h->r = heap(h->r, s);
	return h;
}


char *
strheap(char *s)
{
	static Heap *root = nil;
	root = heap(root, &s);
	return s;
}

char *
strgrow(char *buf, char *p)
{
	int l;

	l = strlen(p);
	if (buf)
		l += strlen(buf);
	buf = erealloc9p(buf, l+2);
	strcpy(buf, p);
	strcat(buf, "\n");
	return buf;
}

char *
strpfx(char *s, char *t)
{
	int n = strlen(t);
	if (strncmp(s, t, n) == 0)
		return s+n;
	return nil;
}

long
str2date(char *str)
{
	Tm tm;
	int n;
	char *a[8];

	/* Beware: dates can be seperated by / or by - depending on server version */

	if ((n = getfields(str, a, nelem(a), 0, " /-:")) < 6)
		return 0;
	tm.year = strtol(a[0], nil, 10) -1900;
	tm.mon = strtol(a[1], nil, 10) -1;
	tm.mday = strtol(a[2], nil, 10);
	tm.hour = strtol(a[3], nil, 10);
	tm.min = strtol(a[4], nil, 10);
	tm.sec = strtol(a[5], nil, 10);

	if (n > 7)
		tm.tzoff = strtol(a[6], nil, 10);
	strcpy(tm.zone, "UTC");	
	return tm2sec(&tm);
}

int
str2mode(char *str)
{
	char *p, *a[8];
	int n, i, m, mode;

	mode = 0;
	n = gettokens(str, a, nelem(a), "=,");
	for (i = 0; i < n -1; i += 2){
		m = 0;
		for (p = a[i+1]; *p; p++){
			switch(*p){
			case 'r':
				m |= DMREAD;
				break;
			case 'w':
				m |= DMWRITE;
				break;
			case 'x':
				m |= DMEXEC;
				break;
			default:
				break;
			}
		}
		switch(*a[i]){
		case 'u':
			mode |= m;
			break;
		case 'g':
			mode |= m << 3;
			break;
		case 'o':
			mode |= m << 6;
			break;
		default:
			break;
		}
	}
	return mode;
}



Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].