Solved

diff command

Posted on 2004-04-05
3
639 Views
Last Modified: 2010-04-21
I am comparing 2 files using diff

Diff –s filename1 filename2

This returns ‘files filename1 and filename2 are identical’

I am doing this through a c program using

sprintf(command, "diff  -s filename1 filename2");
system(command);
 
I want to check the return value and display a message saying the files were identical or not

Preferably using exit(0); if possible

can anyone help

thanks
0
Comment
Question by:plennon
  • 2
3 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10757508
Hi plennon,

use popen if you want to read in the results of diff ...

if all you want is return status of diff, then system returns exactly that

RETURN VALUE
       The value returned is -1 on error (e.g. fork failed),  and
       the  return  status of the command otherwise.  This latter
       return status is  in  the  format  specified  in  wait(2).
       Thus,  the  exit  code  of  the  command will be WEXITSTA­
       TUS(status).  In case /bin/sh could not be  executed,  the
       exit status will be that of a command that does exit(127).

       If the value of string is NULL, system()  returns  nonzero
       if the shell is available, and zero if not.

       system()  does  not  affect  the  wait status of any other
       children.


Sunnycoder
0
 

Author Comment

by:plennon
ID: 10757596
it's how to get the return value that i can't figure out

in my c code i have

 sprintf(diffcommand, "diff -s filename1 filename2");
 system(diffcommand);

it produces the following message

"files filename1 and filename2 are identical"

i don't want this to be displayed i only want  to have an if statement that will display pass if they are identical (return value 0) or fail if not (return is nonzero)

it's actually the c code i'm looking for although i've posted this in the unix section

thanks
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 35 total points
ID: 10757631
sprintf(diffcommand, "diff -s filename1 filename2 > /dev/null");
int  ret =  system(diffcommand);

printf ( "system returned %d", WEXITSTATUS (ret ) );
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vmware Vswitch  VS  Redhat  Bridge 1 106
reinstall 1 77
problem creating new luks volume on new VM disk 5 88
How do disable only TLSv1.0 in Oracle Sun One 7.1 Server 9 91
Installing FreeBSD… FreeBSD is a darling of an operating system. The stability and usability make it a clear choice for servers and desktops (for the cunning). Savvy?  The Ports collection makes available every popular FOSS application and packag…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now