usage of string tokenizer - c

i have a string like this..
/home/Abcd/Pradeep/Jack.sh
/home/Abcd/Pradeep/Paul/Kill.sh
I need to take Jack.sh and Kill.sh alone from these strings. there can be many / in the string.
How to do this using strtok ?

You don't need strtok for this. Just use strrchr to find the last '/' character. Your filename starts one character after that.

From the path name style, it looks like it is a *nix system.
You can use the command basename which does the same thing.
If you want to use it in a c program, try man 3 basename in your system to get the documentation.

Related

How do I get specific names from a text file with a location as condition?

Please help me. I'm new to programming. I've been trying and searching for days to answer this.
I need a C program that will open a text file named users.txt
In that text file there are names then comma and their schools.
John Paul, Legit Univ
Paul John, Yo Univ
Lebron James, School Univ
James Lebron, Legit Univ
All I managed so far is to display them all. The output should be all the users that are from "Legit Univ".
Sample output:
Found 2 users from Legit Univ
John Paul
James Lebron
Use fgets() to read a line from file into a string, then strchr() to to find the position of the comma ',' field separator in the string (or strstr() if the field separator is comma space ", "). Now you can check the part of the string after the field separator for a match on your query with strcmp(). Instead of parsing the file, you could also use a regex and match against the string.
Another approach would be to load one line at a time (as you are doing). Then, search the buffer for the target string (like "Legit Univ") using strstr(). If found, then go about trimming at the comma (perhaps strtok() and outputting the name ( puts() ). If not found, move on to the next record (input line).
Don't do more processing than you have to.
The "Found 2 users at Legit Univ" is a bit of a problem, though. Either you can buffer up the names in a linked-list while counting 'hits', OR you could change the output to print the count of 'hits' AFTER the report of the names. (Or, worse, you could process the file once to count, rewind, then output the count AHEAD of the 'user' names...) How stringent are the requirements of the output?
You can take the strings with the following:
fscanf(<stream_name>,"%[^,] %s",<string_name_1>,<string_name_2> ) here %[^,] basically means read until you see a comma and store it in <string_name_1> and store the rest in <string_name_2>. After that, all you need to do is strncmp the <string_name_2> with the name of the university you are searching for, and if the result of strncmp is zero, that means what is written and what you are searching for is the same.

how to get the file extension in pike

I'm working on a program in pike and looking for a method like the endswith() in python- a method that gives me the extension of a given file.
Can someone help me with that?
thank you
Python's endswith() is something like Pike's has_suffix(string s, string suffix):
has_suffix("index.html", ".html");
Reference:
http://pike.lysator.liu.se/generated/manual/modref/ex/predef_3A_3A/has_suffix.html
extract the end of the string, and compare it with the desired extension:
"hello.html"[<4..] == ".html"
(<4 counts from the end of the string/array)
If you want to see what the extension of a file is, just find the last dot and get the substring after it, e.g. (str/".")[-1]
If you just want to check if the file is of a certain extension, using has_suffix() is a good way, e.g. has_suffix(str, ".html")

system() not working

I am trying to launch executables from a C source file. When there is a space in the path I.e.
system("D:\\Games\\Subway Surfers\\Subway_Surfers.exe")
it does not work but
when I change the folder name and remove the space it works. Is there a way around this?
You have to use escape characters while using spaces in path.
Ex: system("D:\\Games\\Subway\ Surfers\\Subway_Surfers.exe");
Try replacing the \ with \\ and with \. You have to replace the characters with their respective escape characters.
system("\"D:\\Games\\Subway\ Surfers\\Subway_Surfers.exe\"");
This command would be interpreted as:
"D:\Games\Subway Surgers\Subway_Surfers.exe"
And, the quotes around the path with spaces ensure that the string is not truncated about the space.
Thanks guys escape characters didn't work so I just used CreateProcess() function. Its long but works fine even with spaces
You need to quote subdir name containing space character. For example like
system("D:\\Games\\\"Subway Surfers\"\\Subway_Surfers.exe") where \"Subway Surfers\" is quoted subdir with spaces.
I have found a perfect workaround to use the system() function. it requires a string in the argument so i just create a string whose contents are the path e.g char path[50] = "D:\SubwaySurfers\SubwaySurfers.exe" then call the function as
system(path);
however in some specific applications such as Apache(Game) it doesnt work whether i use CreateProcess or System.

Is there a way to prevent sh/bash from performing command substitution?

From a C program I want to call a shell script with a filename as a parameter. Users can control the filename. The C is something like (initialization/error checking omitted):
sprintf(buf, "/bin/sh script.sh \"%s\"", filename);
system(buf);
The target device is actually an embedded system so I don't need to worry about malicious users. Obviously this would be an attack vector in a web environment. Still, if there is a filename on the system which, for example, contains backquotes in its name, the command will fail because the shell will perform expansion on the name. Is there any to prevent command substitution?
Well, you could always reimplement system() using a call to fork() and then execv().
http://www.opengroup.org/onlinepubs/000095399/functions/system.html
Try triggering "unalias " in the system function.
Since you tagged this as C I will provide you with a C answer. You will need to escape the filename -- create a new string that will be treated properly by the shell, so that things like This is a file name produces This\ is\ a\ file\ name or bad;rm *;filename becomes bad\;rm\ \*\;filename. Then you can pass that to the shell.
Another way around this would be to run the shell directly with fork and one of the exec functions. Passing arguments directly to programs does not result in shell command line expansion or interpretation.
As sharth said, you should not use system but fork and execv yourself. But to answer the question of how you make strings safe to pass to the shell (in case you insist on using system), you need to escape the string. The simplest way to do this is to first replace every occurrence of ' (single quote) with '\'' (single quote, backslash, single quote, single quote) then add ' (single quote) at the beginning and end of the string. The other fairly easy (but usually less efficient) method is to place a backslash before every single character, but then you still need to do some special quotation mark tricks to handle embedded newlines, so I prefer the first method.

Is it possible to have the name of the executable without the path?

Hi I'm trying to use the name of the executable and an usage string, I'm using argv[0] for such purpose but instead of the name of the executable itself it gives me the complete path to it.
Is there any way to get only the executable name?
Just search for the last /.
const char *exename = strrchr(argv[0], '/');
if (exename)
// skip past the last /
++exename;
else
exename = argv[0];
As far as I know, (on linux, at least) you just have to extract the executable name from the char* yourself.
The easiest way to do that is to use basename(argv[0]), which you can get by including "libgen.h".
If it's available on your platform, there's a function char *basename(char *path). See basename documentation.
Use GetModuleFileName http://msdn.microsoft.com/en-us/library/ms683197%28VS.85%29.aspx with the handle argument = 0
Just use the last part of the path-string. Some combination of a call to strrchr (get last path delimiter) and e.g. strcpy or similar to copy out the part from last path delimiter to end
You could use getprogname() if the name of the program is set by your OS.

Resources