In C programming language, is it possible to access int argc or char **argv without using the parameters? I know some of you might ask why this is needed, just for research purposes.
Is it possible to generate the cmd line arguments without using the main parameter variables ? For example, to illustrate some pseudo code, that i have in mind,
LPTSTR cmd = GetCommandLine();
splitted = cmd.split(" ") //split from spaces
char **someArgv.pushForEach Splitted, length++
and you'd have a someArgv with the parameters and length as argc, this'd really help to know if possible to illustrate.
If OP already has the command as a string, then:
Form a copy of the string
Parse it for argument count
Allocate for argv[]
Parse & tokenize copy for each argv[]
Call main()
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// Not standard, but commonly available
char *strdup(const char *s);
// Return length of token and adjust offset to the next one
// Adjust as needed
// Presently only ' ' are used to separate
// More advanced would have escape characters, other white-space, etc.
size_t tokenize(const char *s, size_t *offset) {
// find following space
size_t len = strcspn(s + *offset, " ");
*offset += len;
// find first non-space
*offset += strspn(s + *offset, " ");
return len;
}
int call_main(const char *cmd) {
char *cmd2 = strdup(cmd);
cmd2 += strspn(cmd2, " "); // skip leading spaces
size_t offset = 0;
int argc = 0;
while (tokenize(cmd2, &offset) > 0) {
argc++;
}
char **argv = malloc(sizeof *argv * ((unsigned)argc + 1u));
offset = 0;
for (int a = 0; a < argc; a++) {
argv[a] = &cmd2[offset];
size_t len = tokenize(cmd2, &offset);
argv[a][len] = '\0';
}
argv[argc] = NULL;
int retval = 0;
#if 0
retval = main(argc, argv);
#else
printf("argc:%d argv:", argc);
for (int a = 0; a < argc; a++) {
printf("%p \"%s\", ", argv[a], argv[a]);
}
printf("%p\n", argv[argc]);
#endif
free(cmd2);
free(argv);
return retval;
}
Sample
int main() {
call_main(" name 123 abc 456 ");
}
argc:4 argv:0x800062322 "name", 0x800062327 "123", 0x80006232c "abc", 0x800062331 "456", 0x0
Pedantic: The strings provided to main() should be modifiable. Avoid code like
argv[1] = "Hello";
....
main(argc, argv);
#include <stdio.h>
int main(int argc, char *argv[]);
int callMain(void)
{
char *argv[4];
argv[0] = "binary";
argv[1] = "param1";
argv[2] = "param2";
argv[3] = NULL;
return main(3, argv);
}
int main(int argc, char *argv[])
{
if (argc <= 1)
{
return callMain();
}
printf("ARGC: %u\n", argc);
int i;
for (i = 0; i < argc; i++)
printf("ARG: %u - %s\n", i, argv[i]);
return 0;
}
Related
I am trying to use va_list & its associated macros with vsprintf() to create a format string that has a variable number of specifiers. Here is an example program I wrote in which the number of specifiers can only be altered via the NUM_ARG macro:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#define MAXBUF 4096
#define SPECIFIER "(%s)"
#define NUM_ARG 5
char *strmaker(int num_args, ...)
{
char form[MAXBUF] = { [0] = '\0' };
char *prnt = (char *) malloc(sizeof(char) * MAXBUF);
va_list strings;
for (int i = 0; i < num_args; ++i)
strcat(form, SPECIFIER);
va_start(strings, num_args);
vsprintf(prnt, form, strings);
va_end(strings);
return prnt;
}
int main(int argc, char *argv[])
{
if (argc != (NUM_ARG + 1))
return -1;
char *s = strmaker(NUM_ARG, argv[1], argv[2], argv[3], argv[4], argv[5]);
printf("%s\n", s);
free(s);
return 0;
}
However, this isn't exactly what I want to achieve. How could I do this with a variable number of arguments? How could a variable number of strings be passed to a function and used to initialise a va_list?
As far as I know, it is not possible to do that. If you are not so keen about using variadic functions and can redefine the function. The below code suits your need; Iterate through each item in the array and append to the string using snprintf.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXBUF 4096
#define SPECIFIER "(%s)"
char *strmaker(int num_args, char** strings)
{
char *prnt = (char *) malloc(sizeof(char) * MAXBUF);
int cur = 0;
/* Append the strings to the prnt buffer */
for (int i = 0; i < num_args; i++) {
int p_return = snprintf(prnt + cur, MAXBUF - cur, SPECIFIER, strings[i]); // If no error, return the number characters printed excluding nul (man page)
if (p_return >= MAXBUF - cur) // If buffer overflows (man page)
return prnt;
cur = cur + p_return; // Update the index location.
}
return prnt;
}
int main(int argc, char *argv[])
{
if (argc <= 1)
return -1;
char *s = strmaker(argc - 1, argv + 1);
printf("%s\n", s);
free(s);
return 0;
}
Terminal Session:
$ ./a.out 1 2 3
(1)(2)(3)
$ ./a.out 1 2 3 4 5 6 7
(1)(2)(3)(4)(5)(6)(7)
$ ./a.out Hello, This is stackoverflow, Bye
(Hello,)(This)(is)(stackoverflow,)(Bye)
Short answer is: You can't.
However you can work around it by using arrays of strings, possibly dynamically allocated. Then you could basically use the same technique you do now, but iterate over the array instead.
Perhaps something like this:
char *strmaker(size_t count, char *strings[])
{
// First get the length of all strings in the array
size_t result_length = 0;
for (size_t i = 0; i < count; ++i)
{
// +1 for space between the strings
// And for the last string adds space for the string null-terminator
result_length += strlen(strings[i]) + 1;
}
// Now allocate the string (using calloc to initialize memory to zero, same as the string null-terminator)
char *result = calloc(1, result_length);
// And not concatenate all strings in the array into one large string
for (size_t i = 0; i < count; ++i)
{
strcat(result, strings[i]);
if (i != count - 1)
{
strcat(result, " "); // Add space, except after last string
}
}
// Return the resulting string
return string;
}
int main(int argc, char *argv[])
{
// Create an array for all arguments
char **arguments = malloc(sizeof(char *) * argc - 1);
for (int a = 1; a < argc)
{
arguments[a - 1] = argv[a];
}
// Now create the single string
char *result = strmaker(argc - 1, arguments);
// ... and print it
printf("%s\n", result);
// Finally clean up after us
free(result);
free(arguments);
}
For the command-line arguments in argv you don't really need to create a new array to hold them, but it showcases how to create an array of string to pass to strmaker. You can use any strings you want instead of the command-line arguments.
What I want to do is write arguments in the command line separated by a + and concatenate the arguments into a single string
eg:
./concat Wow + this + is + cool
Wow this is cool
I looked up a question for this sort of topic before but that involved concatenating only the first character of each argument and not the entire arguments. And it didn't ignore the separator
This is what I have
void concat(char **argv, int argc, char *string)
{
size_t i = 0;
for(int j=1; j<argc; j++)
{
string[i++] = *argv[j];
if(j+1 != argc)
{
string[i++] = ',';
string[i++] = ' ';
}
}
string[i] = '\0';
}
And this is what I'm doing in main to call this function
int main(int argc, char *argv[])
{
int allnum=0;
char string[1000];
concat(argv, argc, string);
printf("%s\n", string);
}
Using the strcpy or strcat in string.h is more simple to concatenate string.
For example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char * s = malloc (2 * argc *sizeof (char));
if (argc < 2)
exit(-1);
for(int i = 1; i < argc; i += 2) {
strcat(s, argv[i]);
strcat(s, " ");
}
printf("%s\n", s);
return 0;
}
I was wondering if there was a way to read bytes (like this: \x00\x01\x02) from the command line in C.
For example:
#include <stdio.h>
int main(int argc, char *argv[]) {
printf("%s", argv[1]);
return 0;
}
user#UbuntuServer: ~/Code# gcc Program.c -o Program
user#UbuntuServer: ~/Code# ./Program "\x48\x69"
Hiuser#UbuntuServer: ~/Code# ./Program "\x48\x69\x0a"
Hi
user#UbuntuServer: ~/Code#
Thanks!
Unless you use a library to parse regex strings like that, you'll need to parse the hex manually. Check out this answer (which has slightly different syntax but a similar function):
Hexadecimal string to byte array in C
I would go for something like this:
int main(int argc, char **argv)
{
char *buf = malloc(strlen(argv[1]) / 4 + 1);
size_t i = 0;
for (char *tok = strtok(argv[1], "\\x"); tok; tok = strtok(NULL, "\\x"))
{
sscanf(tok, "%02hhx", buf + i);
i++;
}
buf[i] = '\0';
printf("%s", buf);
free(buf);
return 0;
}
I found the HEX to ASCII conversion functions on this thread, and modified it to suit my situation.
#include <stdio.h>
#include <string.h>
int hexToInt(char c) {
int first = c / 16 - 3;
int second = c % 16;
int result = first * 10 + second;
if(result > 9) {
result--;
}
return result;
}
int hexToASCII(char c, char d) {
int high = hexToInt(c) * 16;
int low = hexToInt(d);
return high + low;
}
int main(int argc, char *argv[]) {
char* hexString = argv[1];
char buf = 0;
for(int i = 0; i < strlen(hexString); i++) {
if(i % 2 != 0) {
printf("%c", hexToASCII(buf, hexString[i]));
} else {
buf = hexString[i];
}
}
return 0;
}
I'm trying to obtain a file from its path.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char split_path(char *path){
char *str1, *delim;
char buf[255];
int i, max;
char *token = "/";
for (i = 1, str1 = path; ; i++, str1 = NULL){
delim = strtok(str1, token);
if (delim == NULL){
break;
}
buf[i] = *delim;
max = i;
}
char last = buf[max];
return last;
}
int main(int argc, char *argv[]){
if (argc != 3){
fprintf(stderr, "Usage: %s string delim\n", argv[0]);
exit(EXIT_FAILURE);
}
char last = split_path(argv[1]);
printf("%c\n", last);
return 0;
}
So if I enter an input of /a/b/c/d , it returns 'd' which is what I want.
But if I enter an input of /a/b/c/d.txt, it only returns 'd' without the '.txt', when what I need is 'd.txt'.
It seems to be only getting the first index after the last / . I can't seem to figure out the problem.
Help much appreciated.
Your problem is split_path is returning the first letter of the last good result of strtok.
Change split_path to return a string, and modify it slightly... something like this:
char *split_path(char *path){
char *str1, *delim;
int i, max;
char *last = NULL;
char *token = "/";
for (i = 1, str1 = path; ; i++, str1 = NULL){
delim = strtok(str1, token);
if (delim == NULL){
break;
}
last = delim;
}
return last;
}
int main(int argc, char *argv[]){
if (argc != 3){
fprintf(stderr, "Usage: %s string delim\n", argv[0]);
exit(EXIT_FAILURE);
}
char *last = split_path(argv[1]);
printf("%s\n", last);
return 0;
}
I'll show you how to do it two ways. The easiest is to include libgen.h and use basename(), if this exists on your system. It does what you wanted. But if you want to write your own function, you can do it with strrchr(), which finds the last occurrence of a character in a string.
#include <stdio.h>
#include <string.h> // for strrchr()
#include <libgen.h> // for basename()
const char *my_basename(const char *path)
{
const char *lastSlash = strrchr(path, '/');
if (lastSlash)
return lastSlash+1;
return path;
}
int main(int argc, char *argv[])
{
if (argc < 2)
return;
printf("Full path = %s\n", argv[1]);
printf("basename() = %s\n", basename(argv[1]));
printf("my_basename() = %s\n", my_basename(argv[1]));
return 0;
}
Sample output:
Full path = /aaa/bbb/ccc/ddd.txt
Filename = ddd.txt
Filename = ddd.txt
Also after I want to add a key to each letter like 'a' + 1 = 'b'. So I want to take a string for instance "Hello" then do
char 1 = H + 1;
char 2 = E + 1;
etc.
printf("%c" + "%c" + "%c" + "%c" + "%c", 1 , 2 , 3 , 4 , 5);
also I would love for this to be automated because IDK how long the string might be and what key theyre are going to use.
You can do something like this:
#include<stdio.h>
#include<string.h>
int main()
{
char text[] = "Hello";
int i=0;
int size= strlen(text);
for(i=0;i<size;i++)
{
//something here
}
return 0;
}
Assuming the string is mutable, you can update it in place:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void update(char *s, int delta)
{
while(*s)
*s++ += delta;
}
int main(int argc, char **argv)
{
char str[] = "Hello";
update(str, 1);
printf("Encoded: \"%s\"\n", str);
update(str, -1);
printf("Decoded: \"%s\"\n", str);
return 0;
}
If the string is immutable, you will need to make a copy of it, and update the copy.
int main(int argc, char **argv)
{
const char str[] = "Hello";
char *copy = strdup(str);
update(copy, 1);
printf("Encoded: \"%s\"\n", copy);
update(copy, -1);
printf("Decoded: \"%s\"\n", copy);
free(copy);
return 0;
}
You should read about dynamic arrays in C.
#include <stdio.h>
#include <string.h>
char* code(const char* message)
{
int i = 0;
char* coded;
for (i = 0; i < strlen(message); i++)
{
coded[i] = message[i] - 3;
}
return coded;
}
char* decode(const char* message)
{
int i = 0;
char* coded;
for (i = 0; i < strlen(message); i++)
{
coded[i] = message[i] + 3;
}
return coded;
}
int main()
{
// This is dynamic allocated chars array
char* message = "Hello World!";
message = code(message);
printf("%s\n", message);
message = decode(message);
printf("%s\n", message);
}