source: MondoRescue/branches/stable/mondo/src/lib/mr_str.c@ 1087

Last change on this file since 1087 was 1087, checked in by Bruno Cornec, 17 years ago

WARNING: Not tested yet ! Will be stabilized in next hours.
this patch changes the interface between mindi and mondo.
Everything is now managed through either MONDO_CACHE/mondo-restore.cfg
and MONDO_CACHE/mindi.conf (an additional configuration file)
This removes all the one line files NFS-*, ... that where used
in an anarchic way. Now everything computed by mondo is
under mondo-restore.cfg and everything related to mindi in mindi.conf

Still needed are the removal of the 2 parameters to mindi,
but that will be done after when a clear line is drawn between
where files are put by each tool.

MONDO_TMP and MINDI_TMP should be reserved for temporary files
MONDO_CACHE and MINDI_CACHE for end results and communication
between tools.
These changes were made in order to prepare the USB support in mondo

A first step in the right direction, but still some work to do
before it's working again.
Testers please be warned.

  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1/*
2 * $Id$
3 *
4 * New generation of string handling functions safe from a memory management point of view
5 * Developped by Andree Leidenfrost
6 */
7
8#include <stdio.h>
9#include <string.h>
10#include <time.h>
11
12#include "mr_mem.h"
13
14/**
15 * Safe alternative to standard function strok()
16 * Build a partition name from a drive and a partition number.
17 * @param instr
18 * @param delims
19 * @param lastpos
20 * @return @p
21 * @note this function allocates memory that needs to be freed by caller
22 **/
23char *mr_strtok(char *instr, const char *delims, int *lastpos)
24{
25
26 char *token = NULL;
27 char *strptr = NULL;
28 size_t pos1 = 0;
29 size_t pos2 = 0;
30
31 if (strlen(instr) <= *lastpos) {
32 *lastpos = 0;
33 return token;
34 }
35
36 strptr = instr + *lastpos;
37 pos2 = strspn(strptr, delims);
38 strptr += pos2;
39 pos1 = strcspn(strptr, delims);
40 token = (char *)mr_malloc(sizeof(*token) * (pos1 + 1));
41 strncpy(token, strptr, pos1);
42 token[pos1] = '\0';
43 *lastpos = *lastpos + pos1 + pos2 + 1;
44
45 return token;
46}
47
48
49/**
50 * Returns the string fed to it 'inptr' with all characters to escape given
51 * in 'toesc' prepended by escaping character 'escchr'.
52 * (Prepare strings for use in system() or popen() with this function.)
53 * @param instr
54 * @param toesc
55 * @param escchr
56 * @note this function allocates memory that needs to be freed by caller
57 **/
58char *mr_stresc(char *instr, char *toesc, const char escchr)
59{
60
61 char *inptr = NULL;
62 char *retstr = NULL;
63 char *retptr = NULL;
64 char *escptr = NULL;
65 int cnt = 0;
66
67 inptr = instr;
68
69 // Count how many characters need escaping.
70 while (*inptr != '\0') {
71 escptr = toesc;
72 while (*escptr != '\0') {
73 if (*inptr == *escptr) {
74 // Found it, skip the rest.
75 cnt++;
76 inptr++;
77 break;
78 }
79 inptr++;
80 escptr++;
81 }
82 }
83 inptr = instr;
84
85 retstr = (char *) mr_malloc(strlen(inptr) + cnt + 1);
86 retptr = retstr;
87
88 // Prepend specified characters with escape character.
89 while (*inptr != '\0') {
90 escptr = toesc;
91 while (*escptr != '\0') {
92 if (*inptr == *escptr) {
93 // Found it, skip the rest.
94 *retptr = escchr;
95 retptr++;
96 break;
97 }
98 escptr++;
99 }
100 *retptr = *inptr;
101 retptr++;
102 inptr++;
103 }
104 *retptr = '\0';
105
106 return retstr;
107}
108
109/* Return a string containing the date */
110char *mr_date(void) {
111
112 time_t tcurr;
113
114 tcurr = time(NULL);
115 return(ctime(&tcurr));
116}
117
Note: See TracBrowser for help on using the repository browser.