Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio black screen blue screen boot bsod connection crash dell desktop driver dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor network networking outlook problem processor ram recovery router safe mode slow sound spyware tdlwsp.dll trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: linux write() sys call

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
devil_himself's Avatar
Distinguished Member with 4,921 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
26-Jun-2009, 07:32 AM #1
Solved: linux write() sys call
this program writes the user supplied text ( command line arg ) to the specified text file along with the real user id of the user.

problem -> the user id is not visible inside text editor .. but its present when seen using a hexeditor


Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>

#define FILENAME "/tmp/text"

void usage(char *prog_name, char *filename) {
   printf("Usage: %s <date to write to %s>\n", prog_name, filename);
   exit(0);
}


int main(int argc, char *argv[]) {
   int userid, fd; // file descriptor
   char *buffer;

   buffer = malloc(100);
   if (buffer == NULL) 
   {
	fprintf(stderr, "Error: could not allocate memory.\n");
	exit(1);
   }

   if(argc < 2)                // If there aren't commandline arguments
      usage(argv[0], FILENAME); // display usage message and exit

   strncpy(buffer, argv[1], strlen(argv[1]));  // copy into buffer

 // Opening the file
   fd = open(FILENAME, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
   if(fd == -1)
      perror("error while opening file");

   userid = getuid(); // get the real user ID

   // Writing data
   if(write(fd, &userid, 4) == -1) // write user ID before note data
      perror("error while writing userid to file");
   write(fd, "\n", 1); // terminate line

   if(write(fd, buffer, strlen(buffer)) == -1) // write note
      perror("error while writing buffer to file");
   write(fd, "\n", 1); // terminate line

// Closing file
   if(close(fd) == -1)
      perror("error while closing file");

   free(buffer);

   return 0;
}
here is the terminal session

Code:
[rohit@localhost tmp]$ gcc -Wall -o notes notes.c 
[rohit@localhost tmp]$ ./notes "this is some line of text"
[rohit@localhost tmp]$ cat /tmp/text

this is some line of text
[rohit@localhost tmp]$ od -c /tmp/text
0000000 364 001  \0  \0  \n   t   h   i   s       i   s       s   o   m
0000020   e       l   i   n   e       o   f       t   e   x   t  \n
0000037
[rohit@localhost tmp]$ od -x /tmp/text
0000000 01f4 0000 740a 6968 2073 7369 7320 6d6f
0000020 2065 696c 656e 6f20 2066 6574 7478 000a
0000037
[rohit@localhost tmp]$ id
uid=500(rohit) gid=500(rohit) groups=500(rohit) context=user_u:system_r:unconfined_t
[rohit@localhost tmp]$ exit
01f4 is 500 in decimal

thanks

rohit

Last edited by devil_himself : 26-Jun-2009 08:25 AM.
JimmySeal's Avatar
Senior Member with 206 posts.
 
Join Date: Sep 2007
Experience: Getting there
04-Jul-2009, 11:22 PM #2
You're writing the userid to the file as binary data:

Code:
// Writing data
   if(write(fd, &userid, 4) == -1) // write user ID before note data
      perror("error while writing userid to file");
But you need to convert the userid to a string value and then write the string to the file.
Please look into the itoa function. That should help get you where you need.

http://www.daniweb.com/forums/thread11049.html#
devil_himself's Avatar
Distinguished Member with 4,921 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
05-Jul-2009, 01:41 AM #3
thanks for the reply .. i used the 'sprintf' function as 'itoa' is non standard and it worked.

but can you explain why the data is being written as binary ?

all i know is that write expects a pointer for its source and that requirement is being satisfied.

ssize_t write(int fildes, const void *buf, size_t nbyte);

Code:
if(write(fd, &userid, 4) == -1) // write user ID before note data
JimmySeal's Avatar
Senior Member with 206 posts.
 
Join Date: Sep 2007
Experience: Getting there
05-Jul-2009, 07:27 PM #4
Quote:
Originally Posted by devil_himself View Post
but can you explain why the data is being written as binary ?
That's what write() is designed to do. It's writing buffer as binary character data too, and the only difference is that that's what text editors are designed to read.
devil_himself's Avatar
Distinguished Member with 4,921 posts.
 
Join Date: Apr 2007
Location: India
Experience: Advanced
06-Jul-2009, 01:28 AM #5
thank you
Closed Thread Bookmark and Share

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 06:32 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.