| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the test suite of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT | 
| 21 | ** included in the packaging of this file. Please review the following | 
| 22 | ** information to ensure the GNU General Public License requirements will | 
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. | 
| 24 | ** | 
| 25 | ** $QT_END_LICENSE$ | 
| 26 | ** | 
| 27 | ****************************************************************************/ | 
| 28 | #include <QCoreApplication> | 
| 29 | #include <QDebug> | 
| 30 | #include <QStringList> | 
| 31 | #include <QFile> | 
| 32 | #include <QDir> | 
| 33 |  | 
| 34 | #include <stdio.h> | 
| 35 |  | 
| 36 | #if defined(Q_OS_UNIX) | 
| 37 | #include <sys/types.h> | 
| 38 | #include <unistd.h> | 
| 39 | #elif defined(Q_OS_WIN) | 
| 40 | #include <windows.h> | 
| 41 | #endif | 
| 42 |  | 
| 43 | static void writeStuff(QFile &f) | 
| 44 | { | 
| 45 |     f.write(data: QDir::currentPath().toUtf8()); | 
| 46 |     f.putChar(c: '\n'); | 
| 47 | #if defined(Q_OS_UNIX) | 
| 48 |     f.write(data: QByteArray::number(quint64(getpid()))); | 
| 49 | #elif defined(Q_OS_WIN) | 
| 50 |     f.write(QByteArray::number(quint64(GetCurrentProcessId()))); | 
| 51 | #endif | 
| 52 |     f.putChar(c: '\n'); | 
| 53 |     f.write(data: qgetenv(varName: "tst_QProcess" )); | 
| 54 |     f.putChar(c: '\n'); | 
| 55 | } | 
| 56 |  | 
| 57 | struct Args | 
| 58 | { | 
| 59 |     int exitCode = 0; | 
| 60 |     QByteArray errorMessage; | 
| 61 |     QString fileName; | 
| 62 |     FILE *channel = nullptr; | 
| 63 |     QByteArray channelName; | 
| 64 | }; | 
| 65 |  | 
| 66 | static Args parseArguments(const QStringList &args) | 
| 67 | { | 
| 68 |     Args result; | 
| 69 |     if (args.count() < 2) { | 
| 70 |         result.exitCode = 128; | 
| 71 |         result.errorMessage = "Usage: testDetached [--out-channel={stdout|stderr}] filename.txt\n" ; | 
| 72 |         return result; | 
| 73 |     } | 
| 74 |     for (const QString &arg : args) { | 
| 75 |         if (arg.startsWith(s: "--" )) { | 
| 76 |             if (!arg.startsWith(s: "--out-channel=" )) { | 
| 77 |                 result.exitCode = 2; | 
| 78 |                 result.errorMessage = "Unknown argument "  + arg.toLocal8Bit(); | 
| 79 |                 return result; | 
| 80 |             } | 
| 81 |             result.channelName = arg.mid(position: 14).toLocal8Bit(); | 
| 82 |             if (result.channelName == "stdout" ) { | 
| 83 |                 result.channel = stdout; | 
| 84 |             } else if (result.channelName == "stderr" ) { | 
| 85 |                 result.channel = stderr; | 
| 86 |             } else { | 
| 87 |                 result.exitCode = 3; | 
| 88 |                 result.errorMessage = "Unknown channel "  + result.channelName; | 
| 89 |                 return result; | 
| 90 |             } | 
| 91 |         } else { | 
| 92 |             result.fileName = arg; | 
| 93 |         } | 
| 94 |     } | 
| 95 |     return result; | 
| 96 | } | 
| 97 |  | 
| 98 | int main(int argc, char **argv) | 
| 99 | { | 
| 100 |     QCoreApplication app(argc, argv); | 
| 101 |  | 
| 102 |     const Args args = parseArguments(args: app.arguments()); | 
| 103 |     if (args.exitCode) { | 
| 104 |         fprintf(stderr, format: "testDetached: %s\n" , args.errorMessage.constData()); | 
| 105 |         return args.exitCode; | 
| 106 |     } | 
| 107 |  | 
| 108 |     if (args.channel) { | 
| 109 |         QFile channel; | 
| 110 |         if (!channel.open(f: args.channel, ioFlags: QIODevice::WriteOnly | QIODevice::Text)) { | 
| 111 |             fprintf(stderr, format: "Cannot open channel %s for writing: %s\n" , | 
| 112 |                     qPrintable(args.channelName), qPrintable(channel.errorString())); | 
| 113 |             return 4; | 
| 114 |         } | 
| 115 |         writeStuff(f&: channel); | 
| 116 |     } | 
| 117 |  | 
| 118 |     QFile f(args.fileName); | 
| 119 |     if (!f.open(flags: QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text)) { | 
| 120 |         fprintf(stderr, format: "Cannot open %s for writing: %s\n" , | 
| 121 |                 qPrintable(f.fileName()), qPrintable(f.errorString())); | 
| 122 |         return 1; | 
| 123 |     } | 
| 124 |  | 
| 125 |     writeStuff(f); | 
| 126 |     f.close(); | 
| 127 |  | 
| 128 |     return 0; | 
| 129 | } | 
| 130 |  |