\makeatletter
\def\maxwidth{%
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
This defines a “variable” width which has the properties you want.
Replace \linewidth if you have a different constraint on the width
of the graphic.
Use the command as follows:
\includegraphics[width=\maxwidth]{figure}
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=grmaxwidth