psmile_redirstdout.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #include <math.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010
00011 #include "PSMILe_f2c.h"
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 void psmile_redirstdout (char *filestem, INTEGER *lenstr,
00024 INTEGER *parallel, INTEGER *my_pe,
00025 INTEGER *npes, INTEGER *ierror )
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 {
00074 size_t len_alloc = *lenstr;
00075 register int n_digits, num;
00076 register int redirect = 1;
00077
00078 char *sname, *ename;
00079 register char *p_err, *p_std;
00080
00081 ASSERT (*lenstr > 0)
00082 ASSERT (*my_pe >= 0)
00083
00084 *ierror = 0;
00085
00086
00087
00088 num = (*npes > 0) ? *npes : 1;
00089 n_digits = (int) (log10((double)(num) + (double) 0.5)) + 1;
00090 ASSERT (n_digits > 0)
00091
00092
00093
00094 #define NUMBER_OF_EXTENSIONS 2
00095 #define LENGTH_OF_ERR 3
00096
00097 len_alloc += n_digits + NUMBER_OF_EXTENSIONS + LENGTH_OF_ERR + 1;
00098
00099 sname = (char *) MALLOC (len_alloc);
00100 ename = (char *) MALLOC (len_alloc);
00101
00102 if (!sname || !ename) {
00103 fprintf (stderr, "PSMILe_redirstdout: Cannot allocate memory. %d bytes\n", len_alloc);
00104 *ierror = 1;
00105 return;
00106 }
00107
00108
00109
00110 memcpy (sname, filestem, (size_t) *lenstr);
00111 memcpy (ename, filestem, (size_t) *lenstr);
00112
00113 p_std = sname + (size_t) *lenstr;
00114 p_err = ename + (size_t) *lenstr;
00115
00116 if(* parallel == 1) {
00117
00118
00119 sprintf (p_std, ".%0*d", n_digits, *my_pe);
00120 sprintf (p_err, ".err.%0*d", n_digits, *my_pe);
00121 }
00122 else {
00123
00124 redirect = (*my_pe == 0);
00125
00126 if( redirect ) {
00127 sprintf (p_std, ".log");
00128 sprintf (p_err, ".err");
00129 }
00130 }
00131
00132
00133
00134 if (redirect) {
00135 freopen (sname, "w", stdout);
00136 freopen (ename, "w", stderr);
00137 }
00138
00139
00140
00141 FREE (ename);
00142 FREE (sname);
00143 }