execvpe implicit declaration error - c

I'm testing the execvpe() in c, I tried the below code, which cause the error as "implicit declaration of function 'execvpe' is invalid in C99 [-Wimplicit-function-declaration]".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _GNU_SOURCE
#include <unistd.h>
int main(int argc, char const *argv[])
{
//execl("/bin/echo", "echo", "Hello, world", NULL);
char *path = getenv("PATH");
char pathenv[strlen(path) + sizeof("PATH=")];
sprintf(pathenv, "PATH=%s", path);
char *envp[] = {pathenv, NULL};
char *tests[] = {"ls", "-lR", NULL};
execvpe(tests[0], tests, envp);
fprintf(stderr, "failed to execute \"%s\"\n", tests[0]);
return 0;
}
Then I test this code as below to test the existing status (which I copied from Compiler warnings for execvpe function, this time no error. Is there anyone can help me to figure out what's wrong in my above code? Thanks!
#include <unistd.h>
extern int execvpe(const char *file, char *const argv[], char *const envp[]);

Move the #define _GNU_SOURCE directive to before any of the #include directives, e.g.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
In Glibc, all of these headers pull in features.h which sets various macros based on the setting of _XOPEN_SOURCE, _POSIX_SOURCE, _GNU_SOURCE, etc. At the time of the first include, it is not set. When you get down to unistd.h, features.h has already been included and won't be applied again.

Related

'SO_USELOOPBACK' was not declared in this scope

I am checking socket options and I got this error when I compile. I tried to google it and it looks like no one has encountered this problem before.
#include <netinet/tcp.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
static char *sock_str_flag(union val *, int);
struct sock_opts {
const char *opt_str;
int opt_level;
int opt_name;
char *(*opt_val_str)(union val *, int);
}sock_opts[] = {
{ "SO_USELOOPBACK", SOL_SOCKET, SO_USELOOPBACK, sock_str_flag } //this is the error
};
The socket option SO_USELOOPBACK is not a POSIX standard. The man page setsockopt() describes the nature of SO_USELOOPBACK in detail.
The SO_USELOOPBACK is a [Digital] standard. Text paragraphs preceded by [Digital] document features that are included in the DIGITAL UNIX software but are not currently specified by any standard that applies to the interface being described. Use these features when source code portability across multiple UNIX platforms is less important than the capabilities that the features provide.
For portability, you need to have ifdef checks.
struct sock_opts {
const char *opt_str;
int opt_level;
int opt_name;
char *(*opt_val_str)(union val *, int);
}sock_opts[] = {
/* .... */
#ifdef SO_USELOOPBACK
{"SO_USELOOPBACK", SOL_SOCKET, SO_USELOOPBACK, sock_str_flag }
#endif
/* .... */
};

TCHAR characters are not displayed correctly

I have a simple code and argv[1] is "Привет".
#include <stdio.h>
#include <tchar.h>
#include <Windows.h>
#include <locale.h>
int _tmain(int argc, TCHAR* argv[])
{
TCHAR buf[100];
_fgetts(buf, 100, stdin);
_tprintf(TEXT("\nargv[1] %s\n"), argv[1]);
_tprintf(TEXT("%s\n"), buf);
}
In the console, I write "Мир" and have this result:
If I use setlocale(LC_ALL, ""), I have this result:
What should I do to get the correct string in both cases?
Evidently your program works, except it cannot print correctly on the console window. This is because Windows console is not fully compatible with Unicode. Use _setmode for Visual Studio. This should work for Russian but there could be additional problems with some Asian languages. Use WriteConsole for other compilers.
Visual Studio Example:
#include <stdio.h>
#include <io.h> //for _setmode
#include <fcntl.h> //for _O_U16TEXT
int wmain(int argc, wchar_t* argv[])
{
_setmode(_fileno(stdout), _O_U16TEXT);
wprintf(L"%s", L"Привет\n");
return 0;
}

How to fix Segmentation fault in C

Hello i wrote my c program which will be run on linux.
I am trying to make my own shell for linux.
I have the following code below...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
It'is compiling well but when i try to run the code it gives me segmentation fault error
How can i fix it and why i take this error?
Your main has a wrong signature. You want
int main(int argsCount, char**args) {
and of course you should remove the internal declaration of argCount & args inside your main.
Perhaps you want instead your args & argCount to contain the parsed arguments of your own shell (but you still have to give a good signature to your main, conventionally and very often int main(int argc, char**argv).... you probably want your shell to accept the -c argument as most shells do, this would ease debugging with simplistic test cases). Then you should initialize them, and you should read some line (probably with getline) in a loop.
As I commented, you should compile with all warnings & debug info:
gcc -Wall -Wextra -g yoursource.c -o yourprog
Then use gdb ./yourprog to debug your program (see GDB documentation). valgrind should also be helpful. Of course, be sure to develop on a Linux system!
BTW, your program is not a convincing start for a shell. Use strace on some existing shell to understand what a shell needs to do. Study the source code of some existing free software shell (e.g. sash, fish, GNU bash ...). Read Advanced Linux Programming

Im getting the error: expected '=', ',', ';', 'asm', or ''__attribute__' before 'void'

I am experiencing this error at my preprocessText() function (below) in my .c and I'm not entirely sure why. From browsing it seems most people were missing a { or ( or ; etc somewhere, but I'm fairly certain I am not.
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "Assembler.h"
int main(int argc, char** argv) {
// ...
preprocessText(file, inter1);
// ...
}
public void preprocessText(FILE* file, FILE* file2) { //error happens at this declaration
// ...
}
My header file is:
#ifndef ASSEMBLER_H
#define ASSEMBLER_H
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stddef.h>
// ...
void preprocessText(FILE* file, FILE* file2);
#endif
All methods are implicitly accessible by any other piece of code, if the function name is in scope. There is no public keyword in c
You have 'public' before 'void'. Remember, this is C ;)

Cannot access ifreq structure definition, __USE_MISC macro undefined

I am trying to compile the following single C file (called main.c):
#include <stdio.h>
#define __USE_MISC 1
#include <net/if.h>
int main(int argc, char **argv)
{
ifreq id_ifreq;
fprintf(stdout, ">>>>>> OK <<<<<<\n");
}
... using "gcc main.c -o main". I get the following error:
main.c: In function ‘main’:
main.c:9:2: error: unknown type name ‘ifreq’
I know that "ifreq" structure definition lies within a "#ifdef __USE_MISC" macro, however, I cannot activate that block of code.
I developed the following code for checking which MACROS are defined (compiled with "gcc main.c -o main"):
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <net/if.h>
int main(int argc, char **argv)
{
#ifdef __USE_MISC
printf("__USE_MISC defined\n");
#endif
#ifdef _GNU_SOURCE
printf("_GNU_SOURCE defined\n");
#endif
#ifdef _BSD_SOURCE
printf("_BSD_SOURCE defined\n");
#endif
#ifdef _SVID_SOURCE
printf("_SVID_SOURCE defined\n");
#endif
}
The result is that they are all defined but the "_GNU_SOURCE" one. However, I am still not capable of using the definition of the "ifreq" structure included in the "net/if.h" file.
Anybody can help?
You are omitting the struct keyword (in C, a struct definition is not a typedef)
#include <stdio.h>
#include <net/if.h>
int main(int argc, char **argv)
{
struct ifreq id_ifreq;
fprintf(stdout, ">>>>>> OK <<<<<<\n");
return 0;
}

Resources