Compilation error in C on Linux - c

I'm writing a simple program in Linux and when compiling I keep getting this error and don't understand why — I put all ; or {} in place:
ex1.c:49.1: error:expected declaration or statement at end of input
My code:
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main (int argc, char* argv[])
{
if (argc!=3)
printf("Number of arugements passed is wrong - not 2\n");
char * file1 = argv[1];
char * file2 = argv[2];
int fdout1 = open(file1, O_RDONLY);
int fdout2 = open(file2, O_RDONLY);
struct stat stat_p1,stat_p2;
char * f1;
char * f2;
int i=0;
if (fdout1 < 0 || fdout2<0) /* means file open did not take place */
{
perror("ERROR opening files"); /* text explaining why */
return 1;
}
if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
{
if((read(fdout1,f1,stat_p1.st_size)==-1) ||(read(fdout1,f1,stat_p1.st_size)==-1))
{
printf("ERROR reading files"); /* text explaining why */
return 1;
}
if (stat_p1.st_size!=stat_p2.st_size)
return 1;
if (strcmp(f1,f2)!=0)
return 1;
return 0;
}
Can anyone tell me why I keep getting this error?

It seems there is a typo
if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
{
The last brace shall be a closing brace.
if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
}
Take into account that if argc is less than 3 you have to exit the program
if (argc!=3)
printf("Number of arugements passed is wrong - not 2\n");
Otherwise these statements are invalid
char * file1 = argv[1];
char * file2 = argv[2];

if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
>>>> { >>>>> change this to '}'

This error occurs when you have missing closing braces. As can be seen here:
if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
{
You seem to have a typo, you should have closing brace instead of opening brace.

It should be like this
if ( (stat (file1, &stat_p1) == -1) ||( stat (file2, &stat_p2) == -1) ) /* declare the 'stat' structure */
{
printf(" Error occurred attempting to stat files");
return 1;
}
The closing brace is the problem, here it was opening brace on your program.

Related

C function call is giving me a "too many arguments" error

I'm getting a "too many arguments in function call" error in my C program. The error occurs at a line where I'm calling a function that has a fixed number of arguments. I'm not sure why I'm getting this error, as I'm not passing in more arguments than the function expects. Here's the code where the error occurs:
if (mkdir(path, 0777) == -1)
Here is full code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#define BUF_SIZE 1024
#define MAX_ARGS 10
// Print the usage message for the program
void print_usage() {
fprintf(stderr, "Usage: syscalls <command> [arguments]\n");
}
// Read the contents of a file and write them to stdout
int read_file(const char *path) {
static char buf[BUF_SIZE];
int fd = open(path, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
return 1;
}
ssize_t num_read;
while ((num_read = read(fd, buf, BUF_SIZE)) > 0) {
if (write(STDOUT_FILENO, buf, num_read) != num_read) {
fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
return 1;
}
}
if (num_read == -1) {
fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
// Write a set of lines to a file
int write_file(const char *path, char *lines[], int num_lines) {
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd == -1) {
fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
return 1;
}
int total_bytes = 0;
for (int i = 0; i < num_lines; i++) {
const char *line = lines[i];
size_t len = strlen(line);
ssize_t num_written = pwrite(fd, line, len, total_bytes);
if (num_written == -1) {
fprintf(stderr, "Failed to write to %s: %s\n", path, strerror(errno));
return 1;
}
total_bytes += num_written;
}
printf("Wrote %d B\n", total_bytes);
return 0;
}
// Create a directory
int make_directory(const char *path) {
if (mkdir(path, 0777) == -1) {
if (errno == EEXIST) {
fprintf(stderr, "%s already exists\n", path);
} else {
fprintf(stderr, "Failed to create %s: %s\n", path, strerror(errno));
}
return 1;
}
return 0;
}
// List the contents of a directory
int list_directory(const char *path) {
DIR *dir = opendir(path);
if (dir == NULL) {
fprintf(stderr, "Failed to open directory %s: %s\n", path, strerror(errno));
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
printf("%s\n", entry->d_name);
}
if (closedir(dir) == -1) {
fprintf(stderr, "Failed to close directory %s: %s\n", path, strerror(errno));
return 1;
}
return 0;
}
int main(int argc, char *argv[]) {
if (argc < 2) {
print_usage();
return 1;
}
char *command = argv[1];
if (strcmp(command, "read") == 0) {
if (argc != 3) {
print_usage();
return 1;
}
return read_file(argv[2]);
} else if (strcmp(command, "write") == 0) {
if (argc < 4 || argc > MAX_ARGS + 2) {
print_usage();
return 1;
}
return write_file(argv[2], argv + 3, argc - 3);
} else if (strcmp(command, "mkdir") == 0) {
if (argc != 3) {
print_usage();
return 1;
}
return make_directory(argv[2]);
} else if (strcmp(command, "ls") == 0) {
if (argc != 3) {
print_usage();
return 1;
}
return list_directory(argv[2]);
} else {
print_usage();
return 1;
}
}
I am getting this error in terminal:
syscalls.c: In function 'write_file':
syscalls.c:54:31: warning: implicit declaration of function 'pwrite' [-Wimplicit-function-declaration]
ssize_t num_written = pwrite(fd, line, len, total_bytes);
^~~~~~
syscalls.c: In function 'make_directory':
syscalls.c:67:9: error: too many arguments to function 'mkdir'
if (mkdir(path, 0777) == -1) {
^~~~~
In file included from c:\mingw\include\unistd.h:56:0,
from syscalls.c:3:
c:\mingw\include\io.h:516:38: note: declared here
_CRTIMP __cdecl __MINGW_NOTHROW int mkdir (const char *);
Please help me to resolve this issue. Thank you
mkdir() is not specified in the C standard. It is specified in the POSIX standard, which is more or less a superset of the C standard.
This declaration of mkdir():
c:\mingw\include\io.h:516:38: note: declared here
_CRTIMP __cdecl __MINGW_NOTHROW int mkdir (const char *);
is the Microsoft version of the function, which takes a single argument, and does not conform to the POSIX standard.
From Microsoft's page:
The Microsoft-implemented POSIX function name mkdir is a deprecated
alias for the _mkdir function.
int _mkdir(
const char *dirname
);
Possible fix:
#ifdef _CRTIMP
#define mkdir(d,m) (mkdir)(d)
#endif
Credit: #chqrlie

Destination and Source file don't match - C

I've been trying to find out why my tester is failing, it says that destination and source file don't match. link for testers: https://github.com/ShiraWolf/hwOP.git
Output Requirements & Testing:
It must output one of the following types of messages (precisely and case-sensitive):
Unable to open source file for reading
Unable to open destination file for writing
Unable to write to destination file
Unable to write buffer content to destination file
Unable to read source file
Unable to close source file
Unable to close destination file
File was successfully copied to
Or one of the various arguments parsing errors, as described in the examples above.
My code:
/*
* ex1.c
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_BUFFER_SIZE 65536
#define DESTINATION_FILE_MODE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH
extern int opterr, optind;
void exit_with_usage(const char *message) {
fprintf(stderr, "%s\n", message);
fprintf(stderr, "Usage:\n\tex1 [-f] BUFFER_SIZE SOURCE DEST\n");
exit(EXIT_FAILURE);
}
void copy_file(const char *source_file, const char *dest_file, int buffer_size, int force_flag) {
/*
* Copy source_file content to dest_file, buffer_size bytes at a time.
* If force_flag is true, then also overwrite dest_file. Otherwise print error, and exit.
*
* TODO:
* 1. Open source_file for reading
* 2. Open dest_file for writing (Hint: is force_flag true?)
* 3. Loop reading from source and writing to the destination buffer_size bytes each time
* 4. Close source_file and dest_file
*
* ALWAYS check the return values of syscalls for errors!
* If an error was found, use perror(3) to print it with a message, and then exit(EXIT_FAILURE)
*/
int c = 0;
int sourcef = 0;
int destf = 0;
sourcef = open(source_file, O_RDONLY);
if (sourcef == -1) {
perror("Unable to open source file for reading");
exit(EXIT_FAILURE);
}
destf = open(dest_file, O_WRONLY |O_CREAT | O_EXCL, 00700);
if (destf == -1) {
if (force_flag) {
destf = open(dest_file, O_WRONLY, 00700);
if (destf == -1) {
if (close(sourcef) == -1) {
perror("couldn't close source file");
exit(EXIT_FAILURE);
}
perror("Unable to open destination for writing");
exit(EXIT_FAILURE);
}
} else {
perror("Unable to open destination for writing");
exit(EXIT_FAILURE);
}
}
char *buffer = malloc(sizeof(char) * buffer_size);
while ((c = read(sourcef, buffer, buffer_size)) != 0) {
if (c == -1) {
perror("couldn't read from source file");
if (close(sourcef) == -1) {
perror("couldn't close source file after reading has failed");
exit(EXIT_FAILURE);
}
if (close(destf) == -1) {
perror("couldn't close dest file after reading has failed");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
c = write(destf, buffer, buffer_size);
if (c == -1) {
perror("couldn't write to source file");
if (close(sourcef) == -1) {
perror("couldn't close source file after writing has failed");
exit(EXIT_FAILURE);
}
if (close(destf) == -1) {
perror("couldn't close dest file after writing has failed");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
}
free(buffer);
if (close(sourcef) == -1) {
perror("couldn't close source file");
exit(EXIT_FAILURE);
}
if (close(destf) == -1) {
perror("couldn't close dest file");
exit(EXIT_FAILURE);
}
printf("File %s was copied to %s\n", source_file, dest_file);
exit(EXIT_SUCCESS);
}
void parse_arguments (
int argc, char **argv,
char **source_file, char **dest_file, int *buffer_size, int *force_flag) {
/*
* parses command line arguments and set the arguments required for copy_file
*/
int option_character;
opterr = 0; /* Prevent getopt() from printing an error message to stderr */
while ((option_character = getopt(argc, argv, "f")) != -1) {
switch (option_character) {
case 'f':
*force_flag = 1;
break;
default: /* '?' */
exit_with_usage("Unknown option specified");
}
}
if (argc - optind != 3) {
exit_with_usage("Invalid number of arguments");
} else {
*source_file = argv[argc - 2];
*dest_file = argv[argc - 1];
*buffer_size = atoi(argv[argc - 3]);
if (strlen(*source_file) == 0 || strlen(*dest_file) == 0) {
exit_with_usage("Invalid source / destination file name");
} else if (*buffer_size < 1 || *buffer_size > MAX_BUFFER_SIZE) {
exit_with_usage("Invalid buffer size");
}
}
}
int main(int argc, char **argv) {
int force_flag = 0; /* force flag default: false */
char *source_file = NULL;
char *dest_file = NULL;
int buffer_size = MAX_BUFFER_SIZE;
parse_arguments(argc, argv, &source_file, &dest_file, &buffer_size, &force_flag);
copy_file(source_file, dest_file, buffer_size, force_flag);
return EXIT_SUCCESS;
}
Can anybody see where my mistake is?
c = write(destf, buffer, buffer_size); does not use the correct size: you should write c bytes and store the written count into a separate variable nwritten and keep trying to write more bytes until c bytes have been written or write return 0 or -1.
Here is a modified version of the copying loop:
while ((c = read(sourcef, buffer, buffer_size)) != 0) {
if (c == -1) {
perror("Unable to read from source file");
if (close(sourcef) == -1) {
perror("Unable to close source file");
exit(EXIT_FAILURE);
}
if (close(destf) == -1) {
perror("Unable to close destination file");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
int towrite = c;
int pos = 0;
while (towrite > 0) {
int nwritten = write(destf, buffer + pos, towrite);
if (nwritten <= 0) {
perror("Unable to write to destination file");
if (close(sourcef) == -1) {
perror("Unable to close source file");
exit(EXIT_FAILURE);
}
if (close(destf) == -1) {
perror("Unable to close destination file");
exit(EXIT_FAILURE);
}
exit(EXIT_FAILURE);
}
pos += nwritten;
towrite -= nwritten;
}
}
Also note that the other error messages are different from the specification.

changing size of a block device in c linux

I have written a function that should change the size of any block device
lsblk command gives this output
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 465.8G 0 disk
├─sda1 8:1 0 457.9G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 7.9G 0 part [SWAP]
my function is->
int _some_func(
int path,
int size)
{
struct stat stat_buf;
/*check if file is valid*/
if(!((fcntl(path, F_GETFL) != -1) || (errno != EBADF)))
{
perror("invalid file descriptor\n");
return -1;
}
/*get file properties in stat_buf*/
if( -1 == fstat(path, &stat_buf))
{
perror("error in stat\n");
return -1;
}
/*return error -1 if file is not block device*/
if((stat_buf.st_mode & S_IFMT) != S_IFBLK)
{
perror("not a block device\n");
return -1;
}
return(ftruncate(path,size) );
}
int main()
{
long int curr_size = 0;
struct stat stat_buf;
int fd = open("/dev/sda1",O_RDWR,0777);
if(fd < 0) perror("error\n");
/*get file properties in stat_buf*/
if( -1 == fstat(fd, &stat_buf))
{
perror("error in stat\n");
return -1;
}
curr_size = stat_buf.st_size;
printf("current size =%ld\n",curr_size);
if(_ss_size(fd,curr_size+50) == 0)
{
/*get file properties in stat_buf*/
if( -1 == fstat(fd, &stat_buf))
{
perror("error in stat\n");
return -1;
}
curr_size = stat_buf.st_size;
printf("changed size =%ld\n",curr_size);
}
else
{
perror("error in _ss_size\n");
}
}
output is->
current size =0
error in _ss_size
: Invalid argument
1>why the current size of "/dev/sda1" is showing as 0,but in lsblk size is = 457.9G
2>why error in _ss_size
: Invalid argument is coming(this is the return value of)
ftruncate command.
if ftruncate does not work, then which sys call will work?

How to iterate in a directory and stop at a particular level using C?

I am trying to print the names of all the processes currently in the system, in the terminal. For that I have to get into all the directories named after the process ID in the "proc" directory. So I am looping till before the "acpi" directory and trying to read the status file in each process directory. But I don't exactly understand how to read a file in a directory which is inside a directory. On running my code below :
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
DIR* FD;
struct dirent* in_file;
FILE *process_file;
char ch, pname[1024];
int i=0;
FD = opendir ("/proc");
while ((in_file = readdir(FD)))
{
if (strcmp (in_file->d_name, "acpi") == 0)
break;
else
{
opendir(in_file->d_name);
process_file = fopen("status", "r");
while( ( ch = fgetc(process_file) ) != '\n' )
{
pname[i] = ch;
i++;
}
printf(" %s \n",pname);
fclose(process_file);
closedir(in_file->d_name);
}
}
closedir(FD);
return 0;
}
I get the error :
myps.c: In function ‘main’:
myps.c:38:13: warning: passing argument 1 of ‘closedir’ from incompatible pointer type
closedir(in_file->d_name);
^
In file included from myps.c:5:0:
/usr/include/dirent.h:149:12: note: expected ‘struct DIR *’ but argument is of type ‘char *’
extern int closedir (DIR *__dirp) __nonnull ((1));
^
This is a good example of when to use a recursive function.
The function would take a directory name, open that directory, and loop through the results. For each result that is not . or .., call the stat function to get status on each entry. Then use the S_ISREG and S_ISDIR macros against the file mode to see if it's a regular file or a directory. If it's a directory, build a new string from the parent directory and the one you just found and pass that to the recursive function call.
So the function would look something like this:
void processDirectory(char dirname[])
{
struct stat statbuf;
DIR *dir;
struct dirent *de;
char *subdirname;
int rval, ;
if ((dir = opendir(dirname)) == NULL) {
perror("Failed to open directory %s", dirname);
exit(1);
}
while ((errno = 0, de = readdir(dir)) != NULL) {
rval = stat(de->d_name, &statbuf);
if (rval == -1) {
perror("stat failed");
exit(1);
}
if (S_ISREG(statbuf.st_mode)) {
// process as a regular file
} else if (S_ISDIR(statbuf.st_mode)) {
if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
subdirname = malloc(strlen(dirname) + strlen(de->d_name) + 2);
if (subdirname == NULL) {
perror("malloc failed");
exit(1);
}
strcpy(subdirname, dirname);
strcat(subdirname, "/");
strcat(subdirname, de->d_name);
processDirectory(subdirname);
free(subdirname);
}
}
}
if (errno && (errno != ENOENT)) {
perror("Failed to read directory %s", dirname);
exit(1);
}
closedir(dir);
}
To solve the error, save the directory pointer you open. Then use that to close the directory.
DIR *process_dir = opendir(in_file->d_name);
closedir(process_dir);

Traversing a Path in c

I am writing a program in c that takes a path name and traverses that path, and prints out all file paths it comes across and the size of that file in blocks and then if it is a dir it prints out the dir pathname and size in blocks.
The code is ending up in an infinate loop and keeps saying "Failed to get status:too many files open."
#include <dirent.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char **argv ){
struct stat statbuf;
struct dirent *direntp;
DIR *dirp;
if(stat(argv[1], &statbuf) == -1){
perror("Failed to get file status");
return 1;
}
else if(argc != 2){
perror("Invalid amount of arguments, showtreesize requires 1 pathname");
return 1;
}
else{
if(S_ISDIR(statbuf.st_mode) || S_ISREG(statbuf.st_mode)){
printf("%s %d", argv[1], depthfirstapply(argv[1], sizepathfun(argv[1])));
}
else{
if(S_ISCHR(statbuf.st_mode)){
printf("%s is a character special file.", argv[1]);
}
if(S_ISBLK(statbuf.st_mode)){
printf("%s is a block special file", argv[1]);
}
if(S_ISFIFO(statbuf.st_mode)){
printf("%s is a FIFO special file", argv[1]);
}
else{
printf("%s is not a valid filetype", argv[1]);
}
}
return 0;
}
}
int sum = 0;
int levelcount = 0;
int isDirectory(char *path){
struct stat statbuf;
if(stat(path, &statbuf) == -1)
return 0;
else
return S_ISDIR(statbuf.st_mode);
}
int depthfirstapply(char *path, int pathfun(char *path1)){
struct dirent *direntp;
DIR *dirp;
if(isDirectory(path)){
printf("%s\n", path);
if((dirp = opendir(path)) == NULL){
perror ("Failed to open directory");
return -1;
}
else{
while((direntp = readdir(dirp)) != NULL) {
if(isDirectory(direntp->d_name)){
int result = depthfirstapply(direntp->d_name, pathfun);
if (result > 0){
sum += result;
}
}
else{
if(pathfun(direntp->d_name) >= 0){
sum += pathfun(direntp->d_name);
}
}
}
while ((closedir(dirp) == -1) && (errno == EINTR)) ;
}
}
else{
sum += pathfun(path);
}
return sum;
}
int sizepathfun(char *path){
struct stat statbuf;
if(stat(path, &statbuf) == -1){
perror("Failed to get file status");
return -1;
}
if(S_ISREG(statbuf.st_mode) == 0){
return -1;
}
else{
printf("%s %d", path, statbuf.st_blocks);
return statbuf.st_blocks;
}
}
Several problems:
You need to skip the . and .. entries. Otherwise, you'll keep looping on the same directory.
Use lstat() rather than stat() in isDirectory. Otherwise, you'll recurse into symbolic links to directories, which could cause a loop.
As you go down each directory level, you need to concatenate the directory components to the names.
The 2nd argument to depthfirstapply is supposed to be a function. But in main() you call it with sizepathfun(argv[1)), which returns an integer. The argument should just be sizepathfun. You should be getting a compilation warning because of the type mismatch.
POSIX provides a standard set of functions for this, fts_XXX().

Resources