I have such string (it's a string that represents a system path actually):
./home/user/dir1/dir2/
And now, I would like to be able to create presented directory tree, so I need to create home, user, dir1, dir2.
I know how to create dirs in C in Linux, but have troubles with chopping off the string. Basically, what I need now is to have an array of strings:
tmp[0] = "./home/";
tmp[1] = "./home/user/";
tmp[2] = "./home/user/dir1/";
tmp[3] = "./home/user/dir1/dir2/";
and if I will hav such array it would be pretty easy to make presented dir tree but how to split the string in that way?
This is a bit naive but it should get you started. It will handle paths that may or may not have a trailing / and also escaped paths, such as ./home/some\/user/dir1/dir2
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char path[] = "./home/user/dir1/dir2";
char *start = path;
char *end = start + strlen(path);
while (start < end) {
char *slash = strchr(start, '/');
if (slash) {
if (slash > path && *(slash - 1) == '\\') {
start = slash + 1;
continue;
}
*slash = 0;
}
if (strcmp(start, ".") != 0) {
/* Use 'path' for mkdir here */
/* mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) or whatever */
printf("going to make %s\n", path);
}
if (!slash) {
break;
}
*slash = '/';
start = slash + 1;
}
return 0;
}
I'd go for Sean's advice as "You should just exec() mkdir -p ... and save yourself the headache."
However if C is necessary, there you go:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUF_SIZE 100
int main(){
char s[] = "./home/user/dir1/dir2/";
char** tmp;
int i, j;
int size = 0;
char* token;
/* count the '/' characters */
char c = s[0];
int count = 0;
i = 0;
while (c != '\0'){
if (s[i] == '/')
count++;
i++;
c = s[i];
}
size = i;
/* ready the tmp array */
tmp = (char**)malloc(count);
for (i = 0; i < count; i++){
tmp[i] = (char*)malloc(BUF_SIZE);
for (j = 0; j < BUF_SIZE; ++j)
tmp[i][j] = '\0';
}
/* special treatment for our first tmp[] */
tmp[0][0] = '.';
tmp[0][1] = '/';
i = 0;
/* tokenize the main string */
token = strtok(s, "./");
while (token != NULL){
if (i > 0)
strcat(tmp[i], tmp[i - 1]);
strcat(tmp[i], token);
strcat(tmp[i], "/");
printf("%s\n", tmp[i]);
token = strtok(NULL, "/");
i++;
}
/* clean up */
for (i = 0; i < count; i++)
free(tmp[i]);
getchar();
return 0;
}
The output is:
./home/
./home/user/
./home/user/dir1/
./home/user/dir1/dir2/
I would use strtok to parse the directory names from the string using "/" as the delimiter.
see: http://www.cplusplus.com/reference/cstring/strtok/
Heres how I did it:
#include <stdio.h>
#include <string.h>
// Count the number of times the character appears in the string
size_t countInstances(char* str, char token) {
size_t count = 0;
while(*str) {
if(*str == token) {
count++;
}
str++;
}
return count;
}
int main() {
char path[] = "./home/user/dir1/dir2/"; // strtok might segfault if this is a pointer (rather than an array)
size_t count = countInstances(path, '/');
char** dirs = malloc(sizeof(*dirs) * count);
char* dir;
size_t i = 0;
dir = strtok(path, "/");
while(dir && i < count) {
dirs[i] = dir; // store reference
printf("%s\n",dir);
dir = strtok (NULL, "/");
i++;
}
free(dirs);
return 0;
}
output is:
.
home
user
dir1
dir2
Related
I am trying to write my own Shell in C. I have a problem. I wrote my own _strtok function that uses strtok but returns all the tokens as an array of strings. For testing I use the string "ls -laR" defined in the main function. I get the valgrind error "Invalid write of size 8" when trying to malloc the number of chars in the second pointer in the array of strings named "Doubl". Why is it doing this? I am allocating the proper number of pointers to strings in the doubl array. Any insight or help would be appreciated
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
char **_strtok(char *str, char *delim)
{
char **doubl;
char *s = str;
char *string;
int i = 0;
while (*s)
{
if (*s == *delim)
i++;
s++;
}
doubl = malloc(sizeof(char *) * i + 1);
i = 0;
string = strtok(str, delim);
while (1)
{
doubl[i] = malloc(sizeof(char) * strlen(string) + 1);
strcpy(doubl[i], string);
i++;
if (string == NULL)
break;
string = strtok(NULL, delim);
}
return (doubl);
}
char *get_path(char **env)
{
char **check = env;
char *path = NULL;
char pth[] = "PATH";
int i, j, stop = 0;
for (i = 0; check[i] && stop == 0; i++)
{
for (j = 0; j < 4 && stop == 0; j++)
{
if (check[i][j] != pth[j])
break;
if (check[i][j] == pth[j] && j == 3)
{
path = malloc(strlen(check[i]));
strcpy(path, check[i]);
stop = 1;
}
}
}
return (path);
}
char **cmd_to_arg(char **cmd, char **env)
{
/* FREE PATH BEFORE END */
char *path = get_path(env);
char *slash = "/";
char **args = NULL, **check = _strtok(path, ":"), **checkStart = check, **cmdStart = cmd;
int status = -1, i = 0, j;
while (*checkStart)
{
strcat(*checkStart, slash);
strcat(*checkStart, cmd[0]);
status = access(*checkStart, F_OK | X_OK);
printf("%s\n", *checkStart);
if (status == 0)
break;
checkStart++;
}
for(;*cmdStart; i++, cmdStart++)
printf("%d\n", i);
args = malloc(sizeof(char *) * i);
args[0] = malloc(strlen(*checkStart));
strcpy(args[0], *checkStart);
puts(args[0]);
for (j = 1; j < i && cmd[j] != NULL; j++)
{
//printf("%d\n", j);
args[j] = malloc(strlen(cmd[j]) * sizeof(char));
strcpy(args[j], cmd[j]);
puts(args[j]);
}
return (args);
}
int main(int ac, char **av, char **env)
{
(void)ac, (void)av, (void)env;
char line[] = "ls laR";
//size_t size = 0;
char **cmd; //**cmdStart;
//int i = 0, j = 0;
cmd = _strtok(line, " ");
cmd = cmd_to_arg(cmd, env);
return (0);
}
I have a variable length string that I am trying to divide from plus signs and study on:
char string[] = "var1+vari2+varia3";
for (int i = 0; i != sizeof(string); i++) {
memcpy(buf, string[0], 4);
buf[9] = '\0';
}
since variables are different in size I am trying to write something that is going to take string into loop and extract (divide) variables. Any suggestions ? I am expecting result such as:
var1
vari2
varia3
You can use strtok() to break the string by delimiter
char string[]="var1+vari2+varia3";
const char delim[] = "+";
char *token;
/* get the first token */
token = strtok(string, delim);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, delim);
}
More info about the strtok() here: https://man7.org/linux/man-pages/man3/strtok.3.html
It seems to me that you don't just want to want to print the individual strings but want to save the individual strings in some buffer.
Since you can't know the number of strings nor the length of the individual string, you should allocate memory dynamic, i.e. use functions like realloc, calloc and malloc.
It can be implemented in several ways. Below is one example. To keep the example simple, it's not performance optimized in anyway.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char** split_string(const char* string, const char* token, int* num)
{
assert(string != NULL);
assert(token != NULL);
assert(num != NULL);
assert(strlen(token) != 0);
char** data = NULL;
int num_strings = 0;
while(*string)
{
// Allocate memory for one more string pointer
char** ptemp = realloc(data, (num_strings + 1) * sizeof *data);
if (ptemp == NULL) exit(1);
data = ptemp;
// Look for token
char* tmp = strstr(string, token);
if (tmp == NULL)
{
// Last string
// Allocate memory for one more string and copy it
int len = strlen(string);
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
++num_strings;
break;
}
// Allocate memory for one more string and copy it
int len = tmp - string;
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
// Prepare to search for next string
++num_strings;
string = tmp + strlen(token);
}
*num = num_strings;
return data;
}
int main()
{
char string[]="var1+vari2+varia3";
// Split the string into dynamic allocated memory
int num_strings;
char** data = split_string(string, "+", &num_strings);
// Now data can be used as an array-of-strings
// Example: Print the strings
printf("Found %d strings:\n", num_strings);
for(int i = 0; i < num_strings; ++i) printf("%s\n", data[i]);
// Free the memory
for(int i = 0; i < num_strings; ++i) free(data[i]);
free(data);
}
Output
Found 3 strings:
var1
vari2
varia3
You can use a simple loop scanning the string for + signs:
char string[] = "var1+vari2+varia3";
char buf[sizeof(string)];
int start = 0;
for (int i = 0;;) {
if (string[i] == '+' || string[i] == '\0') {
memcpy(buf, string + start, i - start);
buf[i - start] = '\0';
// buf contains the substring, use it as a C string
printf("%s\n", buf);
if (string[i] == '\0')
break;
start = ++i;
} else {
i++;
}
}
Your code does not have any sense.
I wrote such a function for you. Analyse it as sometimes is good to have some code as a base
char *substr(const char *str, char *buff, const size_t start, const size_t len)
{
size_t srcLen;
char *result = buff;
if(str && buff)
{
if(*str)
{
srcLen = strlen(str);
if(srcLen < start + len)
{
if(start < srcLen) strcpy(buff, str + start);
else buff[0] = 0;
}
else
{
memcpy(buff, str + start, len);
buff[len] = 0;
}
}
else
{
buff[0] = 0;
}
}
return result;
}
https://godbolt.org/z/GjMEqx
i want to reverse the words of string with dots in between them in c.
i tried using for loops in c.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
scanf("%s",str);
printf("%s",str);
int length = strlen(str);
// Traverse string from end
int i;
for (i = length - 1; i >= 0; i--) {
if (str[i] == '.') {
// putting the NULL character at the
// position of space characters for
// next iteration.
str[i] = '.';
// Start from next charatcer
printf("%s ", &(str[i]) + 1);
}
}
// printing the last word
printf("%s", str);
return 0;
}
Example input
i.love.you
Example output
you.love.i
This is how I would do it (omitting error checking):
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char str[100];
char *c;
scanf("%s", str);
while ((c = strrchr(str, '.'))) {
*c = 0;
printf("%s.", c + 1);
}
printf("%s\n", str);
return 0;
}
Output:
$ ./a.out
this.is.a.test.for.stackoverflow
stackoverflow.for.test.a.is.this
Well, there's two glaring errors:
You never actually NUL-terminate (not NULL) the string you're going to pass to printf. I suspect you copied that part from somewhere or somebody.
// putting the NULL character at the
// position of space characters for
// next iteration.
str[i] = '.';
Actually printing spaces instead of whatever delimiter:
printf("%s ", &(str[i]) + 1);
/* ^ */
What I'd do instead:
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
struct {
char input[100];
char result[100];
}testcases[] = {
{.input = "i.love.you",
.result = "you.love.i"},
{.input = ".hi.you",
.result = "you.hi."},
{.input = "there.is.no spoon",
.result = "no spoon.is.there"},
{.input = "..",
.result = ".."},
{.input = "oh.no.",
.result = ".no.oh"},
{.input = "",
.result = ""}
};
char *function(char * s) {
size_t max_length = strlen(s) + 1;
/* Choosing to allocate and return a new string,
but you can do whatever you like: */
char * scratch = malloc(max_length);
/* Keeping an offset to where we concatenate substrings in the output: */
size_t offset = 0;
for (char * delimiter = NULL; (delimiter = strrchr(s, '.')) != NULL;) {
/* NUL-terminate where delimiter was found, without modifying the input argument,
we'll need it later: */
*delimiter = 0;
/* Copy the substring after the delimiter: */
int nwritten = snprintf(scratch + offset, max_length - offset, "%s.", delimiter + 1);
/* Adjust offset: */
offset += nwritten;
}
/* Now copy what's left over (if anything) at the beginning of the input string: */
snprintf(scratch + offset, max_length - offset, "%s", s);
return scratch;
}
int main(void) {
for (size_t idx = 0; idx < sizeof testcases / sizeof *testcases; ++idx) {
char * reversed = function(testcases[idx].input);
assert(!strcmp(reversed, testcases[idx].result));
printf("Testcase #%lu passed\n", idx);
free (reversed);
}
return EXIT_SUCCESS;
}
Hope you have time to understand how it works before your homework's deadline is up. :)
#include <stdio.h>
#include <string.h>
int main()
{
char buf[50] = "user/local/etc/bin/example.txt";
char* ptr;
ptr = strchr(buf, '/');
char path[20];
strncpy(path, buf, ptr-buf);
path[ptr-buf] =0;
printf("%s\n", path);
return 0;
}
I am able the get the substring before the first occurence of '/' i.e. I can get user but how can I get the substring after the second occurrence of '/' i.e. local and also the last occurence of '/' i.e. example without the extension .txt? How can it be done efficiently
I wouldn't use strchr for this. The problem is that strchr can only find one specific character but you care about both / and . Instead I would iterate the string using a pointer and check for both characters inside the loop..
Something like:
int main()
{
char buf[50] = "user/local/etc/bin/example.txt";
char* pStart = buf;
char* pCurrent = buf;
while(*pCurrent != '\0')
{
if (*pCurrent == '/' || *pCurrent == '.')
{
char str[20] = {0};
strncpy(str, pStart, pCurrent - pStart);
printf("%s\n", str);
pStart = pCurrent+1;
}
++pCurrent;
}
return 0;
}
Output:
user
local
etc
bin
example
If you really want to do this using strchr it could be like:
int main()
{
char buf[50] = "user/local/etc/bin/example.txt";
char* pStart = buf;
char* pCurrent = strchr(pStart, '/');
while(pCurrent != NULL)
{
char str[20] = {0};
strncpy(str, pStart, pCurrent - pStart);
printf("%s\n", str);
pStart = pCurrent+1;
pCurrent = strchr(pStart, '/');
}
pCurrent = strchr(pStart, '.');
if (pCurrent != NULL)
{
char str[20] = {0};
strncpy(str, pStart, pCurrent - pStart);
printf("%s\n", str);
}
return 0;
}
but as you can see, it requires a bit more code than the first example.
Use strtok() in this case For detailed description check this link ! Hope this helps!
Use this:
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int main(void)
{
char buf[] = "user/local/etc/bin/example.txt";
char substring[MAXLEN+1];
char *a, *b;
int len;
b = buf;
while ( (a = strchr(b, '/')) != NULL || (a = strchr(b, '.')) != NULL)
{
len = a - b;
if (len > MAXLEN)
return 0;
memcpy(substring, b, len);
substring[len] = 0;
printf("'%s'\n", substring);
b = a + 1;
}
return 0;
}
Here is the ouput:
'user'
'local'
'etc'
'bin'
'example'
Is there a better of parsing the below string instead of doing a strtok() to get each field.
"subject=what&cc=bose#yahoo.com&server=smtp.yahoo.com:8000"
Basically I want to retrieve the value for each field into another char buf's.
Here is my code. Just wanted to know if there is any other better way of doing it (any better string parsing algos)
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#define SUBJECT "subject="
#define CC_LIST "cc="
#define SERVER "server="
static void
get_value (const char *tok, char **rval_buf, size_t field_len)
{
size_t val_size = 0;
if (!tok || !rval_buf)
return;
val_size = strlen(tok + field_len) + 1;
*rval_buf = calloc(1, val_size);
if (*rval_buf) {
strlcpy(*rval_buf, tok + field_len, val_size);
}
}
int
main (int argc, char **argv)
{
/* hard coded buf for testing */
char buf[] = "subject=what&cc=bose#yahoo.com&server=smtp.yahoo.com:8000";
char *subject_text = NULL;
char *cc_list = NULL;
char *server_addr = NULL;
char *tok = NULL;
int field_len = 0;
int val_len = 0;
tok = strtok(buf, "&");
while(tok) {
/*
* Handle the token
*/
/* check if it is subject */
if (strstr(tok, SUBJECT)) {
get_value(tok, &subject_text, strlen(SUBJECT));
} else if (strstr(tok, CC_LIST)) { /* check if it is CC */
get_value(tok, &cc_list, strlen(CC_LIST));
} else if (strstr(tok, SERVER)) { /* check if it is server */
get_value(tok, &server_addr, strlen(SERVER));
}
tok = strtok(NULL, "&");
}
/* dump data */
fprintf(stdout, "\nSUBJECT: \"%s\"\nCC_LIST: \"%s\"\nSERVER: \"%s\" \n\n",
subject_text, cc_list, server_addr);
return EXIT_SUCCESS;
}
strstr searches for one string ("the needle") inside another ("the haystack"), but you really only want to know whether the needle is the beginning of the haystack.
Here's a small suggestion: (requires #include <stdbool> or change the booleans to ints. I like bools.)
static bool
getval(const char* haystack, const char** res, const char* needle, size_t len) {
if (haystack && 0 == strncmp(haystack, needle, len)) {
*res = strdup(haystack + len);
return true;
}
return false;
}
and later:
for (tok = strtok(buf, "&"); tok; tok = strtok(NULL, "&")) {
getval(tok, &subject_text, SUBJECT, strlen(SUBJECT)) ||
getval(tok, &cc_list, CC_LIST, strlen(CC_LIST)) ||
getval(tok, &server_addr, SERVER, strlen(SERVER));
}
You can actually get away with doing the strlen inside of getval, which cuts down a lot on the noise, because most modern compilers are clever enough to inline getval and constant-fold the length of a constant string.
Use strtok()
char *strtok(char *str, const char *delim)
You can put '&' as a delimeter
I wrote a quick-n-dirty splitter for you:
int split(char* input, char delim, char*** parts)
{
int count = 1;
char** result;
char* t = input;
while(*t != '\0')
{
if (*t++ == delim)
{
count++;
}
}
result = (char**)malloc(count * sizeof(char*));
t = input;
int i = 0;
result[i] = input;
while(*t != '\0')
{
if (*t == delim)
{
*t = '\0';
result[++i] = ++t;
}
else
{
t++;
}
}
*parts = result;
return count;
}
int main()
{
char raw[] = "subject=\"some text\"&cc=abcd&server=acd.com";
char* str = _strdup(raw);
char** parts;
char** keyval;
int cnt = split(str, '&', &parts);
for(int i=0; i<cnt; ++i)
{
split(parts[i], '=', &keyval);
printf("[%d]: %s <--> %s\n", i, keyval[0], keyval[1]);
free(keyval);
}
free(parts);
getchar();
return 0;
}
Output
[0]: subject <--> "some text"
[1]: cc <--> abcd
[2]: server <--> acd.com