crypt() function in C not working - c

The following is my file named crack.c:
#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>
void execute(char *alpha)
{
char *beta = crypt(alpha);
printf("%s", beta);
}
int main(int argc, string argv[]){
....
execute(argv[1]);
else{
printf("You submitted %d command line arguments. That's an issue. You need to submit exactly one.", argc);
return 1;
}
}
The following is what I type into the command line:
jharvard#appliance (~/Dropbox/hacker2): clang -o crack -lcrypt crack.c
The following is what the command line spits back out at me:
crack.c:8:19: warning: implicit declaration of function 'crypt' is
invalid in
C99 [-Wimplicit-function-declaration]
string beta = crypt(alpha);
^ crack.c:8:12: warning: incompatible integer to pointer conversion initializing
'string' (aka 'char *') with an expression of type 'int'
[-Wint-conversion]
string beta = crypt(alpha);
^ ~~~~~~~~~~~~ 2 warnings generated.
Anyone know what's going on?

I had the same problem, and I realized that changing the header
#define _XOPEN_SOURCE
#include <unistd.h>
by
#define _GNU_SOURCE
#include <crypt.h>
makes disapear the error in compilation time

The function signature of crypt is:
char * crypt (const char *key, const char *salt)
It seems that you forgot one parameter! So your line:
string beta = crypt(alpha);
Should be something like that:
string beta = crypt(alpha, salt);

Make sure you have the define and include for crypt as the very first lines at the top of your file before any other includes.
#define _XOPEN_SOURCE
#include <unistd.h>

Related

Catching Missing Semicolons in C header file prototypes

This topic ought to have been flogged to death. I just spent 30 minutes locating what ended up being a missing semicolon at the end of a function prototype in a header file:
void foo(void);
void bar(void) // <<< Error on this line
void squee(void);
This is a common typo caused by copy-pasting the prototype from the C file. Of course according to the compiler the universe just fell apart, with an endless stream of absolutely nonsensical errors, none of them helpful.
This could be avoided by having an optional parsing phase to check for this condition in .h files then report a warning (promoted to error if settings mandate). This would require some restrictions on what you put in header files (no code, consistent format for prototypes, etc). But that's an easy compromise.
I can write my own SW tool to do this, but it would be more helpful to run it as a part of the build process. I use GCC in Eclipse. Any advice on where you'd start with this? Or anything pre-existing / off the shelf? Or perhaps just a better way to approach it?
Thank you.
it's far more common and more difficult to guess the following problem (in a header file):
struct something {
type1 var1;
type2 var2;
}
/* EOF */
and when you #include "header.h" into hello.c
#include <stdio.h>
#include "header.h"
int main(int argc, char **argv)
{
printf("Hello, world\n");
}
you get an error e.g.
lcu#europa:~$ gcc main.c
main.c:4:1: error: expected ';', identifier or '(' before 'int'
4 | int main(int argc, char **argv)
| ^~~
and the compiler has got out of header.h to signal the error in the line of main function. The thing can be worse if you happen to use legacy code and declare main() the old way:
main(argc, argv)
char **argv;
{
...
because then, the struct is a valid type and it is taken as the type returned by main() and you get (if you get it) the error far below (or no error at all, just a warning):
lcu#europa:~$ gcc main.c
main.c: In function 'main':
main.c:4:1: warning: type of 'argc' defaults to 'int' [-Wimplicit-int]
4 | main(argc, argv)
| ^~~~
In this case, the contents of main.c were:
#include <stdio.h>
#include "header.h"
main(argc, argv)
char **argv;
{
printf("Hello, world\n");
}
which is still valid c code.
(these examples were made by gcc, because clang ---the native compiler of freebsd--- detects the EOF in the header file and shows a warning stating that the type was not ended before the end of the include file. But this only happens if the type definition is the last of the file.
Note:
if you declare main as:
#include <stdio.h>
#include "header.h"
main(argc, argv)
int argc;
char **argv;
{
printf("Hello, world\n");
}
you get a complete compilation, without even a warning.

Basic MathGL example in c does not compile

Here is the basic c example from the documentation:
#include <mgl2/mgl_cf.h>
int sample(HMGL gr, void *)
{
mgl_rotate(gr,60,40,0);
mgl_box(gr);
}
int main(int argc,char **argv)
{
HMGL gr;
gr = mgl_create_graph_qt(sample,"MathGL examples",0,0);
return mgl_qt_run();
/* generally I should call mgl_delete_graph() here,
* but I omit it in main() function. */
}
Here is the start of compilation output:
$ gcc test.c -lmgl-qt5 -lmgl
In file included from /usr/include/mgl2/mgl_cf.h:29,
from test.c:1:
/usr/include/mgl2/data_cf.h:527:17: error: expected ‘,’ or ‘;’ before ‘mgl_find_roots’
527 | bool MGL_EXPORT mgl_find_roots(size_t n, void (*func)(const mreal *x, mreal *f, void *par), mreal *x0, void *par);
| ^~~~~~~~~~~~~~
test.c: In function ‘sample’:
test.c:2:21: error: parameter name omitted
2 | int sample(HMGL gr, void *)
| ^~~~~~
It seems clear to me that the example is not even valid c, missing a parameter (that is not actually used) to the sample() function. I have tried removing it but still get the first (internal mathgl) error.
Any ideas how to proceed?
It seems MathGL doesn't have their internal #include statements in order, and require you to be careful about what you #include and in what order. In particular, ensure you #include <mgl2/mgl.h> before any other MathGL header, and before that one ensure you #include <stdbool.h>. Futhermore, when you use for example Qt-related functions, ensure you #include <mgl2/qt.h>. This should work:
#include <stdbool.h>
#include <mgl2/mgl.h>
#include <mgl2/qt.h>
int sample(HMGL gr, void *ignored)
{
mgl_rotate(gr,60,40,0);
mgl_box(gr);
}
int main(int argc, char **argv)
{
HMGL gr = mgl_create_graph_qt(sample, "MathGL examples", 0, 0);
return mgl_qt_run();
}

Fgets compilation error

I'm stuck with what seems a beginner's compilation error:
My simple program:
#include <stdlib.h>
#include <stdio.h>
#include "Tiles_Circular_Linked_List.h"
#define MAX_LINE_LENGTH 128;
int main(int argc, char **argv) {
struct node *head_tail;
FILE *file;
/*char filename[] = "/home/student/Desktop/Studies/C/testing_fodder/tiles";*/
argv++; /*go to second character-array argument*/
file = fopen(*argv, "r");
char *curr_line;
fgets(curr_line, MAX_LINE_LENGTH, file);
return 0;
}
I try to compile it using this command:
gcc -g -Wall -ansi launch_tiles.c -o tiles_prog
and get these errors:
launch_tiles.c: In function ‘main’:
launch_tiles.c:17:19: error: expected ‘)’ before ‘;’ token
launch_tiles.c:17:19: error: too few arguments to function ‘fgets’ /usr/include/stdio.h:628:14: note: declared here
launch_tiles.c:9:8: warning: variable ‘file’ set but not used [-Wunused-but-set-variable]
launch_tiles.c:8:15: warning: unused variable ‘head_tail’ [-Wunused-variable]
I am interested about the errors, not the warnings.
I can count three arguments that I pass to fgets and don't understand where do I miss parentheses so what's the problem?
Thanks!
change
#define MAX_LINE_LENGTH 128;
to
#define MAX_LINE_LENGTH 128
(without the ;). Easy mistake to make.
The C preprocessor does very simple, textual substitution, so with the wrongly-defined macro, you end up with
fgets(curr_line, 128;, file);
which is obviously a syntax error.

How to handle given extern function in C

I've been given function called statPrint to handle printing of the system call stat(). The function is provided with another .o file. I'm getting errors when compiling my implementation with that function:
In function ‘main’:
statcall.c:9:19: error: expected expression before ‘,’ token
statPrint(argv[1]*,sb*);
^
statcall.c:9:19: error: incompatible type for argument 2 of ‘statPrint’
statcall.c:4:8: note: expected ‘struct stat *’ but argument is of type ‘struct stat’
extern statPrint(char*,struct stat*);
Here is my code:
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
extern statPrint(char∗,struct stat∗);
int main(int argc, char *argv[])
{
struct stat sb;
stat(argv[1],&sb); ///argv[1] contains input from the terminal/shell
statPrint(argv[1]*,sb*);
}
I compile it with(libstat contains the external function):
gcc -o statcall statcall.c libstat.o
How do I get rid of the errors?
This line makes no sense:
statPrint(argv[1]*,sb*);
There's no valid syntax that ends with *.
I think you want:
statPrint(argv[1], &sb);
Recommend you read up on addresses of variables and pointers.
Your function expects char * please provide it
statPrint(argv[1],sb);
I really didn't get what is argv[1]*

Not sure why the compiler is complaining ... implicit declaration of function strchrnul

This is my code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
static int valid_line(char *cur_line) {
if (*cur_line == '#') {
return 0;
}
char *end = strchrnul(cur_line, '#');
while(end > cur_line && isspace(*end)) end--;
return (!(*cur_line == *end));
}
I am going through the line and am getting rid of leading and trailing white spaces and anything that occurs after the '#' (including the '#').
My compiler is saying this:
parser.c:20:2: warning: implicit declaration of function ‘strchrnul’ [-Wimplicit- function-declaration]
parser.c:20:14: warning: initialisation makes pointer from integer without a cast [enabled by default]
EVen though I have string.h above.
Could someone please explain.
strchrnul() is a GNU extension, and you can get this function included warning free via a feature test macro.
#define _GNU_SOURCE // required for strchrnul()
#include <stdio.h>
#include <ctype.h>
#include <string.h> // required for strchrnul()
#include <stdlib.h>
static int valid_line(char *cur_line) {
if (*cur_line == '#') {
return 0;
}
char *end = strchrnul(cur_line, '#');
while(end > cur_line && isspace(*end)) end--;
return (!(*cur_line == *end));
}
Please note from the second linked man page, the placement of the #define is important:
NOTE: In order to be effective, a feature test macro must be defined before including any header files
If you are using gcc compiler, don't use -std=c89 or -std=c99 but rather use -std=gnu89 or -std=gnu99 as strchrnul is a GNU extension.

Resources