4

OK this is embarrassing and as much as I hate to, I have no other option. I don't know C but I was presented with a problem that I need to solve and although I've done some researching the time it would take me to modify the program myself is just too much so I have to swallow my pride (and I'm guessing some rep pts) to ask for help.

This is a simple program to convert a unix file to dos, the only problem is that I need it to accept wildcards (eg.. c:/>unix2dos *.txt or file*.txt ) Nothing fancy.

Here is the code that I have now..

    // UNIX2DOS - a Win32 utility to convert single text files from Unix to MS-DOS format.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/utime.h>

#ifndef TRUE
#   define TRUE  (1)
#   define FALSE (0)
#endif

#define R_CNTRL   "rb"
#define W_CNTRL   "wb"

struct stat s_buf;

int u2dos (path)
char *path;
{
    FILE *in, *out;
    int ch,
        prev_ch= 0, 
        rval = FALSE;
    char temppath [16];
    struct _utimbuf ut_buf;
    strcpy (temppath, "./clntmp");
    strcat (temppath, "XXXXXX");
    mktemp (temppath);
    if ((in=fopen (path, R_CNTRL)) == (FILE *) 0)
        return TRUE;
    if ((out=fopen (temppath, W_CNTRL)) == (FILE *) 0)
    {
        fclose (in);
        return TRUE;
    }

    #define LF        0x0A
    #define CR        0x0D

    while ((ch = getc (in)) != EOF)
    {
        if (    ( ch == LF)
             && ( prev_ch != CR)
             && ( putc( CR, out) == EOF)
             || ( putc( ch, out) == EOF)
           )
        {
            rval = TRUE;
            break;
        }
        prev_ch= ch ;
    }

    if (fclose (in) == EOF)
    {
        rval = TRUE;
    }
    if (fclose (out) == EOF)
    {
        rval = TRUE;
    }
    ut_buf.actime = s_buf.st_atime;
    ut_buf.modtime = s_buf.st_mtime;
    if (_utime (temppath, &ut_buf) == -1)
        rval = TRUE;
    if (unlink (path) == -1)
        rval = TRUE;
    if (rval)
    {
        unlink (temppath);
        return TRUE;
    }
    if (rename (temppath,path) == -1)
    {
        fprintf (stderr, "Unix2Dos: Problems renaming '%s' to '%s'.\n", temppath, path);
        fprintf (stderr, "          However, file '%s' remains.\n", temppath);
        exit (1);
    }
    unlink (temppath);
    return FALSE;
}

void main (argc, argv)
int argc;
char **argv;
{
    char *path;
    while (--argc>0)
    {
        if (stat (path=*++argv, &s_buf) != -1)
        {
            printf ("Unix2Dos: Processing file %s ...\n", path);
            if (u2dos (path))
            {
                fprintf (stderr, "Unix2Dos: Problems processing file %s.\n", path);
                exit (1);
            }
        }
        else
        {
            fprintf (stderr, "Unix2Dos: Can't stat '%s'.\n", path);
            exit (1);
        }
    }
}

I cant believe I have digressed to one of the "Send me da codez" people I have grown to despise but right now it seems like this is my best option.

I'm going to go burry my head in the sand now. Thanks for your time.

EDIT

Although implied, I thought I should make the question obvious. Can you please provide some assistance in modifying this program to accept wildcard variables in a windows environment?

eddie
  • 1,252
  • 3
  • 15
  • 20
Ryan
  • 4,443
  • 4
  • 26
  • 26
  • 2
    I'd say it's not "Send me da codez" because you clearly have made some research effort before posting this – Flexo Oct 07 '11 at 12:25
  • 2
    Here you'll find a couple of win32 solutions: http://stackoverflow.com/questions/1269480/globbing-in-c-c-on-windows – Karoly Horvath Oct 07 '11 at 12:32
  • Is this meant to run on Windows or Unix? – Ori Pessach Oct 07 '11 at 12:32
  • Windows, Ive read in other posts that Unix would be able to handle the wildcard. – Ryan Oct 07 '11 at 12:42
  • Instead of hard coding the values defined in `LF` and `CR` use character literals like `'\n'` and `'\r'`. Also, is there any specific reason to use K&R definitions in functions? – sidyll Oct 07 '11 at 12:49
  • Unfortunately this isnt my program, and I know very little (if nothing at all) about C which is why Im here asking for help. I probably shouldnt even be poking my nose into this problem but Im just trying to help out a friend. – Ryan Oct 07 '11 at 13:15

4 Answers4

4

*nix shells, (e.g. bash), automatically expand wildcard arguments. Windows shell does not. But Microsoft Visual C runtime library does have an option to do it automatically on your program's startup. How to do it is explained here. Basically you need to link against setargv.obj.

cyco130
  • 4,654
  • 25
  • 34
4

Since this will run on Windows, you can use FindFirstFile, FindNextFile and FindClose. There is an example here: http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx

IronMensan
  • 6,761
  • 1
  • 26
  • 35
  • 1
    ...because the Windows API is so intuitive and easy to work with when you have little to no experience of the C language :) But yeah, indeed those are the functions to use when searching for and listing files. – Lundin Oct 07 '11 at 13:27
3

You'd have to reinvent a whole bunch of wheels to use wildcards in the file names, and even more to do it portably. Try using cygwin's bash and unix2dos.

thiton
  • 35,651
  • 4
  • 70
  • 100
  • That would be ideal, but I would like to automate the process on Win 2003 Server. If its as difficult than you say, then I might have to find a different solution like changing the file name to a constant first, then running the program. ..but this could have some implications too. Thanks for the help +1 – Ryan Oct 07 '11 at 12:44
  • +1 a bash script can be run with cygwin in a relatively easily automated way. We've got cygwin running on a number of servers at work, bash is such an amazing step up from the windows shell and batch files. – asc99c Oct 07 '11 at 20:59
0

Why not take the program as is, and let some (.)bat(ch) file magic do the expansion of wildcards?

On how to do this please see here: http://support.microsoft.com/kb/68268/en-us

Although you'd be starting a new process for each file to be converted, which is quiet expensive, it'll keep you from hacking around using a language you do not seem to like ... ;-)

alk
  • 69,737
  • 10
  • 105
  • 255