MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
string.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stddef.h>
5 #include <stdarg.h>
6 
7 #include <sys/stat.h>
8 #include <unistd.h>
9 
10 #include "string.h"
11 #include "mymalloc.h"
12 
13 char *
14 fastpm_file_get_content(const char * filename)
15 {
16  FILE * fp = fopen(filename, "r");
17  if(!fp) return NULL;
18 
19  fseek(fp, 0, SEEK_END);
20  size_t file_length = ftell(fp);
21 
22  char * buf = ta_malloc2(filename, char, file_length + 1);
23  fseek(fp, 0, SEEK_SET);
24  file_length = fread(buf, 1, file_length, fp);
25  fclose(fp);
26  buf[file_length] = 0;
27  return buf;
28 }
29 
30 char *
31 fastpm_strdup(const char * str)
32 {
33  size_t N = strlen(str);
34  char * d = ta_malloc("strdup", char, N + 1);
35  strcpy(d, str);
36  d[N] = '\0';
37  return d;
38 }
39 
40 char *
41 fastpm_strdup_printf(const char * fmt, ...)
42 {
43  va_list va;
44  va_start(va, fmt);
45  char * buf = fastpm_strdup_vprintf(fmt, va);
46  va_end(va);
47  return buf;
48 }
49 
50 char *
51 fastpm_strdup_vprintf(const char * fmt, va_list va)
52 {
53  va_list va2;
54  va_copy(va2, va);
55  /* This relies on a good LIBC vsprintf that returns the number of char */
56  char buf0[128];
57  size_t N = vsnprintf(buf0, 1, fmt, va);
58 
59  char * buf = ta_malloc("strdup_vprintf", char, N + 100);
60  vsnprintf(buf, N + 1, fmt, va2);
61  buf[N + 1] = 0;
62  va_end(va2);
63  return buf;
64 }
65 
66 static void
67 _mkdir(const char *dir);
68 
69 void
70 fastpm_path_ensure_dirname(const char * path)
71 {
72  int i = strlen(path);
73  char * dup = ta_malloc("dirname", char, strlen(path) + 1);
74  strcpy(dup, path);
75  dup[strlen(path)]='\0';
76  char * p;
77  for(p = i + dup; p >= dup && *p != '/'; p --) {
78  continue;
79  }
80  /* plain file name in current directory */
81  if(p < dup) return;
82 
83  /* p == '/', so set it to NULL, dup is the dirname */
84  *p = 0;
85  _mkdir(dup);
86  myfree(dup);
87 }
88 
89 static void
90 _mkdir(const char *dir)
91 {
92  char * tmp= ta_malloc("dirname", char, strlen(dir) + 1);
93  strcpy(tmp, dir);
94  tmp[strlen(dir)]='\0';
95  char *p = NULL;
96  size_t len;
97 
98  len = strlen(tmp);
99  if(tmp[len - 1] == '/')
100  tmp[len - 1] = 0;
101  for(p = tmp + 1; *p; p++)
102  if(*p == '/') {
103  *p = 0;
104  mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
105  *p = '/';
106  }
107  mkdir(tmp, S_IRWXU | S_IRWXG | S_IRWXO);
108  myfree(tmp);
109 }
110 
111 
#define ta_malloc2(name, type, nele)
Definition: mymalloc.h:26
#define ta_malloc(name, type, nele)
Definition: mymalloc.h:25
#define myfree(x)
Definition: mymalloc.h:19
static void _mkdir(const char *dir)
Definition: string.c:90
void fastpm_path_ensure_dirname(const char *path)
Definition: string.c:70
char * fastpm_file_get_content(const char *filename)
Definition: string.c:14
char * fastpm_strdup_printf(const char *fmt,...)
Definition: string.c:41
char * fastpm_strdup(const char *str)
Definition: string.c:31
char * fastpm_strdup_vprintf(const char *fmt, va_list va)
Definition: string.c:51