Annotation of embedaddon/trafshow/util.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (c) 2004 Rinet Corp., Novosibirsk, Russia
3: *
4: * Redistribution and use in source forms, with and without modification,
5: * are permitted provided that this entire comment appears intact.
6: *
7: * THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
8: */
9:
10: #ifdef HAVE_CONFIG_H
11: #include <config.h>
12: #endif
13:
14: #include <sys/types.h>
15: #include <sys/stat.h>
16: #include <fcntl.h>
17: #include <stdio.h>
18: #include <stdlib.h>
19: #include <string.h>
20: #include <unistd.h>
21:
22: #include "util.h"
23:
24: char *
25: strip_path(path)
26: const char *path;
27: {
28: char *cp;
29: if (path && (cp = strrchr(path, '/')) != 0)
30: return (++cp);
31: return (char *)path;
32: }
33:
34: char *
35: strip_blanks(str)
36: char *str;
37: {
38: char *bp, *ep;
39:
40: if (!str || !*str)
41: return str;
42:
43: bp = str;
44: while (*bp == ' ' || *bp == '\t') bp++;
45:
46: ep = bp + (strlen(bp)-1);
47: while (ep >= bp &&
48: (*ep == ' ' || *ep == '\t' || *ep == '\r' || *ep == '\n'))
49: *ep-- = '\0';
50:
51: return bp;
52: }
53:
54: /*
55: * Copy arg vector into a new buffer, concatenating arguments with spaces.
56: */
57: char *
58: copy_argv(av)
59: char **av;
60: {
61: unsigned int len = 0;
62: char **p, *buf, *src, *dst;
63:
64: p = av;
65: if (*p == 0) return 0;
66: while (*p) len += strlen(*p++) + 1;
67: if ((buf = (char *)malloc(len)) == 0) {
68: perror("malloc");
69: return 0;
70: }
71: p = av;
72: dst = buf;
73: while ((src = *p++) != 0) {
74: while ((*dst++ = *src++) != '\0');
75: dst[-1] = ' ';
76: }
77: dst[-1] = '\0';
78:
79: return buf;
80: }
81:
82: /*
83: * Return size in bytes of regular file `name'.
84: */
85: long
86: fd_size(fd)
87: int fd;
88: {
89: struct stat st;
90: if (fstat(fd, &st) < 0)
91: return -1;
92: return (long)st.st_size;
93: }
94:
95: char *
96: load_file(name)
97: const char *name;
98: {
99: int fd;
100: long sz;
101: char *cp;
102:
103: if ((fd = open(name, O_RDONLY)) < 0) {
104: perror(name);
105: return 0;
106: }
107: if ((sz = fd_size(fd)) < 0) {
108: perror(name);
109: close(fd);
110: return 0;
111: }
112: if ((cp = (char *)malloc(sz + 1)) == 0) {
113: perror(name);
114: close(fd);
115: return 0;
116: }
117: if ((sz = read(fd, cp, sz)) < 0) {
118: perror(name);
119: close(fd);
120: free(cp);
121: return 0;
122: }
123: close(fd);
124: cp[sz] = '\0';
125:
126: return cp;
127: }
128:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>